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