]> git.gir.st - tmk_keyboard.git/blob - keyboard/hhkb_rn42/rn42/rn42_task.c
31a415cedfe1ba232553f71fa7b3cce7b4214ab6
[tmk_keyboard.git] / keyboard / hhkb_rn42 / rn42 / rn42_task.c
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 #include "battery.h"
13
14 static bool config_mode = false;
15 static bool force_usb = false;
16
17 static void status_led(bool on)
18 {
19 if (on) {
20 DDRE |= (1<<6);
21 PORTE &= ~(1<<6);
22 } else {
23 DDRE |= (1<<6);
24 PORTE |= (1<<6);
25 }
26 }
27
28 void rn42_task_init(void)
29 {
30 battery_init();
31 }
32
33 void rn42_task(void)
34 {
35 int16_t c;
36 if (config_mode) {
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
40 xprintf("%c", c);
41 }
42 } else {
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;
48 xprintf("%02X\n", c);
49 switch (state) {
50 case LED_INIT:
51 if (c == 0xFE) state = LED_FE;
52 else state = LED_INIT;
53 break;
54 case LED_FE:
55 if (c == 0x02) state = LED_02;
56 else state = LED_INIT;
57 break;
58 case LED_02:
59 if (c == 0x01) state = LED_01;
60 else state = LED_INIT;
61 break;
62 case LED_01:
63 // TODO: move to rn42.c and make accessible with keyboard_leds()
64 xprintf("LED status: %02X\n", c);
65 state = LED_INIT;
66 break;
67 default:
68 state = LED_INIT;
69 }
70 }
71 }
72
73 /* Bluetooth mode when ready */
74 if (!config_mode && !force_usb) {
75 if (!rn42_rts() && host_get_driver() != &rn42_driver) {
76 clear_keyboard();
77 host_set_driver(&rn42_driver);
78 } else if (rn42_rts() && host_get_driver() != &lufa_driver) {
79 clear_keyboard();
80 host_set_driver(&lufa_driver);
81 }
82 }
83
84
85 static uint16_t prev_timer = 0;
86 uint16_t e = timer_elapsed(prev_timer);
87 if (e > 1000) {
88 /* every second */
89 prev_timer += e/1000*1000;
90
91 /* Low voltage alert */
92 uint8_t bs = battery_status();
93 if (bs == LOW_VOLTAGE) {
94 battery_led(LED_ON);
95 } else {
96 battery_led(LED_CHARGER);
97 }
98
99 static uint8_t prev_status = UNKNOWN;
100 if (bs != prev_status) {
101 prev_status = bs;
102 switch (bs) {
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;
108 };
109 }
110
111 /* every minute */
112 uint32_t t = timer_read32()/1000;
113 if (t%60 == 0) {
114 uint16_t v = battery_voltage();
115 uint8_t h = t/3600;
116 uint8_t m = t%3600/60;
117 uint8_t s = t%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);
121 */
122 }
123 }
124
125
126 /* Connection monitor */
127 if (rn42_linked()) {
128 status_led(true);
129 } else {
130 status_led(false);
131 }
132 }
133
134
135
136 /******************************************************************************
137 * Command
138 ******************************************************************************/
139 bool command_extra(uint8_t code)
140 {
141 uint32_t t;
142 uint16_t b;
143 static host_driver_t *prev_driver = &rn42_driver;
144 switch (code) {
145 case KC_H:
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");
151
152 if (config_mode) {
153 return true;
154 } else {
155 print("u: Force USB mode\n");
156 return false; // to display default command help
157 }
158 case KC_DELETE:
159 if (rn42_autoconnecting()) {
160 prev_driver = host_get_driver();
161 clear_keyboard();
162 _delay_ms(500);
163 host_set_driver(&rn42_config_driver); // null driver; not to send a key to host
164 rn42_disconnect();
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;
169 config_mode = true;
170 } else {
171 rn42_autoconnect();
172 print("\nRN-42: auto_connect\n");
173 print("Exit config mode\n");
174 command_state = ONESHOT;
175 config_mode = false;
176 //clear_keyboard();
177 host_set_driver(prev_driver);
178 }
179 return true;
180 case KC_U:
181 if (config_mode) return false;
182 if (force_usb) {
183 print("Auto mode\n");
184 force_usb = false;
185 } else {
186 print("USB mode\n");
187 force_usb = true;
188 clear_keyboard();
189 host_set_driver(&lufa_driver);
190 }
191 return true;
192 case KC_I:
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());
203 return true;
204 case KC_B:
205 // battery monitor
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);
212 return true;
213 default:
214 if (config_mode)
215 return true;
216 else
217 return false; // exec default command
218 }
219 return true;
220 }
221
222 static uint8_t code2asc(uint8_t code);
223 bool command_console_extra(uint8_t code)
224 {
225 switch (code) {
226 default:
227 rn42_putc(code2asc(code));
228 return true;
229 }
230 return false;
231 }
232
233 // convert keycode into ascii charactor
234 static uint8_t code2asc(uint8_t code)
235 {
236 bool shifted = (get_mods() & (MOD_BIT(KC_LSHIFT)|MOD_BIT(KC_RSHIFT))) ? true : false;
237 switch (code) {
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
292 default: return ' ';
293 }
294 }
Imprint / Impressum