1 #include <avr/interrupt.h>
2 #include <avr/pgmspace.h>
3 #include "usb_keycodes.h"
4 #include "usb_keyboard.h"
11 // protocol setting from the host. We use exactly the same report
12 // either way, so this variable only stores the setting since we
13 // are required to be able to report which setting is in use.
14 uint8_t usb_keyboard_protocol=1;
16 // the idle configuration, how often we send the report to the
17 // host (ms * 4) even when it hasn't changed
18 uint8_t usb_keyboard_idle_config=125;
20 // count until idle timeout
21 uint8_t usb_keyboard_idle_count=0;
23 // 1=num lock, 2=caps lock, 4=scroll lock, 8=compose, 16=kana
24 volatile uint8_t usb_keyboard_leds=0;
27 static inline int8_t send_report(report_keyboard_t *report, uint8_t endpoint, uint8_t keys_start, uint8_t keys_end);
30 int8_t usb_keyboard_send_report(report_keyboard_t *report)
34 #ifdef USB_NKRO_ENABLE
36 result = send_report(report, KBD2_ENDPOINT, 0, KBD2_REPORT_KEYS);
40 if (usb_keyboard_protocol)
41 result = send_report(report, KBD_ENDPOINT, 0, KBD_REPORT_KEYS);
43 result = send_report(report, KBD_ENDPOINT, 0, 6);
46 if (result) return result;
47 usb_keyboard_idle_count = 0;
48 usb_keyboard_print_report(report);
52 void usb_keyboard_print_report(report_keyboard_t *report)
54 if (!debug_keyboard) return;
56 for (int i = 0; i < REPORT_KEYS; i++) { phex(report->keys[i]); print(" "); }
57 print(" mods: "); phex(report->mods); print("\n");
61 static inline int8_t send_report(report_keyboard_t *report, uint8_t endpoint, uint8_t keys_start, uint8_t keys_end)
63 uint8_t intr_state, timeout;
65 if (!usb_configured()) return -1;
69 timeout = UDFNUML + 50;
71 // are we ready to transmit?
72 if (UEINTX & (1<<RWAL)) break;
74 // has the USB gone offline?
75 if (!usb_configured()) return -1;
76 // have we waited too long?
77 if (UDFNUML == timeout) return -1;
78 // get ready to try checking again
83 UEDATX = report->mods;
84 #ifdef USB_NKRO_ENABLE
90 for (uint8_t i = keys_start; i < keys_end; i++) {
91 UEDATX = report->keys[i];