]> git.donarmstrong.com Git - tmk_firmware.git/blob - keyboard.c
refactor keyboard.h, host.h
[tmk_firmware.git] / keyboard.c
1 #include "keyboard.h"
2 #include "host.h"
3 #include "layer.h"
4 #include "matrix_skel.h"
5 #include "led.h"
6 #include "usb_keycodes.h"
7 #include "timer.h"
8 #include "print.h"
9 #include "debug.h"
10 #include "command.h"
11 #ifdef MOUSEKEY_ENABLE
12 #include "mousekey.h"
13 #endif
14
15
16 static uint8_t last_led = 0;
17
18
19 void keyboard_init(void)
20 {
21     timer_init();
22     matrix_init();
23 #ifdef PS2_MOUSE_ENABLE
24     ps2_mouse_init();
25 #endif
26 }
27
28 void keyboard_proc(void)
29 {
30     uint8_t fn_bits = 0;
31
32     matrix_scan();
33
34     if (matrix_is_modified()) {
35         if (debug_matrix) matrix_print();
36 #ifdef DEBUG_LED
37         // LED flash for debug
38         DEBUG_LED_CONFIG;
39         DEBUG_LED_ON;
40 #endif
41     }
42
43     if (matrix_has_ghost()) {
44         // should send error?
45         debug("matrix has ghost!!\n");
46         return;
47     }
48
49     host_swap_keyboard_report();
50     host_clear_keyboard_report();
51     for (int row = 0; row < matrix_rows(); row++) {
52         for (int col = 0; col < matrix_cols(); col++) {
53             if (!matrix_is_on(row, col)) continue;
54
55             uint8_t code = layer_get_keycode(row, col);
56             if (code == KB_NO) {
57                 // do nothing
58             } else if (IS_MOD(code)) {
59                 host_add_mod_bit(MOD_BIT(code));
60             } else if (IS_FN(code)) {
61                 fn_bits |= FN_BIT(code);
62             }
63 #ifdef USB_EXTRA_ENABLE
64 /* TODO: use new API
65             // audio control & system control
66             else if (code == KB_MUTE) {
67                 usb_extra_audio_send(AUDIO_MUTE);
68                 usb_extra_audio_send(0);
69                 _delay_ms(500);
70             } else if (code == KB_VOLU) {
71                 usb_extra_audio_send(AUDIO_VOL_UP);
72                 usb_extra_audio_send(0);
73                 _delay_ms(200);
74             } else if (code == KB_VOLD) {
75                 usb_extra_audio_send(AUDIO_VOL_DOWN);
76                 usb_extra_audio_send(0);
77                 _delay_ms(200);
78             } else if (code == KB_PWR) {
79                 if (suspend && remote_wakeup) {
80                     usb_remote_wakeup();
81                 } else {
82                     usb_extra_system_send(SYSTEM_POWER_DOWN);
83                 }
84                 _delay_ms(1000);
85             }
86 */
87 #endif
88             else if (IS_KEY(code)) {
89                 host_add_key(code);
90             }
91 #ifdef MOUSEKEY_ENABLE
92             else if (IS_MOUSEKEY(code)) {
93                 mousekey_decode(code);
94             }
95 #endif
96             else {
97                 debug("ignore keycode: "); debug_hex(code); debug("\n");
98             }
99         }
100     }
101
102     layer_switching(fn_bits);
103
104     if (command_proc()) {
105         // not send report
106         return;
107     }
108
109     if (matrix_is_modified()) {
110         host_send_keyboard_report();
111 #ifdef DEBUG_LED
112         // LED flash for debug
113         DEBUG_LED_CONFIG;
114         DEBUG_LED_OFF;
115 #endif
116     }
117
118 #ifdef MOUSEKEY_ENABLE
119     mousekey_send();
120 #endif
121
122 #ifdef PS2_MOUSE_ENABLE
123     // TODO: should comform new API
124     if (ps2_mouse_read() == 0)
125         ps2_mouse_usb_send();
126 #endif
127
128     if (last_led != host_keyboard_led()) {
129         led_set(host_keyboard_led());
130         last_led = host_keyboard_led();
131     }
132 }