13 #include "action_util.h"
17 bool config_mode = false;
19 static int8_t sendchar_func(uint8_t c)
25 static void SetupHardware(void)
27 /* Disable watchdog if enabled by bootloader/fuses */
28 MCUSR &= ~(1 << WDRF);
31 /* Disable clock division */
32 clock_prescale_set(clock_div_1);
34 // Leonardo needs. Without this USB device is not recognized.
40 USB_Device_EnableSOFEvents();
41 print_set_sendchar(sendchar_func);
43 // SUART PD0:output, PD1:input
53 static bool force_usb = false;
54 int main(void) __attribute__ ((weak));
60 /* wait for USB startup to get ready for debug output */
61 uint8_t timeout = 255; // timeout when USB is not available(Bluetooth)
62 while (timeout-- && USB_DeviceState != DEVICE_STATE_Configured) {
64 #if defined(INTERRUPT_CONTROL_ENDPOINT)
70 print("USB configured.\n");
73 print("RN-42 init\n");
79 host_set_driver(&rn42_driver);
81 host_set_driver(&lufa_driver);
84 #ifdef SLEEP_LED_ENABLE
89 //ADMUX = (1<<REFS0); // Ref:AVCC, Input:ADC0(PF0)
90 ADMUX = (1<<REFS1) | (1<<REFS0); // Ref:AVCC, Input:ADC0(PF0)
91 ADCSRA = (1<<ADPS2) | (1<<ADPS1) | (1<<ADPS0); // Prescale:128
92 ADCSRA |= (1<<ADEN); // enable ADC
94 print("Keyboard start.\n");
97 while (USB_DeviceState == DEVICE_STATE_Suspended) {
99 if (USB_Device_RemoteWakeupEnabled && suspend_wakeup_condition()) {
100 USB_Device_SendRemoteWakeup();
107 #if !defined(INTERRUPT_CONTROL_ENDPOINT)
113 while ((c = serial_recv2()) != -1) {
114 // without flow control it'll fail to receive data when flooded
120 while ((c = serial_recv2()) != -1) {
121 // LED Out report: 0xFE, 0x02, 0x01, <leds>
122 // To get the report over UART set bit3 with SH, command.
123 static enum {LED_INIT, LED_FE, LED_02, LED_01} state = LED_INIT;
127 if (c == 0xFE) state = LED_FE;
128 else state = LED_INIT;
131 if (c == 0x02) state = LED_02;
132 else state = LED_INIT;
135 if (c == 0x01) state = LED_01;
136 else state = LED_INIT;
139 // TODO: move to rn42.c and make accessible with keyboard_leds()
140 xprintf("LED status: %X\n", c);
149 /* Bluetooth mode when ready */
150 if (!config_mode && !force_usb) {
151 if (rn42_ready() && host_get_driver() != &rn42_driver) {
153 host_set_driver(&rn42_driver);
154 } else if (!rn42_ready() && host_get_driver() != &lufa_driver) {
156 host_set_driver(&lufa_driver);
162 static bool local_echo = false;
163 bool command_extra(uint8_t code)
165 static host_driver_t *prev_driver = &rn42_driver;
168 case KC_SLASH: /* ? */
169 print("\n\n----- Bluetooth RN-42 Help -----\n");
170 print("w: toggle RN-42 config mode(enter/exit)\n");
171 print("l: toggle print module output(local echo)\n");
172 print("a: Bluetooth auto connect\n");
173 print("del: Bluetooth disconnect\n");
175 print("b: battery voltage\n");
180 print("u: force USB mode\n");
181 return false; // to display default command help
185 print("\nEnter RN-42 config mode\n");
186 print("type $$$ to enter RN-42 command mode\n");
187 print("type Delete to disconnect Bluetooth connection\n");
188 command_state = CONSOLE;
190 prev_driver = host_get_driver();
192 host_set_driver(&rn42_config_driver);
194 print("\nExit RN-42 config mode\n");
195 command_state = ONESHOT;
198 host_set_driver(prev_driver);
203 print("local echo off\n");
206 print("local echo on\n");
211 if (config_mode) return false;
213 print("Auto mode\n");
219 host_set_driver(&lufa_driver);
223 print("auto connect\n");
227 print("disconnect\n");
229 //rn42_putc('\0'); // see 5.3.4.4 DISCONNECT KEY of User's Guide
232 print("\nRN-42 info\n");
233 xprintf("protocol: %s\n", (host_get_driver() == &rn42_driver) ? "RN-42" : "LUFA");
234 xprintf("force_usb: %X\n", force_usb);
235 xprintf("rn42_ready(): %X\n", rn42_ready());
236 xprintf("config_mode: %X\n", config_mode);
240 ADCSRA |= (1<<ADEN) | (1<<ADSC);
241 while (ADCSRA & (1<<ADSC)) ;
244 xprintf("BAT: %04X\n", bat);
246 ADCSRA |= (1<<ADEN) | (1<<ADSC);
247 while (ADCSRA & (1<<ADSC)) ;
250 xprintf("BAT: %04X\n", bat);
252 ADCSRA &= ~(1<<ADEN);
258 return false; // exec default command
263 static uint8_t code2asc(uint8_t code);
264 bool command_console_extra(uint8_t code)
268 rn42_putc(code2asc(code));
269 if (local_echo) xprintf("%c", code2asc(code));
275 // convert keycode into ascii charactor
276 static uint8_t code2asc(uint8_t code)
278 bool shifted = (get_mods() & (MOD_BIT(KC_LSHIFT)|MOD_BIT(KC_RSHIFT))) ? true : false;
280 case KC_A: return (shifted ? 'A' : 'a');
281 case KC_B: return (shifted ? 'B' : 'b');
282 case KC_C: return (shifted ? 'C' : 'c');
283 case KC_D: return (shifted ? 'D' : 'd');
284 case KC_E: return (shifted ? 'E' : 'e');
285 case KC_F: return (shifted ? 'F' : 'f');
286 case KC_G: return (shifted ? 'G' : 'g');
287 case KC_H: return (shifted ? 'H' : 'h');
288 case KC_I: return (shifted ? 'I' : 'i');
289 case KC_J: return (shifted ? 'J' : 'j');
290 case KC_K: return (shifted ? 'K' : 'k');
291 case KC_L: return (shifted ? 'L' : 'l');
292 case KC_M: return (shifted ? 'M' : 'm');
293 case KC_N: return (shifted ? 'N' : 'n');
294 case KC_O: return (shifted ? 'O' : 'o');
295 case KC_P: return (shifted ? 'P' : 'p');
296 case KC_Q: return (shifted ? 'Q' : 'q');
297 case KC_R: return (shifted ? 'R' : 'r');
298 case KC_S: return (shifted ? 'S' : 's');
299 case KC_T: return (shifted ? 'T' : 't');
300 case KC_U: return (shifted ? 'U' : 'u');
301 case KC_V: return (shifted ? 'V' : 'v');
302 case KC_W: return (shifted ? 'W' : 'w');
303 case KC_X: return (shifted ? 'X' : 'x');
304 case KC_Y: return (shifted ? 'Y' : 'y');
305 case KC_Z: return (shifted ? 'Z' : 'z');
306 case KC_1: return (shifted ? '!' : '1');
307 case KC_2: return (shifted ? '@' : '2');
308 case KC_3: return (shifted ? '#' : '3');
309 case KC_4: return (shifted ? '$' : '4');
310 case KC_5: return (shifted ? '%' : '5');
311 case KC_6: return (shifted ? '^' : '6');
312 case KC_7: return (shifted ? '&' : '7');
313 case KC_8: return (shifted ? '*' : '8');
314 case KC_9: return (shifted ? '(' : '9');
315 case KC_0: return (shifted ? ')' : '0');
316 case KC_ENTER: return '\n';
317 case KC_ESCAPE: return 0x1B;
318 case KC_BSPACE: return '\b';
319 case KC_TAB: return '\t';
320 case KC_SPACE: return ' ';
321 case KC_MINUS: return (shifted ? '_' : '-');
322 case KC_EQUAL: return (shifted ? '+' : '=');
323 case KC_LBRACKET: return (shifted ? '{' : '[');
324 case KC_RBRACKET: return (shifted ? '}' : ']');
325 case KC_BSLASH: return (shifted ? '|' : '\\');
326 case KC_NONUS_HASH: return (shifted ? '|' : '\\');
327 case KC_SCOLON: return (shifted ? ':' : ';');
328 case KC_QUOTE: return (shifted ? '"' : '\'');
329 case KC_GRAVE: return (shifted ? '~' : '`');
330 case KC_COMMA: return (shifted ? '<' : ',');
331 case KC_DOT: return (shifted ? '>' : '.');
332 case KC_SLASH: return (shifted ? '?' : '/');
333 case KC_DELETE: return '\0'; // Delete to disconnect