]> git.friedersdorff.com Git - max/tmk_keyboard.git/blob - usb_keyboard.c
44365bb857b1441abea8df57ee9bf046d3536530
[max/tmk_keyboard.git] / usb_keyboard.c
1 #include <avr/interrupt.h>
2 #include <avr/pgmspace.h>
3 #include "usb_keyboard.h"
4 #include "print.h"
5
6
7 static bool is_sent = false;
8
9 // which modifier keys are currently pressed
10 // 1=left ctrl,    2=left shift,   4=left alt,    8=left gui
11 // 16=right ctrl, 32=right shift, 64=right alt, 128=right gui
12 uint8_t keyboard_modifier_keys=0;
13
14 // which keys are currently pressed, up to 6 keys may be down at once
15 uint8_t keyboard_keys[6]={0,0,0,0,0,0};
16
17 // protocol setting from the host.  We use exactly the same report
18 // either way, so this variable only stores the setting since we
19 // are required to be able to report which setting is in use.
20 uint8_t keyboard_protocol=1;
21
22 // the idle configuration, how often we send the report to the
23 // host (ms * 4) even when it hasn't changed
24 uint8_t keyboard_idle_config=125;
25
26 // count until idle timeout
27 uint8_t keyboard_idle_count=0;
28
29 // 1=num lock, 2=caps lock, 4=scroll lock, 8=compose, 16=kana
30 volatile uint8_t keyboard_leds=0;
31
32
33 // perform a single keystroke
34 int8_t usb_keyboard_press(uint8_t key, uint8_t modifier)
35 {
36         int8_t r;
37
38         keyboard_modifier_keys = modifier;
39         keyboard_keys[0] = key;
40         r = usb_keyboard_send();
41         if (r) return r;
42         keyboard_modifier_keys = 0;
43         keyboard_keys[0] = 0;
44         return usb_keyboard_send();
45 }
46
47 // send the contents of keyboard_keys and keyboard_modifier_keys
48 int8_t usb_keyboard_send(void)
49 {
50         uint8_t i, intr_state, timeout;
51
52         if (!usb_configured()) return -1;
53         intr_state = SREG;
54         cli();
55         UENUM = KEYBOARD_ENDPOINT;
56         timeout = UDFNUML + 50;
57         while (1) {
58                 // are we ready to transmit?
59                 if (UEINTX & (1<<RWAL)) break;
60                 SREG = intr_state;
61                 // has the USB gone offline?
62                 if (!usb_configured()) return -1;
63                 // have we waited too long?
64                 if (UDFNUML == timeout) return -1;
65                 // get ready to try checking again
66                 intr_state = SREG;
67                 cli();
68                 UENUM = KEYBOARD_ENDPOINT;
69         }
70         UEDATX = keyboard_modifier_keys;
71         UEDATX = 0;
72         for (i=0; i<6; i++) {
73                 UEDATX = keyboard_keys[i];
74         }
75         UEINTX = 0x3A;
76         keyboard_idle_count = 0;
77         SREG = intr_state;
78         is_sent = true;
79         return 0;
80 }
81
82 void usb_keyboard_init(void) {
83     usb_keyboard_clear();
84     is_sent = false;
85 }
86
87 void usb_keyboard_clear(void) {
88     usb_keyboard_clear_key();
89     usb_keyboard_clear_mod();
90 }
91
92 void usb_keyboard_clear_key(void) {
93     for (int i = 0; i < 6; i++) keyboard_keys[i] = 0;
94 }
95
96 void usb_keyboard_clear_mod(void) {
97     keyboard_modifier_keys = 0;
98 }
99
100 bool usb_keyboard_is_sent(void) {
101     return is_sent;
102 }
103
104 bool usb_keyboard_has_key(void) {
105     uint8_t keys = 0;    
106     for (int i = 0; i < 6; i++) keys |= keyboard_keys[i];
107     return keys ? true : false;
108 }
109
110 bool usb_keyboard_has_mod(void) {
111     return keyboard_modifier_keys ? true : false;
112 }
113
114 void usb_keyboard_print(void) {
115     print("\nkeys: ");
116     for (int i = 0; i < 6; i++) { phex(keyboard_keys[i]); print(" "); }
117     print("\n");
118     print("mods: "); phex(keyboard_modifier_keys); print("\n");
119 }