Keyboard firmwares for Atmel AVR and Cortex-M
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.

rn42_task.c 9.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. #include <stdint.h>
  2. #include "keycode.h"
  3. #include "serial.h"
  4. #include "host.h"
  5. #include "action.h"
  6. #include "action_util.h"
  7. #include "lufa.h"
  8. #include "rn42_task.h"
  9. #include "print.h"
  10. #include "timer.h"
  11. #include "command.h"
  12. static bool config_mode = false;
  13. static bool force_usb = false;
  14. static void status_led(bool on)
  15. {
  16. if (on) {
  17. DDRE |= (1<<6);
  18. PORTE &= ~(1<<6);
  19. } else {
  20. DDRE |= (1<<6);
  21. PORTE |= (1<<6);
  22. }
  23. }
  24. static void battery_adc_init(void)
  25. {
  26. ADMUX = (1<<REFS1) | (1<<REFS0); // Ref:2.56V band-gap, Input:ADC0(PF0)
  27. ADCSRA = (1<<ADPS2) | (1<<ADPS1) | (1<<ADPS0); // Prescale:128 16MHz/128=125KHz
  28. ADCSRA |= (1<<ADEN); // enable ADC
  29. }
  30. static uint16_t battery_adc(void)
  31. {
  32. volatile uint16_t bat;
  33. ADCSRA |= (1<<ADEN);
  34. // discard first result
  35. ADCSRA |= (1<<ADSC);
  36. while (ADCSRA & (1<<ADSC)) ;
  37. bat = ADC;
  38. // discard second result
  39. ADCSRA |= (1<<ADSC);
  40. while (ADCSRA & (1<<ADSC)) ;
  41. bat = ADC;
  42. ADCSRA |= (1<<ADSC);
  43. while (ADCSRA & (1<<ADSC)) ;
  44. bat = ADC;
  45. ADCSRA &= ~(1<<ADEN);
  46. return bat;
  47. }
  48. static void battery_led(bool on)
  49. {
  50. if (on) {
  51. DDRF |= (1<<5);
  52. PORTF &= ~(1<<5); // Low
  53. } else {
  54. DDRF &= ~(1<<5);
  55. PORTF &= ~(1<<5); // HiZ
  56. }
  57. }
  58. static bool battery_charging(void)
  59. {
  60. // MCP73831:STAT
  61. // Hi-Z: Shutdown/No Battery
  62. // Low: Charging
  63. // Hi: Charged
  64. DDRF &= ~(1<<5);
  65. PORTF |= (1<<5);
  66. return PINF&(1<<5) ? false : true;
  67. }
  68. void rn42_task_init(void)
  69. {
  70. battery_adc_init();
  71. // battery charging(HiZ)
  72. DDRF &= ~(1<<5);
  73. PORTF &= ~(1<<5);
  74. }
  75. void rn42_task(void)
  76. {
  77. int16_t c;
  78. if (config_mode) {
  79. // Config mode: print output from RN-42
  80. while ((c = serial_recv2()) != -1) {
  81. // without flow control it'll fail to receive data when flooded
  82. xprintf("%c", c);
  83. }
  84. } else {
  85. // Raw mode: interpret output report of LED state
  86. while ((c = serial_recv2()) != -1) {
  87. // LED Out report: 0xFE, 0x02, 0x01, <leds>
  88. // To get the report over UART set bit3 with SH, command.
  89. static enum {LED_INIT, LED_FE, LED_02, LED_01} state = LED_INIT;
  90. xprintf("%02X\n", c);
  91. switch (state) {
  92. case LED_INIT:
  93. if (c == 0xFE) state = LED_FE;
  94. else state = LED_INIT;
  95. break;
  96. case LED_FE:
  97. if (c == 0x02) state = LED_02;
  98. else state = LED_INIT;
  99. break;
  100. case LED_02:
  101. if (c == 0x01) state = LED_01;
  102. else state = LED_INIT;
  103. break;
  104. case LED_01:
  105. // TODO: move to rn42.c and make accessible with keyboard_leds()
  106. xprintf("LED status: %02X\n", c);
  107. state = LED_INIT;
  108. break;
  109. default:
  110. state = LED_INIT;
  111. }
  112. }
  113. }
  114. /* Bluetooth mode when ready */
  115. if (!config_mode && !force_usb) {
  116. if (!rn42_rts() && host_get_driver() != &rn42_driver) {
  117. clear_keyboard();
  118. host_set_driver(&rn42_driver);
  119. } else if (rn42_rts() && host_get_driver() != &lufa_driver) {
  120. clear_keyboard();
  121. host_set_driver(&lufa_driver);
  122. }
  123. }
  124. /* Battery monitor */
  125. /* Connection monitor */
  126. if (rn42_linked()) {
  127. status_led(true);
  128. } else {
  129. status_led(false);
  130. }
  131. }
  132. /******************************************************************************
  133. * Command
  134. ******************************************************************************/
  135. bool command_extra(uint8_t code)
  136. {
  137. uint32_t t;
  138. uint16_t b;
  139. static host_driver_t *prev_driver = &rn42_driver;
  140. switch (code) {
  141. case KC_H:
  142. case KC_SLASH: /* ? */
  143. print("\n\n----- Bluetooth RN-42 Help -----\n");
  144. print("Del: enter/exit config mode(auto_connect/disconnect)\n");
  145. print("i: RN-42 info\n");
  146. print("b: battery voltage\n");
  147. if (config_mode) {
  148. return true;
  149. } else {
  150. print("u: Force USB mode\n");
  151. return false; // to display default command help
  152. }
  153. case KC_DELETE:
  154. if (rn42_autoconnecting()) {
  155. prev_driver = host_get_driver();
  156. clear_keyboard();
  157. _delay_ms(500);
  158. host_set_driver(&rn42_config_driver); // null driver; not to send a key to host
  159. rn42_disconnect();
  160. print("\nRN-42: disconnect\n");
  161. print("Enter config mode\n");
  162. print("type $$$ to start and + for local echo\n");
  163. command_state = CONSOLE;
  164. config_mode = true;
  165. } else {
  166. rn42_autoconnect();
  167. print("\nRN-42: auto_connect\n");
  168. print("Exit config mode\n");
  169. command_state = ONESHOT;
  170. config_mode = false;
  171. //clear_keyboard();
  172. host_set_driver(prev_driver);
  173. }
  174. return true;
  175. case KC_U:
  176. if (config_mode) return false;
  177. if (force_usb) {
  178. print("Auto mode\n");
  179. force_usb = false;
  180. } else {
  181. print("USB mode\n");
  182. force_usb = true;
  183. clear_keyboard();
  184. host_set_driver(&lufa_driver);
  185. }
  186. return true;
  187. case KC_I:
  188. print("\n----- RN-42 info -----\n");
  189. xprintf("protocol: %s\n", (host_get_driver() == &rn42_driver) ? "RN-42" : "LUFA");
  190. xprintf("force_usb: %X\n", force_usb);
  191. xprintf("rn42_autoconnecting(): %X\n", rn42_autoconnecting());
  192. xprintf("rn42_linked(): %X\n", rn42_linked());
  193. xprintf("rn42_rts(): %X\n", rn42_rts());
  194. xprintf("config_mode: %X\n", config_mode);
  195. xprintf("VBUS: %X\n", USBSTA&(1<<VBUS));
  196. xprintf("battery_charging: %X\n", battery_charging());
  197. return true;
  198. case KC_B:
  199. // battery monitor
  200. t = timer_read32()/1000;
  201. b = battery_adc();
  202. xprintf("BAT: %umV(%04X)\t", (b-16)*5, b);
  203. xprintf("%02u:", t/3600);
  204. xprintf("%02u:", t%3600/60);
  205. xprintf("%02u\n", t%60);
  206. return true;
  207. default:
  208. if (config_mode)
  209. return true;
  210. else
  211. return false; // exec default command
  212. }
  213. return true;
  214. }
  215. static uint8_t code2asc(uint8_t code);
  216. bool command_console_extra(uint8_t code)
  217. {
  218. switch (code) {
  219. default:
  220. rn42_putc(code2asc(code));
  221. return true;
  222. }
  223. return false;
  224. }
  225. // convert keycode into ascii charactor
  226. static uint8_t code2asc(uint8_t code)
  227. {
  228. bool shifted = (get_mods() & (MOD_BIT(KC_LSHIFT)|MOD_BIT(KC_RSHIFT))) ? true : false;
  229. switch (code) {
  230. case KC_A: return (shifted ? 'A' : 'a');
  231. case KC_B: return (shifted ? 'B' : 'b');
  232. case KC_C: return (shifted ? 'C' : 'c');
  233. case KC_D: return (shifted ? 'D' : 'd');
  234. case KC_E: return (shifted ? 'E' : 'e');
  235. case KC_F: return (shifted ? 'F' : 'f');
  236. case KC_G: return (shifted ? 'G' : 'g');
  237. case KC_H: return (shifted ? 'H' : 'h');
  238. case KC_I: return (shifted ? 'I' : 'i');
  239. case KC_J: return (shifted ? 'J' : 'j');
  240. case KC_K: return (shifted ? 'K' : 'k');
  241. case KC_L: return (shifted ? 'L' : 'l');
  242. case KC_M: return (shifted ? 'M' : 'm');
  243. case KC_N: return (shifted ? 'N' : 'n');
  244. case KC_O: return (shifted ? 'O' : 'o');
  245. case KC_P: return (shifted ? 'P' : 'p');
  246. case KC_Q: return (shifted ? 'Q' : 'q');
  247. case KC_R: return (shifted ? 'R' : 'r');
  248. case KC_S: return (shifted ? 'S' : 's');
  249. case KC_T: return (shifted ? 'T' : 't');
  250. case KC_U: return (shifted ? 'U' : 'u');
  251. case KC_V: return (shifted ? 'V' : 'v');
  252. case KC_W: return (shifted ? 'W' : 'w');
  253. case KC_X: return (shifted ? 'X' : 'x');
  254. case KC_Y: return (shifted ? 'Y' : 'y');
  255. case KC_Z: return (shifted ? 'Z' : 'z');
  256. case KC_1: return (shifted ? '!' : '1');
  257. case KC_2: return (shifted ? '@' : '2');
  258. case KC_3: return (shifted ? '#' : '3');
  259. case KC_4: return (shifted ? '$' : '4');
  260. case KC_5: return (shifted ? '%' : '5');
  261. case KC_6: return (shifted ? '^' : '6');
  262. case KC_7: return (shifted ? '&' : '7');
  263. case KC_8: return (shifted ? '*' : '8');
  264. case KC_9: return (shifted ? '(' : '9');
  265. case KC_0: return (shifted ? ')' : '0');
  266. case KC_ENTER: return '\n';
  267. case KC_ESCAPE: return 0x1B;
  268. case KC_BSPACE: return '\b';
  269. case KC_TAB: return '\t';
  270. case KC_SPACE: return ' ';
  271. case KC_MINUS: return (shifted ? '_' : '-');
  272. case KC_EQUAL: return (shifted ? '+' : '=');
  273. case KC_LBRACKET: return (shifted ? '{' : '[');
  274. case KC_RBRACKET: return (shifted ? '}' : ']');
  275. case KC_BSLASH: return (shifted ? '|' : '\\');
  276. case KC_NONUS_HASH: return (shifted ? '|' : '\\');
  277. case KC_SCOLON: return (shifted ? ':' : ';');
  278. case KC_QUOTE: return (shifted ? '"' : '\'');
  279. case KC_GRAVE: return (shifted ? '~' : '`');
  280. case KC_COMMA: return (shifted ? '<' : ',');
  281. case KC_DOT: return (shifted ? '>' : '.');
  282. case KC_SLASH: return (shifted ? '?' : '/');
  283. case KC_DELETE: return '\0'; // Delete to disconnect
  284. default: return ' ';
  285. }
  286. }