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