]> git.donarmstrong.com Git - qmk_firmware.git/blob - key_process.c
FIX: clear keyboard_keys.
[qmk_firmware.git] / key_process.c
1 // TODO: clean unused headers
2 #include <stdbool.h>
3 #include <avr/io.h>
4 #include <avr/pgmspace.h>
5 #include <avr/interrupt.h>
6 #include <util/delay.h>
7 #include "usb.h"
8 #include "usb_keyboard.h"
9 #include "usb_mouse.h"
10 #include "print.h"
11 #include "matrix.h"
12 #include "keymap.h"
13 #include "jump_bootloader.h"
14
15 #include "key_process.h"
16
17
18 // for Teensy/Teensy++ 2.0
19 #define LED_CONFIG    (DDRD |= (1<<6))
20 #define LED_ON        (PORTD |= (1<<6))
21 #define LED_OFF       (PORTD &= ~(1<<6))
22
23 #define MOUSE_MOVE_UNIT 10
24 #define MOUSE_DELAY_MS 200
25 #define MOUSE_DELAY_ACC 5
26
27
28
29 static void print_matrix(void);
30 static void print_keys(void);
31 static void print_mouse(int8_t mouse_x, int8_t mouse_y, int8_t wheel_v, int8_t wheel_h);
32
33 void proc_matrix(void) {
34     static int mouse_repeat = 0;
35
36     bool modified = false;
37     bool has_ghost = false;
38     int layer = 0;
39     int key_index = 0;
40     uint8_t mouse_btn = 0;
41     int8_t mouse_x = 0;
42     int8_t mouse_y = 0;
43     int8_t mouse_wheel = 0;
44     int8_t mouse_hwheel = 0;
45
46         matrix_scan();
47         modified = matrix_is_modified();
48         has_ghost = matrix_has_ghost();
49         layer = get_layer();
50
51         // print matrix state for debug
52         if (modified) {
53             print_matrix();
54
55             // LED flash for debug
56             LED_CONFIG;
57             LED_ON;
58         }
59
60         keyboard_modifier_keys = 0;
61         for (int i = 0; i < 6; i++) keyboard_keys[i] = KB_NO;
62         key_index = 0;
63         mouse_btn = 0;
64         mouse_x = 0;
65         mouse_y = 0;
66         mouse_wheel = 0;
67         mouse_hwheel = 0;
68
69         // convert matrix state to HID report
70         for (int row = 0; row < MATRIX_ROWS; row++) {
71             for (int col = 0; col < MATRIX_COLS; col++) {
72                 if (matrix[row] & 1<<col) continue;
73
74                 uint8_t code = get_keycode(layer, row, col);
75                 if (code == KB_NO) {
76                     continue;
77                 } else if (KB_LCTRL <= code && code <= KB_RGUI) {
78                     // modifier keys(0xE0-0xE7)
79                     keyboard_modifier_keys |= 1<<(code & 0x07);
80                 } else if (code >= MS_UP) {
81                     // mouse
82                     if (code == MS_UP)    mouse_y -= MOUSE_MOVE_UNIT + (mouse_repeat < 50 ? mouse_repeat/5 : 10);
83                     if (code == MS_DOWN)  mouse_y += MOUSE_MOVE_UNIT + (mouse_repeat < 50 ? mouse_repeat/5 : 10);
84                     if (code == MS_LEFT)  mouse_x -= MOUSE_MOVE_UNIT + (mouse_repeat < 50 ? mouse_repeat/5 : 10);
85                     if (code == MS_RIGHT) mouse_x += MOUSE_MOVE_UNIT + (mouse_repeat < 50 ? mouse_repeat/5 : 10);
86                     if (code == MS_BTN1)  mouse_btn |= 1<<0;
87                     if (code == MS_BTN2)  mouse_btn |= 1<<1;
88                     if (code == MS_BTN3)  mouse_btn |= 1<<2;
89                     if (code == MS_BTN4)  mouse_btn |= 1<<3;
90                     if (code == MS_BTN5)  mouse_btn |= 1<<4;
91                     if (code == MS_WH_UP)  mouse_wheel += 1;
92                     if (code == MS_WH_DOWN)  mouse_wheel -= 1;
93                     if (code == MS_WH_LEFT)  mouse_hwheel -= 1;
94                     if (code == MS_WH_RIGHT) mouse_hwheel += 1;
95                 } else {
96                     // normal keys
97                     if (key_index < 6)
98                         keyboard_keys[key_index] = code;
99                     key_index++;
100                 }
101             }
102         }
103
104         if (!has_ghost)  {
105             // when 4 left modifier keys down
106             if (keyboard_modifier_keys == (MOD_LCTRL | MOD_LSHIFT | MOD_LALT | MOD_LGUI)) {
107                 // cancel all keys
108                 keyboard_modifier_keys = 0;
109                 for (int i = 0; i < 6; i++) keyboard_keys[i] = KB_NO;
110                 usb_keyboard_send();
111
112                 print("jump to bootloader...\n");
113                 _delay_ms(100);
114                 jump_bootloader(); // not return
115             }
116
117             if (mouse_x || mouse_y || mouse_wheel || mouse_hwheel || mouse_btn != mouse_buttons) {
118                 mouse_buttons = mouse_btn;
119                 usb_mouse_move(mouse_x, mouse_y, mouse_wheel, mouse_hwheel);
120                 print_mouse(mouse_x, mouse_y, mouse_wheel, mouse_hwheel);
121                 key_sent = true;
122
123                 // acceleration
124                 _delay_ms(MOUSE_DELAY_MS >> (mouse_repeat < MOUSE_DELAY_ACC ? mouse_repeat : MOUSE_DELAY_ACC));
125                 mouse_repeat++;
126             } else {
127                 mouse_repeat = 0;
128             }
129
130
131             // send keys to host
132             if (modified) {
133                 if (key_index > 6) {
134                     //Rollover
135                 }
136                 usb_keyboard_send();
137                 if (keyboard_keys[0])
138                     key_sent = true;
139
140                 print_keys();
141                 // LED flash for debug
142                 LED_CONFIG;
143                 LED_OFF;
144             }
145         }
146 }
147
148 static void print_matrix(void) {
149     print("\nr/c 01234567\n");
150     for (int row = 0; row < MATRIX_ROWS; row++) {
151         phex(row); print(": ");
152         pbin_reverse(matrix[row]);
153         if (matrix_has_ghost_in_row(row)) {
154             print(" <ghost");
155         }
156         print("\n");
157     }
158 }
159
160 static void print_keys(void) {
161     print("\nkeys: ");
162     for (int i = 0; i < 6; i++) { phex(keyboard_keys[i]); print(" "); }
163     print("\n");
164     print("mods: "); phex(keyboard_modifier_keys); print("\n");
165 }
166
167 static void print_mouse(int8_t mouse_x, int8_t mouse_y, int8_t wheel_v, int8_t wheel_h) {
168     print("\nmouse_x y v h: ");
169     phex(mouse_x); print(" ");
170     phex(mouse_y); print(" ");
171     phex(wheel_v); print(" ");
172     phex(wheel_h); print("\n");
173     print("buttons: "); phex(mouse_buttons); print("\n");
174 }