1 #include <avr/interrupt.h>
2 #include <avr/pgmspace.h>
3 #include "usb_keyboard.h"
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;
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};
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;
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;
23 // count until idle timeout
24 uint8_t keyboard_idle_count=0;
26 // 1=num lock, 2=caps lock, 4=scroll lock, 8=compose, 16=kana
27 volatile uint8_t keyboard_leds=0;
30 // perform a single keystroke
31 int8_t usb_keyboard_press(uint8_t key, uint8_t modifier)
35 keyboard_modifier_keys = modifier;
36 keyboard_keys[0] = key;
37 r = usb_keyboard_send();
39 keyboard_modifier_keys = 0;
41 return usb_keyboard_send();
44 // send the contents of keyboard_keys and keyboard_modifier_keys
45 int8_t usb_keyboard_send(void)
47 uint8_t i, intr_state, timeout;
49 if (!usb_configured()) return -1;
52 UENUM = KEYBOARD_ENDPOINT;
53 timeout = UDFNUML + 50;
55 // are we ready to transmit?
56 if (UEINTX & (1<<RWAL)) break;
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
65 UENUM = KEYBOARD_ENDPOINT;
67 UEDATX = keyboard_modifier_keys;
70 UEDATX = keyboard_keys[i];
73 keyboard_idle_count = 0;