2 Copyright 2011 Jun Wako <wakojun@gmail.com>
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>.
18 #include <avr/interrupt.h>
20 //#include <avr/wdt.h>
21 #include "wd.h" // in order to use watchdog in interrupt mode
22 #include <avr/sleep.h>
23 #include <util/delay.h>
24 #include <avr/power.h>
41 static void sleep(uint8_t term);
42 static bool console(void);
43 static uint8_t console_command(uint8_t c);
44 static uint8_t key2asc(uint8_t key);
48 static void set_prr(void)
54 //power_timer0_disable(); // used in timer.c
56 power_timer1_disable();
57 power_timer2_disable();
62 static void pullup_pins(void)
64 // DDRs are set to 0(input) by default.
82 static void disable_vusb(void)
84 // disable interrupt & disconnect to prevent host from enumerating
85 USB_INTR_ENABLE &= ~(1 << USB_INTR_ENABLE_BIT);
86 usbDeviceDisconnect();
89 static void enable_vusb(void)
91 USB_INTR_ENABLE |= (1 << USB_INTR_ENABLE_BIT);
95 static void init_vusb(void)
101 /* fake USB disconnect for > 250 ms */
109 void change_driver(host_driver_t *driver)
111 host_clear_keyboard_report();
112 host_swap_keyboard_report();
113 host_clear_keyboard_report();
114 host_send_keyboard_report();
116 host_set_driver(driver);
120 static bool sleeping = false;
121 static bool insomniac = false; // TODO: should be false for power saving
122 static uint16_t last_timer = 0;
127 clock_prescale_set(clock_div_1);
130 // power saving: the result is worse than nothing... why?
135 debug_enable = false;
142 print("\nSend BREAK for UART Console Commands.\n");
144 // TODO: move to iWRAP/suart file
145 print("suart init\n");
147 // PC4: Tx Output IDLE(Hi)
150 // PC5: Rx Input(pull-up)
153 // suart receive interrut(PC5/PCINT13)
157 host_set_driver(iwrap_driver());
159 print("iwrap_init()\n");
163 last_timer = timer_read();
166 if (host_get_driver() == vusb_driver())
171 if (host_get_driver() == vusb_driver())
172 vusb_transfer_keyboard();
174 if (matrix_is_modified() || console()) {
175 last_timer = timer_read();
177 } else if (!sleeping && timer_elapsed(last_timer) > 4000) {
179 iwrap_check_connection();
182 if (host_get_driver() == iwrap_driver()) {
183 if (sleeping && !insomniac) {
184 _delay_ms(1); // wait for UART to send
192 static void sleep(uint8_t term)
194 WD_SET(WD_IRQ, term);
197 set_sleep_mode(SLEEP_MODE_PWR_DOWN);
212 static bool console(void)
214 // Send to Bluetoot module WT12
215 static bool breaked = false;
216 if (!uart_available())
223 case 0x00: // BREAK signal
225 print("break(? for help): ");
250 uint8_t command_extra()
252 return console_command(key2asc(host_get_first_key()));
255 static uint8_t console_command(uint8_t c)
260 print("\nCommands for Bluetooth(WT12/iWRAP):\n");
261 print("r: reset. software reset by watchdog\n");
262 print("i: insomniac. prevent KB from sleeping\n");
263 print("c: iwrap_call. CALL for BT connection.\n");
265 print("u: USB mode. switch to USB.\n");
266 print("w: BT mode. switch to Bluetooth.\n");
268 print("k: kill first connection.\n");
269 print("Del: unpair first pairing.\n");
277 insomniac = !insomniac;
279 print("insomniac\n");
281 print("not insomniac\n");
284 print("iwrap_call()\n");
291 change_driver(vusb_driver());
294 // disable suart receive interrut(PC5/PCINT13)
295 PCMSK1 &= ~(0b00100000);
296 PCICR &= ~(0b00000010);
299 print("iWRAP mode\n");
300 change_driver(iwrap_driver());
302 // enable suart receive interrut(PC5/PCINT13)
303 PCMSK1 |= 0b00100000;
319 // convert keycode into ascii charactor
320 static uint8_t key2asc(uint8_t key)
323 case KC_A: return 'a';
324 case KC_B: return 'b';
325 case KC_C: return 'c';
326 case KC_D: return 'd';
327 case KC_E: return 'e';
328 case KC_F: return 'f';
329 case KC_G: return 'g';
330 case KC_H: return 'h';
331 case KC_I: return 'i';
332 case KC_J: return 'j';
333 case KC_K: return 'k';
334 case KC_L: return 'l';
335 case KC_M: return 'm';
336 case KC_N: return 'n';
337 case KC_O: return 'o';
338 case KC_P: return 'p';
339 case KC_Q: return 'q';
340 case KC_R: return 'r';
341 case KC_S: return 's';
342 case KC_T: return 't';
343 case KC_U: return 'u';
344 case KC_V: return 'v';
345 case KC_W: return 'w';
346 case KC_X: return 'x';
347 case KC_Y: return 'y';
348 case KC_Z: return 'z';
349 case KC_1: return '1';
350 case KC_2: return '2';
351 case KC_3: return '3';
352 case KC_4: return '4';
353 case KC_5: return '5';
354 case KC_6: return '6';
355 case KC_7: return '7';
356 case KC_8: return '8';
357 case KC_9: return '9';
358 case KC_0: return '0';
359 case KC_ENTER: return '\n';
360 case KC_ESCAPE: return 0x1B;
361 case KC_BSPACE: return '\b';
362 case KC_TAB: return '\t';
363 case KC_SPACE: return ' ';
364 case KC_MINUS: return '-';
365 case KC_EQUAL: return '=';
366 case KC_LBRACKET: return '[';
367 case KC_RBRACKET: return ']';
368 case KC_BSLASH: return '\\';
369 case KC_NONUS_HASH: return '\\';
370 case KC_SCOLON: return ';';
371 case KC_QUOTE: return '\'';
372 case KC_GRAVE: return '`';
373 case KC_COMMA: return ',';
374 case KC_DOT: return '.';
375 case KC_SLASH: return '/';
376 default: return 0x00;