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