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

output_com.c 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /* Copyright (C) 2011-2013 by Jacob Alexander
  2. *
  3. * Permission is hereby granted, free of charge, to any person obtaining a copy
  4. * of this software and associated documentation files (the "Software"), to deal
  5. * in the Software without restriction, including without limitation the rights
  6. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. * copies of the Software, and to permit persons to whom the Software is
  8. * furnished to do so, subject to the following conditions:
  9. *
  10. * The above copyright notice and this permission notice shall be included in
  11. * all copies or substantial portions of the Software.
  12. *
  13. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  18. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  19. * THE SOFTWARE.
  20. */
  21. // ----- Includes -----
  22. // Compiler Includes
  23. #include <Lib/OutputLib.h>
  24. // Project Includes
  25. #include <scan_loop.h>
  26. // USB Includes
  27. #if defined(_at90usb162_) || defined(_atmega32u4_) || defined(_at90usb646_) || defined(_at90usb1286_)
  28. #include "avr/usb_keyboard_debug.h"
  29. #elif defined(_mk20dx128_) || defined(_mk20dx256_)
  30. #include "arm/usb_keyboard.h"
  31. #include "arm/usb_dev.h"
  32. #endif
  33. // Local Includes
  34. #include "output_com.h"
  35. // ----- Variables -----
  36. // which modifier keys are currently pressed
  37. // 1=left ctrl, 2=left shift, 4=left alt, 8=left gui
  38. // 16=right ctrl, 32=right shift, 64=right alt, 128=right gui
  39. uint8_t USBKeys_Modifiers = 0;
  40. // which keys are currently pressed, up to 6 keys may be down at once
  41. uint8_t USBKeys_Array[USB_MAX_KEY_SEND] = {0,0,0,0,0,0};
  42. // The number of keys sent to the usb in the array
  43. uint8_t USBKeys_Sent;
  44. // 1=num lock, 2=caps lock, 4=scroll lock, 8=compose, 16=kana
  45. volatile uint8_t USBKeys_LEDs = 0;
  46. // protocol setting from the host. We use exactly the same report
  47. // either way, so this variable only stores the setting since we
  48. // are required to be able to report which setting is in use.
  49. uint8_t USBKeys_Protocol = 1;
  50. // the idle configuration, how often we send the report to the
  51. // host (ms * 4) even when it hasn't changed
  52. uint8_t USBKeys_Idle_Config = 125;
  53. // count until idle timeout
  54. uint8_t USBKeys_Idle_Count = 0;
  55. // ----- Functions -----
  56. // USB Module Setup
  57. inline void output_setup()
  58. {
  59. // Initialize the USB, and then wait for the host to set configuration.
  60. // If the Teensy is powered without a PC connected to the USB port,
  61. // this will wait forever.
  62. usb_init();
  63. while ( !usb_configured() ) /* wait */ ;
  64. // Wait an extra second for the PC's operating system to load drivers
  65. // and do whatever it does to actually be ready for input
  66. //_delay_ms(1000); // TODO
  67. }
  68. // USB Data Send
  69. inline void usb_send(void)
  70. {
  71. // TODO undo potentially old keys
  72. for ( uint8_t c = USBKeys_Sent; c < USBKeys_MaxSize; c++ )
  73. USBKeys_Array[c] = 0;
  74. // Send keypresses
  75. usb_keyboard_send();
  76. // Clear modifiers and keys
  77. USBKeys_Modifiers = 0;
  78. USBKeys_Sent = 0;
  79. // Signal Scan Module we are finishedA
  80. scan_finishedWithUSBBuffer( USBKeys_Sent <= USBKeys_MaxSize ? USBKeys_Sent : USBKeys_MaxSize );
  81. }
  82. // Sets the device into firmware reload mode
  83. inline void output_firmwareReload()
  84. {
  85. #if defined(_at90usb162_) || defined(_atmega32u4_) || defined(_at90usb646_) || defined(_at90usb1286_)
  86. #elif defined(_mk20dx128_) || defined(_mk20dx256_)
  87. usb_device_reload();
  88. #endif
  89. }