]> git.donarmstrong.com Git - tmk_firmware.git/blob - keyboard/hhkb_rn42/main.c
Fix configure mode of RN-42
[tmk_firmware.git] / keyboard / hhkb_rn42 / main.c
1 #include <avr/io.h>
2 #include <avr/power.h>
3 #include <avr/wdt.h>
4 #include "lufa.h"
5 #include "print.h"
6 #include "sendchar.h"
7 #include "rn42.h"
8 #include "serial.h"
9 #include "keyboard.h"
10 #include "command.h"
11 #include "keycode.h"
12 #include "action.h"
13 #include "action_util.h"
14 #include "wait.h"
15
16
17 bool config_mode = false;
18
19 static void SetupHardware(void)
20 {
21     /* Disable watchdog if enabled by bootloader/fuses */
22     MCUSR &= ~(1 << WDRF);
23     wdt_disable();
24
25     /* Disable clock division */
26     clock_prescale_set(clock_div_1);
27
28     // Leonardo needs. Without this USB device is not recognized.
29     USB_Disable();
30
31     USB_Init();
32
33     // for Console_Task
34     USB_Device_EnableSOFEvents();
35     print_set_sendchar(sendchar);
36 }
37
38 static bool force_usb = false;
39 int main(void)  __attribute__ ((weak));
40 int main(void)
41 {
42     SetupHardware();
43     sei();
44
45     /* wait for USB startup to get ready for debug output */
46     uint8_t timeout = 255;  // timeout when USB is not available(Bluetooth)
47     while (timeout-- && USB_DeviceState != DEVICE_STATE_Configured) {
48         wait_ms(4);
49 #if defined(INTERRUPT_CONTROL_ENDPOINT)
50         ;
51 #else
52         USB_USBTask();
53 #endif
54     }
55     print("USB configured.\n");
56
57     rn42_init();
58     print("RN-42 init\n");
59
60     /* init modules */
61     keyboard_init();
62
63     if (rn42_ready()) {
64         host_set_driver(&rn42_driver);
65     } else {
66         host_set_driver(&lufa_driver);
67     }
68
69 #ifdef SLEEP_LED_ENABLE
70     sleep_led_init();
71 #endif
72
73     print("Keyboard start.\n");
74     while (1) {
75 /*
76         while (USB_DeviceState == DEVICE_STATE_Suspended) {
77             suspend_power_down();
78             if (USB_Device_RemoteWakeupEnabled && suspend_wakeup_condition()) {
79                     USB_Device_SendRemoteWakeup();
80             }
81         }
82 */
83
84         keyboard_task();
85
86 #if !defined(INTERRUPT_CONTROL_ENDPOINT)
87         USB_USBTask();
88 #endif
89
90         int16_t c;
91         if (config_mode) {
92             while ((c = serial_recv2()) != -1) xprintf("%c", c);
93         } else {
94             while ((c = serial_recv2()) != -1) {
95                 // LED Out report: 0xFE, 0x02, 0x01, <leds>
96                 // To get the report over UART set bit3 with SH, command.
97                 static enum {LED_INIT, LED_FE, LED_02, LED_01} state = LED_INIT;
98                 xprintf("%X\n", c);
99                 switch (state) {
100                     case LED_INIT:
101                         if (c == 0xFE) state = LED_FE;
102                         else           state = LED_INIT;
103                         break;
104                     case LED_FE:
105                         if (c == 0x02) state = LED_02;
106                         else           state = LED_INIT;
107                         break;
108                     case LED_02:
109                         if (c == 0x01) state = LED_01;
110                         else           state = LED_INIT;
111                         break;
112                     case LED_01:
113                         // TODO: move to rn42.c and make accessible with keyboard_leds()
114                         xprintf("LED status: %X\n", c);
115                         state = LED_INIT;
116                         break;
117                     default:
118                         state = LED_INIT;
119                 }
120             }
121         }
122
123         /* Bluetooth mode when ready */
124         if (!config_mode && !force_usb) {
125             if (rn42_ready() && host_get_driver() != &rn42_driver) {
126                 clear_keyboard();
127                 host_set_driver(&rn42_driver);
128             } else if (!rn42_ready() && host_get_driver() != &lufa_driver) {
129                 clear_keyboard();
130                 host_set_driver(&lufa_driver);
131             }
132         }
133     }
134 }
135
136 static bool local_echo = false;
137 bool command_extra(uint8_t code)
138 {
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("w:   toggle RN-42 config mode(enter/exit)\n");
145             print("l:   toggle print module output(local echo)\n");
146             print("a:   Bluetooth auto connect\n");
147             print("del: Bluetooth disconnect\n");
148             print("i:   info\n");
149
150             if (config_mode) {
151                 return true;
152             } else {
153                 print("u:   force USB mode\n");
154                 return false;   // to display default command help
155             }
156         case KC_W:
157             if (!config_mode) {
158                 print("\nEnter RN-42 config mode\n");
159                 print("type $$$ to enter RN-42 command mode\n");
160                 print("type Delete to disconnect Bluetooth connection\n");
161                 command_state = CONSOLE;
162                 config_mode = true;
163                 prev_driver = host_get_driver();
164                 clear_keyboard();
165                 host_set_driver(&rn42_config_driver);
166             } else {
167                 print("\nExit RN-42 config mode\n");
168                 command_state = ONESHOT;
169                 config_mode = false;
170                 clear_keyboard();
171                 host_set_driver(prev_driver);
172             }
173             return true;
174         case KC_L:
175             if (local_echo) {
176                 print("local echo off\n");
177                 local_echo = false;
178             } else {
179                 print("local echo on\n");
180                 local_echo = true;
181             }
182             return true;
183         case KC_U:
184             if (config_mode) return false;
185             if (force_usb) {
186                 print("Auto mode\n");
187                 force_usb = false;
188             } else {
189                 print("USB mode\n");
190                 force_usb = true;
191                 clear_keyboard();
192                 host_set_driver(&lufa_driver);
193             }
194             return true;
195         case KC_A:
196             print("auto connect\n");
197             rn42_autoconnect();
198             return true;
199         case KC_DELETE:
200             print("disconnect\n");
201             rn42_disconnect();
202             //rn42_putc('\0');    // see 5.3.4.4 DISCONNECT KEY of User's Guide
203             return true;
204         case KC_I:
205             print("\nRN-42 info\n");
206             xprintf("protocol: %s\n", (host_get_driver() == &rn42_driver) ? "RN-42" : "LUFA");
207             xprintf("force_usb: %X\n", force_usb);
208             xprintf("rn42_ready(): %X\n", rn42_ready());
209             xprintf("config_mode: %X\n", config_mode);
210             return true;
211         default:
212             if (config_mode)
213                 return true;
214             else
215                 return false;   // exec default command
216     }
217     return true;
218 }
219
220 static uint8_t code2asc(uint8_t code);
221 bool command_console_extra(uint8_t code)
222 {
223     switch (code) {
224         default:
225             rn42_putc(code2asc(code));
226             if (local_echo) xprintf("%c", code2asc(code));
227             return true;
228     }
229     return false;
230 }
231
232 // convert keycode into ascii charactor
233 static uint8_t code2asc(uint8_t code)
234 {
235     bool shifted = (get_mods() & (MOD_BIT(KC_LSHIFT)|MOD_BIT(KC_RSHIFT))) ? true : false;
236     switch (code) {
237         case KC_A: return (shifted ? 'A' : 'a');
238         case KC_B: return (shifted ? 'B' : 'b');
239         case KC_C: return (shifted ? 'C' : 'c');
240         case KC_D: return (shifted ? 'D' : 'd');
241         case KC_E: return (shifted ? 'E' : 'e');
242         case KC_F: return (shifted ? 'F' : 'f');
243         case KC_G: return (shifted ? 'G' : 'g');
244         case KC_H: return (shifted ? 'H' : 'h');
245         case KC_I: return (shifted ? 'I' : 'i');
246         case KC_J: return (shifted ? 'J' : 'j');
247         case KC_K: return (shifted ? 'K' : 'k');
248         case KC_L: return (shifted ? 'L' : 'l');
249         case KC_M: return (shifted ? 'M' : 'm');
250         case KC_N: return (shifted ? 'N' : 'n');
251         case KC_O: return (shifted ? 'O' : 'o');
252         case KC_P: return (shifted ? 'P' : 'p');
253         case KC_Q: return (shifted ? 'Q' : 'q');
254         case KC_R: return (shifted ? 'R' : 'r');
255         case KC_S: return (shifted ? 'S' : 's');
256         case KC_T: return (shifted ? 'T' : 't');
257         case KC_U: return (shifted ? 'U' : 'u');
258         case KC_V: return (shifted ? 'V' : 'v');
259         case KC_W: return (shifted ? 'W' : 'w');
260         case KC_X: return (shifted ? 'X' : 'x');
261         case KC_Y: return (shifted ? 'Y' : 'y');
262         case KC_Z: return (shifted ? 'Z' : 'z');
263         case KC_1: return (shifted ? '!' : '1');
264         case KC_2: return (shifted ? '@' : '2');
265         case KC_3: return (shifted ? '#' : '3');
266         case KC_4: return (shifted ? '$' : '4');
267         case KC_5: return (shifted ? '%' : '5');
268         case KC_6: return (shifted ? '^' : '6');
269         case KC_7: return (shifted ? '&' : '7');
270         case KC_8: return (shifted ? '*' : '8');
271         case KC_9: return (shifted ? '(' : '9');
272         case KC_0: return (shifted ? ')' : '0');
273         case KC_ENTER: return '\n';
274         case KC_ESCAPE: return 0x1B;
275         case KC_BSPACE: return '\b';
276         case KC_TAB: return '\t';
277         case KC_SPACE: return ' ';
278         case KC_MINUS: return (shifted ? '_' : '-');
279         case KC_EQUAL: return (shifted ? '+' : '=');
280         case KC_LBRACKET: return (shifted ? '{' : '[');
281         case KC_RBRACKET: return (shifted ? '}' : ']');
282         case KC_BSLASH: return (shifted ? '|' : '\\');
283         case KC_NONUS_HASH: return (shifted ? '|' : '\\');
284         case KC_SCOLON: return (shifted ? ':' : ';');
285         case KC_QUOTE: return (shifted ? '"' : '\'');
286         case KC_GRAVE: return (shifted ? '~' : '`');
287         case KC_COMMA: return (shifted ? '<' : ',');
288         case KC_DOT: return (shifted ? '>' : '.');
289         case KC_SLASH: return (shifted ? '?' : '/');
290         case KC_DELETE: return '\0';    // Delete to disconnect
291         default: return ' ';
292     }
293 }