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