Fix typo of variable name
[tmk_keyboard.git] / protocol / pjrc / usb_keyboard.c
1 /* USB Keyboard Plus Debug Channel Example for Teensy USB Development Board
2 * http://www.pjrc.com/teensy/usb_keyboard.html
3 * Copyright (c) 2009 PJRC.COM, LLC
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a copy
6 * of this software and associated documentation files (the "Software"), to deal
7 * in the Software without restriction, including without limitation the rights
8 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 * copies of the Software, and to permit persons to whom the Software is
10 * furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21 * THE SOFTWARE.
22 */
23
24 #include <avr/interrupt.h>
25 #include <avr/pgmspace.h>
26 #include "keycode.h"
27 #include "usb_keyboard.h"
28 #include "print.h"
29 #include "debug.h"
30 #include "util.h"
31 #include "host.h"
32
33
34 // protocol setting from the host. We use exactly the same report
35 // either way, so this variable only stores the setting since we
36 // are required to be able to report which setting is in use.
37 uint8_t keyboard_protocol=1;
38
39 // the idle configuration, how often we send the report to the
40 // host (ms * 4) even when it hasn't changed
41 // Windows and Linux set 0 while OS X sets 6(24ms) by SET_IDLE request.
42 uint8_t keyboard_idle=125;
43
44 // count until idle timeout
45 uint8_t usb_keyboard_idle_count=0;
46
47 // 1=num lock, 2=caps lock, 4=scroll lock, 8=compose, 16=kana
48 volatile uint8_t usb_keyboard_leds=0;
49
50
51 static inline int8_t send_report(report_keyboard_t *report, uint8_t endpoint, uint8_t keys_start, uint8_t keys_end);
52
53
54 int8_t usb_keyboard_send_report(report_keyboard_t *report)
55 {
56 int8_t result = 0;
57
58 #ifdef NKRO_ENABLE
59 if (keyboard_nkro)
60 result = send_report(report, KBD2_ENDPOINT, 0, KBD2_SIZE);
61 else
62 #endif
63 {
64 result = send_report(report, KBD_ENDPOINT, 0, KBD_SIZE);
65 }
66
67 if (result) return result;
68 usb_keyboard_idle_count = 0;
69 usb_keyboard_print_report(report);
70 return 0;
71 }
72
73 void usb_keyboard_print_report(report_keyboard_t *report)
74 {
75 if (!debug_keyboard) return;
76 print("keys: ");
77 for (int i = 0; i < REPORT_KEYS; i++) { phex(report->keys[i]); print(" "); }
78 print(" mods: "); phex(report->mods); print("\n");
79 }
80
81
82 static inline int8_t send_report(report_keyboard_t *report, uint8_t endpoint, uint8_t keys_start, uint8_t keys_end)
83 {
84 uint8_t intr_state, timeout;
85
86 if (!usb_configured()) return -1;
87 intr_state = SREG;
88 cli();
89 UENUM = endpoint;
90 timeout = UDFNUML + 50;
91 while (1) {
92 // are we ready to transmit?
93 if (UEINTX & (1<<RWAL)) break;
94 SREG = intr_state;
95 // has the USB gone offline?
96 if (!usb_configured()) return -1;
97 // have we waited too long?
98 if (UDFNUML == timeout) return -1;
99 // get ready to try checking again
100 intr_state = SREG;
101 cli();
102 UENUM = endpoint;
103 }
104 for (uint8_t i = keys_start; i < keys_end; i++) {
105 UEDATX = report->raw[i];
106 }
107 UEINTX = 0x3A;
108 SREG = intr_state;
109 return 0;
110 }
Imprint / Impressum