aboutsummaryrefslogtreecommitdiff
path: root/tmk_core/protocol/iwrap/main.c
diff options
context:
space:
mode:
Diffstat (limited to 'tmk_core/protocol/iwrap/main.c')
-rw-r--r--tmk_core/protocol/iwrap/main.c412
1 files changed, 0 insertions, 412 deletions
diff --git a/tmk_core/protocol/iwrap/main.c b/tmk_core/protocol/iwrap/main.c
deleted file mode 100644
index 4048a9791..000000000
--- a/tmk_core/protocol/iwrap/main.c
+++ /dev/null
@@ -1,412 +0,0 @@
1/*
2Copyright 2011 Jun Wako <wakojun@gmail.com>
3
4This program is free software: you can redistribute it and/or modify
5it under the terms of the GNU General Public License as published by
6the Free Software Foundation, either version 2 of the License, or
7(at your option) any later version.
8
9This program is distributed in the hope that it will be useful,
10but WITHOUT ANY WARRANTY; without even the implied warranty of
11MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12GNU General Public License for more details.
13
14You should have received a copy of the GNU General Public License
15along with this program. If not, see <http://www.gnu.org/licenses/>.
16*/
17#include <stdint.h>
18#include <avr/interrupt.h>
19#include <avr/io.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>
25#include "keyboard.h"
26#include "matrix.h"
27#include "host.h"
28#include "action.h"
29#include "iwrap.h"
30#ifdef PROTOCOL_VUSB
31# include "vusb.h"
32# include <usbdrv/usbdrv.h>
33#endif
34#include "uart.h"
35#include "suart.h"
36#include "timer.h"
37#include "debug.h"
38#include "keycode.h"
39#include "command.h"
40
41static void sleep(uint8_t term);
42static bool console(void);
43static bool console_command(uint8_t c);
44static uint8_t key2asc(uint8_t key);
45
46/*
47static void set_prr(void)
48{
49 power_adc_disable();
50 power_spi_disable();
51 power_twi_disable();
52#ifndef TIMER_H
53 //power_timer0_disable(); // used in timer.c
54#endif
55 power_timer1_disable();
56 power_timer2_disable();
57}
58*/
59
60/*
61static void pullup_pins(void)
62{
63 // DDRs are set to 0(input) by default.
64#ifdef PORTA
65 PORTA = 0xFF;
66#endif
67 PORTB = 0xFF;
68 PORTC = 0xFF;
69 PORTD = 0xFF;
70#ifdef PORTE
71 PORTE = 0xFF;
72#endif
73#ifdef PORTE
74 PORTF = 0xFF;
75#endif
76}
77*/
78
79#ifdef PROTOCOL_VUSB
80static void disable_vusb(void) {
81 // disable interrupt & disconnect to prevent host from enumerating
82 USB_INTR_ENABLE &= ~(1 << USB_INTR_ENABLE_BIT);
83 usbDeviceDisconnect();
84}
85
86static void enable_vusb(void) {
87 USB_INTR_ENABLE |= (1 << USB_INTR_ENABLE_BIT);
88 usbDeviceConnect();
89}
90
91static void init_vusb(void) {
92 uint8_t i = 0;
93
94 usbInit();
95 disable_vusb();
96 /* fake USB disconnect for > 250 ms */
97 while (--i) {
98 _delay_ms(1);
99 }
100 enable_vusb();
101}
102#endif
103
104void change_driver(host_driver_t *driver) {
105 /*
106 host_clear_keyboard_report();
107 host_swap_keyboard_report();
108 host_clear_keyboard_report();
109 host_send_keyboard_report();
110 */
111 clear_keyboard();
112 _delay_ms(1000);
113 host_set_driver(driver);
114}
115
116static bool sleeping = false;
117static bool insomniac = false; // TODO: should be false for power saving
118static uint16_t last_timer = 0;
119
120int main(void) {
121 MCUSR = 0;
122 clock_prescale_set(clock_div_1);
123 WD_SET(WD_OFF);
124
125 // power saving: the result is worse than nothing... why?
126 // pullup_pins();
127 // set_prr();
128
129#ifdef PROTOCOL_VUSB
130 disable_vusb();
131#endif
132 uart_init(115200);
133 keyboard_init();
134 print("\nSend BREAK for UART Console Commands.\n");
135
136 // TODO: move to iWRAP/suart file
137 print("suart init\n");
138 // suart init
139 // PC4: Tx Output IDLE(Hi)
140 PORTC |= (1 << 4);
141 DDRC |= (1 << 4);
142 // PC5: Rx Input(pull-up)
143 PORTC |= (1 << 5);
144 DDRC &= ~(1 << 5);
145 // suart receive interrut(PC5/PCINT13)
146 PCMSK1 = 0b00100000;
147 PCICR = 0b00000010;
148
149 host_set_driver(iwrap_driver());
150
151 print("iwrap_init()\n");
152 iwrap_init();
153 iwrap_call();
154
155 last_timer = timer_read();
156 while (true) {
157#ifdef PROTOCOL_VUSB
158 if (host_get_driver() == vusb_driver()) usbPoll();
159#endif
160 keyboard_task();
161#ifdef PROTOCOL_VUSB
162 if (host_get_driver() == vusb_driver()) vusb_transfer_keyboard();
163#endif
164 // TODO: depricated
165 if (matrix_is_modified() || console()) {
166 last_timer = timer_read();
167 sleeping = false;
168 } else if (!sleeping && timer_elapsed(last_timer) > 4000) {
169 sleeping = true;
170 iwrap_check_connection();
171 }
172
173 // TODO: suspend.h
174 if (host_get_driver() == iwrap_driver()) {
175 if (sleeping && !insomniac) {
176 _delay_ms(1); // wait for UART to send
177 iwrap_sleep();
178 sleep(WDTO_60MS);
179 }
180 }
181 }
182}
183
184static void sleep(uint8_t term) {
185 WD_SET(WD_IRQ, term);
186
187 cli();
188 set_sleep_mode(SLEEP_MODE_PWR_DOWN);
189 sleep_enable();
190 sleep_bod_disable();
191 sei();
192 sleep_cpu();
193 sleep_disable();
194
195 WD_SET(WD_OFF);
196}
197
198static bool console(void) {
199 // Send to Bluetoot module WT12
200 static bool breaked = false;
201 if (!uart_available())
202 return false;
203 else {
204 uint8_t c;
205 c = uart_getchar();
206 uart_putchar(c);
207 switch (c) {
208 case 0x00: // BREAK signal
209 if (!breaked) {
210 print("break(? for help): ");
211 breaked = true;
212 }
213 break;
214 case '\r':
215 uart_putchar('\n');
216 iwrap_buf_send();
217 break;
218 case '\b':
219 iwrap_buf_del();
220 break;
221 default:
222 if (breaked) {
223 print("\n");
224 console_command(c);
225 breaked = false;
226 } else {
227 iwrap_buf_add(c);
228 }
229 break;
230 }
231 return true;
232 }
233}
234
235bool command_extra(uint8_t code) { return console_command(key2asc(code)); }
236
237static bool console_command(uint8_t c) {
238 switch (c) {
239 case 'h':
240 case '?':
241 print("\nCommands for Bluetooth(WT12/iWRAP):\n");
242 print("r: reset. software reset by watchdog\n");
243 print("i: insomniac. prevent KB from sleeping\n");
244 print("c: iwrap_call. CALL for BT connection.\n");
245#ifdef PROTOCOL_VUSB
246 print("u: USB mode. switch to USB.\n");
247 print("w: BT mode. switch to Bluetooth.\n");
248#endif
249 print("k: kill first connection.\n");
250 print("Del: unpair first pairing.\n");
251 print("\n");
252 return 0;
253 case 'r':
254 print("reset\n");
255 WD_AVR_RESET();
256 return 1;
257 case 'i':
258 insomniac = !insomniac;
259 if (insomniac)
260 print("insomniac\n");
261 else
262 print("not insomniac\n");
263 return 1;
264 case 'c':
265 print("iwrap_call()\n");
266 iwrap_call();
267 return 1;
268#ifdef PROTOCOL_VUSB
269 case 'u':
270 print("USB mode\n");
271 init_vusb();
272 change_driver(vusb_driver());
273 // iwrap_kill();
274 // iwrap_sleep();
275 // disable suart receive interrut(PC5/PCINT13)
276 PCMSK1 &= ~(0b00100000);
277 PCICR &= ~(0b00000010);
278 return 1;
279 case 'w':
280 print("iWRAP mode\n");
281 change_driver(iwrap_driver());
282 disable_vusb();
283 // enable suart receive interrut(PC5/PCINT13)
284 PCMSK1 |= 0b00100000;
285 PCICR |= 0b00000010;
286 return 1;
287#endif
288 case 'k':
289 print("kill\n");
290 iwrap_kill();
291 return 1;
292 case 0x7F: // DELETE
293 print("unpair\n");
294 iwrap_unpair();
295 return 1;
296 }
297 return 0;
298}
299
300// convert keycode into ascii charactor
301static uint8_t key2asc(uint8_t key) {
302 switch (key) {
303 case KC_A:
304 return 'a';
305 case KC_B:
306 return 'b';
307 case KC_C:
308 return 'c';
309 case KC_D:
310 return 'd';
311 case KC_E:
312 return 'e';
313 case KC_F:
314 return 'f';
315 case KC_G:
316 return 'g';
317 case KC_H:
318 return 'h';
319 case KC_I:
320 return 'i';
321 case KC_J:
322 return 'j';
323 case KC_K:
324 return 'k';
325 case KC_L:
326 return 'l';
327 case KC_M:
328 return 'm';
329 case KC_N:
330 return 'n';
331 case KC_O:
332 return 'o';
333 case KC_P:
334 return 'p';
335 case KC_Q:
336 return 'q';
337 case KC_R:
338 return 'r';
339 case KC_S:
340 return 's';
341 case KC_T:
342 return 't';
343 case KC_U:
344 return 'u';
345 case KC_V:
346 return 'v';
347 case KC_W:
348 return 'w';
349 case KC_X:
350 return 'x';
351 case KC_Y:
352 return 'y';
353 case KC_Z:
354 return 'z';
355 case KC_1:
356 return '1';
357 case KC_2:
358 return '2';
359 case KC_3:
360 return '3';
361 case KC_4:
362 return '4';
363 case KC_5:
364 return '5';
365 case KC_6:
366 return '6';
367 case KC_7:
368 return '7';
369 case KC_8:
370 return '8';
371 case KC_9:
372 return '9';
373 case KC_0:
374 return '0';
375 case KC_ENTER:
376 return '\n';
377 case KC_ESCAPE:
378 return 0x1B;
379 case KC_BSPACE:
380 return '\b';
381 case KC_TAB:
382 return '\t';
383 case KC_SPACE:
384 return ' ';
385 case KC_MINUS:
386 return '-';
387 case KC_EQUAL:
388 return '=';
389 case KC_LBRACKET:
390 return '[';
391 case KC_RBRACKET:
392 return ']';
393 case KC_BSLASH:
394 return '\\';
395 case KC_NONUS_HASH:
396 return '#';
397 case KC_SCOLON:
398 return ';';
399 case KC_QUOTE:
400 return '\'';
401 case KC_GRAVE:
402 return '`';
403 case KC_COMMA:
404 return ',';
405 case KC_DOT:
406 return '.';
407 case KC_SLASH:
408 return '/';
409 default:
410 return 0x00;
411 }
412}