Kiibohd Controller
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.

output_com.c 8.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  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 <led.h>
  27. #include <print.h>
  28. #include <scan_loop.h>
  29. // USB Includes
  30. #if defined(_at90usb162_) || defined(_atmega32u4_) || defined(_at90usb646_) || defined(_at90usb1286_)
  31. #include "avr/usb_keyboard_serial.h"
  32. #elif defined(_mk20dx128_) || defined(_mk20dx128vlf5_) || defined(_mk20dx256_)
  33. #include "arm/usb_dev.h"
  34. #include "arm/usb_keyboard.h"
  35. #include "arm/usb_serial.h"
  36. #endif
  37. // Local Includes
  38. #include "output_com.h"
  39. // ----- Function Declarations -----
  40. void cliFunc_kbdProtocol( char* args );
  41. void cliFunc_readLEDs ( char* args );
  42. void cliFunc_sendKeys ( char* args );
  43. void cliFunc_setKeys ( char* args );
  44. void cliFunc_setMod ( char* args );
  45. // ----- Variables -----
  46. // Output Module command dictionary
  47. const char outputCLIDictName[] = "USB Module Commands";
  48. const CLIDictItem outputCLIDict[] = {
  49. { "kbdProtocol", "Keyboard Protocol Mode: 0 - Boot, 1 - OS/NKRO Mode", cliFunc_kbdProtocol },
  50. { "readLEDs", "Read LED byte:" NL "\t\t1 NumLck, 2 CapsLck, 4 ScrlLck, 16 Kana, etc.", cliFunc_readLEDs },
  51. { "sendKeys", "Send the prepared list of USB codes and modifier byte.", cliFunc_sendKeys },
  52. { "setKeys", "Prepare a space separated list of USB codes (decimal). Waits until \033[35msendKeys\033[0m.", cliFunc_setKeys },
  53. { "setMod", "Set the modfier byte:" NL "\t\t1 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. uint8_t USBKeys_ModifiersCLI = 0; // Separate CLI send buffer
  61. // Currently pressed keys, max is defined by USB_MAX_KEY_SEND
  62. uint8_t USBKeys_Array [USB_MAX_KEY_SEND];
  63. uint8_t USBKeys_ArrayCLI[USB_MAX_KEY_SEND]; // Separate CLI send buffer
  64. // The number of keys sent to the usb in the array
  65. uint8_t USBKeys_Sent = 0;
  66. uint8_t USBKeys_SentCLI = 0;
  67. // 1=num lock, 2=caps lock, 4=scroll lock, 8=compose, 16=kana
  68. volatile uint8_t USBKeys_LEDs = 0;
  69. // Protocol setting from the host.
  70. // 0 - Boot Mode (Default, until set by the host)
  71. // 1 - NKRO Mode
  72. volatile uint8_t USBKeys_Protocol = 1;
  73. // the idle configuration, how often we send the report to the
  74. // host (ms * 4) even when it hasn't changed
  75. uint8_t USBKeys_Idle_Config = 125;
  76. // count until idle timeout
  77. uint8_t USBKeys_Idle_Count = 0;
  78. // ----- Capabilities -----
  79. // Adds a single USB Code to the USB Output buffer
  80. // Argument #1: USB Code
  81. void Output_usbCodeSend_capability( uint8_t state, uint8_t stateType, uint8_t *args )
  82. {
  83. // Display capability name
  84. if ( stateType == 0xFF && state == 0xFF )
  85. {
  86. print("Output_usbCodeSend(usbCode)");
  87. return;
  88. }
  89. // TODO Analog inputs
  90. // Only send keypresses if press or hold state
  91. if ( stateType == 0x00 && state == 0x03 ) // Release state
  92. return;
  93. // Get the keycode from arguments
  94. uint8_t key = args[0];
  95. // Set the modifier bit if this key is a modifier
  96. if ( (key & 0xE0) == 0xE0 ) // AND with 0xE0 (Left Ctrl, first modifier)
  97. {
  98. USBKeys_Modifiers |= 1 << (key ^ 0xE0); // Left shift 1 by key XOR 0xE0
  99. }
  100. // Normal USB Code
  101. else
  102. {
  103. // USB Key limit reached (important for Boot Mode)
  104. if ( USBKeys_Sent >= USBKeys_MaxSize )
  105. {
  106. warn_msg("USB Key limit reached");
  107. errorLED( 1 );
  108. return;
  109. }
  110. USBKeys_Array[USBKeys_Sent++] = key;
  111. }
  112. }
  113. // ----- Functions -----
  114. // USB Module Setup
  115. inline void Output_setup()
  116. {
  117. // Initialize the USB, and then wait for the host to set configuration.
  118. // If the Teensy is powered without a PC connected to the USB port,
  119. // this will wait forever.
  120. usb_init();
  121. while ( !usb_configured() ) /* wait */ ;
  122. // Register USB Output CLI dictionary
  123. CLI_registerDictionary( outputCLIDict, outputCLIDictName );
  124. // Wait an extra second for the PC's operating system to load drivers
  125. // and do whatever it does to actually be ready for input
  126. //_delay_ms(1000); // TODO (is this actually necessary?)
  127. }
  128. // USB Data Send
  129. inline void Output_send(void)
  130. {
  131. // TODO undo potentially old keys
  132. for ( uint8_t c = USBKeys_Sent; c < USBKeys_MaxSize; c++ )
  133. USBKeys_Array[c] = 0;
  134. // Send keypresses
  135. usb_keyboard_send();
  136. // Clear modifiers and keys
  137. USBKeys_Modifiers = 0;
  138. USBKeys_Sent = 0;
  139. // Signal Scan Module we are finished
  140. Scan_finishedWithOutput( USBKeys_Sent <= USBKeys_MaxSize ? USBKeys_Sent : USBKeys_MaxSize );
  141. }
  142. // Sets the device into firmware reload mode
  143. inline void Output_firmwareReload()
  144. {
  145. usb_device_reload();
  146. }
  147. // USB Input buffer available
  148. inline unsigned int Output_availablechar()
  149. {
  150. return usb_serial_available();
  151. }
  152. // USB Get Character from input buffer
  153. inline int Output_getchar()
  154. {
  155. // XXX Make sure to check output_availablechar() first! Information is lost with the cast (error codes) (AVR)
  156. return (int)usb_serial_getchar();
  157. }
  158. // USB Send Character to output buffer
  159. inline int Output_putchar( char c )
  160. {
  161. return usb_serial_putchar( c );
  162. }
  163. // USB Send String to output buffer, null terminated
  164. inline int Output_putstr( char* str )
  165. {
  166. #if defined(_at90usb162_) || defined(_atmega32u4_) || defined(_at90usb646_) || defined(_at90usb1286_) // AVR
  167. uint16_t count = 0;
  168. #elif defined(_mk20dx128_) || defined(_mk20dx128vlf5_) || defined(_mk20dx256_) // ARM
  169. uint32_t count = 0;
  170. #endif
  171. // Count characters until NULL character, then send the amount counted
  172. while ( str[count] != '\0' )
  173. count++;
  174. return usb_serial_write( str, count );
  175. }
  176. // Soft Chip Reset
  177. inline void Output_softReset()
  178. {
  179. usb_device_software_reset();
  180. }
  181. // ----- CLI Command Functions -----
  182. void cliFunc_kbdProtocol( char* args )
  183. {
  184. print( NL );
  185. info_msg("Keyboard Protocol: ");
  186. printInt8( USBKeys_Protocol );
  187. }
  188. void cliFunc_readLEDs( char* args )
  189. {
  190. print( NL );
  191. info_msg("LED State: ");
  192. printInt8( USBKeys_LEDs );
  193. }
  194. void cliFunc_sendKeys( char* args )
  195. {
  196. // Copy USBKeys_ArrayCLI to USBKeys_Array
  197. for ( uint8_t key = 0; key < USBKeys_SentCLI; ++key )
  198. {
  199. USBKeys_Array[key] = USBKeys_ArrayCLI[key];
  200. }
  201. USBKeys_Sent = USBKeys_SentCLI;
  202. // Set modifier byte
  203. USBKeys_Modifiers = USBKeys_ModifiersCLI;
  204. }
  205. void cliFunc_setKeys( char* args )
  206. {
  207. char* curArgs;
  208. char* arg1Ptr;
  209. char* arg2Ptr = args;
  210. // Parse up to USBKeys_MaxSize args (whichever is least)
  211. for ( USBKeys_SentCLI = 0; USBKeys_SentCLI < USBKeys_MaxSize; ++USBKeys_SentCLI )
  212. {
  213. curArgs = arg2Ptr;
  214. CLI_argumentIsolation( curArgs, &arg1Ptr, &arg2Ptr );
  215. // Stop processing args if no more are found
  216. if ( *arg1Ptr == '\0' )
  217. break;
  218. // Add the USB code to be sent
  219. USBKeys_ArrayCLI[USBKeys_SentCLI] = numToInt( arg1Ptr );
  220. }
  221. }
  222. void cliFunc_setMod( char* args )
  223. {
  224. // Parse number from argument
  225. // NOTE: Only first argument is used
  226. char* arg1Ptr;
  227. char* arg2Ptr;
  228. CLI_argumentIsolation( args, &arg1Ptr, &arg2Ptr );
  229. USBKeys_ModifiersCLI = numToInt( arg1Ptr );
  230. }