summaryrefslogtreecommitdiff
path: root/keyboard/hhkb_rn42/main.c
blob: 0b455193fb9fefed7a4956e9e9acff3bd3ef34ae (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
#include <avr/io.h>
#include <avr/power.h>
#include <avr/wdt.h>
#include "lufa.h"
#include "print.h"
#include "sendchar.h"
#include "rn42.h"
#include "serial.h"
#include "keyboard.h"
#include "command.h"
#include "keycode.h"
#include "action.h"
#include "action_util.h"
#include "wait.h"
#include "suart.h"

bool config_mode = false;

static int8_t sendchar_func(uint8_t c)
{
    sendchar(c);    // LUFA
    xmit(c);        // SUART
}

static void SetupHardware(void)
{
    /* Disable watchdog if enabled by bootloader/fuses */
    MCUSR &= ~(1 << WDRF);
    wdt_disable();

    /* Disable clock division */
    clock_prescale_set(clock_div_1);

    // Leonardo needs. Without this USB device is not recognized.
    USB_Disable();

    USB_Init();

    // for Console_Task
    USB_Device_EnableSOFEvents();
    print_set_sendchar(sendchar_func);

    // SUART PD0:output, PD1:input
    DDRD |= (1<<0);
    PORTD |= (1<<0);
    DDRD &= ~(1<<1);
    PORTD |= (1<<1);

    // CTS control
    CTS_INIT();
}

static bool force_usb = false;
int main(void)  __attribute__ ((weak));
int main(void)
{
    SetupHardware();
    sei();

    /* wait for USB startup to get ready for debug output */
    uint8_t timeout = 255;  // timeout when USB is not available(Bluetooth)
    while (timeout-- && USB_DeviceState != DEVICE_STATE_Configured) {
        wait_ms(4);
#if defined(INTERRUPT_CONTROL_ENDPOINT)
        ;
#else
        USB_USBTask();
#endif
    }
    print("USB configured.\n");

    rn42_init();
    print("RN-42 init\n");

    /* init modules */
    keyboard_init();

    if (rn42_ready()) {
        host_set_driver(&rn42_driver);
    } else {
        host_set_driver(&lufa_driver);
    }

#ifdef SLEEP_LED_ENABLE
    sleep_led_init();
#endif

    // ADC for battery
    //ADMUX = (1<<REFS0); // Ref:AVCC, Input:ADC0(PF0)
    ADMUX = (1<<REFS1) | (1<<REFS0); // Ref:AVCC, Input:ADC0(PF0)
    ADCSRA = (1<<ADPS2) | (1<<ADPS1) | (1<<ADPS0); // Prescale:128
    ADCSRA |= (1<<ADEN); // enable ADC

    print("Keyboard start.\n");
    while (1) {
/*
        while (USB_DeviceState == DEVICE_STATE_Suspended) {
            suspend_power_down();
            if (USB_Device_RemoteWakeupEnabled && suspend_wakeup_condition()) {
                    USB_Device_SendRemoteWakeup();
            }
        }
*/

        keyboard_task();

#if !defined(INTERRUPT_CONTROL_ENDPOINT)
        USB_USBTask();
#endif

        int16_t c;
        if (config_mode) {
            while ((c = serial_recv2()) != -1) {
                // without flow control it'll fail to receive data when flooded
                CTS_HI();
                xprintf("%c", c);
                CTS_LO();
            }
        } else {
            while ((c = serial_recv2()) != -1) {
                // LED Out report: 0xFE, 0x02, 0x01, <leds>
                // To get the report over UART set bit3 with SH, command.
                static enum {LED_INIT, LED_FE, LED_02, LED_01} state = LED_INIT;
                xprintf("%X\n", c);
                switch (state) {
                    case LED_INIT:
                        if (c == 0xFE) state = LED_FE;
                        else           state = LED_INIT;
                        break;
                    case LED_FE:
                        if (c == 0x02) state = LED_02;
                        else           state = LED_INIT;
                        break;
                    case LED_02:
                        if (c == 0x01) state = LED_01;
                        else           state = LED_INIT;
                        break;
                    case LED_01:
                        // TODO: move to rn42.c and make accessible with keyboard_leds()
                        xprintf("LED status: %X\n", c);
                        state = LED_INIT;
                        break;
                    default:
                        state = LED_INIT;
                }
            }
        }

        /* Bluetooth mode when ready */
        if (!config_mode && !force_usb) {
            if (rn42_ready() && host_get_driver() != &rn42_driver) {
                clear_keyboard();
                host_set_driver(&rn42_driver);
            } else if (!rn42_ready() && host_get_driver() != &lufa_driver) {
                clear_keyboard();
                host_set_driver(&lufa_driver);
            }
        }
    }
}

static bool local_echo = false;
bool command_extra(uint8_t code)
{
    static host_driver_t *prev_driver = &rn42_driver;
    switch (code) {
        case KC_H:
        case KC_SLASH: /* ? */
            print("\n\n----- Bluetooth RN-42 Help -----\n");
            print("w:   toggle RN-42 config mode(enter/exit)\n");
            print("l:   toggle print module output(local echo)\n");
            print("a:   Bluetooth auto connect\n");
            print("del: Bluetooth disconnect\n");
            print("i:   info\n");
            print("b:   battery voltage\n");

            if (config_mode) {
                return true;
            } else {
                print("u:   force USB mode\n");
                return false;   // to display default command help
            }
        case KC_W:
            if (!config_mode) {
                print("\nEnter RN-42 config mode\n");
                print("type $$$ to enter RN-42 command mode\n");
                print("type Delete to disconnect Bluetooth connection\n");
                command_state = CONSOLE;
                config_mode = true;
                prev_driver = host_get_driver();
                clear_keyboard();
                host_set_driver(&rn42_config_driver);
            } else {
                print("\nExit RN-42 config mode\n");
                command_state = ONESHOT;
                config_mode = false;
                clear_keyboard();
                host_set_driver(prev_driver);
            }
            return true;
        case KC_L:
            if (local_echo) {
                print("local echo off\n");
                local_echo = false;
            } else {
                print("local echo on\n");
                local_echo = true;
            }
            return true;
        case KC_U:
            if (config_mode) return false;
            if (force_usb) {
                print("Auto mode\n");
                force_usb = false;
            } else {
                print("USB mode\n");
                force_usb = true;
                clear_keyboard();
                host_set_driver(&lufa_driver);
            }
            return true;
        case KC_A:
            print("auto connect\n");
            rn42_autoconnect();
            return true;
        case KC_DELETE:
            print("disconnect\n");
            rn42_disconnect();
            //rn42_putc('\0');    // see 5.3.4.4 DISCONNECT KEY of User's Guide
            return true;
        case KC_I:
            print("\nRN-42 info\n");
            xprintf("protocol: %s\n", (host_get_driver() == &rn42_driver) ? "RN-42" : "LUFA");
            xprintf("force_usb: %X\n", force_usb);
            xprintf("rn42_ready(): %X\n", rn42_ready());
            xprintf("config_mode: %X\n", config_mode);
            return true;
        case KC_B:
            // battery monitor
            ADCSRA |= (1<<ADEN) | (1<<ADSC);
            while (ADCSRA & (1<<ADSC)) ;
            uint16_t bat = ADCL;
            bat = ADCH<<8 | bat;
            xprintf("BAT: %04X\n", bat);

            ADCSRA |= (1<<ADEN) | (1<<ADSC);
            while (ADCSRA & (1<<ADSC)) ;
            bat = ADCL;
            bat = ADCH<<8 | bat;
            xprintf("BAT: %04X\n", bat);

            ADCSRA &= ~(1<<ADEN);
            return true;
        default:
            if (config_mode)
                return true;
            else
                return false;   // exec default command
    }
    return true;
}

static uint8_t code2asc(uint8_t code);
bool command_console_extra(uint8_t code)
{
    switch (code) {
        default:
            rn42_putc(code2asc(code));
            if (local_echo) xprintf("%c", code2asc(code));
            return true;
    }
    return false;
}

// convert keycode into ascii charactor
static uint8_t code2asc(uint8_t code)
{
    bool shifted = (get_mods() & (MOD_BIT(KC_LSHIFT)|MOD_BIT(KC_RSHIFT))) ? true : false;
    switch (code) {
        case KC_A: return (shifted ? 'A' : 'a');
        case KC_B: return (shifted ? 'B' : 'b');
        case KC_C: return (shifted ? 'C' : 'c');
        case KC_D: return (shifted ? 'D' : 'd');
        case KC_E: return (shifted ? 'E' : 'e');
        case KC_F: return (shifted ? 'F' : 'f');
        case KC_G: return (shifted ? 'G' : 'g');
        case KC_H: return (shifted ? 'H' : 'h');
        case KC_I: return (shifted ? 'I' : 'i');
        case KC_J: return (shifted ? 'J' : 'j');
        case KC_K: return (shifted ? 'K' : 'k');
        case KC_L: return (shifted ? 'L' : 'l');
        case KC_M: return (shifted ? 'M' : 'm');
        case KC_N: return (shifted ? 'N' : 'n');
        case KC_O: return (shifted ? 'O' : 'o');
        case KC_P: return (shifted ? 'P' : 'p');
        case KC_Q: return (shifted ? 'Q' : 'q');
        case KC_R: return (shifted ? 'R' : 'r');
        case KC_S: return (shifted ? 'S' : 's');
        case KC_T: return (shifted ? 'T' : 't');
        case KC_U: return (shifted ? 'U' : 'u');
        case KC_V: return (shifted ? 'V' : 'v');
        case KC_W: return (shifted ? 'W' : 'w');
        case KC_X: return (shifted ? 'X' : 'x');
        case KC_Y: return (shifted ? 'Y' : 'y');
        case KC_Z: return (shifted ? 'Z' : 'z');
        case KC_1: return (shifted ? '!' : '1');
        case KC_2: return (shifted ? '@' : '2');
        case KC_3: return (shifted ? '#' : '3');
        case KC_4: return (shifted ? '$' : '4');
        case KC_5: return (shifted ? '%' : '5');
        case KC_6: return (shifted ? '^' : '6');
        case KC_7: return (shifted ? '&' : '7');
        case KC_8: return (shifted ? '*' : '8');
        case KC_9: return (shifted ? '(' : '9');
        case KC_0: return (shifted ? ')' : '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 (shifted ? '_' : '-');
        case KC_EQUAL: return (shifted ? '+' : '=');
        case KC_LBRACKET: return (shifted ? '{' : '[');
        case KC_RBRACKET: return (shifted ? '}' : ']');
        case KC_BSLASH: return (shifted ? '|' : '\\');
        case KC_NONUS_HASH: return (shifted ? '|' : '\\');
        case KC_SCOLON: return (shifted ? ':' : ';');
        case KC_QUOTE: return (shifted ? '"' : '\'');
        case KC_GRAVE: return (shifted ? '~' : '`');
        case KC_COMMA: return (shifted ? '<' : ',');
        case KC_DOT: return (shifted ? '>' : '.');
        case KC_SLASH: return (shifted ? '?' : '/');
        case KC_DELETE: return '\0';    // Delete to disconnect
        default: return ' ';
    }
}