]>
git.gir.st - tmk_keyboard.git/blob - protocol/iwrap/main.c
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>
42 static void sleep(uint8_t term
);
43 static bool console(void);
44 static bool console_command(uint8_t c
);
45 static uint8_t key2asc(uint8_t key
);
49 static void set_prr(void)
55 //power_timer0_disable(); // used in timer.c
57 power_timer1_disable();
58 power_timer2_disable();
63 static void pullup_pins(void)
65 // DDRs are set to 0(input) by default.
83 static void disable_vusb(void)
85 // disable interrupt & disconnect to prevent host from enumerating
86 USB_INTR_ENABLE
&= ~(1 << USB_INTR_ENABLE_BIT
);
87 usbDeviceDisconnect();
90 static void enable_vusb(void)
92 USB_INTR_ENABLE
|= (1 << USB_INTR_ENABLE_BIT
);
96 static void init_vusb(void)
102 /* fake USB disconnect for > 250 ms */
110 void change_driver(host_driver_t
*driver
)
113 host_clear_keyboard_report();
114 host_swap_keyboard_report();
115 host_clear_keyboard_report();
116 host_send_keyboard_report();
120 host_set_driver(driver
);
124 static bool sleeping
= false;
125 static bool insomniac
= false; // TODO: should be false for power saving
126 static uint16_t last_timer
= 0;
131 clock_prescale_set(clock_div_1
);
134 // power saving: the result is worse than nothing... why?
143 print("\nSend BREAK for UART Console Commands.\n");
145 // TODO: move to iWRAP/suart file
146 print("suart init\n");
148 // PC4: Tx Output IDLE(Hi)
151 // PC5: Rx Input(pull-up)
154 // suart receive interrut(PC5/PCINT13)
158 host_set_driver(iwrap_driver());
160 print("iwrap_init()\n");
164 last_timer
= timer_read();
167 if (host_get_driver() == vusb_driver())
172 if (host_get_driver() == vusb_driver())
173 vusb_transfer_keyboard();
176 if (matrix_is_modified() || console()) {
177 last_timer
= timer_read();
179 } else if (!sleeping
&& timer_elapsed(last_timer
) > 4000) {
181 iwrap_check_connection();
185 if (host_get_driver() == iwrap_driver()) {
186 if (sleeping
&& !insomniac
) {
187 _delay_ms(1); // wait for UART to send
195 static void sleep(uint8_t term
)
197 WD_SET(WD_IRQ
, term
);
200 set_sleep_mode(SLEEP_MODE_PWR_DOWN
);
210 static bool console(void)
212 // Send to Bluetoot module WT12
213 static bool breaked
= false;
214 if (!uart_available())
221 case 0x00: // BREAK signal
223 print("break(? for help): ");
248 bool command_extra(uint8_t code
)
250 return console_command(key2asc(code
));
253 static bool console_command(uint8_t c
)
258 print("\nCommands for Bluetooth(WT12/iWRAP):\n");
259 print("r: reset. software reset by watchdog\n");
260 print("i: insomniac. prevent KB from sleeping\n");
261 print("c: iwrap_call. CALL for BT connection.\n");
263 print("u: USB mode. switch to USB.\n");
264 print("w: BT mode. switch to Bluetooth.\n");
266 print("k: kill first connection.\n");
267 print("Del: unpair first pairing.\n");
275 insomniac
= !insomniac
;
277 print("insomniac\n");
279 print("not insomniac\n");
282 print("iwrap_call()\n");
289 change_driver(vusb_driver());
292 // disable suart receive interrut(PC5/PCINT13)
293 PCMSK1
&= ~(0b00100000);
294 PCICR
&= ~(0b00000010);
297 print("iWRAP mode\n");
298 change_driver(iwrap_driver());
300 // enable suart receive interrut(PC5/PCINT13)
301 PCMSK1
|= 0b00100000;
317 // convert keycode into ascii charactor
318 static uint8_t key2asc(uint8_t key
)
321 case KC_A
: return 'a';
322 case KC_B
: return 'b';
323 case KC_C
: return 'c';
324 case KC_D
: return 'd';
325 case KC_E
: return 'e';
326 case KC_F
: return 'f';
327 case KC_G
: return 'g';
328 case KC_H
: return 'h';
329 case KC_I
: return 'i';
330 case KC_J
: return 'j';
331 case KC_K
: return 'k';
332 case KC_L
: return 'l';
333 case KC_M
: return 'm';
334 case KC_N
: return 'n';
335 case KC_O
: return 'o';
336 case KC_P
: return 'p';
337 case KC_Q
: return 'q';
338 case KC_R
: return 'r';
339 case KC_S
: return 's';
340 case KC_T
: return 't';
341 case KC_U
: return 'u';
342 case KC_V
: return 'v';
343 case KC_W
: return 'w';
344 case KC_X
: return 'x';
345 case KC_Y
: return 'y';
346 case KC_Z
: return 'z';
347 case KC_1
: return '1';
348 case KC_2
: return '2';
349 case KC_3
: return '3';
350 case KC_4
: return '4';
351 case KC_5
: return '5';
352 case KC_6
: return '6';
353 case KC_7
: return '7';
354 case KC_8
: return '8';
355 case KC_9
: return '9';
356 case KC_0
: return '0';
357 case KC_ENTER
: return '\n';
358 case KC_ESCAPE
: return 0x1B;
359 case KC_BSPACE
: return '\b';
360 case KC_TAB
: return '\t';
361 case KC_SPACE
: return ' ';
362 case KC_MINUS
: return '-';
363 case KC_EQUAL
: return '=';
364 case KC_LBRACKET
: return '[';
365 case KC_RBRACKET
: return ']';
366 case KC_BSLASH
: return '\\';
367 case KC_NONUS_HASH
: return '\\';
368 case KC_SCOLON
: return ';';
369 case KC_QUOTE
: return '\'';
370 case KC_GRAVE
: return '`';
371 case KC_COMMA
: return ',';
372 case KC_DOT
: return '.';
373 case KC_SLASH
: return '/';
374 default: return 0x00;