]> git.friedersdorff.com Git - max/tmk_keyboard.git/blob - usb_keyboard.c
revise Fn key processing.
[max/tmk_keyboard.git] / usb_keyboard.c
1 #include <avr/interrupt.h>
2 #include <avr/pgmspace.h>
3 #include "usb_keycodes.h"
4 #include "usb_keyboard.h"
5 #include "print.h"
6 #include "debug.h"
7
8
9 // keyboard report.
10 static usb_keyboard_report_t _report0 = { {0,0,0,0,0,0}, 0 };
11 static usb_keyboard_report_t _report1 = { {0,0,0,0,0,0}, 0 };
12 usb_keyboard_report_t *usb_keyboard_report = &_report0;
13 usb_keyboard_report_t *usb_keyboard_report_back = &_report1;
14
15 // protocol setting from the host.  We use exactly the same report
16 // either way, so this variable only stores the setting since we
17 // are required to be able to report which setting is in use.
18 uint8_t usb_keyboard_protocol=1;
19
20 // the idle configuration, how often we send the report to the
21 // host (ms * 4) even when it hasn't changed
22 uint8_t usb_keyboard_idle_config=125;
23
24 // count until idle timeout
25 uint8_t usb_keyboard_idle_count=0;
26
27 // 1=num lock, 2=caps lock, 4=scroll lock, 8=compose, 16=kana
28 volatile uint8_t usb_keyboard_leds=0;
29
30
31 int8_t usb_keyboard_send(void)
32 {
33     return usb_keyboard_send_report(usb_keyboard_report);
34 }
35
36
37 int8_t usb_keyboard_send_report(usb_keyboard_report_t *report)
38 {
39         uint8_t i, intr_state, timeout;
40
41         if (!usb_configured()) return -1;
42         intr_state = SREG;
43         cli();
44         UENUM = KEYBOARD_ENDPOINT;
45         timeout = UDFNUML + 50;
46         while (1) {
47                 // are we ready to transmit?
48                 if (UEINTX & (1<<RWAL)) break;
49                 SREG = intr_state;
50                 // has the USB gone offline?
51                 if (!usb_configured()) return -1;
52                 // have we waited too long?
53                 if (UDFNUML == timeout) return -1;
54                 // get ready to try checking again
55                 intr_state = SREG;
56                 cli();
57                 UENUM = KEYBOARD_ENDPOINT;
58         }
59         UEDATX = report->mods;
60         UEDATX = 0;
61         for (i=0; i<6; i++) {
62                 UEDATX = report->keys[i];
63         }
64         UEINTX = 0x3A;
65         usb_keyboard_idle_count = 0;
66         SREG = intr_state;
67
68         report->is_sent =true;
69
70         usb_keyboard_print_report(report);
71         return 0;
72 }
73
74 void usb_keyboard_swap_report(void) {
75     usb_keyboard_report_t *tmp = usb_keyboard_report_back;
76     usb_keyboard_report_back = usb_keyboard_report;
77     usb_keyboard_report = tmp;
78 }
79
80 void usb_keyboard_clear_report(void) {
81     usb_keyboard_clear_keys();
82     usb_keyboard_clear_mods();
83     usb_keyboard_report->is_sent = false;
84 }
85
86 void usb_keyboard_clear_keys(void) {
87     for (int i = 0; i < 6; i++) usb_keyboard_report->keys[i] = 0;
88 }
89
90 void usb_keyboard_clear_mods(void)
91 {
92     usb_keyboard_report->mods = 0;
93 }
94
95 void usb_keyboard_add_code(uint8_t code)
96 {
97     if (IS_MOD(code)) {
98         usb_keyboard_add_mod(code);
99     } else {
100         usb_keyboard_add_key(code);
101     }
102 }
103
104 void usb_keyboard_add_key(uint8_t code)
105 {
106     for (int i = 0; i < 6; i++) {
107         if (!usb_keyboard_report->keys[i]) {
108             usb_keyboard_report->keys[i] = code;
109             return;
110         }
111     }
112 }
113
114 void usb_keyboard_set_keys(uint8_t keys[6])
115 {
116     for (int i = 0; i < 6; i++)
117         usb_keyboard_report->keys[i] = keys[i];
118 }
119
120 void usb_keyboard_set_mods(uint8_t mods)
121 {
122     usb_keyboard_report->mods = mods;
123 }
124
125 void usb_keyboard_add_mod(uint8_t code)
126 {
127     usb_keyboard_report->mods |= MOD_BIT(code);
128 }
129
130 void usb_keyboard_del_code(uint8_t code)
131 {
132     if (IS_MOD(code)) {
133         usb_keyboard_del_mod(code);
134     } else {
135         usb_keyboard_del_key(code);
136     }
137 }
138
139 void usb_keyboard_del_key(uint8_t code)
140 {
141     for (int i = 0; i < 6; i++) {
142         if (usb_keyboard_report->keys[i] == code) {
143             usb_keyboard_report->keys[i] = KB_NO;
144             return;
145         }
146     }
147 }
148
149 void usb_keyboard_del_mod(uint8_t code)
150 {
151     usb_keyboard_report->mods &= ~MOD_BIT(code);
152 }
153
154 bool usb_keyboard_is_sent(void)
155 {
156     return usb_keyboard_report->is_sent;
157 }
158
159 bool usb_keyboard_has_key(void)
160 {
161     uint8_t keys = 0;    
162     for (int i = 0; i < 6; i++) keys |= usb_keyboard_report->keys[i];
163     return keys ? true : false;
164 }
165
166 bool usb_keyboard_has_mod(void)
167 {
168     return usb_keyboard_report->mods ? true : false;
169 }
170
171 void usb_keyboard_print_report(usb_keyboard_report_t *report)
172 {
173     if (!debug_keyboard) return;
174     print("keys: ");
175     for (int i = 0; i < 6; i++) { phex(report->keys[i]); print(" "); }
176     print(" mods: "); phex(report->mods); print("\n");
177 }