1 // TODO: clean unused headers
4 #include <avr/pgmspace.h>
5 #include <avr/interrupt.h>
6 #include <util/delay.h>
8 #include "usb_keyboard.h"
13 #include "jump_bootloader.h"
15 #include "key_process.h"
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))
23 #define MOUSE_MOVE_UNIT 10
24 #define MOUSE_DELAY_MS 200
25 #define MOUSE_DELAY_ACC 5
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);
33 void proc_matrix(void) {
34 static int mouse_repeat = 0;
36 bool modified = false;
37 bool has_ghost = false;
40 uint8_t mouse_btn = 0;
43 int8_t mouse_wheel = 0;
44 int8_t mouse_hwheel = 0;
47 modified = matrix_is_modified();
48 has_ghost = matrix_has_ghost();
51 // print matrix state for debug
55 // LED flash for debug
60 keyboard_modifier_keys = 0;
61 for (int i = 0; i < 3; i++) keyboard_keys[i] = KB_NO;
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;
74 uint8_t code = get_keycode(layer, row, col);
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) {
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;
98 keyboard_keys[key_index] = code;
105 // when 4 left modifier keys down
106 if (keyboard_modifier_keys == (MOD_LCTRL | MOD_LSHIFT | MOD_LALT | MOD_LGUI)) {
108 keyboard_modifier_keys = 0;
109 for (int i = 0; i < 6; i++) keyboard_keys[i] = KB_NO;
112 print("jump to bootloader...\n");
114 jump_bootloader(); // not return
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);
124 _delay_ms(MOUSE_DELAY_MS >> (mouse_repeat < MOUSE_DELAY_ACC ? mouse_repeat : MOUSE_DELAY_ACC));
137 if (keyboard_keys[0])
141 // LED flash for debug
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)) {
160 static void print_keys(void) {
162 for (int i = 0; i < 6; i++) { phex(keyboard_keys[i]); print(" "); }
164 print("mods: "); phex(keyboard_modifier_keys); print("\n");
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");