]> git.donarmstrong.com Git - tmk_firmware.git/blob - common/keyboard.c
New tapping logic.
[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 #include "bootloader.h"
30 #ifdef MOUSEKEY_ENABLE
31 #include "mousekey.h"
32 #endif
33
34
35 void keyboard_init(void)
36 {
37     // TODO: to enable debug print magic key bind on boot time
38
39     // TODO: configuration of sendchar impl
40     print_sendchar_func = sendchar;
41
42     timer_init();
43     matrix_init();
44
45     /* boot magic keys goes here */
46     matrix_scan();
47 #ifdef IS_BOOTMAGIC_BOOTLOADER
48     /* kick up bootloader */
49     if (IS_BOOTMAGIC_BOOTLOADER()) bootloader_jump();
50 #endif
51 #ifdef IS_BOOTMAGIC_DEBUG
52     if (IS_BOOTMAGIC_DEBUG()) debug_enable = true;
53 #endif
54
55 #ifdef PS2_MOUSE_ENABLE
56     ps2_mouse_init();
57 #endif
58 }
59
60 /*
61  * Do keyboard routine jobs: scan mantrix, light LEDs, ...
62  * This is repeatedly called as fast as possible.
63  */
64 void keyboard_task(void)
65 {
66     static matrix_row_t matrix_prev[MATRIX_ROWS];
67     static uint8_t led_status = 0;
68     matrix_row_t matrix_row = 0;
69     matrix_row_t matrix_change = 0;
70
71     matrix_scan();
72     for (uint8_t r = 0; r < MATRIX_ROWS; r++) {
73         matrix_row = matrix_get_row(r);
74         matrix_change = matrix_row ^ matrix_prev[r];
75         if (matrix_change) {
76             if (debug_matrix) matrix_print();
77
78             for (uint8_t c = 0; c < MATRIX_COLS; c++) {
79                 if (matrix_change & ((matrix_row_t)1<<c)) {
80                     action_exec((keyevent_t){
81                         .key.pos  = (keypos_t){ .row = r, .col = c },
82                         .pressed = (matrix_row & (1<<c)),
83                         .time = timer_read()
84                     });
85                     // record a processed key
86                     matrix_prev[r] ^= ((matrix_row_t)1<<c);
87                     // process a key per task call
88                     goto MATRIX_LOOP_END;
89                 }
90             }
91         }
92     }
93     // call with pseudo tick event when no real key event.
94     action_exec(TICK);
95
96 MATRIX_LOOP_END:
97
98 #ifdef MOUSEKEY_ENABLE
99     // mousekey repeat & acceleration
100     mousekey_task();
101 #endif
102
103     // update LED
104     if (led_status != host_keyboard_leds()) {
105         led_status = host_keyboard_leds();
106         keyboard_set_leds(led_status);
107     }
108
109     return;
110 }
111
112 void keyboard_set_leds(uint8_t leds)
113 {
114     led_set(leds);
115 }