]> git.gir.st - tmk_keyboard.git/blob - pjrc/host.c
added USB_EXTRA feature to HHKB/V-USB
[tmk_keyboard.git] / pjrc / host.c
1 #include <stdint.h>
2 #include <avr/interrupt.h>
3 #include "usb_keycodes.h"
4 #include "usb_keyboard.h"
5 #if defined(MOUSEKEY_ENABLE) || defined(PS2_MOUSE_ENABLE)
6 #include "usb_mouse.h"
7 #endif
8 #ifdef USB_EXTRA_ENABLE
9 #include "usb_extra.h"
10 #endif
11 #include "debug.h"
12 #include "host.h"
13 #include "util.h"
14
15
16 #ifdef USB_NKRO_ENABLE
17 bool keyboard_nkro = false;
18 #endif
19
20 static report_keyboard_t report0;
21 static report_keyboard_t report1;
22 report_keyboard_t *keyboard_report = &report0;
23 report_keyboard_t *keyboard_report_prev = &report1;
24
25 static inline void add_key_byte(uint8_t code);
26 static inline void add_key_bit(uint8_t code);
27
28
29 uint8_t host_keyboard_leds(void)
30 {
31 return usb_keyboard_leds;
32 }
33
34 /* keyboard report operations */
35 void host_add_key(uint8_t key)
36 {
37 #ifdef USB_NKRO_ENABLE
38 if (keyboard_nkro) {
39 add_key_bit(key);
40 return;
41 }
42 #endif
43 add_key_byte(key);
44 }
45
46 void host_add_mod_bit(uint8_t mod)
47 {
48 keyboard_report->mods |= mod;
49 }
50
51 void host_set_mods(uint8_t mods)
52 {
53 keyboard_report->mods = mods;
54 }
55
56 void host_add_code(uint8_t code)
57 {
58 if (IS_MOD(code)) {
59 host_add_mod_bit(MOD_BIT(code));
60 } else {
61 host_add_key(code);
62 }
63 }
64
65 void host_swap_keyboard_report(void)
66 {
67 uint8_t sreg = SREG;
68 cli();
69 report_keyboard_t *tmp = keyboard_report_prev;
70 keyboard_report_prev = keyboard_report;
71 keyboard_report = tmp;
72 SREG = sreg;
73 }
74
75 void host_clear_keyboard_report(void)
76 {
77 keyboard_report->mods = 0;
78 for (int8_t i = 0; i < REPORT_KEYS; i++) {
79 keyboard_report->keys[i] = 0;
80 }
81 }
82
83 uint8_t host_has_anykey(void)
84 {
85 uint8_t cnt = 0;
86 for (int i = 0; i < REPORT_KEYS; i++) {
87 if (keyboard_report->keys[i])
88 cnt++;
89 }
90 return cnt;
91 }
92
93 uint8_t host_get_first_key(void)
94 {
95 #ifdef USB_NKRO_ENABLE
96 if (keyboard_nkro) {
97 uint8_t i = 0;
98 for (; i < REPORT_KEYS && !keyboard_report->keys[i]; i++)
99 ;
100 return i<<3 | biton(keyboard_report->keys[i]);
101 }
102 #endif
103 return keyboard_report->keys[0];
104 }
105
106
107 void host_send_keyboard_report(void)
108 {
109 usb_keyboard_send_report(keyboard_report);
110 }
111
112 #if defined(MOUSEKEY_ENABLE) || defined(PS2_MOUSE_ENABLE)
113 void host_mouse_send(report_mouse_t *report)
114 {
115 usb_mouse_send(report->x, report->y, report->v, report->h, report->buttons);
116 }
117 #endif
118
119 #ifdef USB_EXTRA_ENABLE
120 void host_system_send(uint8_t data)
121 {
122 usb_extra_system_send(data);
123 }
124
125 void host_audio_send(uint8_t data)
126 {
127 usb_extra_audio_send(data);
128 }
129 #endif
130
131
132 static inline void add_key_byte(uint8_t code)
133 {
134 // TODO: fix ugly code
135 int8_t i = 0;
136 int8_t empty = -1;
137 for (; i < REPORT_KEYS; i++) {
138 if (keyboard_report_prev->keys[i] == code) {
139 keyboard_report->keys[i] = code;
140 break;
141 }
142 if (empty == -1 &&
143 keyboard_report_prev->keys[i] == 0 &&
144 keyboard_report->keys[i] == 0) {
145 empty = i;
146 }
147 }
148 if (i == REPORT_KEYS) {
149 if (empty != -1) {
150 keyboard_report->keys[empty] = code;
151 }
152 }
153 }
154
155 static inline void add_key_bit(uint8_t code)
156 {
157 if ((code>>3) < REPORT_KEYS) {
158 keyboard_report->keys[code>>3] |= 1<<(code&7);
159 } else {
160 debug("add_key_bit: can't add: "); phex(code); debug("\n");
161 }
162 }
Imprint / Impressum