]> git.friedersdorff.com Git - max/tmk_keyboard.git/blob - keyboard.c
Change layer pram and keymap of HHKB. Fix vusb/host.c.
[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 #ifdef USB_EXTRA_ENABLE
35     uint16_t consumer_code = 0;
36 #endif
37
38     matrix_scan();
39
40     if (matrix_is_modified()) {
41         if (debug_matrix) matrix_print();
42 #ifdef DEBUG_LED
43         // LED flash for debug
44         DEBUG_LED_CONFIG;
45         DEBUG_LED_ON;
46 #endif
47     }
48
49     if (matrix_has_ghost()) {
50         // should send error?
51         debug("matrix has ghost!!\n");
52         return;
53     }
54
55     host_swap_keyboard_report();
56     host_clear_keyboard_report();
57     for (int row = 0; row < matrix_rows(); row++) {
58         for (int col = 0; col < matrix_cols(); col++) {
59             if (!matrix_is_on(row, col)) continue;
60
61             uint8_t code = layer_get_keycode(row, col);
62             if (code == KB_NO) {
63                 // do nothing
64             } else if (IS_MOD(code)) {
65                 host_add_mod_bit(MOD_BIT(code));
66             } else if (IS_FN(code)) {
67                 fn_bits |= FN_BIT(code);
68             }
69 #ifdef USB_EXTRA_ENABLE
70             // System Control
71             else if (code == KB_SYSTEM_POWER) {
72 #ifdef HOST_PJRC
73                 if (suspend && remote_wakeup) {
74                     usb_remote_wakeup();
75                 } else {
76                     host_system_send(SYSTEM_POWER_DOWN);
77                 }
78 #else
79                 host_system_send(SYSTEM_POWER_DOWN);
80 #endif
81                 host_system_send(0);
82                 _delay_ms(500);
83             } else if (code == KB_SYSTEM_SLEEP) {
84                 host_system_send(SYSTEM_SLEEP);
85                 host_system_send(0);
86                 _delay_ms(500);
87             } else if (code == KB_SYSTEM_WAKE) {
88                 host_system_send(SYSTEM_WAKE_UP);
89                 host_system_send(0);
90                 _delay_ms(500);
91             }
92             // Consumer Page
93             else if (code == KB_AUDIO_MUTE) {
94                 consumer_code = AUDIO_MUTE;
95             } else if (code == KB_AUDIO_VOL_UP) {
96                 consumer_code = AUDIO_VOL_UP;
97             } else if (code == KB_AUDIO_VOL_DOWN) {
98                 consumer_code = AUDIO_VOL_DOWN;
99             }
100             else if (code == KB_MEDIA_NEXT_TRACK) {
101                 consumer_code = TRANSPORT_NEXT_TRACK;
102             } else if (code == KB_MEDIA_PREV_TRACK) {
103                 consumer_code = TRANSPORT_PREV_TRACK;
104             } else if (code == KB_MEDIA_STOP) {
105                 consumer_code = TRANSPORT_STOP;
106             } else if (code == KB_MEDIA_PLAY_PAUSE) {
107                 consumer_code = TRANSPORT_PLAY_PAUSE;
108             } else if (code == KB_MEDIA_SELECT) {
109                 consumer_code = AL_CC_CONFIG;
110             }
111             else if (code == KB_MAIL) {
112                 consumer_code = AL_EMAIL;
113             } else if (code == KB_CALCULATOR) {
114                 consumer_code = AL_CALCULATOR;
115             } else if (code == KB_MY_COMPUTER) {
116                 consumer_code = AL_LOCAL_BROWSER;
117             }
118             else if (code == KB_WWW_SEARCH) {
119                 consumer_code = AC_SEARCH;
120             } else if (code == KB_WWW_HOME) {
121                 consumer_code = AC_HOME;
122             } else if (code == KB_WWW_BACK) {
123                 consumer_code = AC_BACK;
124             } else if (code == KB_WWW_FORWARD) {
125                 consumer_code = AC_FORWARD;
126             } else if (code == KB_WWW_STOP) {
127                 consumer_code = AC_STOP;
128             } else if (code == KB_WWW_REFRESH) {
129                 consumer_code = AC_REFRESH;
130             } else if (code == KB_WWW_FAVORITES) {
131                 consumer_code = AC_BOOKMARKS;
132             }
133 #endif
134             else if (IS_KEY(code)) {
135                 host_add_key(code);
136             }
137 #ifdef MOUSEKEY_ENABLE
138             else if (IS_MOUSEKEY(code)) {
139                 mousekey_decode(code);
140             }
141 #endif
142             else {
143                 debug("ignore keycode: "); debug_hex(code); debug("\n");
144             }
145         }
146     }
147
148     layer_switching(fn_bits);
149
150     if (command_proc()) {
151         return;
152     }
153
154     // TODO: should send only when changed from last report
155     if (matrix_is_modified()) {
156         host_send_keyboard_report();
157 #ifdef USB_EXTRA_ENABLE
158         host_consumer_send(consumer_code);
159 #endif
160 #ifdef DEBUG_LED
161         // LED flash for debug
162         DEBUG_LED_CONFIG;
163         DEBUG_LED_OFF;
164 #endif
165     }
166
167 #ifdef MOUSEKEY_ENABLE
168     mousekey_send();
169 #endif
170
171 #ifdef PS2_MOUSE_ENABLE
172     // TODO: should comform new API
173     if (ps2_mouse_read() == 0)
174         ps2_mouse_usb_send();
175 #endif
176
177     if (last_leds != host_keyboard_leds()) {
178         keyboard_set_leds(host_keyboard_leds());
179         last_leds = host_keyboard_leds();
180     }
181 }
182
183 void keyboard_set_leds(uint8_t leds)
184 {
185     led_set(leds);
186 }