]> git.gir.st - tmk_keyboard.git/blob - keyboard/hhkb_rn42/rn42/rn42_task.c
Add battery monitor and LED control
[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
13 static bool config_mode = false;
14 static bool force_usb = false;
15
16 static void status_led(bool on)
17 {
18 if (on) {
19 DDRE |= (1<<6);
20 PORTE &= ~(1<<6);
21 } else {
22 DDRE |= (1<<6);
23 PORTE |= (1<<6);
24 }
25 }
26
27 static void battery_adc_init(void)
28 {
29 ADMUX = (1<<REFS1) | (1<<REFS0); // Ref:2.56V band-gap, Input:ADC0(PF0)
30 ADCSRA = (1<<ADPS2) | (1<<ADPS1) | (1<<ADPS0); // Prescale:128 16MHz/128=125KHz
31 ADCSRA |= (1<<ADEN); // enable ADC
32 }
33
34 static uint16_t battery_adc(void)
35 {
36 volatile uint16_t bat;
37 ADCSRA |= (1<<ADEN);
38
39 // discard first result
40 ADCSRA |= (1<<ADSC);
41 while (ADCSRA & (1<<ADSC)) ;
42 bat = ADC;
43
44 // discard second result
45 ADCSRA |= (1<<ADSC);
46 while (ADCSRA & (1<<ADSC)) ;
47 bat = ADC;
48
49 ADCSRA |= (1<<ADSC);
50 while (ADCSRA & (1<<ADSC)) ;
51 bat = ADC;
52
53 ADCSRA &= ~(1<<ADEN);
54 return bat;
55 }
56
57 static void battery_led(bool on)
58 {
59 if (on) {
60 DDRF |= (1<<5);
61 PORTF &= ~(1<<5);
62 } else {
63 DDRF &= ~(1<<5);
64 PORTF |= (1<<5);
65 }
66 }
67
68 static bool battery_charging(void)
69 {
70 // MCP73831:STAT
71 // Hi-Z: Shutdown/No Battery
72 // Low: Charging
73 // Hi: Charged
74 DDRF &= ~(1<<5);
75 PORTF |= (1<<5);
76 return PINF&(1<<5) ? false : true;
77 }
78
79 void rn42_task_init(void)
80 {
81 battery_adc_init();
82
83 // battery charging(input with pull-up)
84 DDRF &= ~(1<<5);
85 PORTF |= (1<<5);
86 }
87
88 void rn42_task(void)
89 {
90 int16_t c;
91 if (config_mode) {
92 // Config mode: print output from RN-42
93 while ((c = serial_recv2()) != -1) {
94 // without flow control it'll fail to receive data when flooded
95 xprintf("%c", c);
96 }
97 } else {
98 // Raw mode: interpret output report of LED state
99 while ((c = serial_recv2()) != -1) {
100 // LED Out report: 0xFE, 0x02, 0x01, <leds>
101 // To get the report over UART set bit3 with SH, command.
102 static enum {LED_INIT, LED_FE, LED_02, LED_01} state = LED_INIT;
103 xprintf("%02X\n", c);
104 switch (state) {
105 case LED_INIT:
106 if (c == 0xFE) state = LED_FE;
107 else state = LED_INIT;
108 break;
109 case LED_FE:
110 if (c == 0x02) state = LED_02;
111 else state = LED_INIT;
112 break;
113 case LED_02:
114 if (c == 0x01) state = LED_01;
115 else state = LED_INIT;
116 break;
117 case LED_01:
118 // TODO: move to rn42.c and make accessible with keyboard_leds()
119 xprintf("LED status: %02X\n", c);
120 state = LED_INIT;
121 break;
122 default:
123 state = LED_INIT;
124 }
125 }
126 }
127
128 /* Bluetooth mode when ready */
129 if (!config_mode && !force_usb) {
130 if (!rn42_rts() && host_get_driver() != &rn42_driver) {
131 clear_keyboard();
132 host_set_driver(&rn42_driver);
133 } else if (rn42_rts() && host_get_driver() != &lufa_driver) {
134 clear_keyboard();
135 host_set_driver(&lufa_driver);
136 }
137 }
138 }
139
140
141
142 /******************************************************************************
143 * Command
144 ******************************************************************************/
145 bool command_extra(uint8_t code)
146 {
147 uint32_t t;
148 uint16_t b;
149 static host_driver_t *prev_driver = &rn42_driver;
150 switch (code) {
151 case KC_H:
152 case KC_SLASH: /* ? */
153 print("\n\n----- Bluetooth RN-42 Help -----\n");
154 print("Del: enter/exit config mode(auto_connect/disconnect)\n");
155 print("i: RN-42 info\n");
156 print("b: battery voltage\n");
157
158 if (config_mode) {
159 return true;
160 } else {
161 print("u: Force USB mode\n");
162 return false; // to display default command help
163 }
164 case KC_DELETE:
165 if (rn42_autoconnecting()) {
166 prev_driver = host_get_driver();
167 clear_keyboard();
168 _delay_ms(500);
169 host_set_driver(&rn42_config_driver); // null driver; not to send a key to host
170 rn42_disconnect();
171 print("\nRN-42: disconnect\n");
172 print("Enter config mode\n");
173 print("type $$$ to start and + for local echo\n");
174 command_state = CONSOLE;
175 config_mode = true;
176 } else {
177 rn42_autoconnect();
178 print("\nRN-42: auto_connect\n");
179 print("Exit config mode\n");
180 command_state = ONESHOT;
181 config_mode = false;
182 //clear_keyboard();
183 host_set_driver(prev_driver);
184 }
185 return true;
186 case KC_U:
187 if (config_mode) return false;
188 if (force_usb) {
189 print("Auto mode\n");
190 force_usb = false;
191 } else {
192 print("USB mode\n");
193 force_usb = true;
194 clear_keyboard();
195 host_set_driver(&lufa_driver);
196 }
197 return true;
198 case KC_I:
199 print("\n----- RN-42 info -----\n");
200 xprintf("protocol: %s\n", (host_get_driver() == &rn42_driver) ? "RN-42" : "LUFA");
201 xprintf("force_usb: %X\n", force_usb);
202 xprintf("rn42_autoconnecting(): %X\n", rn42_autoconnecting());
203 xprintf("rn42_linked(): %X\n", rn42_linked());
204 xprintf("rn42_rts(): %X\n", rn42_rts());
205 xprintf("config_mode: %X\n", config_mode);
206 xprintf("VBUS: %X\n", USBSTA&(1<<VBUS));
207 xprintf("battery_charging: %X\n", battery_charging());
208 return true;
209 case KC_B:
210 // battery monitor
211 t = timer_read32()/1000;
212 b = battery_adc();
213 xprintf("BAT: %umV(%04X)\t", (b-16)*5, b);
214 xprintf("%02u:", t/3600);
215 xprintf("%02u:", t%3600/60);
216 xprintf("%02u\n", t%60);
217 return true;
218 default:
219 if (config_mode)
220 return true;
221 else
222 return false; // exec default command
223 }
224 return true;
225 }
226
227 static uint8_t code2asc(uint8_t code);
228 bool command_console_extra(uint8_t code)
229 {
230 switch (code) {
231 default:
232 rn42_putc(code2asc(code));
233 return true;
234 }
235 return false;
236 }
237
238 // convert keycode into ascii charactor
239 static uint8_t code2asc(uint8_t code)
240 {
241 bool shifted = (get_mods() & (MOD_BIT(KC_LSHIFT)|MOD_BIT(KC_RSHIFT))) ? true : false;
242 switch (code) {
243 case KC_A: return (shifted ? 'A' : 'a');
244 case KC_B: return (shifted ? 'B' : 'b');
245 case KC_C: return (shifted ? 'C' : 'c');
246 case KC_D: return (shifted ? 'D' : 'd');
247 case KC_E: return (shifted ? 'E' : 'e');
248 case KC_F: return (shifted ? 'F' : 'f');
249 case KC_G: return (shifted ? 'G' : 'g');
250 case KC_H: return (shifted ? 'H' : 'h');
251 case KC_I: return (shifted ? 'I' : 'i');
252 case KC_J: return (shifted ? 'J' : 'j');
253 case KC_K: return (shifted ? 'K' : 'k');
254 case KC_L: return (shifted ? 'L' : 'l');
255 case KC_M: return (shifted ? 'M' : 'm');
256 case KC_N: return (shifted ? 'N' : 'n');
257 case KC_O: return (shifted ? 'O' : 'o');
258 case KC_P: return (shifted ? 'P' : 'p');
259 case KC_Q: return (shifted ? 'Q' : 'q');
260 case KC_R: return (shifted ? 'R' : 'r');
261 case KC_S: return (shifted ? 'S' : 's');
262 case KC_T: return (shifted ? 'T' : 't');
263 case KC_U: return (shifted ? 'U' : 'u');
264 case KC_V: return (shifted ? 'V' : 'v');
265 case KC_W: return (shifted ? 'W' : 'w');
266 case KC_X: return (shifted ? 'X' : 'x');
267 case KC_Y: return (shifted ? 'Y' : 'y');
268 case KC_Z: return (shifted ? 'Z' : 'z');
269 case KC_1: return (shifted ? '!' : '1');
270 case KC_2: return (shifted ? '@' : '2');
271 case KC_3: return (shifted ? '#' : '3');
272 case KC_4: return (shifted ? '$' : '4');
273 case KC_5: return (shifted ? '%' : '5');
274 case KC_6: return (shifted ? '^' : '6');
275 case KC_7: return (shifted ? '&' : '7');
276 case KC_8: return (shifted ? '*' : '8');
277 case KC_9: return (shifted ? '(' : '9');
278 case KC_0: return (shifted ? ')' : '0');
279 case KC_ENTER: return '\n';
280 case KC_ESCAPE: return 0x1B;
281 case KC_BSPACE: return '\b';
282 case KC_TAB: return '\t';
283 case KC_SPACE: return ' ';
284 case KC_MINUS: return (shifted ? '_' : '-');
285 case KC_EQUAL: return (shifted ? '+' : '=');
286 case KC_LBRACKET: return (shifted ? '{' : '[');
287 case KC_RBRACKET: return (shifted ? '}' : ']');
288 case KC_BSLASH: return (shifted ? '|' : '\\');
289 case KC_NONUS_HASH: return (shifted ? '|' : '\\');
290 case KC_SCOLON: return (shifted ? ':' : ';');
291 case KC_QUOTE: return (shifted ? '"' : '\'');
292 case KC_GRAVE: return (shifted ? '~' : '`');
293 case KC_COMMA: return (shifted ? '<' : ',');
294 case KC_DOT: return (shifted ? '>' : '.');
295 case KC_SLASH: return (shifted ? '?' : '/');
296 case KC_DELETE: return '\0'; // Delete to disconnect
297 default: return ' ';
298 }
299 }
Imprint / Impressum