2 Copyright 2011,2012 Jun Wako <wakojun@gmail.com>
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>.
19 #include <avr/interrupt.h>
27 bool keyboard_nkro = false;
30 static host_driver_t *driver;
31 report_keyboard_t *keyboard_report = &(report_keyboard_t){};
34 static inline void add_key_byte(uint8_t code);
35 static inline void del_key_byte(uint8_t code);
36 static inline void add_key_bit(uint8_t code);
37 static inline void del_key_bit(uint8_t code);
40 void host_set_driver(host_driver_t *d)
45 host_driver_t *host_get_driver(void)
50 uint8_t host_keyboard_leds(void)
52 if (!driver) return 0;
53 return (*driver->keyboard_leds)();
56 /* keyboard report operations */
57 void host_add_key(uint8_t key)
68 void host_del_key(uint8_t key)
79 void host_clear_keys(void)
81 for (int8_t i = 0; i < REPORT_KEYS; i++) {
82 keyboard_report->keys[i] = 0;
86 void host_add_mod_bit(uint8_t mod)
88 keyboard_report->mods |= mod;
91 void host_del_mod_bit(uint8_t mod)
93 keyboard_report->mods &= ~mod;
96 void host_set_mods(uint8_t mods)
98 keyboard_report->mods = mods;
101 void host_clear_mods(void)
103 keyboard_report->mods = 0;
106 uint8_t host_has_anykey(void)
109 for (int i = 0; i < REPORT_KEYS; i++) {
110 if (keyboard_report->keys[i])
116 uint8_t host_get_first_key(void)
121 for (; i < REPORT_KEYS && !keyboard_report->keys[i]; i++)
123 return i<<3 | biton(keyboard_report->keys[i]);
126 return keyboard_report->keys[0];
129 void host_send_keyboard_report(void)
132 (*driver->send_keyboard)(keyboard_report);
134 if (debug_keyboard) {
136 for (int i = 0; i < REPORT_KEYS; i++) {
137 phex(keyboard_report->keys[i]); print(" ");
139 print(" mods: "); phex(keyboard_report->mods); print("\n");
145 void host_keyboard_send(report_keyboard_t *report)
148 (*driver->send_keyboard)(report);
151 void host_mouse_send(report_mouse_t *report)
154 (*driver->send_mouse)(report);
157 void host_system_send(uint16_t data)
159 static uint16_t last_data = 0;
160 if (data == last_data) return;
164 (*driver->send_system)(data);
167 void host_consumer_send(uint16_t data)
169 static uint16_t last_data = 0;
170 if (data == last_data) return;
174 (*driver->send_consumer)(data);
178 static inline void add_key_byte(uint8_t code)
182 for (; i < REPORT_KEYS; i++) {
183 if (keyboard_report->keys[i] == code) {
186 if (empty == -1 && keyboard_report->keys[i] == 0) {
190 if (i == REPORT_KEYS) {
192 keyboard_report->keys[empty] = code;
197 static inline void del_key_byte(uint8_t code)
200 for (; i < REPORT_KEYS; i++) {
201 if (keyboard_report->keys[i] == code) {
202 keyboard_report->keys[i] = 0;
207 static inline void add_key_bit(uint8_t code)
209 if ((code>>3) < REPORT_KEYS) {
210 keyboard_report->keys[code>>3] |= 1<<(code&7);
212 debug("add_key_bit: can't add: "); phex(code); debug("\n");
216 static inline void del_key_bit(uint8_t code)
218 if ((code>>3) < REPORT_KEYS) {
219 keyboard_report->keys[code>>3] &= ~(1<<(code&7));
221 debug("del_key_bit: can't del: "); phex(code); debug("\n");