Kiibohd Controller
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
此仓库已存档。您可以查看文件和克隆,但不能推送或创建工单/合并请求。

usb_keyboard.c 1.2KB

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