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
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:
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
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
24 #include <avr/interrupt.h>
25 #include <avr/pgmspace.h>
27 #include "usb_keyboard.h"
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 usb_keyboard_protocol=1;
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 usb_keyboard_idle_config=125;
44 // count until idle timeout
45 uint8_t usb_keyboard_idle_count=0;
47 // 1=num lock, 2=caps lock, 4=scroll lock, 8=compose, 16=kana
48 volatile uint8_t usb_keyboard_leds=0;
51 static inline int8_t send_report(report_keyboard_t *report, uint8_t endpoint, uint8_t keys_start, uint8_t keys_end);
54 int8_t usb_keyboard_send_report(report_keyboard_t *report)
60 result = send_report(report, KBD2_ENDPOINT, 0, KBD2_SIZE);
64 if (usb_keyboard_protocol)
65 result = send_report(report, KBD_ENDPOINT, 0, KBD_SIZE);
67 result = send_report(report, KBD_ENDPOINT, 0, 6);
70 if (result) return result;
71 usb_keyboard_idle_count = 0;
72 usb_keyboard_print_report(report);
76 void usb_keyboard_print_report(report_keyboard_t *report)
78 if (!debug_keyboard) return;
80 for (int i = 0; i < REPORT_KEYS; i++) { phex(report->keys[i]); print(" "); }
81 print(" mods: "); phex(report->mods); print("\n");
85 static inline int8_t send_report(report_keyboard_t *report, uint8_t endpoint, uint8_t keys_start, uint8_t keys_end)
87 uint8_t intr_state, timeout;
89 if (!usb_configured()) return -1;
93 timeout = UDFNUML + 50;
95 // are we ready to transmit?
96 if (UEINTX & (1<<RWAL)) break;
98 // has the USB gone offline?
99 if (!usb_configured()) return -1;
100 // have we waited too long?
101 if (UDFNUML == timeout) return -1;
102 // get ready to try checking again
107 for (uint8_t i = keys_start; i < keys_end; i++) {
108 UEDATX = report->raw[i];