aboutsummaryrefslogtreecommitdiff
path: root/keyboards/bioi/ble.c
diff options
context:
space:
mode:
authorAkshay <[email protected]>2022-04-10 12:13:40 +0100
committerAkshay <[email protected]>2022-04-10 12:13:40 +0100
commitdc90387ce7d8ba7b607d9c48540bf6d8b560f14d (patch)
tree4ccb8fa5886b66fa9d480edef74236c27f035e16 /keyboards/bioi/ble.c
Diffstat (limited to 'keyboards/bioi/ble.c')
-rw-r--r--keyboards/bioi/ble.c219
1 files changed, 219 insertions, 0 deletions
diff --git a/keyboards/bioi/ble.c b/keyboards/bioi/ble.c
new file mode 100644
index 000000000..d941f7c43
--- /dev/null
+++ b/keyboards/bioi/ble.c
@@ -0,0 +1,219 @@
1/*
2Copyright 2019 Basic I/O Instruments(Scott Wei) <[email protected]>
3This program is free software: you can redistribute it and/or modify
4it under the terms of the GNU General Public License as published by
5the Free Software Foundation, either version 2 of the License, or
6(at your option) any later version.
7This program is distributed in the hope that it will be useful,
8but WITHOUT ANY WARRANTY; without even the implied warranty of
9MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10GNU General Public License for more details.
11You should have received a copy of the GNU General Public License
12along with this program. If not, see <http://www.gnu.org/licenses/>.
13*/
14
15#include <avr/pgmspace.h>
16#include "report.h"
17#include "host.h"
18#include "host_driver.h"
19#include "keyboard.h"
20#include "action.h"
21#include "led.h"
22
23#include "sendchar.h"
24#include "debug.h"
25#ifdef SLEEP_LED_ENABLE
26#include "sleep_led.h"
27#endif
28#include "suspend.h"
29
30#include "usb_descriptor.h"
31#include "lufa.h"
32#include "quantum.h"
33#include <util/atomic.h>
34
35#include "print.h"
36
37#include "ble.h"
38#include "usart.h"
39
40keyboard_config_t ble_config;
41
42static uint8_t bluefruit_keyboard_leds = 0;
43
44static void bluefruit_serial_send(uint8_t);
45
46void send_str(const char *str)
47{
48 uint8_t c;
49 while ((c = pgm_read_byte(str++)))
50 uart1_putc(c);
51}
52
53void serial_send(uint8_t data)
54{
55 dprintf("Sending: %u\n", data);
56}
57
58void send_bytes(uint8_t data)
59{
60 char hexStr[3];
61 sprintf(hexStr, "%02X", data);
62 for (int j = 0; j < sizeof(hexStr) - 1; j++)
63 {
64 uart1_putc(hexStr[j]);
65 }
66}
67
68#ifdef BLUEFRUIT_TRACE_SERIAL
69static void bluefruit_trace_header(void)
70{
71 dprintf("+------------------------------------+\n");
72 dprintf("| HID report to Bluefruit via serial |\n");
73 dprintf("+------------------------------------+\n|");
74}
75
76static void bluefruit_trace_footer(void)
77{
78 dprintf("|\n+------------------------------------+\n\n");
79}
80#endif
81
82static void bluefruit_serial_send(uint8_t data)
83{
84#ifdef BLUEFRUIT_TRACE_SERIAL
85 dprintf(" ");
86 debug_hex8(data);
87 dprintf(" ");
88#endif
89 serial_send(data);
90}
91
92/*------------------------------------------------------------------*
93 * Host driver
94 *------------------------------------------------------------------*/
95
96static uint8_t keyboard_leds(void);
97static void send_keyboard(report_keyboard_t *report);
98static void send_mouse(report_mouse_t *report);
99static void send_system(uint16_t data);
100static void send_consumer(uint16_t data);
101
102host_driver_t bluefruit_driver = {
103 keyboard_leds,
104 send_keyboard,
105 send_mouse,
106 send_system,
107 send_consumer};
108
109host_driver_t null_driver = {};
110
111static uint8_t keyboard_leds(void)
112{
113 return bluefruit_keyboard_leds;
114}
115
116static void send_keyboard(report_keyboard_t *report)
117{
118#ifdef BLUEFRUIT_TRACE_SERIAL
119 bluefruit_trace_header();
120#endif
121 dprintf("Sending...\n");
122
123 send_str(PSTR("AT+BLEKEYBOARDCODE="));
124
125 for (uint8_t i = 0; i < KEYBOARD_EPSIZE; i++)
126 {
127 send_bytes(report->raw[i]);
128 if (i < (KEYBOARD_EPSIZE - 1))
129 {
130 send_str(PSTR("-"));
131 }
132 }
133
134 send_str(PSTR("\r\n"));
135#ifdef BLUEFRUIT_TRACE_SERIAL
136 bluefruit_trace_footer();
137#endif
138}
139
140static void send_mouse(report_mouse_t *report)
141{
142#ifdef BLUEFRUIT_TRACE_SERIAL
143 bluefruit_trace_header();
144#endif
145 bluefruit_serial_send(0xFD);
146 bluefruit_serial_send(0x00);
147 bluefruit_serial_send(0x03);
148 bluefruit_serial_send(report->buttons);
149 bluefruit_serial_send(report->x);
150 bluefruit_serial_send(report->y);
151 bluefruit_serial_send(report->v); // should try sending the wheel v here
152 bluefruit_serial_send(report->h); // should try sending the wheel h here
153 bluefruit_serial_send(0x00);
154#ifdef BLUEFRUIT_TRACE_SERIAL
155 bluefruit_trace_footer();
156#endif
157}
158
159static void send_system(uint16_t data)
160{
161}
162
163/*
164+-----------------+-------------------+-------+
165| Consumer Key | Bit Map | Hex |
166+-----------------+-------------------+-------+
167| Home | 00000001 00000000 | 01 00 |
168| KeyboardLayout | 00000010 00000000 | 02 00 |
169| Search | 00000100 00000000 | 04 00 |
170| Snapshot | 00001000 00000000 | 08 00 |
171| VolumeUp | 00010000 00000000 | 10 00 |
172| VolumeDown | 00100000 00000000 | 20 00 |
173| Play/Pause | 01000000 00000000 | 40 00 |
174| Fast Forward | 10000000 00000000 | 80 00 |
175| Rewind | 00000000 00000001 | 00 01 |
176| Scan Next Track | 00000000 00000010 | 00 02 |
177| Scan Prev Track | 00000000 00000100 | 00 04 |
178| Random Play | 00000000 00001000 | 00 08 |
179| Stop | 00000000 00010000 | 00 10 |
180+-------------------------------------+-------+
181*/
182#define CONSUMER2BLUEFRUIT(usage) \
183 (usage == AUDIO_MUTE ? 0x00e2 : (usage == AUDIO_VOL_UP ? 0x00e9 : (usage == AUDIO_VOL_DOWN ? 0x00ea : (usage == TRANSPORT_NEXT_TRACK ? 0x00b5 : (usage == TRANSPORT_PREV_TRACK ? 0x00b6 : (usage == TRANSPORT_STOP ? 0x00b7 : (usage == TRANSPORT_STOP_EJECT ? 0x00b8 : (usage == TRANSPORT_PLAY_PAUSE ? 0x00b1 : (usage == AL_CC_CONFIG ? 0x0183 : (usage == AL_EMAIL ? 0x018c : (usage == AL_CALCULATOR ? 0x0192 : (usage == AL_LOCAL_BROWSER ? 0x0196 : (usage == AC_SEARCH ? 0x021f : (usage == AC_HOME ? 0x0223 : (usage == AC_BACK ? 0x0224 : (usage == AC_FORWARD ? 0x0225 : (usage == AC_STOP ? 0x0226 : (usage == AC_REFRESH ? 0x0227 : (usage == AC_BOOKMARKS ? 0x022a : 0)))))))))))))))))))
184
185static void send_consumer(uint16_t data)
186{
187 static uint16_t last_data = 0;
188 if (data == last_data)
189 return;
190 last_data = data;
191
192 uint16_t bitmap = CONSUMER2BLUEFRUIT(data);
193
194#ifdef BLUEFRUIT_TRACE_SERIAL
195 dprintf("\nData: ");
196 debug_hex16(data);
197 dprintf("; bitmap: ");
198 debug_hex16(bitmap);
199 dprintf("\n");
200 bluefruit_trace_header();
201#endif
202 send_str(PSTR("AT+BLEHIDCONTROLKEY=0x"));
203 send_bytes((bitmap >> 8) & 0xFF);
204 send_bytes(bitmap & 0xFF);
205 send_str(PSTR("\r\n"));
206#ifdef BLUEFRUIT_TRACE_SERIAL
207 bluefruit_trace_footer();
208#endif
209}
210
211void usart_init(void)
212{
213 uart1_init(UART_BAUD_SELECT_DOUBLE_SPEED(76800, 8000000L));
214 wait_ms(250);
215
216 send_str(PSTR("\r\n"));
217 send_str(PSTR("\r\n"));
218 send_str(PSTR("\r\n"));
219}