]> git.friedersdorff.com Git - max/tmk_keyboard.git/blob - pjrc/usb_keyboard.c
fixed adb_usb to comply new API.
[max/tmk_keyboard.git] / pjrc / 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 #include "util.h"
8 #include "host.h"
9
10
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;
15
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;
19
20 // count until idle timeout
21 uint8_t usb_keyboard_idle_count=0;
22
23 // 1=num lock, 2=caps lock, 4=scroll lock, 8=compose, 16=kana
24 volatile uint8_t usb_keyboard_leds=0;
25
26
27 static inline int8_t send_report(report_keyboard_t *report, uint8_t endpoint, uint8_t keys_start, uint8_t keys_end);
28
29
30 int8_t usb_keyboard_send_report(report_keyboard_t *report)
31 {
32     int8_t result = 0;
33
34 #ifdef USB_NKRO_ENABLE
35     if (keyboard_nkro)
36         result = send_report(report, KBD2_ENDPOINT, 0, KBD2_REPORT_KEYS);
37     else
38 #endif
39     {
40         if (usb_keyboard_protocol)
41             result = send_report(report, KBD_ENDPOINT, 0, KBD_REPORT_KEYS);
42         else
43             result = send_report(report, KBD_ENDPOINT, 0, 6);
44     }
45
46     if (result) return result;
47     usb_keyboard_idle_count = 0;
48     usb_keyboard_print_report(report);
49     return 0;
50 }
51
52 void usb_keyboard_print_report(report_keyboard_t *report)
53 {
54     if (!debug_keyboard) return;
55     print("keys: ");
56     for (int i = 0; i < REPORT_KEYS; i++) { phex(report->keys[i]); print(" "); }
57     print(" mods: "); phex(report->mods); print("\n");
58 }
59
60
61 static inline int8_t send_report(report_keyboard_t *report, uint8_t endpoint, uint8_t keys_start, uint8_t keys_end)
62 {
63     uint8_t intr_state, timeout;
64
65     if (!usb_configured()) return -1;
66     intr_state = SREG;
67     cli();
68     UENUM = endpoint;
69     timeout = UDFNUML + 50;
70     while (1) {
71             // are we ready to transmit?
72             if (UEINTX & (1<<RWAL)) break;
73             SREG = intr_state;
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
79             intr_state = SREG;
80             cli();
81             UENUM = endpoint;
82     }
83     UEDATX = report->mods;
84 #ifdef USB_NKRO_ENABLE
85     if (!keyboard_nkro)
86         UEDATX = 0;
87 #else
88     UEDATX = 0;
89 #endif
90     for (uint8_t i = keys_start; i < keys_end; i++) {
91             UEDATX = report->keys[i];
92     }
93     UEINTX = 0x3A;
94     SREG = intr_state;
95     return 0;
96 }