]> git.donarmstrong.com Git - tmk_firmware.git/blob - common/keyboard.c
Add bootmagic keys.(hhkb)
[tmk_firmware.git] / common / keyboard.c
1 /*
2 Copyright 2011,2012 Jun Wako <wakojun@gmail.com>
3
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.
8
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.
13
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/>.
16 */
17 #include "keyboard.h"
18 #include "matrix.h"
19 #include "keymap.h"
20 #include "host.h"
21 #include "led.h"
22 #include "keycode.h"
23 #include "timer.h"
24 #include "print.h"
25 #include "debug.h"
26 #include "command.h"
27 #include "util.h"
28 #include "sendchar.h"
29 #ifdef MOUSEKEY_ENABLE
30 #include "mousekey.h"
31 #endif
32
33
34 void keyboard_init(void)
35 {
36     // TODO: to enable debug print magic key bind on boot time
37
38     // TODO: configuration of sendchar impl
39     print_sendchar_func = sendchar;
40
41     timer_init();
42     matrix_init();
43
44     /* boot magic keys goes here */
45     matrix_scan();
46 #ifdef IS_BOOTMAGIC_BOOTLOADER
47     /* kick up bootloader */
48     if (IS_BOOTMAGIC_BOOTLOADER()) bootloader_jump();
49 #endif
50 #ifdef IS_BOOTMAGIC_DEBUG
51     if (IS_BOOTMAGIC_DEBUG()) debug_enable = true;
52 #endif
53
54 #ifdef PS2_MOUSE_ENABLE
55     ps2_mouse_init();
56 #endif
57 }
58
59 /*
60  * Do keyboard routine jobs: scan mantrix, light LEDs, ...
61  * This is repeatedly called as fast as possible.
62  */
63 void keyboard_task(void)
64 {
65     static matrix_row_t matrix_prev[MATRIX_ROWS];
66     static uint8_t led_status = 0;
67     matrix_row_t matrix_row = 0;
68     matrix_row_t matrix_change = 0;
69
70     matrix_scan();
71     for (int r = 0; r < MATRIX_ROWS; r++) {
72         matrix_row = matrix_get_row(r);
73         matrix_change = matrix_row ^ matrix_prev[r];
74         if (matrix_change) {
75             if (debug_matrix) matrix_print();
76
77             for (int c = 0; c < MATRIX_COLS; c++) {
78                 if (matrix_change & (1<<c)) {
79                     action_exec((keyevent_t){
80                         .key.pos  = (keypos_t){ .row = r, .col = c },
81                         .pressed = (matrix_row & (1<<c)),
82                         .time = (timer_read() | 1) /* NOTE: 0 means no event */
83                     });
84                     // record a processed key
85                     matrix_prev[r] ^= (1<<c);
86                     // process a key per task call
87                     goto MATRIX_LOOP_END;
88                 }
89             }
90         }
91     }
92     // call to update delaying layer when no real event
93     action_exec((keyevent_t) {
94         .key.pos = (keypos_t){ .row = 255, .col = 255 }, // assume this key doesn't exist
95         .pressed = false,
96         .time = 0,
97     });
98
99 MATRIX_LOOP_END:
100
101 #ifdef MOUSEKEY_ENABLE
102     // mousekey repeat & acceleration
103     mousekey_task();
104 #endif
105
106     // update LED
107     if (led_status != host_keyboard_leds()) {
108         led_status = host_keyboard_leds();
109         keyboard_set_leds(led_status);
110     }
111
112     return;
113 }
114
115 void keyboard_set_leds(uint8_t leds)
116 {
117     led_set(leds);
118 }