summaryrefslogtreecommitdiff
path: root/tmk_core/protocol/iwrap/main.c
blob: 6e9b5455b13adfb5233471d0bc1c8004875b72b1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
/*
Copyright 2011 Jun Wako <wakojun@gmail.com>

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdint.h>
#include <avr/interrupt.h>
#include <avr/io.h>
//#include <avr/wdt.h>
#include "wd.h"  // in order to use watchdog in interrupt mode
#include <avr/sleep.h>
#include <util/delay.h>
#include <avr/power.h>
#include "keyboard.h"
#include "matrix.h"
#include "host.h"
#include "action.h"
#include "iwrap.h"
#ifdef PROTOCOL_VUSB
#    include "vusb.h"
#    include "usbdrv.h"
#endif
#include "uart.h"
#include "suart.h"
#include "timer.h"
#include "debug.h"
#include "keycode.h"
#include "command.h"

static void    sleep(uint8_t term);
static bool    console(void);
static bool    console_command(uint8_t c);
static uint8_t key2asc(uint8_t key);

/*
static void set_prr(void)
{
    power_adc_disable();
    power_spi_disable();
    power_twi_disable();
#ifndef TIMER_H
    //power_timer0_disable(); // used in timer.c
#endif
    power_timer1_disable();
    power_timer2_disable();
}
*/

/*
static void pullup_pins(void)
{
    // DDRs are set to 0(input) by default.
#ifdef PORTA
    PORTA = 0xFF;
#endif
    PORTB = 0xFF;
    PORTC = 0xFF;
    PORTD = 0xFF;
#ifdef PORTE
    PORTE = 0xFF;
#endif
#ifdef PORTE
    PORTF = 0xFF;
#endif
}
*/

#ifdef PROTOCOL_VUSB
static void disable_vusb(void) {
    // disable interrupt & disconnect to prevent host from enumerating
    USB_INTR_ENABLE &= ~(1 << USB_INTR_ENABLE_BIT);
    usbDeviceDisconnect();
}

static void enable_vusb(void) {
    USB_INTR_ENABLE |= (1 << USB_INTR_ENABLE_BIT);
    usbDeviceConnect();
}

static void init_vusb(void) {
    uint8_t i = 0;

    usbInit();
    disable_vusb();
    /* fake USB disconnect for > 250 ms */
    while (--i) {
        _delay_ms(1);
    }
    enable_vusb();
}
#endif

void change_driver(host_driver_t *driver) {
    /*
    host_clear_keyboard_report();
    host_swap_keyboard_report();
    host_clear_keyboard_report();
    host_send_keyboard_report();
    */
    clear_keyboard();
    _delay_ms(1000);
    host_set_driver(driver);
}

static bool     sleeping   = false;
static bool     insomniac  = false;  // TODO: should be false for power saving
static uint16_t last_timer = 0;

int main(void) {
    MCUSR = 0;
    clock_prescale_set(clock_div_1);
    WD_SET(WD_OFF);

    // power saving: the result is worse than nothing... why?
    // pullup_pins();
    // set_prr();

#ifdef PROTOCOL_VUSB
    disable_vusb();
#endif
    uart_init(115200);
    keyboard_init();
    print("\nSend BREAK for UART Console Commands.\n");

    // TODO: move to iWRAP/suart file
    print("suart init\n");
    // suart init
    // PC4: Tx Output IDLE(Hi)
    PORTC |= (1 << 4);
    DDRC |= (1 << 4);
    // PC5: Rx Input(pull-up)
    PORTC |= (1 << 5);
    DDRC &= ~(1 << 5);
    // suart receive interrut(PC5/PCINT13)
    PCMSK1 = 0b00100000;
    PCICR  = 0b00000010;

    host_set_driver(iwrap_driver());

    print("iwrap_init()\n");
    iwrap_init();
    iwrap_call();

    last_timer = timer_read();
    while (true) {
#ifdef PROTOCOL_VUSB
        if (host_get_driver() == vusb_driver()) usbPoll();
#endif
        keyboard_task();
#ifdef PROTOCOL_VUSB
        if (host_get_driver() == vusb_driver()) vusb_transfer_keyboard();
#endif
        // TODO: depricated
        if (matrix_is_modified() || console()) {
            last_timer = timer_read();
            sleeping   = false;
        } else if (!sleeping && timer_elapsed(last_timer) > 4000) {
            sleeping = true;
            iwrap_check_connection();
        }

        // TODO: suspend.h
        if (host_get_driver() == iwrap_driver()) {
            if (sleeping && !insomniac) {
                _delay_ms(1);  // wait for UART to send
                iwrap_sleep();
                sleep(WDTO_60MS);
            }
        }
    }
}

static void sleep(uint8_t term) {
    WD_SET(WD_IRQ, term);

    cli();
    set_sleep_mode(SLEEP_MODE_PWR_DOWN);
    sleep_enable();
    sleep_bod_disable();
    sei();
    sleep_cpu();
    sleep_disable();

    WD_SET(WD_OFF);
}

