]> git.donarmstrong.com Git - qmk_firmware.git/blob - tmk_core/common/keyboard.c
13b3cb4c0c1f5a035034760cef32c38fa6edc055
[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 STENO_ENABLE
55 #   include "process_steno.h"
56 #endif
57 #ifdef FAUXCLICKY_ENABLE
58 #   include "fauxclicky.h"
59 #endif
60 #ifdef SERIAL_LINK_ENABLE
61 #   include "serial_link/system/serial_link.h"
62 #endif
63 #ifdef VISUALIZER_ENABLE
64 #   include "visualizer/visualizer.h"
65 #endif
66 #ifdef POINTING_DEVICE_ENABLE
67 #   include "pointing_device.h"
68 #endif
69 #ifdef MIDI_ENABLE
70 #   include "process_midi.h"
71 #endif
72 #ifdef HD44780_ENABLE
73 #   include "hd44780.h"
74 #endif
75
76 #ifdef MATRIX_HAS_GHOST
77 extern const uint16_t keymaps[][MATRIX_ROWS][MATRIX_COLS];
78 static matrix_row_t get_real_keys(uint8_t row, matrix_row_t rowdata){
79     matrix_row_t out = 0;
80     for (uint8_t col = 0; col < MATRIX_COLS; col++) {
81         //read each key in the row data and check if the keymap defines it as a real key
82         if (pgm_read_byte(&keymaps[0][row][col]) && (rowdata & (1<<col))){
83             //this creates new row data, if a key is defined in the keymap, it will be set here
84             out |= 1<<col;
85         }
86     }
87     return out;
88 }
89
90 static inline bool popcount_more_than_one(matrix_row_t rowdata)
91 {
92     rowdata &= rowdata-1; //if there are less than two bits (keys) set, rowdata will become zero
93     return rowdata;
94 }
95
96 static inline bool has_ghost_in_row(uint8_t row, matrix_row_t rowdata)
97 {
98     /* No ghost exists when less than 2 keys are down on the row.
99     If there are "active" blanks in the matrix, the key can't be pressed by the user,
100     there is no doubt as to which keys are really being pressed.
101     The ghosts will be ignored, they are KC_NO.   */
102     rowdata = get_real_keys(row, rowdata);
103     if ((popcount_more_than_one(rowdata)) == 0){
104         return false;
105     }
106     /* Ghost occurs when the row shares a column line with other row,
107     and two columns are read on each row. Blanks in the matrix don't matter,
108     so they are filtered out.
109     If there are two or more real keys pressed and they match columns with
110     at least two of another row's real keys, the row will be ignored. Keep in mind,
111     we are checking one row at a time, not all of them at once.
112     */
113     for (uint8_t i=0; i < MATRIX_ROWS; i++) {
114         if (i != row && popcount_more_than_one(get_real_keys(i, matrix_get_row(i)) & rowdata)){
115             return true;
116         }
117     }
118     return false;
119 }
120
121 #endif
122
123 /** \brief matrix_setup
124  *
125  * FIXME: needs doc
126  */
127 __attribute__ ((weak))
128 void matrix_setup(void) {
129 }
130
131 /** \brief keyboard_setup
132  *
133  * FIXME: needs doc
134  */
135 void keyboard_setup(void) {
136     matrix_setup();
137 }
138
139 /** \brief is_keyboard_master
140  *
141  * FIXME: needs doc
142  */
143 __attribute__((weak))
144 bool is_keyboard_master(void) {
145     return true;
146 }
147
148 /** \brief keyboard_init
149  *
150  * FIXME: needs doc
151  */
152 void keyboard_init(void) {
153     timer_init();
154 // To use PORTF disable JTAG with writing JTD bit twice within four cycles.
155 #if  (defined(__AVR_AT90USB1286__) || defined(__AVR_AT90USB1287__) || defined(__AVR_ATmega32U4__))
156   MCUCR |= _BV(JTD);
157   MCUCR |= _BV(JTD);
158 #endif
159     matrix_init();
160 #ifdef PS2_MOUSE_ENABLE
161     ps2_mouse_init();
162 #endif
163 #ifdef SERIAL_MOUSE_ENABLE
164     serial_mouse_init();
165 #endif
166 #ifdef ADB_MOUSE_ENABLE
167     adb_mouse_init();
168 #endif
169 #ifdef BOOTMAGIC_ENABLE
170     bootmagic();
171 #else
172     magic();
173 #endif
174 #ifdef BACKLIGHT_ENABLE
175     backlight_init();
176 #endif
177 #ifdef RGBLIGHT_ENABLE
178     rgblight_init();
179 #endif
180 #ifdef STENO_ENABLE
181     steno_init();
182 #endif
183 #ifdef FAUXCLICKY_ENABLE
184     fauxclicky_init();
185 #endif
186 #ifdef POINTING_DEVICE_ENABLE
187     pointing_device_init();
188 #endif
189 #if defined(NKRO_ENABLE) && defined(FORCE_NKRO)
190     keymap_config.nkro = 1;
191 #endif
192 }
193
194 /** \brief Keyboard task: Do keyboard routine jobs
195  *
196  * Do routine keyboard jobs:
197  *
198  * * scan matrix
199  * * handle mouse movements
200  * * run visualizer code
201  * * handle midi commands
202  * * light LEDs
203  *
204  * This is repeatedly called as fast as possible.
205  */
206 void keyboard_task(void)
207 {
208     static matrix_row_t matrix_prev[MATRIX_ROWS];
209 #ifdef MATRIX_HAS_GHOST
210   //  static matrix_row_t matrix_ghost[MATRIX_ROWS];
211 #endif
212     static uint8_t led_status = 0;
213     matrix_row_t matrix_row = 0;
214     matrix_row_t matrix_change = 0;
215 #ifdef QMK_KEYS_PER_SCAN
216     uint8_t keys_processed = 0;
217 #endif
218
219     matrix_scan();
220     if (is_keyboard_master()) {
221         for (uint8_t r = 0; r < MATRIX_ROWS; r++) {
222             matrix_row = matrix_get_row(r);
223             matrix_change = matrix_row ^ matrix_prev[r];
224             if (matrix_change) {
225 #ifdef MATRIX_HAS_GHOST
226                 if (has_ghost_in_row(r, matrix_row)) {
227                     /* Keep track of whether ghosted status has changed for
228                     * debugging. But don't update matrix_prev until un-ghosted, or
229                     * the last key would be lost.
230                     */
231                     //if (debug_matrix && matrix_ghost[r] != matrix_row) {
232                     //    matrix_print();
233                     //}
234                     //matrix_ghost[r] = matrix_row;
235                     continue;
236                 }
237                 //matrix_ghost[r] = matrix_row;
238 #endif
239                 if (debug_matrix) matrix_print();
240                 for (uint8_t c = 0; c < MATRIX_COLS; c++) {
241                     if (matrix_change & ((matrix_row_t)1<<c)) {
242                         action_exec((keyevent_t){
243                             .key = (keypos_t){ .row = r, .col = c },
244                             .pressed = (matrix_row & ((matrix_row_t)1<<c)),
245                             .time = (timer_read() | 1) /* time should not be 0 */
246                         });
247                         // record a processed key
248                         matrix_prev[r] ^= ((matrix_row_t)1<<c);
249 #ifdef QMK_KEYS_PER_SCAN
250                         // only jump out if we have processed "enough" keys.
251                         if (++keys_processed >= QMK_KEYS_PER_SCAN)
252 #endif
253                         // process a key per task call
254                         goto MATRIX_LOOP_END;
255                     }
256                 }
257             }
258         }
259     }
260     // call with pseudo tick event when no real key event.
261 #ifdef QMK_KEYS_PER_SCAN
262     // we can get here with some keys processed now.
263     if (!keys_processed)
264 #endif
265     action_exec(TICK);
266
267 MATRIX_LOOP_END:
268
269 #ifdef MOUSEKEY_ENABLE
270     // mousekey repeat & acceleration
271     mousekey_task();
272 #endif
273
274 #ifdef PS2_MOUSE_ENABLE
275     ps2_mouse_task();
276 #endif
277
278 #ifdef SERIAL_MOUSE_ENABLE
279     serial_mouse_task();
280 #endif
281
282 #ifdef ADB_MOUSE_ENABLE
283     adb_mouse_task();
284 #endif
285
286 #ifdef SERIAL_LINK_ENABLE
287         serial_link_update();
288 #endif
289
290 #ifdef VISUALIZER_ENABLE
291     visualizer_update(default_layer_state, layer_state, visualizer_get_mods(), host_keyboard_leds());
292 #endif
293
294 #ifdef POINTING_DEVICE_ENABLE
295     pointing_device_task();
296 #endif
297
298 #ifdef MIDI_ENABLE
299     midi_task();
300 #endif
301
302     // update LED
303     if (led_status != host_keyboard_leds()) {
304         led_status = host_keyboard_leds();
305         keyboard_set_leds(led_status);
306     }
307 }
308
309 /** \brief keyboard set leds
310  *
311  * FIXME: needs doc
312  */
313 void keyboard_set_leds(uint8_t leds)
314 {
315     if (debug_keyboard) { debug("keyboard_set_led: "); debug_hex8(leds); debug("\n"); }
316     led_set(leds);
317 }