]> git.donarmstrong.com Git - kiibohd-controller.git/blob - Output/pjrcUSB/arm/usb_keyboard.c
Moving USB to Output in preparation for additional Output types.
[kiibohd-controller.git] / Output / pjrcUSB / arm / usb_keyboard.c
1 #include "usb_dev.h"
2 #include "usb_keyboard.h"
3 #include <Lib/USBLib.h>
4 #include <string.h> // for memcpy()
5
6
7 // Maximum number of transmit packets to queue so we don't starve other endpoints for memory
8 #define TX_PACKET_LIMIT 4
9
10 static uint8_t transmit_previous_timeout=0;
11
12 // When the PC isn't listening, how long do we wait before discarding data?
13 #define TX_TIMEOUT_MSEC 50
14
15 #if F_CPU == 96000000
16   #define TX_TIMEOUT (TX_TIMEOUT_MSEC * 596)
17 #elif F_CPU == 48000000
18   #define TX_TIMEOUT (TX_TIMEOUT_MSEC * 428)
19 #elif F_CPU == 24000000
20   #define TX_TIMEOUT (TX_TIMEOUT_MSEC * 262)
21 #endif
22
23
24 // send the contents of keyboard_keys and keyboard_modifier_keys
25 uint8_t usb_keyboard_send(void)
26 {
27         uint32_t wait_count=0;
28         usb_packet_t *tx_packet;
29
30         while (1) {
31                 if (!usb_configuration) {
32                         return -1;
33                 }
34                 if (usb_tx_packet_count(KEYBOARD_ENDPOINT) < TX_PACKET_LIMIT) {
35                         tx_packet = usb_malloc();
36                         if (tx_packet) break;
37                 }
38                 if (++wait_count > TX_TIMEOUT || transmit_previous_timeout) {
39                         transmit_previous_timeout = 1;
40                         return -1;
41                 }
42                 yield();
43         }
44         *(tx_packet->buf) = USBKeys_Modifiers;
45         *(tx_packet->buf + 1) = 0;
46         memcpy(tx_packet->buf + 2, USBKeys_Array, USB_MAX_KEY_SEND);
47         tx_packet->len = 8;
48         usb_tx(KEYBOARD_ENDPOINT, tx_packet);
49
50         return 0;
51 }
52