]>
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>
37 #include "usb_keycodes.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 KB_A
: return 'a';
324 case KB_B
: return 'b';
325 case KB_C
: return 'c';
326 case KB_D
: return 'd';
327 case KB_E
: return 'e';
328 case KB_F
: return 'f';
329 case KB_G
: return 'g';
330 case KB_H
: return 'h';
331 case KB_I
: return 'i';
332 case KB_J
: return 'j';
333 case KB_K
: return 'k';
334 case KB_L
: return 'l';
335 case KB_M
: return 'm';
336 case KB_N
: return 'n';
337 case KB_O
: return 'o';
338 case KB_P
: return 'p';
339 case KB_Q
: return 'q';
340 case KB_R
: return 'r';
341 case KB_S
: return 's';
342 case KB_T
: return 't';
343 case KB_U
: return 'u';
344 case KB_V
: return 'v';
345 case KB_W
: return 'w';
346 case KB_X
: return 'x';
347 case KB_Y
: return 'y';
348 case KB_Z
: return 'z';
349 case KB_1
: return '1';
350 case KB_2
: return '2';
351 case KB_3
: return '3';
352 case KB_4
: return '4';
353 case KB_5
: return '5';
354 case KB_6
: return '6';
355 case KB_7
: return '7';
356 case KB_8
: return '8';
357 case KB_9
: return '9';
358 case KB_0
: return '0';
359 case KB_ENTER
: return '\n';
360 case KB_ESCAPE
: return 0x1B;
361 case KB_BSPACE
: return '\b';
362 case KB_TAB
: return '\t';
363 case KB_SPACE
: return ' ';
364 case KB_MINUS
: return '-';
365 case KB_EQUAL
: return '=';
366 case KB_LBRACKET
: return '[';
367 case KB_RBRACKET
: return ']';
368 case KB_BSLASH
: return '\\';
369 case KB_NONUS_HASH
: return '\\';
370 case KB_SCOLON
: return ';';
371 case KB_QUOTE
: return '\'';
372 case KB_GRAVE
: return '`';
373 case KB_COMMA
: return ',';
374 case KB_DOT
: return '.';
375 case KB_SLASH
: return '/';
376 default: return 0x00;