]> git.friedersdorff.com Git - max/tmk_keyboard.git/blob - key_process.c
10cac032b145da8a81676fa3d3bd96a8103d6c59
[max/tmk_keyboard.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_skel.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 // TODO: refactoring
29 void proc_matrix(void) {
30     static int mouse_repeat = 0;
31
32     bool modified = false;
33     //bool has_ghost = false;
34     int key_index = 0;
35     uint8_t mouse_btn = 0;
36     int8_t mouse_x = 0;
37     int8_t mouse_y = 0;
38     int8_t mouse_wheel = 0;
39     int8_t mouse_hwheel = 0;
40     int fn_bits = 0;
41
42     matrix_scan();
43     modified = matrix_is_modified();
44
45     if (modified) {
46         matrix_print();
47
48         // LED flash for debug
49         LED_CONFIG;
50         LED_ON;
51     }
52
53     if (matrix_has_ghost()) {
54         // should send error?
55         print("matrix has ghost!!\n");
56         return;
57     }
58
59     usb_keyboard_clear();
60     for (int row = 0; row < matrix_rows(); row++) {
61         for (int col = 0; col < matrix_cols(); col++) {
62             if (matrix_get_row(row) & 1<<col) continue;
63
64             uint8_t code = keymap_get_keycode(row, col);
65             if (code == KB_NO) {
66                 code = keymap_get_keycodel(0, row, col);
67                 if (FN_0 <= code && code <= FN_7) {
68                     fn_bits |= 1<<(code - FN_0);
69                 }
70             } else if (KB_LCTRL <= code && code <= KB_RGUI) {
71                 // modifier keys(0xE0-0xE7)
72                 keyboard_modifier_keys |= 1<<(code & 0x07);
73             } else if (code >= MS_UP) {
74                 // mouse
75                 if (code == MS_UP)    mouse_y -= MOUSE_MOVE_UNIT + (mouse_repeat < 50 ? mouse_repeat/5 : 10);
76                 if (code == MS_DOWN)  mouse_y += MOUSE_MOVE_UNIT + (mouse_repeat < 50 ? mouse_repeat/5 : 10);
77                 if (code == MS_LEFT)  mouse_x -= MOUSE_MOVE_UNIT + (mouse_repeat < 50 ? mouse_repeat/5 : 10);
78                 if (code == MS_RIGHT) mouse_x += MOUSE_MOVE_UNIT + (mouse_repeat < 50 ? mouse_repeat/5 : 10);
79                 if (code == MS_BTN1)  mouse_btn |= 1<<0;
80                 if (code == MS_BTN2)  mouse_btn |= 1<<1;
81                 if (code == MS_BTN3)  mouse_btn |= 1<<2;
82                 if (code == MS_BTN4)  mouse_btn |= 1<<3;
83                 if (code == MS_BTN5)  mouse_btn |= 1<<4;
84                 if (code == MS_WH_UP)  mouse_wheel += 1;
85                 if (code == MS_WH_DOWN)  mouse_wheel -= 1;
86                 if (code == MS_WH_LEFT)  mouse_hwheel -= 1;
87                 if (code == MS_WH_RIGHT) mouse_hwheel += 1;
88             } else if (FN_0 <= code && code <= FN_7) {
89                 fn_bits |= 1<<(code - FN_0);
90             } else {
91                 // normal keys
92                 if (key_index < 6)
93                     keyboard_keys[key_index] = code;
94                 key_index++;
95             }
96         }
97     }
98     keymap_fn_proc(fn_bits);
99
100     // when 4 left modifier keys down
101     if (keyboard_modifier_keys == (MOD_LCTRL | MOD_LSHIFT | MOD_LALT | MOD_LGUI)) {
102         // cancel all keys
103         keyboard_modifier_keys = 0;
104         for (int i = 0; i < 6; i++) keyboard_keys[i] = KB_NO;
105         usb_keyboard_send();
106
107         print("jump to bootloader...\n");
108         _delay_ms(100);
109         jump_bootloader(); // not return
110     }
111
112
113     if (mouse_x || mouse_y || mouse_wheel || mouse_hwheel || mouse_btn != mouse_buttons) {
114         mouse_buttons = mouse_btn;
115         usb_mouse_move(mouse_x, mouse_y, mouse_wheel, mouse_hwheel);
116         usb_mouse_print(mouse_x, mouse_y, mouse_wheel, mouse_hwheel);
117
118         // acceleration
119         _delay_ms(MOUSE_DELAY_MS >> (mouse_repeat < MOUSE_DELAY_ACC ? mouse_repeat : MOUSE_DELAY_ACC));
120         mouse_repeat++;
121     } else {
122         mouse_repeat = 0;
123     }
124
125
126     // send keys to host
127     if (modified) {
128         if (key_index > 6) {
129             //Rollover
130         }
131         usb_keyboard_send();
132
133         usb_keyboard_print();
134         // LED flash for debug
135         LED_CONFIG;
136         LED_OFF;
137     }
138 }