]> git.gir.st - tmk_keyboard.git/blob - usb_keyboard.c
some fixes. LED flush for debug.
[tmk_keyboard.git] / usb_keyboard.c
1 #include <avr/interrupt.h>
2 #include <avr/pgmspace.h>
3 #include "usb_keyboard.h"
4
5
6 // which modifier keys are currently pressed
7 // 1=left ctrl, 2=left shift, 4=left alt, 8=left gui
8 // 16=right ctrl, 32=right shift, 64=right alt, 128=right gui
9 uint8_t keyboard_modifier_keys=0;
10
11 // which keys are currently pressed, up to 6 keys may be down at once
12 uint8_t keyboard_keys[6]={0,0,0,0,0,0};
13
14 // protocol setting from the host. We use exactly the same report
15 // either way, so this variable only stores the setting since we
16 // are required to be able to report which setting is in use.
17 uint8_t keyboard_protocol=1;
18
19 // the idle configuration, how often we send the report to the
20 // host (ms * 4) even when it hasn't changed
21 uint8_t keyboard_idle_config=125;
22
23 // count until idle timeout
24 uint8_t keyboard_idle_count=0;
25
26 // 1=num lock, 2=caps lock, 4=scroll lock, 8=compose, 16=kana
27 volatile uint8_t keyboard_leds=0;
28
29
30 // perform a single keystroke
31 int8_t usb_keyboard_press(uint8_t key, uint8_t modifier)
32 {
33 int8_t r;
34
35 keyboard_modifier_keys = modifier;
36 keyboard_keys[0] = key;
37 r = usb_keyboard_send();
38 if (r) return r;
39 keyboard_modifier_keys = 0;
40 keyboard_keys[0] = 0;
41 return usb_keyboard_send();
42 }
43
44 // send the contents of keyboard_keys and keyboard_modifier_keys
45 int8_t usb_keyboard_send(void)
46 {
47 uint8_t i, intr_state, timeout;
48
49 if (!usb_configured()) return -1;
50 intr_state = SREG;
51 cli();
52 UENUM = KEYBOARD_ENDPOINT;
53 timeout = UDFNUML + 50;
54 while (1) {
55 // are we ready to transmit?
56 if (UEINTX & (1<<RWAL)) break;
57 SREG = intr_state;
58 // has the USB gone offline?
59 if (!usb_configured()) return -1;
60 // have we waited too long?
61 if (UDFNUML == timeout) return -1;
62 // get ready to try checking again
63 intr_state = SREG;
64 cli();
65 UENUM = KEYBOARD_ENDPOINT;
66 }
67 UEDATX = keyboard_modifier_keys;
68 UEDATX = 0;
69 for (i=0; i<6; i++) {
70 UEDATX = keyboard_keys[i];
71 }
72 UEINTX = 0x3A;
73 keyboard_idle_count = 0;
74 SREG = intr_state;
75 return 0;
76 }
Imprint / Impressum