]> git.donarmstrong.com Git - qmk_firmware.git/blob - tmk_core/common/keyboard.c
c46a701b3bfd1dae174b2915e4ce60b3b9eedca4
[qmk_firmware.git] / tmk_core / common / keyboard.c
1 /*
2 Copyright 2011, 2012, 2013 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 <stdint.h>
18 #include "keyboard.h"
19 #include "matrix.h"
20 #include "keymap.h"
21 #include "host.h"
22 #include "led.h"
23 #include "keycode.h"
24 #include "timer.h"
25 #include "print.h"
26 #include "debug.h"
27 #include "command.h"
28 #include "util.h"
29 #include "sendchar.h"
30 #include "eeconfig.h"
31 #include "backlight.h"
32 #include "action_layer.h"
33 #ifdef BOOTMAGIC_ENABLE
34 #   include "bootmagic.h"
35 #else
36 #   include "magic.h"
37 #endif
38 #ifdef MOUSEKEY_ENABLE
39 #   include "mousekey.h"
40 #endif
41 #ifdef PS2_MOUSE_ENABLE
42 #   include "ps2_mouse.h"
43 #endif
44 #ifdef SERIAL_MOUSE_ENABLE
45 #   include "serial_mouse.h"
46 #endif
47 #ifdef ADB_MOUSE_ENABLE
48 #   include "adb.h"
49 #endif
50 #ifdef RGBLIGHT_ENABLE
51 #   include "rgblight.h"
52 #endif
53 #ifdef SERIAL_LINK_ENABLE
54 #   include "serial_link/system/serial_link.h"
55 #endif
56 #ifdef VISUALIZER_ENABLE
57 #   include "visualizer/visualizer.h"
58 #endif
59
60 #ifdef MATRIX_HAS_GHOST
61 static bool has_ghost_in_row(uint8_t row)
62 {
63     matrix_row_t matrix_row = matrix_get_row(row);
64     // No ghost exists when less than 2 keys are down on the row
65     if (((matrix_row - 1) & matrix_row) == 0)
66         return false;
67
68     // Ghost occurs when the row shares column line with other row
69     for (uint8_t i=0; i < MATRIX_ROWS; i++) {
70         if (i != row && (matrix_get_row(i) & matrix_row))
71             return true;
72     }
73     return false;
74 }
75 #endif
76
77 __attribute__ ((weak))
78 void matrix_setup(void) {
79 }
80
81 void keyboard_setup(void) {
82     matrix_setup();
83 }
84
85 void keyboard_init(void) {
86     timer_init();
87     matrix_init();
88 #ifdef PS2_MOUSE_ENABLE
89     ps2_mouse_init();
90 #endif
91 #ifdef SERIAL_MOUSE_ENABLE
92     serial_mouse_init();
93 #endif
94 #ifdef ADB_MOUSE_ENABLE
95     adb_mouse_init();
96 #endif
97 #ifdef BOOTMAGIC_ENABLE
98     bootmagic();
99 #else
100     magic();
101 #endif
102 #ifdef BACKLIGHT_ENABLE
103     backlight_init();
104 #endif
105 #ifdef RGBLIGHT_ENABLE
106     rgblight_init();
107 #endif
108 #if defined(NKRO_ENABLE) && defined(FORCE_NKRO)
109         keyboard_nkro = true;
110 #endif
111 }
112
113 /*
114  * Do keyboard routine jobs: scan mantrix, light LEDs, ...
115  * This is repeatedly called as fast as possible.
116  */
117 void keyboard_task(void)
118 {
119     static matrix_row_t matrix_prev[MATRIX_ROWS];
120 #ifdef MATRIX_HAS_GHOST
121     static matrix_row_t matrix_ghost[MATRIX_ROWS];
122 #endif
123     static uint8_t led_status = 0;
124     matrix_row_t matrix_row = 0;
125     matrix_row_t matrix_change = 0;
126
127     matrix_scan();
128     for (uint8_t r = 0; r < MATRIX_ROWS; r++) {
129         matrix_row = matrix_get_row(r);
130         matrix_change = matrix_row ^ matrix_prev[r];
131         if (matrix_change) {
132 #ifdef MATRIX_HAS_GHOST
133             if (has_ghost_in_row(r)) {
134                 /* Keep track of whether ghosted status has changed for
135                  * debugging. But don't update matrix_prev until un-ghosted, or
136                  * the last key would be lost.
137                  */
138                 if (debug_matrix && matrix_ghost[r] != matrix_row) {
139                     matrix_print();
140                 }
141                 matrix_ghost[r] = matrix_row;
142                 continue;
143             }
144             matrix_ghost[r] = matrix_row;
145 #endif
146             if (debug_matrix) matrix_print();
147             for (uint8_t c = 0; c < MATRIX_COLS; c++) {
148                 if (matrix_change & ((matrix_row_t)1<<c)) {
149                     action_exec((keyevent_t){
150                         .key = (keypos_t){ .row = r, .col = c },
151                         .pressed = (matrix_row & ((matrix_row_t)1<<c)),
152                         .time = (timer_read() | 1) /* time should not be 0 */
153                     });
154                     // record a processed key
155                     matrix_prev[r] ^= ((matrix_row_t)1<<c);
156                     // process a key per task call
157                     goto MATRIX_LOOP_END;
158                 }
159             }
160         }
161     }
162     // call with pseudo tick event when no real key event.
163     action_exec(TICK);
164
165 MATRIX_LOOP_END:
166
167 #ifdef MOUSEKEY_ENABLE
168     // mousekey repeat & acceleration
169     mousekey_task();
170 #endif
171
172 #ifdef PS2_MOUSE_ENABLE
173     ps2_mouse_task();
174 #endif
175
176 #ifdef SERIAL_MOUSE_ENABLE
177     serial_mouse_task();
178 #endif
179
180 #ifdef ADB_MOUSE_ENABLE
181     adb_mouse_task();
182 #endif
183
184 #ifdef SERIAL_LINK_ENABLE
185         serial_link_update();
186 #endif
187
188 #ifdef VISUALIZER_ENABLE
189     visualizer_update(default_layer_state, layer_state, host_keyboard_leds());
190 #endif
191
192     // update LED
193     if (led_status != host_keyboard_leds()) {
194         led_status = host_keyboard_leds();
195         keyboard_set_leds(led_status);
196     }
197 }
198
199 void keyboard_set_leds(uint8_t leds)
200 {
201     if (debug_keyboard) { debug("keyboard_set_led: "); debug_hex8(leds); debug("\n"); }
202     led_set(leds);
203 }