]> git.donarmstrong.com Git - tmk_firmware.git/blob - keyboard/hhkb_rn42/rn42/rn42_task.c
Monitor battery and alert low voltage
[tmk_firmware.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     static uint8_t sec = 0;
87     // NOTE: not exact 1 sec
88     if (timer_elapsed(prev_timer) > 1000) {
89         /* every second */
90         prev_timer = timer_read();
91
92         /* Low voltage alert */
93         uint8_t bs = battery_status();
94         if (bs == LOW_VOLTAGE) {
95             battery_led(LED_ON);
96         } else {
97             battery_led(LED_CHARGER);
98         }
99
100         static uint8_t prev_status = UNKNOWN;
101         if (bs != prev_status) {
102             prev_status = bs;
103             switch (bs) {
104                 case FULL_CHARGED:  xprintf("FULL_CHARGED\n"); break;
105                 case CHARGING:      xprintf("CHARGING\n"); break;
106                 case DISCHARGING:   xprintf("DISCHARGING\n"); break;
107                 case LOW_VOLTAGE:   xprintf("LOW_VOLTAGE\n"); break;
108                 default:            xprintf("UNKNOWN STATUS\n"); break;
109             };
110         }
111
112         /* every minute */
113         if (sec == 0) {
114             uint32_t t = timer_read32()/1000;
115             uint16_t v = battery_voltage();
116             uint8_t h = t/3600;
117             uint8_t m = t%3600/60;
118             uint8_t s = t%60;
119             xprintf("%02u:%02u:%02u\t%umV\n", h, m, s, v);
120             /* TODO: xprintf doesn't work for this.
121             xprintf("%02u:%02u:%02u\t%umV\n", (t/3600), (t%3600/60), (t%60), v);
122             */
123         }
124         sec++; sec = sec%60;
125     }
126
127
128     /* Connection monitor */
129     if (rn42_linked()) {
130         status_led(true);
131     } else {
132         status_led(false);
133     }
134 }
135
136
137
138 /******************************************************************************
139  * Command
140  ******************************************************************************/
141 bool command_extra(uint8_t code)
142 {
143     uint32_t t;
144     uint16_t b;
145     static host_driver_t *prev_driver = &rn42_driver;
146     switch (code) {
147         case KC_H:
148         case KC_SLASH: /* ? */
149             print("\n\n----- Bluetooth RN-42 Help -----\n");
150             print("Del: enter/exit config mode(auto_connect/disconnect)\n");
151             print("i:   RN-42 info\n");
152             print("b:   battery voltage\n");
153
154             if (config_mode) {
155                 return true;
156             } else {
157                 print("u:   Force USB mode\n");
158                 return false;   // to display default command help
159             }
160         case KC_DELETE:
161             if (rn42_autoconnecting()) {
162                 prev_driver = host_get_driver();
163                 clear_keyboard();
164                 _delay_ms(500);
165                 host_set_driver(&rn42_config_driver);   // null driver; not to send a key to host
166                 rn42_disconnect();
167                 print("\nRN-42: disconnect\n");
168                 print("Enter config mode\n");
169                 print("type $$$ to start and + for local echo\n");
170                 command_state = CONSOLE;
171                 config_mode = true;
172             } else {
173                 rn42_autoconnect();
174                 print("\nRN-42: auto_connect\n");
175                 print("Exit config mode\n");
176                 command_state = ONESHOT;
177                 config_mode = false;
178                 //clear_keyboard();
179                 host_set_driver(prev_driver);
180             }
181             return true;
182         case KC_U:
183             if (config_mode) return false;
184             if (force_usb) {
185                 print("Auto mode\n");
186                 force_usb = false;
187             } else {
188                 print("USB mode\n");
189                 force_usb = true;
190                 clear_keyboard();
191                 host_set_driver(&lufa_driver);
192             }
193             return true;
194         case KC_I:
195             print("\n----- RN-42 info -----\n");
196             xprintf("protocol: %s\n", (host_get_driver() == &rn42_driver) ? "RN-42" : "LUFA");
197             xprintf("force_usb: %X\n", force_usb);
198             xprintf("rn42_autoconnecting(): %X\n", rn42_autoconnecting());
199             xprintf("rn42_linked(): %X\n", rn42_linked());
200             xprintf("rn42_rts(): %X\n", rn42_rts());
201             xprintf("config_mode: %X\n", config_mode);
202             xprintf("VBUS: %X\n", USBSTA&(1<<VBUS));
203             xprintf("battery_charging: %X\n", battery_charging());
204             xprintf("battery_status: %X\n", battery_status());
205             return true;
206         case KC_B:
207             // battery monitor
208             t = timer_read32()/1000;
209             b = battery_voltage();
210             xprintf("BAT: %umV\t", b);
211             xprintf("%02u:",   t/3600);
212             xprintf("%02u:",   t%3600/60);
213             xprintf("%02u\n",  t%60);
214             return true;
215         default:
216             if (config_mode)
217                 return true;
218             else
219                 return false;   // exec default command
220     }
221     return true;
222 }
223
224 static uint8_t code2asc(uint8_t code);
225 bool command_console_extra(uint8_t code)
226 {
227     switch (code) {
228         default:
229             rn42_putc(code2asc(code));
230             return true;
231     }
232     return false;
233 }
234
235 // convert keycode into ascii charactor
236 static uint8_t code2asc(uint8_t code)
237 {
238     bool shifted = (get_mods() & (MOD_BIT(KC_LSHIFT)|MOD_BIT(KC_RSHIFT))) ? true : false;
239     switch (code) {
240         case KC_A: return (shifted ? 'A' : 'a');
241         case KC_B: return (shifted ? 'B' : 'b');
242         case KC_C: return (shifted ? 'C' : 'c');
243         case KC_D: return (shifted ? 'D' : 'd');
244         case KC_E: return (shifted ? 'E' : 'e');
245         case KC_F: return (shifted ? 'F' : 'f');
246         case KC_G: return (shifted ? 'G' : 'g');
247         case KC_H: return (shifted ? 'H' : 'h');
248         case KC_I: return (shifted ? 'I' : 'i');
249         case KC_J: return (shifted ? 'J' : 'j');
250         case KC_K: return (shifted ? 'K' : 'k');
251         case KC_L: return (shifted ? 'L' : 'l');
252         case KC_M: return (shifted ? 'M' : 'm');
253         case KC_N: return (shifted ? 'N' : 'n');
254         case KC_O: return (shifted ? 'O' : 'o');
255         case KC_P: return (shifted ? 'P' : 'p');
256         case KC_Q: return (shifted ? 'Q' : 'q');
257         case KC_R: return (shifted ? 'R' : 'r');
258         case KC_S: return (shifted ? 'S' : 's');
259         case KC_T: return (shifted ? 'T' : 't');
260         case KC_U: return (shifted ? 'U' : 'u');
261         case KC_V: return (shifted ? 'V' : 'v');
262         case KC_W: return (shifted ? 'W' : 'w');
263         case KC_X: return (shifted ? 'X' : 'x');
264         case KC_Y: return (shifted ? 'Y' : 'y');
265         case KC_Z: return (shifted ? 'Z' : 'z');
266         case KC_1: return (shifted ? '!' : '1');
267         case KC_2: return (shifted ? '@' : '2');
268         case KC_3: return (shifted ? '#' : '3');
269         case KC_4: return (shifted ? '$' : '4');
270         case KC_5: return (shifted ? '%' : '5');
271         case KC_6: return (shifted ? '^' : '6');
272         case KC_7: return (shifted ? '&' : '7');
273         case KC_8: return (shifted ? '*' : '8');
274         case KC_9: return (shifted ? '(' : '9');
275         case KC_0: return (shifted ? ')' : '0');
276         case KC_ENTER: return '\n';
277         case KC_ESCAPE: return 0x1B;
278         case KC_BSPACE: return '\b';
279         case KC_TAB: return '\t';
280         case KC_SPACE: return ' ';
281         case KC_MINUS: return (shifted ? '_' : '-');
282         case KC_EQUAL: return (shifted ? '+' : '=');
283         case KC_LBRACKET: return (shifted ? '{' : '[');
284         case KC_RBRACKET: return (shifted ? '}' : ']');
285         case KC_BSLASH: return (shifted ? '|' : '\\');
286         case KC_NONUS_HASH: return (shifted ? '|' : '\\');
287         case KC_SCOLON: return (shifted ? ':' : ';');
288         case KC_QUOTE: return (shifted ? '"' : '\'');
289         case KC_GRAVE: return (shifted ? '~' : '`');
290         case KC_COMMA: return (shifted ? '<' : ',');
291         case KC_DOT: return (shifted ? '>' : '.');
292         case KC_SLASH: return (shifted ? '?' : '/');
293         case KC_DELETE: return '\0';    // Delete to disconnect
294         default: return ' ';
295     }
296 }