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