6 #include "action_util.h"
14 static bool config_mode = false;
15 static bool force_usb = false;
17 static void status_led(bool on)
28 void rn42_task_init(void)
37 // Config mode: print output from RN-42
38 while ((c = serial_recv2()) != -1) {
39 // without flow control it'll fail to receive data when flooded
43 // Raw mode: interpret output report of LED state
44 while ((c = serial_recv2()) != -1) {
45 // LED Out report: 0xFE, 0x02, 0x01, <leds>
46 // To get the report over UART set bit3 with SH, command.
47 static enum {LED_INIT, LED_FE, LED_02, LED_01} state = LED_INIT;
51 if (c == 0xFE) state = LED_FE;
52 else state = LED_INIT;
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 // TODO: move to rn42.c and make accessible with keyboard_leds()
64 xprintf("LED status: %02X\n", c);
73 /* Bluetooth mode when ready */
74 if (!config_mode && !force_usb) {
75 if (!rn42_rts() && host_get_driver() != &rn42_driver) {
77 host_set_driver(&rn42_driver);
78 } else if (rn42_rts() && host_get_driver() != &lufa_driver) {
80 host_set_driver(&lufa_driver);
85 static uint16_t prev_timer = 0;
86 uint16_t e = timer_elapsed(prev_timer);
89 prev_timer += e/1000*1000;
91 /* Low voltage alert */
92 uint8_t bs = battery_status();
93 if (bs == LOW_VOLTAGE) {
96 battery_led(LED_CHARGER);
99 static uint8_t prev_status = UNKNOWN;
100 if (bs != prev_status) {
103 case FULL_CHARGED: xprintf("FULL_CHARGED\n"); break;
104 case CHARGING: xprintf("CHARGING\n"); break;
105 case DISCHARGING: xprintf("DISCHARGING\n"); break;
106 case LOW_VOLTAGE: xprintf("LOW_VOLTAGE\n"); break;
107 default: xprintf("UNKNOWN STATUS\n"); break;
112 uint32_t t = timer_read32()/1000;
114 uint16_t v = battery_voltage();
116 uint8_t m = t%3600/60;
118 xprintf("%02u:%02u:%02u\t%umV\n", h, m, s, v);
119 /* TODO: xprintf doesn't work for this.
120 xprintf("%02u:%02u:%02u\t%umV\n", (t/3600), (t%3600/60), (t%60), v);
126 /* Connection monitor */
136 /******************************************************************************
138 ******************************************************************************/
139 bool command_extra(uint8_t code)
143 static host_driver_t *prev_driver = &rn42_driver;
146 case KC_SLASH: /* ? */
147 print("\n\n----- Bluetooth RN-42 Help -----\n");
148 print("Del: enter/exit config mode(auto_connect/disconnect)\n");
149 print("i: RN-42 info\n");
150 print("b: battery voltage\n");
155 print("u: Force USB mode\n");
156 return false; // to display default command help
159 if (rn42_autoconnecting()) {
160 prev_driver = host_get_driver();
163 host_set_driver(&rn42_config_driver); // null driver; not to send a key to host
165 print("\nRN-42: disconnect\n");
166 print("Enter config mode\n");
167 print("type $$$ to start and + for local echo\n");
168 command_state = CONSOLE;
172 print("\nRN-42: auto_connect\n");
173 print("Exit config mode\n");
174 command_state = ONESHOT;
177 host_set_driver(prev_driver);
181 if (config_mode) return false;
183 print("Auto mode\n");
189 host_set_driver(&lufa_driver);
193 print("\n----- RN-42 info -----\n");
194 xprintf("protocol: %s\n", (host_get_driver() == &rn42_driver) ? "RN-42" : "LUFA");
195 xprintf("force_usb: %X\n", force_usb);
196 xprintf("rn42_autoconnecting(): %X\n", rn42_autoconnecting());
197 xprintf("rn42_linked(): %X\n", rn42_linked());
198 xprintf("rn42_rts(): %X\n", rn42_rts());
199 xprintf("config_mode: %X\n", config_mode);
200 xprintf("VBUS: %X\n", USBSTA&(1<<VBUS));
201 xprintf("battery_charging: %X\n", battery_charging());
202 xprintf("battery_status: %X\n", battery_status());
206 t = timer_read32()/1000;
207 b = battery_voltage();
208 xprintf("BAT: %umV\t", b);
209 xprintf("%02u:", t/3600);
210 xprintf("%02u:", t%3600/60);
211 xprintf("%02u\n", t%60);
217 return false; // exec default command
222 static uint8_t code2asc(uint8_t code);
223 bool command_console_extra(uint8_t code)
227 rn42_putc(code2asc(code));
233 // convert keycode into ascii charactor
234 static uint8_t code2asc(uint8_t code)
236 bool shifted = (get_mods() & (MOD_BIT(KC_LSHIFT)|MOD_BIT(KC_RSHIFT))) ? true : false;
238 case KC_A: return (shifted ? 'A' : 'a');
239 case KC_B: return (shifted ? 'B' : 'b');
240 case KC_C: return (shifted ? 'C' : 'c');
241 case KC_D: return (shifted ? 'D' : 'd');
242 case KC_E: return (shifted ? 'E' : 'e');
243 case KC_F: return (shifted ? 'F' : 'f');
244 case KC_G: return (shifted ? 'G' : 'g');
245 case KC_H: return (shifted ? 'H' : 'h');
246 case KC_I: return (shifted ? 'I' : 'i');
247 case KC_J: return (shifted ? 'J' : 'j');
248 case KC_K: return (shifted ? 'K' : 'k');
249 case KC_L: return (shifted ? 'L' : 'l');
250 case KC_M: return (shifted ? 'M' : 'm');
251 case KC_N: return (shifted ? 'N' : 'n');
252 case KC_O: return (shifted ? 'O' : 'o');
253 case KC_P: return (shifted ? 'P' : 'p');
254 case KC_Q: return (shifted ? 'Q' : 'q');
255 case KC_R: return (shifted ? 'R' : 'r');
256 case KC_S: return (shifted ? 'S' : 's');
257 case KC_T: return (shifted ? 'T' : 't');
258 case KC_U: return (shifted ? 'U' : 'u');
259 case KC_V: return (shifted ? 'V' : 'v');
260 case KC_W: return (shifted ? 'W' : 'w');
261 case KC_X: return (shifted ? 'X' : 'x');
262 case KC_Y: return (shifted ? 'Y' : 'y');
263 case KC_Z: return (shifted ? 'Z' : 'z');
264 case KC_1: return (shifted ? '!' : '1');
265 case KC_2: return (shifted ? '@' : '2');
266 case KC_3: return (shifted ? '#' : '3');
267 case KC_4: return (shifted ? '$' : '4');
268 case KC_5: return (shifted ? '%' : '5');
269 case KC_6: return (shifted ? '^' : '6');
270 case KC_7: return (shifted ? '&' : '7');
271 case KC_8: return (shifted ? '*' : '8');
272 case KC_9: return (shifted ? '(' : '9');
273 case KC_0: return (shifted ? ')' : '0');
274 case KC_ENTER: return '\n';
275 case KC_ESCAPE: return 0x1B;
276 case KC_BSPACE: return '\b';
277 case KC_TAB: return '\t';
278 case KC_SPACE: return ' ';
279 case KC_MINUS: return (shifted ? '_' : '-');
280 case KC_EQUAL: return (shifted ? '+' : '=');
281 case KC_LBRACKET: return (shifted ? '{' : '[');
282 case KC_RBRACKET: return (shifted ? '}' : ']');
283 case KC_BSLASH: return (shifted ? '|' : '\\');
284 case KC_NONUS_HASH: return (shifted ? '|' : '\\');
285 case KC_SCOLON: return (shifted ? ':' : ';');
286 case KC_QUOTE: return (shifted ? '"' : '\'');
287 case KC_GRAVE: return (shifted ? '~' : '`');
288 case KC_COMMA: return (shifted ? '<' : ',');
289 case KC_DOT: return (shifted ? '>' : '.');
290 case KC_SLASH: return (shifted ? '?' : '/');
291 case KC_DELETE: return '\0'; // Delete to disconnect