Kiibohd Controller
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.

output_com.c 6.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. /* Copyright (C) 2011-2014 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 <cli.h>
  26. #include <print.h>
  27. #include <scan_loop.h>
  28. // USB Includes
  29. #if defined(_at90usb162_) || defined(_atmega32u4_) || defined(_at90usb646_) || defined(_at90usb1286_)
  30. #include "avr/usb_keyboard_serial.h"
  31. #elif defined(_mk20dx128_) || defined(_mk20dx256_)
  32. #include "arm/usb_keyboard.h"
  33. #include "arm/usb_dev.h"
  34. #endif
  35. // Local Includes
  36. #include "output_com.h"
  37. // ----- Function Declarations -----
  38. void cliFunc_holdKey ( char* args );
  39. void cliFunc_readLEDs ( char* args );
  40. void cliFunc_releaseKey( char* args );
  41. void cliFunc_sendKey ( char* args );
  42. void cliFunc_setLEDs ( char* args );
  43. void cliFunc_setMod ( char* args );
  44. // ----- Variables -----
  45. // Output Module command dictionary
  46. char* outputCLIDictName = "USB Module Commands";
  47. CLIDictItem outputCLIDict[] = {
  48. { "holdKey", "Hold a space separated list of USB codes. Ignores already pressed keys.", cliFunc_holdKey },
  49. { "readLEDs", "Read LED byte. See setLEDs.", cliFunc_readLEDs },
  50. { "releaseKey", "Release a space separated list of USB codes. Ignores unpressed keys.", cliFunc_releaseKey },
  51. { "sendKey", "Send a space separated list of USB codes. Press/Release.", cliFunc_sendKey },
  52. { "setLEDs", "Set LED byte: 1 NumLck, 2 CapsLck, 4 ScrlLck, 16 Kana, etc.", cliFunc_setLEDs },
  53. { "setMod", "Set the modfier byte: 1 LCtrl, 2 LShft, 4 LAlt, 8 LGUI, 16 RCtrl, 32 RShft, 64 RAlt, 128 RGUI", cliFunc_setMod },
  54. { 0, 0, 0 } // Null entry for dictionary end
  55. };
  56. // which modifier keys are currently pressed
  57. // 1=left ctrl, 2=left shift, 4=left alt, 8=left gui
  58. // 16=right ctrl, 32=right shift, 64=right alt, 128=right gui
  59. uint8_t USBKeys_Modifiers = 0;
  60. // which keys are currently pressed, up to 6 keys may be down at once
  61. uint8_t USBKeys_Array[USB_MAX_KEY_SEND] = {0,0,0,0,0,0};
  62. // The number of keys sent to the usb in the array
  63. uint8_t USBKeys_Sent;
  64. // 1=num lock, 2=caps lock, 4=scroll lock, 8=compose, 16=kana
  65. volatile uint8_t USBKeys_LEDs = 0;
  66. // protocol setting from the host. We use exactly the same report
  67. // either way, so this variable only stores the setting since we
  68. // are required to be able to report which setting is in use.
  69. uint8_t USBKeys_Protocol = 1;
  70. // the idle configuration, how often we send the report to the
  71. // host (ms * 4) even when it hasn't changed
  72. uint8_t USBKeys_Idle_Config = 125;
  73. // count until idle timeout
  74. uint8_t USBKeys_Idle_Count = 0;
  75. // ----- Functions -----
  76. // USB Module Setup
  77. inline void output_setup()
  78. {
  79. // Initialize the USB, and then wait for the host to set configuration.
  80. // If the Teensy is powered without a PC connected to the USB port,
  81. // this will wait forever.
  82. usb_init();
  83. while ( !usb_configured() ) /* wait */ ;
  84. // Register USB Output dictionary
  85. registerDictionary_cli( outputCLIDict, outputCLIDictName );
  86. // Wait an extra second for the PC's operating system to load drivers
  87. // and do whatever it does to actually be ready for input
  88. //_delay_ms(1000); // TODO (is this actually necessary?)
  89. }
  90. // USB Data Send
  91. inline void output_send(void)
  92. {
  93. // TODO undo potentially old keys
  94. for ( uint8_t c = USBKeys_Sent; c < USBKeys_MaxSize; c++ )
  95. USBKeys_Array[c] = 0;
  96. // Send keypresses
  97. usb_keyboard_send();
  98. // Clear modifiers and keys
  99. USBKeys_Modifiers = 0;
  100. USBKeys_Sent = 0;
  101. // Signal Scan Module we are finishedA
  102. scan_finishedWithUSBBuffer( USBKeys_Sent <= USBKeys_MaxSize ? USBKeys_Sent : USBKeys_MaxSize );
  103. }
  104. // Sets the device into firmware reload mode
  105. inline void output_firmwareReload()
  106. {
  107. #if defined(_at90usb162_) || defined(_atmega32u4_) || defined(_at90usb646_) || defined(_at90usb1286_)
  108. usb_debug_reload();
  109. #elif defined(_mk20dx128_) || defined(_mk20dx256_)
  110. usb_device_reload();
  111. #endif
  112. }
  113. // USB Input buffer available
  114. inline unsigned int output_availablechar()
  115. {
  116. return usb_serial_available();
  117. }
  118. // USB Get Character from input buffer
  119. inline int output_getchar()
  120. {
  121. #if defined(_at90usb162_) || defined(_atmega32u4_) || defined(_at90usb646_) || defined(_at90usb1286_)
  122. // XXX Make sure to check output_availablechar() first! Information is lost with the cast (error codes)
  123. return (int)usb_serial_getchar();
  124. #elif defined(_mk20dx128_) || defined(_mk20dx256_)
  125. return usb_serial_getchar();
  126. #endif
  127. }
  128. // USB Send Character to output buffer
  129. inline int output_putchar( char c )
  130. {
  131. return usb_serial_putchar( c );
  132. }
  133. // USB Send String to output buffer, null terminated
  134. inline int output_putstr( char* str )
  135. {
  136. #if defined(_at90usb162_) || defined(_atmega32u4_) || defined(_at90usb646_) || defined(_at90usb1286_) // AVR
  137. uint16_t count = 0;
  138. #elif defined(_mk20dx128_) || defined(_mk20dx256_) // ARM
  139. uint32_t count = 0;
  140. #endif
  141. // Count characters until NULL character, then send the amount counted
  142. while ( str[count] != '\0' )
  143. count++;
  144. return usb_serial_write( str, count );
  145. }
  146. // Soft Chip Reset
  147. inline void output_softReset()
  148. {
  149. #if defined(_at90usb162_) || defined(_atmega32u4_) || defined(_at90usb646_) || defined(_at90usb1286_)
  150. usb_debug_software_reset();
  151. #elif defined(_mk20dx128_) || defined(_mk20dx256_)
  152. SOFTWARE_RESET();
  153. #endif
  154. }
  155. // ----- CLI Command Functions -----
  156. void cliFunc_holdKey( char* args )
  157. {
  158. // TODO
  159. }
  160. void cliFunc_readLEDs( char* args )
  161. {
  162. // TODO
  163. }
  164. void cliFunc_releaseKey( char* args )
  165. {
  166. // TODO
  167. }
  168. void cliFunc_sendKey( char* args )
  169. {
  170. // TODO Argument handling
  171. USBKeys_Array[0] = 4; // KEY_A
  172. USBKeys_Sent = 1;
  173. }
  174. void cliFunc_setLEDs( char* args )
  175. {
  176. // TODO
  177. }
  178. void cliFunc_setMod( char* args )
  179. {
  180. // TODO
  181. }