3 #include <avr/pgmspace.h>
4 #include <avr/eeprom.h>
9 #include "action_util.h"
11 #include "rn42_task.h"
19 static bool config_mode = false;
20 static bool force_usb = false;
22 static void status_led(bool on)
33 void rn42_task_init(void)
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;
48 if (c == 0xFE) state = LED_FE;
50 if (0x0 <= c && c <= 0x7f) xprintf("%c", c);
51 else xprintf(" %02X", c);
55 if (c == 0x02) state = LED_02;
56 else state = LED_INIT;
59 if (c == 0x01) state = LED_01;
60 else state = LED_INIT;
63 dprintf("LED status: %02X\n", c);
72 /* Bluetooth mode when ready */
73 if (!config_mode && !force_usb) {
74 if (!rn42_rts() && host_get_driver() != &rn42_driver) {
76 host_set_driver(&rn42_driver);
77 } else if (rn42_rts() && host_get_driver() != &lufa_driver) {
79 host_set_driver(&lufa_driver);
84 static uint16_t prev_timer = 0;
85 uint16_t e = timer_elapsed(prev_timer);
88 prev_timer += e/1000*1000;
90 /* Low voltage alert */
91 uint8_t bs = battery_status();
92 if (bs == LOW_VOLTAGE) {
95 battery_led(LED_CHARGER);
99 uint32_t t = timer_read32()/1000;
101 uint16_t v = battery_voltage();
103 uint8_t m = t%3600/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);
113 /* Connection monitor */
114 if (!rn42_rts() && rn42_linked()) {
123 /******************************************************************************
125 ******************************************************************************/
126 static host_driver_t *prev_driver = &rn42_driver;
128 static void enter_command_mode(void)
130 prev_driver = host_get_driver();
132 host_set_driver(&rn42_config_driver); // null driver; not to send a key to host
134 while (rn42_linked()) ;
136 print("Entering config mode ...\n");
137 wait_ms(1100); // need 1 sec
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
145 static void exit_command_mode(void)
147 print("Exiting config mode ...\n");
148 SEND_COMMAND("---\r\n"); // exit
152 host_set_driver(prev_driver);
155 static void init_rn42(void)
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();
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)
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));
196 static void restore_link(const uint8_t *eeaddr)
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);
207 SEND_COMMAND("\r\n");
208 SEND_COMMAND("R,1\r\n"); // reboot
212 static const char *get_link(uint8_t * eeaddr)
215 for (int i = 0; i < 12; i++) {
216 uint8_t c = eeprom_read_byte(eeaddr+i);
224 static void pairing(void)
226 enter_command_mode();
227 SEND_COMMAND("SR,Z\r\n"); // remove remote address
228 SEND_COMMAND("R,1\r\n"); // reboot
232 bool command_extra(uint8_t code)
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");
245 print("1-4: restore link\n");
246 print("F1-F4: store link\n");
248 print("p: pairing\n");
253 print("u: toggle Force USB mode\n");
254 return false; // to display default command help
260 /* Store link address to EEPROM */
262 store_link(RN42_LINK0);
265 store_link(RN42_LINK1);
268 store_link(RN42_LINK2);
271 store_link(RN42_LINK3);
273 /* Restore link address to EEPROM */
275 restore_link(RN42_LINK0);
278 restore_link(RN42_LINK1);
281 restore_link(RN42_LINK2);
284 restore_link(RN42_LINK3);
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;
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;
315 uint8_t m = t%3600/60;
317 xprintf("uptime: %02u %02u:%02u:%02u\n", d, h, m, s);
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));
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);
335 if (config_mode) return false;
337 print("Auto mode\n");
343 host_set_driver(&lufa_driver);
347 /* RN-42 Command mode */
348 if (rn42_autoconnecting()) {
349 enter_command_mode();
351 command_state = CONSOLE;
356 command_state = ONESHOT;
367 return false; // yield to default command
374 * sends charactors to the module
376 static uint8_t code2asc(uint8_t code);
377 bool command_console_extra(uint8_t code)
379 rn42_putc(code2asc(code));
383 // convert keycode into ascii charactor
384 static uint8_t code2asc(uint8_t code)
386 bool shifted = (get_mods() & (MOD_BIT(KC_LSHIFT)|MOD_BIT(KC_RSHIFT))) ? true : false;
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