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 // Windows and Linux set 0 while OS X sets 6(24ms) by SET_IDLE request.
19 uint8_t usb_keyboard_idle_config=125;
21 // count until idle timeout
22 uint8_t usb_keyboard_idle_count=0;
24 // 1=num lock, 2=caps lock, 4=scroll lock, 8=compose, 16=kana
25 volatile uint8_t usb_keyboard_leds=0;
28 static inline int8_t send_report(report_keyboard_t *report, uint8_t endpoint, uint8_t keys_start, uint8_t keys_end);
31 int8_t usb_keyboard_send_report(report_keyboard_t *report)
35 #ifdef USB_NKRO_ENABLE
37 result = send_report(report, KBD2_ENDPOINT, 0, KBD2_REPORT_KEYS);
41 if (usb_keyboard_protocol)
42 result = send_report(report, KBD_ENDPOINT, 0, KBD_REPORT_KEYS);
44 result = send_report(report, KBD_ENDPOINT, 0, 6);
47 if (result) return result;
48 usb_keyboard_idle_count = 0;
49 usb_keyboard_print_report(report);
53 void usb_keyboard_print_report(report_keyboard_t *report)
55 if (!debug_keyboard) return;
57 for (int i = 0; i < REPORT_KEYS; i++) { phex(report->keys[i]); print(" "); }
58 print(" mods: "); phex(report->mods); print("\n");
62 static inline int8_t send_report(report_keyboard_t *report, uint8_t endpoint, uint8_t keys_start, uint8_t keys_end)
64 uint8_t intr_state, timeout;
66 if (!usb_configured()) return -1;
70 timeout = UDFNUML + 50;
72 // are we ready to transmit?
73 if (UEINTX & (1<<RWAL)) break;
75 // has the USB gone offline?
76 if (!usb_configured()) return -1;
77 // have we waited too long?
78 if (UDFNUML == timeout) return -1;
79 // get ready to try checking again
84 UEDATX = report->mods;
85 #ifdef USB_NKRO_ENABLE
91 for (uint8_t i = keys_start; i < keys_end; i++) {
92 UEDATX = report->keys[i];