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"
11 #include "matrix_skel.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
28 void proc_matrix(void) {
29 static int mouse_repeat = 0;
31 bool modified = false;
32 //bool has_ghost = false;
34 uint8_t mouse_btn = 0;
37 int8_t mouse_wheel = 0;
38 int8_t mouse_hwheel = 0;
42 modified = matrix_is_modified();
47 // LED flash for debug
52 if (matrix_has_ghost()) {
54 print("matrix has ghost!!\n");
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;
63 uint8_t code = keymap_get_keycode(row, col);
65 code = keymap_get_keycodel(0, row, col);
66 if (FN_0 <= code && code <= FN_7) {
67 fn_bits |= 1<<(code - FN_0);
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) {
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);
92 keyboard_keys[key_index] = code;
97 keymap_fn_proc(fn_bits);
99 // when 4 left modifier keys down
100 if (keyboard_modifier_keys == (MOD_LCTRL | MOD_LSHIFT | MOD_LALT | MOD_LGUI)) {
102 keyboard_modifier_keys = 0;
103 for (int i = 0; i < 6; i++) keyboard_keys[i] = KB_NO;
106 print("jump to bootloader...\n");
108 jump_bootloader(); // not return
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);
118 _delay_ms(MOUSE_DELAY_MS >> (mouse_repeat < MOUSE_DELAY_ACC ? mouse_repeat : MOUSE_DELAY_ACC));
132 usb_keyboard_print();
133 // LED flash for debug