hhkb: Move functions which communicate with RN42
[tmk_keyboard.git] / keyboard / hhkb / rn42 / rn42_task.c
1 #include <stdint.h>
2 #include <string.h>
3 #include <avr/pgmspace.h>
4 #include <avr/eeprom.h>
5 #include "keycode.h"
6 #include "serial.h"
7 #include "host.h"
8 #include "action.h"
9 #include "action_util.h"
10 #include "lufa.h"
11 #include "rn42_task.h"
12 #include "print.h"
13 #include "debug.h"
14 #include "timer.h"
15 #include "wait.h"
16 #include "command.h"
17 #include "battery.h"
18
19 static bool config_mode = false;
20 static bool force_usb = false;
21
22 static void status_led(bool on)
23 {
24 if (on) {
25 DDRE |= (1<<6);
26 PORTE &= ~(1<<6);
27 } else {
28 DDRE |= (1<<6);
29 PORTE |= (1<<6);
30 }
31 }
32
33 void rn42_task_init(void)
34 {
35 battery_init();
36 }
37
38 void rn42_task(void)
39 {
40 int16_t c;
41 // Raw mode: interpret output report of LED state
42 while ((c = rn42_getc()) != -1) {
43 // LED Out report: 0xFE, 0x02, 0x01, <leds>
44 // To get the report over UART set bit3 with SH, command.
45 static enum {LED_INIT, LED_FE, LED_02, LED_01} state = LED_INIT;
46 switch (state) {
47 case LED_INIT:
48 if (c == 0xFE) state = LED_FE;
49 else {
50 if (0x0 <= c && c <= 0x7f) xprintf("%c", c);
51 else xprintf(" %02X", c);
52 }
53 break;
54 case LED_FE:
55 if (c == 0x02) state = LED_02;
56 else state = LED_INIT;
57 break;
58 case LED_02:
59 if (c == 0x01) state = LED_01;
60 else state = LED_INIT;
61 break;
62 case LED_01:
63 dprintf("LED status: %02X\n", c);
64 rn42_set_leds(c);
65 state = LED_INIT;
66 break;
67 default:
68 state = LED_INIT;
69 }
70 }
71
72 /* Bluetooth mode when ready */
73 if (!config_mode && !force_usb) {
74 if (!rn42_rts() && host_get_driver() != &rn42_driver) {
75 clear_keyboard();
76 host_set_driver(&rn42_driver);
77 } else if (rn42_rts() && host_get_driver() != &lufa_driver) {
78 clear_keyboard();
79 host_set_driver(&lufa_driver);
80 }
81 }
82
83
84 static uint16_t prev_timer = 0;
85 uint16_t e = timer_elapsed(prev_timer);
86 if (e > 1000) {
87 /* every second */
88 prev_timer += e/1000*1000;
89
90 /* Low voltage alert */
91 uint8_t bs = battery_status();
92 if (bs == LOW_VOLTAGE) {
93 battery_led(LED_ON);
94 } else {
95 battery_led(LED_CHARGER);
96 }
97
98 /* every minute */
99 uint32_t t = timer_read32()/1000;
100 if (t%60 == 0) {
101 uint16_t v = battery_voltage();
102 uint8_t h = t/3600;
103 uint8_t m = t%3600/60;
104 uint8_t s = t%60;
105 dprintf("%02u:%02u:%02u\t%umV\n", h, m, s, v);
106 /* TODO: xprintf doesn't work for this.
107 xprintf("%02u:%02u:%02u\t%umV\n", (t/3600), (t%3600/60), (t%60), v);
108 */
109 }
110 }
111
112
113 /* Connection monitor */
114 if (!rn42_rts() && rn42_linked()) {
115 status_led(true);
116 } else {
117 status_led(false);
118 }
119 }
120
121
122
123 /******************************************************************************
124 * Command
125 ******************************************************************************/
126 static host_driver_t *prev_driver = &rn42_driver;
127
128 static void enter_command_mode(void)
129 {
130 prev_driver = host_get_driver();
131 clear_keyboard();
132 host_set_driver(&rn42_config_driver); // null driver; not to send a key to host
133 rn42_disconnect();
134 while (rn42_linked()) ;
135
136 print("Entering config mode ...\n");
137 wait_ms(1100); // need 1 sec
138 SEND_COMMAND("$$$");
139 wait_ms(600); // need 1 sec
140 rn42_print_response();
141 const char *s = SEND_COMMAND("v\r\n");
142 if (strncmp("v", s, 1) != 0) SEND_COMMAND("+\r\n"); // local echo on
143 }
144
145 static void exit_command_mode(void)
146 {
147 print("Exiting config mode ...\n");
148 SEND_COMMAND("---\r\n"); // exit
149
150 rn42_autoconnect();
151 clear_keyboard();
152 host_set_driver(prev_driver);
153 }
154
155 static void init_rn42(void)
156 {
157 // RN-42 configure
158 if (!config_mode) enter_command_mode();
159 SEND_COMMAND("SF,1\r\n"); // factory defaults
160 SEND_COMMAND("S-,TmkBT\r\n");
161 SEND_COMMAND("SS,Keyboard/Mouse\r\n");
162 SEND_COMMAND("SM,4\r\n"); // auto connect(DTR)
163 SEND_COMMAND("SW,8000\r\n"); // Sniff disable
164 SEND_COMMAND("S~,6\r\n"); // HID profile
165 SEND_COMMAND("SH,003C\r\n"); // combo device, out-report, 4-reconnect
166 SEND_COMMAND("SY,FFF4\r\n"); // transmit power -12
167 SEND_COMMAND("R,1\r\n");
168 if (!config_mode) exit_command_mode();
169 }
170
171 #if 0
172 // Switching connections
173 // NOTE: Remote Address doesn't work in the way manual says.
174 // EEPROM address for link store
175 #define RN42_LINK0 (uint8_t *)128
176 #define RN42_LINK1 (uint8_t *)140
177 #define RN42_LINK2 (uint8_t *)152
178 #define RN42_LINK3 (uint8_t *)164
179 static void store_link(uint8_t *eeaddr)
180 {
181 enter_command_mode();
182 SEND_STR("GR\r\n"); // remote address
183 const char *s = rn42_gets(500);
184 if (strcmp("GR", s) == 0) s = rn42_gets(500); // ignore local echo
185 xprintf("%s(%d)\r\n", s, strlen(s));
186 if (strlen(s) == 12) {
187 for (int i = 0; i < 12; i++) {
188 eeprom_write_byte(eeaddr+i, *(s+i));
189 dprintf("%c ", *(s+i));
190 }
191 dprint("\r\n");
192 }
193 exit_command_mode();
194 }
195
196 static void restore_link(const uint8_t *eeaddr)
197 {
198 enter_command_mode();
199 SEND_COMMAND("SR,Z\r\n"); // remove remote address
200 SEND_STR("SR,"); // set remote address from EEPROM
201 for (int i = 0; i < 12; i++) {
202 uint8_t c = eeprom_read_byte(eeaddr+i);
203 rn42_putc(c);
204 dprintf("%c ", c);
205 }
206 dprintf("\r\n");
207 SEND_COMMAND("\r\n");
208 SEND_COMMAND("R,1\r\n"); // reboot
209 exit_command_mode();
210 }
211
212 static const char *get_link(uint8_t * eeaddr)
213 {
214 static char s[13];
215 for (int i = 0; i < 12; i++) {
216 uint8_t c = eeprom_read_byte(eeaddr+i);
217 s[i] = c;
218 }
219 s[12] = '\0';
220 return s;
221 }
222 #endif
223
224 static void pairing(void)
225 {
226 enter_command_mode();
227 SEND_COMMAND("SR,Z\r\n"); // remove remote address
228 SEND_COMMAND("R,1\r\n"); // reboot
229 exit_command_mode();
230 }
231
232 bool command_extra(uint8_t code)
233 {
234 uint32_t t;
235 uint16_t b;
236 switch (code) {
237 case KC_H:
238 case KC_SLASH: /* ? */
239 print("\n\n----- Bluetooth RN-42 Help -----\n");
240 print("i: RN-42 info\n");
241 print("b: battery voltage\n");
242 print("Del: enter/exit RN-42 config mode\n");
243 print("Slck: RN-42 initialize\n");
244 #if 0
245 print("1-4: restore link\n");
246 print("F1-F4: store link\n");
247 #endif
248 print("p: pairing\n");
249
250 if (config_mode) {
251 return true;
252 } else {
253 print("u: toggle Force USB mode\n");
254 return false; // to display default command help
255 }
256 case KC_P:
257 pairing();
258 return true;
259 #if 0
260 /* Store link address to EEPROM */
261 case KC_F1:
262 store_link(RN42_LINK0);
263 return true;
264 case KC_F2:
265 store_link(RN42_LINK1);
266 return true;
267 case KC_F3:
268 store_link(RN42_LINK2);
269 return true;
270 case KC_F4:
271 store_link(RN42_LINK3);
272 return true;
273 /* Restore link address to EEPROM */
274 case KC_1:
275 restore_link(RN42_LINK0);
276 return true;
277 case KC_2:
278 restore_link(RN42_LINK1);
279 return true;
280 case KC_3:
281 restore_link(RN42_LINK2);
282 return true;
283 case KC_4:
284 restore_link(RN42_LINK3);
285 return true;
286 #endif
287 case KC_I:
288 print("\n----- RN-42 info -----\n");
289 xprintf("protocol: %s\n", (host_get_driver() == &rn42_driver) ? "RN-42" : "LUFA");
290 xprintf("force_usb: %X\n", force_usb);
291 xprintf("rn42: %s\n", rn42_rts() ? "OFF" : (rn42_linked() ? "CONN" : "ON"));
292 xprintf("rn42_autoconnecting(): %X\n", rn42_autoconnecting());
293 xprintf("config_mode: %X\n", config_mode);
294 xprintf("USB State: %s\n",
295 (USB_DeviceState == DEVICE_STATE_Unattached) ? "Unattached" :
296 (USB_DeviceState == DEVICE_STATE_Powered) ? "Powered" :
297 (USB_DeviceState == DEVICE_STATE_Default) ? "Default" :
298 (USB_DeviceState == DEVICE_STATE_Addressed) ? "Addressed" :
299 (USB_DeviceState == DEVICE_STATE_Configured) ? "Configured" :
300 (USB_DeviceState == DEVICE_STATE_Suspended) ? "Suspended" : "?");
301 xprintf("battery: ");
302 switch (battery_status()) {
303 case FULL_CHARGED: xprintf("FULL"); break;
304 case CHARGING: xprintf("CHARG"); break;
305 case DISCHARGING: xprintf("DISCHG"); break;
306 case LOW_VOLTAGE: xprintf("LOW"); break;
307 default: xprintf("?"); break;
308 };
309 xprintf("\n");
310 xprintf("RemoteWakeupEnabled: %X\n", USB_Device_RemoteWakeupEnabled);
311 xprintf("VBUS: %X\n", USBSTA&(1<<VBUS));
312 t = timer_read32()/1000;
313 uint8_t d = t/3600/24;
314 uint8_t h = t/3600;
315 uint8_t m = t%3600/60;
316 uint8_t s = t%60;
317 xprintf("uptime: %02u %02u:%02u:%02u\n", d, h, m, s);
318 #if 0
319 xprintf("LINK0: %s\r\n", get_link(RN42_LINK0));
320 xprintf("LINK1: %s\r\n", get_link(RN42_LINK1));
321 xprintf("LINK2: %s\r\n", get_link(RN42_LINK2));
322 xprintf("LINK3: %s\r\n", get_link(RN42_LINK3));
323 #endif
324 return true;
325 case KC_B:
326 // battery monitor
327 t = timer_read32()/1000;
328 b = battery_voltage();
329 xprintf("BAT: %umV\t", b);
330 xprintf("%02u:", t/3600);
331 xprintf("%02u:", t%3600/60);
332 xprintf("%02u\n", t%60);
333 return true;
334 case KC_U:
335 if (config_mode) return false;
336 if (force_usb) {
337 print("Auto mode\n");
338 force_usb = false;
339 } else {
340 print("USB mode\n");
341 force_usb = true;
342 clear_keyboard();
343 host_set_driver(&lufa_driver);
344 }
345 return true;
346 case KC_DELETE:
347 /* RN-42 Command mode */
348 if (rn42_autoconnecting()) {
349 enter_command_mode();
350
351 command_state = CONSOLE;
352 config_mode = true;
353 } else {
354 exit_command_mode();
355
356 command_state = ONESHOT;
357 config_mode = false;
358 }
359 return true;
360 case KC_SCROLLLOCK:
361 init_rn42();
362 return true;
363 default:
364 if (config_mode)
365 return true;
366 else
367 return false; // yield to default command
368 }
369 return true;
370 }
371
372 /*
373 * RN-42 Command mode
374 * sends charactors to the module
375 */
376 static uint8_t code2asc(uint8_t code);
377 bool command_console_extra(uint8_t code)
378 {
379 rn42_putc(code2asc(code));
380 return true;
381 }
382
383 // convert keycode into ascii charactor
384 static uint8_t code2asc(uint8_t code)
385 {
386 bool shifted = (get_mods() & (MOD_BIT(KC_LSHIFT)|MOD_BIT(KC_RSHIFT))) ? true : false;
387 switch (code) {
388 case KC_A: return (shifted ? 'A' : 'a');
389 case KC_B: return (shifted ? 'B' : 'b');
390 case KC_C: return (shifted ? 'C' : 'c');
391 case KC_D: return (shifted ? 'D' : 'd');
392 case KC_E: return (shifted ? 'E' : 'e');
393 case KC_F: return (shifted ? 'F' : 'f');
394 case KC_G: return (shifted ? 'G' : 'g');
395 case KC_H: return (shifted ? 'H' : 'h');
396 case KC_I: return (shifted ? 'I' : 'i');
397 case KC_J: return (shifted ? 'J' : 'j');
398 case KC_K: return (shifted ? 'K' : 'k');
399 case KC_L: return (shifted ? 'L' : 'l');
400 case KC_M: return (shifted ? 'M' : 'm');
401 case KC_N: return (shifted ? 'N' : 'n');
402 case KC_O: return (shifted ? 'O' : 'o');
403 case KC_P: return (shifted ? 'P' : 'p');
404 case KC_Q: return (shifted ? 'Q' : 'q');
405 case KC_R: return (shifted ? 'R' : 'r');
406 case KC_S: return (shifted ? 'S' : 's');
407 case KC_T: return (shifted ? 'T' : 't');
408 case KC_U: return (shifted ? 'U' : 'u');
409 case KC_V: return (shifted ? 'V' : 'v');
410 case KC_W: return (shifted ? 'W' : 'w');
411 case KC_X: return (shifted ? 'X' : 'x');
412 case KC_Y: return (shifted ? 'Y' : 'y');
413 case KC_Z: return (shifted ? 'Z' : 'z');
414 case KC_1: return (shifted ? '!' : '1');
415 case KC_2: return (shifted ? '@' : '2');
416 case KC_3: return (shifted ? '#' : '3');
417 case KC_4: return (shifted ? '$' : '4');
418 case KC_5: return (shifted ? '%' : '5');
419 case KC_6: return (shifted ? '^' : '6');
420 case KC_7: return (shifted ? '&' : '7');
421 case KC_8: return (shifted ? '*' : '8');
422 case KC_9: return (shifted ? '(' : '9');
423 case KC_0: return (shifted ? ')' : '0');
424 case KC_ENTER: return '\n';
425 case KC_ESCAPE: return 0x1B;
426 case KC_BSPACE: return '\b';
427 case KC_TAB: return '\t';
428 case KC_SPACE: return ' ';
429 case KC_MINUS: return (shifted ? '_' : '-');
430 case KC_EQUAL: return (shifted ? '+' : '=');
431 case KC_LBRACKET: return (shifted ? '{' : '[');
432 case KC_RBRACKET: return (shifted ? '}' : ']');
433 case KC_BSLASH: return (shifted ? '|' : '\\');
434 case KC_NONUS_HASH: return (shifted ? '|' : '\\');
435 case KC_SCOLON: return (shifted ? ':' : ';');
436 case KC_QUOTE: return (shifted ? '"' : '\'');
437 case KC_GRAVE: return (shifted ? '~' : '`');
438 case KC_COMMA: return (shifted ? '<' : ',');
439 case KC_DOT: return (shifted ? '>' : '.');
440 case KC_SLASH: return (shifted ? '?' : '/');
441 case KC_DELETE: return '\0'; // Delete to disconnect
442 default: return ' ';
443 }
444 }
Imprint / Impressum