static bool console(void) {
    // Send to Bluetoot module WT12
    static bool breaked = false;
    if (!uart_available())
        return false;
    else {
        uint8_t c;
        c = uart_getchar();
        uart_putchar(c);
        switch (c) {
            case 0x00:  // BREAK signal
                if (!breaked) {
                    print("break(? for help): ");
                    breaked = true;
                }
                break;
            case '\r':
                uart_putchar('\n');
                iwrap_buf_send();
                break;
            case '\b':
                iwrap_buf_del();
                break;
            default:
                if (breaked) {
                    print("\n");
                    console_command(c);
                    breaked = false;
                } else {
                    iwrap_buf_add(c);
                }
                break;
        }
        return true;
    }
}

bool command_extra(uint8_t code) { return console_command(key2asc(code)); }

static bool console_command(uint8_t c) {
    switch (c) {
        case 'h':
        case '?':
            print("\nCommands for Bluetooth(WT12/iWRAP):\n");
            print("r: reset. software reset by watchdog\n");
            print("i: insomniac. prevent KB from sleeping\n");
            print("c: iwrap_call. CALL for BT connection.\n");
#ifdef PROTOCOL_VUSB
            print("u: USB mode. switch to USB.\n");
            print("w: BT mode. switch to Bluetooth.\n");
#endif
            print("k: kill first connection.\n");
            print("Del: unpair first pairing.\n");
            print("\n");
            return 0;
        case 'r':
            print("reset\n");
            WD_AVR_RESET();
            return 1;
        case 'i':
            insomniac = !insomniac;
            if (insomniac)
                print("insomniac\n");
            else
                print("not insomniac\n");
            return 1;
        case 'c':
            print("iwrap_call()\n");
            iwrap_call();
            return 1;
#ifdef PROTOCOL_VUSB
        case 'u':
            print("USB mode\n");
            init_vusb();
            change_driver(vusb_driver());
            // iwrap_kill();
            // iwrap_sleep();
            // disable suart receive interrut(PC5/PCINT13)
            PCMSK1 &= ~(0b00100000);
            PCICR &= ~(0b00000010);
            return 1;
        case 'w':
            print("iWRAP mode\n");
            change_driver(iwrap_driver());
            disable_vusb();
            // enable suart receive interrut(PC5/PCINT13)
            PCMSK1 |= 0b00100000;
            PCICR |= 0b00000010;
            return 1;
#endif
        case 'k':
            print("kill\n");
            iwrap_kill();
            return 1;
        case 0x7F:  // DELETE
            print("unpair\n");
            iwrap_unpair();
            return 1;
    }
    return 0;
}

// convert keycode into ascii charactor
static uint8_t key2asc(uint8_t key) {
    switch (key) {
        case KC_A:
            return 'a';
        case KC_B:
            return 'b';
        case KC_C:
            return 'c';
        case KC_D:
            return 'd';
        case KC_E:
            return 'e';
        case KC_F:
            return 'f';
        case KC_G:
            return 'g';
        case KC_H:
            return 'h';
        case KC_I:
            return 'i';
        case KC_J:
            return 'j';
        case KC_K:
            return 'k';
        case KC_L:
            return 'l';
        case KC_M:
            return 'm';
        case KC_N:
            return 'n';
        case KC_O:
            return 'o';
        case KC_P:
            return 'p';
        case KC_Q:
            return 'q';
        case KC_R:
            return 'r';
        case KC_S:
            return 's';
        case KC_T:
            return 't';
        case KC_U:
            return 'u';
        case KC_V:
            return 'v';
        case KC_W:
            return 'w';
        case KC_X:
            return 'x';
        case KC_Y:
            return 'y';
        case KC_Z:
            return 'z';
        case KC_1:
            return '1';
        case KC_2:
            return '2';
        case KC_3:
            return '3';
        case KC_4:
            return '4';
        case KC_5:
            return '5';
        case KC_6:
            return '6';
        case KC_7:
            return '7';
        case KC_8:
            return '8';
        case KC_9:
            return '9';
        case KC_0:
            return '0';
        case KC_ENTER:
            return '\n';
        case KC_ESCAPE:
            return 0x1B;
        case KC_BSPACE:
            return '\b';
        case KC_TAB:
            return '\t';
        case KC_SPACE:
            return ' ';
        case KC_MINUS:
            return '-';
        case KC_EQUAL:
            return '=';
        case KC_LBRACKET:
            return '[';
        case KC_RBRACKET:
            return ']';
        case KC_BSLASH:
            return '\\';
        case KC_NONUS_HASH:
            return '#';
        case KC_SCOLON:
            return ';';
        case KC_QUOTE:
            return '\'';
        case KC_GRAVE:
            return '`';
        case KC_COMMA:
            return ',';
        case KC_DOT:
            return '.';
        case KC_SLASH:
            return '/';
        default:
            return 0x00;
    }
}