You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.

rn42_task.c 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478
  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. static bool config_mode = false;
  19. static bool force_usb = false;
  20. static void status_led(bool on)
  21. {
  22. if (on) {
  23. DDRE |= (1<<6);
  24. PORTE &= ~(1<<6);
  25. } else {
  26. DDRE |= (1<<6);
  27. PORTE |= (1<<6);
  28. }
  29. }
  30. void rn42_task_init(void)
  31. {
  32. battery_init();
  33. }
  34. void rn42_task(void)
  35. {
  36. int16_t c;
  37. // Raw mode: interpret output report of LED state
  38. while ((c = rn42_getc()) != -1) {
  39. // LED Out report: 0xFE, 0x02, 0x01, <leds>
  40. // To get the report over UART set bit3 with SH, command.
  41. static enum {LED_INIT, LED_FE, LED_02, LED_01} state = LED_INIT;
  42. switch (state) {
  43. case LED_INIT:
  44. if (c == 0xFE) state = LED_FE;
  45. else {
  46. if (0x0 <= c && c <= 0x7f) xprintf("%c", c);
  47. else xprintf(" %02X", c);
  48. }
  49. break;
  50. case LED_FE:
  51. if (c == 0x02) state = LED_02;
  52. else state = LED_INIT;
  53. break;
  54. case LED_02:
  55. if (c == 0x01) state = LED_01;
  56. else state = LED_INIT;
  57. break;
  58. case LED_01:
  59. dprintf("LED status: %02X\n", c);
  60. rn42_set_leds(c);
  61. state = LED_INIT;
  62. break;
  63. default:
  64. state = LED_INIT;
  65. }
  66. }
  67. /* Bluetooth mode when ready */
  68. if (!config_mode && !force_usb) {
  69. if (!rn42_rts() && host_get_driver() != &rn42_driver) {
  70. clear_keyboard();
  71. host_set_driver(&rn42_driver);
  72. } else if (rn42_rts() && host_get_driver() != &lufa_driver) {
  73. clear_keyboard();
  74. host_set_driver(&lufa_driver);
  75. }
  76. }
  77. static uint16_t prev_timer = 0;
  78. uint16_t e = timer_elapsed(prev_timer);
  79. if (e > 1000) {
  80. /* every second */
  81. prev_timer += e/1000*1000;
  82. /* Low voltage alert */
  83. uint8_t bs = battery_status();
  84. if (bs == LOW_VOLTAGE) {
  85. battery_led(LED_ON);
  86. } else {
  87. battery_led(LED_CHARGER);
  88. }
  89. /* every minute */
  90. uint32_t t = timer_read32()/1000;
  91. if (t%60 == 0) {
  92. uint16_t v = battery_voltage();
  93. uint8_t h = t/3600;
  94. uint8_t m = t%3600/60;
  95. uint8_t s = t%60;
  96. dprintf("%02u:%02u:%02u\t%umV\n", h, m, s, v);
  97. /* TODO: xprintf doesn't work for this.
  98. xprintf("%02u:%02u:%02u\t%umV\n", (t/3600), (t%3600/60), (t%60), v);
  99. */
  100. }
  101. }
  102. /* Connection monitor */
  103. if (!rn42_rts() && rn42_linked()) {
  104. status_led(true);
  105. } else {
  106. status_led(false);
  107. }
  108. }
  109. /******************************************************************************
  110. * Command
  111. ******************************************************************************/
  112. static host_driver_t *prev_driver = &rn42_driver;
  113. static void print_rn42(void)
  114. {
  115. int16_t c;
  116. while ((c = rn42_getc()) != -1) {
  117. xprintf("%c", c);
  118. }
  119. }
  120. static void clear_rn42(void)
  121. {
  122. while (rn42_getc() != -1) ;
  123. }
  124. #define SEND_STR(str) send_str(PSTR(str))
  125. #define SEND_COMMAND(cmd) send_command(PSTR(cmd))
  126. static void send_str(const char *str)
  127. {
  128. uint8_t c;
  129. while ((c = pgm_read_byte(str++)))
  130. rn42_putc(c);
  131. }
  132. static const char *send_command(const char *cmd)
  133. {
  134. static const char *s;
  135. send_str(cmd);
  136. wait_ms(500);
  137. s = rn42_gets(100);
  138. xprintf("%s\r\n", s);
  139. print_rn42();
  140. return s;
  141. }
  142. static void enter_command_mode(void)
  143. {
  144. prev_driver = host_get_driver();
  145. clear_keyboard();
  146. host_set_driver(&rn42_config_driver); // null driver; not to send a key to host
  147. rn42_disconnect();
  148. while (rn42_linked()) ;
  149. print("Entering config mode ...\n");
  150. wait_ms(1100); // need 1 sec
  151. SEND_COMMAND("$$$");
  152. wait_ms(600); // need 1 sec
  153. print_rn42();
  154. const char *s = SEND_COMMAND("v\r\n");
  155. if (strncmp("v", s, 1) != 0) SEND_COMMAND("+\r\n"); // local echo on
  156. }
  157. static void exit_command_mode(void)
  158. {
  159. print("Exiting config mode ...\n");
  160. SEND_COMMAND("---\r\n"); // exit
  161. rn42_autoconnect();
  162. clear_keyboard();
  163. host_set_driver(prev_driver);
  164. }
  165. static void init_rn42(void)
  166. {
  167. // RN-42 configure
  168. if (!config_mode) enter_command_mode();
  169. SEND_COMMAND("SF,1\r\n"); // factory defaults
  170. SEND_COMMAND("S-,TmkBT\r\n");
  171. SEND_COMMAND("SS,Keyboard/Mouse\r\n");
  172. SEND_COMMAND("SM,4\r\n"); // auto connect(DTR)
  173. SEND_COMMAND("SW,8000\r\n"); // Sniff disable
  174. SEND_COMMAND("S~,6\r\n"); // HID profile
  175. SEND_COMMAND("SH,003C\r\n"); // combo device, out-report, 4-reconnect
  176. SEND_COMMAND("SY,FFF4\r\n"); // transmit power -12
  177. SEND_COMMAND("R,1\r\n");
  178. if (!config_mode) exit_command_mode();
  179. }
  180. #if 0
  181. // Switching connections
  182. // NOTE: Remote Address doesn't work in the way manual says.
  183. // EEPROM address for link store
  184. #define RN42_LINK0 (uint8_t *)128
  185. #define RN42_LINK1 (uint8_t *)140
  186. #define RN42_LINK2 (uint8_t *)152
  187. #define RN42_LINK3 (uint8_t *)164
  188. static void store_link(uint8_t *eeaddr)
  189. {
  190. enter_command_mode();
  191. SEND_STR("GR\r\n"); // remote address
  192. const char *s = rn42_gets(500);
  193. if (strcmp("GR", s) == 0) s = rn42_gets(500); // ignore local echo
  194. xprintf("%s(%d)\r\n", s, strlen(s));
  195. if (strlen(s) == 12) {
  196. for (int i = 0; i < 12; i++) {
  197. eeprom_write_byte(eeaddr+i, *(s+i));
  198. dprintf("%c ", *(s+i));
  199. }
  200. dprint("\r\n");
  201. }
  202. exit_command_mode();
  203. }
  204. static void restore_link(const uint8_t *eeaddr)
  205. {
  206. enter_command_mode();
  207. SEND_COMMAND("SR,Z\r\n"); // remove remote address
  208. SEND_STR("SR,"); // set remote address from EEPROM
  209. for (int i = 0; i < 12; i++) {
  210. uint8_t c = eeprom_read_byte(eeaddr+i);
  211. rn42_putc(c);
  212. dprintf("%c ", c);
  213. }
  214. dprintf("\r\n");
  215. SEND_COMMAND("\r\n");
  216. SEND_COMMAND("R,1\r\n"); // reboot
  217. exit_command_mode();
  218. }
  219. static const char *get_link(uint8_t * eeaddr)
  220. {
  221. static char s[13];
  222. for (int i = 0; i < 12; i++) {
  223. uint8_t c = eeprom_read_byte(eeaddr+i);
  224. s[i] = c;
  225. }
  226. s[12] = '\0';
  227. return s;
  228. }
  229. #endif
  230. static void pairing(void)
  231. {
  232. enter_command_mode();
  233. SEND_COMMAND("SR,Z\r\n"); // remove remote address
  234. SEND_COMMAND("R,1\r\n"); // reboot
  235. exit_command_mode();
  236. }
  237. bool command_extra(uint8_t code)
  238. {
  239. uint32_t t;
  240. uint16_t b;
  241. switch (code) {
  242. case KC_H:
  243. case KC_SLASH: /* ? */
  244. print("\n\n----- Bluetooth RN-42 Help -----\n");
  245. print("i: RN-42 info\n");
  246. print("b: battery voltage\n");
  247. print("Del: enter/exit RN-42 config mode\n");
  248. print("Slck: RN-42 initialize\n");
  249. #if 0
  250. print("1-4: restore link\n");
  251. print("F1-F4: store link\n");
  252. #endif
  253. print("p: pairing\n");
  254. if (config_mode) {
  255. return true;
  256. } else {
  257. print("u: toggle Force USB mode\n");
  258. return false; // to display default command help
  259. }
  260. case KC_P:
  261. pairing();
  262. return true;
  263. #if 0
  264. /* Store link address to EEPROM */
  265. case KC_F1:
  266. store_link(RN42_LINK0);
  267. return true;
  268. case KC_F2:
  269. store_link(RN42_LINK1);
  270. return true;
  271. case KC_F3:
  272. store_link(RN42_LINK2);
  273. return true;
  274. case KC_F4:
  275. store_link(RN42_LINK3);
  276. return true;
  277. /* Restore link address to EEPROM */
  278. case KC_1:
  279. restore_link(RN42_LINK0);
  280. return true;
  281. case KC_2:
  282. restore_link(RN42_LINK1);
  283. return true;
  284. case KC_3:
  285. restore_link(RN42_LINK2);
  286. return true;
  287. case KC_4:
  288. restore_link(RN42_LINK3);
  289. return true;
  290. #endif
  291. case KC_I:
  292. print("\n----- RN-42 info -----\n");
  293. xprintf("protocol: %s\n", (host_get_driver() == &rn42_driver) ? "RN-42" : "LUFA");
  294. xprintf("force_usb: %X\n", force_usb);
  295. xprintf("rn42: %s\n", rn42_rts() ? "OFF" : (rn42_linked() ? "CONN" : "ON"));
  296. xprintf("rn42_autoconnecting(): %X\n", rn42_autoconnecting());
  297. xprintf("config_mode: %X\n", config_mode);
  298. xprintf("USB State: %s\n",
  299. (USB_DeviceState == DEVICE_STATE_Unattached) ? "Unattached" :
  300. (USB_DeviceState == DEVICE_STATE_Powered) ? "Powered" :
  301. (USB_DeviceState == DEVICE_STATE_Default) ? "Default" :
  302. (USB_DeviceState == DEVICE_STATE_Addressed) ? "Addressed" :
  303. (USB_DeviceState == DEVICE_STATE_Configured) ? "Configured" :
  304. (USB_DeviceState == DEVICE_STATE_Suspended) ? "Suspended" : "?");
  305. xprintf("battery: ");
  306. switch (battery_status()) {
  307. case FULL_CHARGED: xprintf("FULL"); break;
  308. case CHARGING: xprintf("CHARG"); break;
  309. case DISCHARGING: xprintf("DISCHG"); break;
  310. case LOW_VOLTAGE: xprintf("LOW"); break;
  311. default: xprintf("?"); break;
  312. };
  313. xprintf("\n");
  314. xprintf("RemoteWakeupEnabled: %X\n", USB_Device_RemoteWakeupEnabled);
  315. xprintf("VBUS: %X\n", USBSTA&(1<<VBUS));
  316. t = timer_read32()/1000;
  317. uint8_t d = t/3600/24;
  318. uint8_t h = t/3600;
  319. uint8_t m = t%3600/60;
  320. uint8_t s = t%60;
  321. xprintf("uptime: %02u %02u:%02u:%02u\n", d, h, m, s);
  322. #if 0
  323. xprintf("LINK0: %s\r\n", get_link(RN42_LINK0));
  324. xprintf("LINK1: %s\r\n", get_link(RN42_LINK1));
  325. xprintf("LINK2: %s\r\n", get_link(RN42_LINK2));
  326. xprintf("LINK3: %s\r\n", get_link(RN42_LINK3));
  327. #endif
  328. return true;
  329. case KC_B:
  330. // battery monitor
  331. t = timer_read32()/1000;
  332. b = battery_voltage();
  333. xprintf("BAT: %umV\t", b);
  334. xprintf("%02u:", t/3600);
  335. xprintf("%02u:", t%3600/60);
  336. xprintf("%02u\n", t%60);
  337. return true;
  338. case KC_U:
  339. if (config_mode) return false;
  340. if (force_usb) {
  341. print("Auto mode\n");
  342. force_usb = false;
  343. } else {
  344. print("USB mode\n");
  345. force_usb = true;
  346. clear_keyboard();
  347. host_set_driver(&lufa_driver);
  348. }
  349. return true;
  350. case KC_DELETE:
  351. /* RN-42 Command mode */
  352. if (rn42_autoconnecting()) {
  353. enter_command_mode();
  354. command_state = CONSOLE;
  355. config_mode = true;
  356. } else {
  357. exit_command_mode();
  358. command_state = ONESHOT;
  359. config_mode = false;
  360. }
  361. return true;
  362. case KC_SCROLLLOCK:
  363. init_rn42();
  364. return true;
  365. default:
  366. if (config_mode)
  367. return true;
  368. else
  369. return false; // yield to default command
  370. }
  371. return true;
  372. }
  373. /*
  374. * RN-42 Command mode
  375. * sends charactors to the module
  376. */
  377. static uint8_t code2asc(uint8_t code);
  378. bool command_console_extra(uint8_t code)
  379. {
  380. rn42_putc(code2asc(code));
  381. return true;
  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. }