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 7.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  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(_mk20dx128vlf5_) || defined(_mk20dx256_)
  32. #include "arm/usb_dev.h"
  33. #include "arm/usb_keyboard.h"
  34. #include "arm/usb_serial.h"
  35. #endif
  36. // Local Includes
  37. #include "output_com.h"
  38. // ----- Function Declarations -----
  39. void cliFunc_kbdProtocol( char* args );
  40. void cliFunc_readLEDs ( char* args );
  41. void cliFunc_sendKeys ( char* args );
  42. void cliFunc_setKeys ( 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. { "kbdProtocol", "Keyboard Protocol Mode: 0 - Boot, 1 - OS/NKRO Mode", cliFunc_kbdProtocol },
  49. { "readLEDs", "Read LED byte:" NL "\t\t1 NumLck, 2 CapsLck, 4 ScrlLck, 16 Kana, etc.", cliFunc_readLEDs },
  50. { "sendKeys", "Send the prepared list of USB codes and modifier byte.", cliFunc_sendKeys },
  51. { "setKeys", "Prepare a space separated list of USB codes (decimal). Waits until \033[35msendKeys\033[0m.", cliFunc_setKeys },
  52. { "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 },
  53. { 0, 0, 0 } // Null entry for dictionary end
  54. };
  55. // Which modifier keys are currently pressed
  56. // 1=left ctrl, 2=left shift, 4=left alt, 8=left gui
  57. // 16=right ctrl, 32=right shift, 64=right alt, 128=right gui
  58. uint8_t USBKeys_Modifiers = 0;
  59. uint8_t USBKeys_ModifiersCLI = 0; // Separate CLI send buffer
  60. // Currently pressed keys, max is defined by USB_MAX_KEY_SEND
  61. uint8_t USBKeys_Array [USB_MAX_KEY_SEND];
  62. uint8_t USBKeys_ArrayCLI[USB_MAX_KEY_SEND]; // Separate CLI send buffer
  63. // The number of keys sent to the usb in the array
  64. uint8_t USBKeys_Sent = 0;
  65. uint8_t USBKeys_SentCLI = 0;
  66. // 1=num lock, 2=caps lock, 4=scroll lock, 8=compose, 16=kana
  67. volatile uint8_t USBKeys_LEDs = 0;
  68. // Protocol setting from the host.
  69. // 0 - Boot Mode (Default, until set by the host)
  70. // 1 - NKRO Mode
  71. volatile uint8_t USBKeys_Protocol = 1;
  72. // the idle configuration, how often we send the report to the
  73. // host (ms * 4) even when it hasn't changed
  74. uint8_t USBKeys_Idle_Config = 125;
  75. // count until idle timeout
  76. uint8_t USBKeys_Idle_Count = 0;
  77. // ----- Functions -----
  78. // USB Module Setup
  79. inline void Output_setup()
  80. {
  81. // Initialize the USB, and then wait for the host to set configuration.
  82. // If the Teensy is powered without a PC connected to the USB port,
  83. // this will wait forever.
  84. usb_init();
  85. while ( !usb_configured() ) /* wait */ ;
  86. // Register USB Output CLI dictionary
  87. CLI_registerDictionary( outputCLIDict, outputCLIDictName );
  88. // Wait an extra second for the PC's operating system to load drivers
  89. // and do whatever it does to actually be ready for input
  90. //_delay_ms(1000); // TODO (is this actually necessary?)
  91. }
  92. // USB Data Send
  93. inline void Output_send(void)
  94. {
  95. // TODO undo potentially old keys
  96. for ( uint8_t c = USBKeys_Sent; c < USBKeys_MaxSize; c++ )
  97. USBKeys_Array[c] = 0;
  98. // Send keypresses
  99. usb_keyboard_send();
  100. // Clear modifiers and keys
  101. USBKeys_Modifiers = 0;
  102. USBKeys_Sent = 0;
  103. // Signal Scan Module we are finishedA
  104. Scan_finishedWithUSBBuffer( USBKeys_Sent <= USBKeys_MaxSize ? USBKeys_Sent : USBKeys_MaxSize );
  105. }
  106. // Sets the device into firmware reload mode
  107. inline void Output_firmwareReload()
  108. {
  109. #if defined(_at90usb162_) || defined(_atmega32u4_) || defined(_at90usb646_) || defined(_at90usb1286_)
  110. usb_debug_reload();
  111. #elif defined(_mk20dx128_) || defined(_mk20dx128vlf5_) || defined(_mk20dx256_)
  112. usb_device_reload();
  113. #endif
  114. }
  115. // USB Input buffer available
  116. inline unsigned int Output_availablechar()
  117. {
  118. return usb_serial_available();
  119. }
  120. // USB Get Character from input buffer
  121. inline int Output_getchar()
  122. {
  123. #if defined(_at90usb162_) || defined(_atmega32u4_) || defined(_at90usb646_) || defined(_at90usb1286_)
  124. // XXX Make sure to check output_availablechar() first! Information is lost with the cast (error codes)
  125. return (int)usb_serial_getchar();
  126. #elif defined(_mk20dx128_) || defined(_mk20dx128vlf5_) || defined(_mk20dx256_)
  127. return usb_serial_getchar();
  128. #endif
  129. }
  130. // USB Send Character to output buffer
  131. inline int Output_putchar( char c )
  132. {
  133. return usb_serial_putchar( c );
  134. }
  135. // USB Send String to output buffer, null terminated
  136. inline int Output_putstr( char* str )
  137. {
  138. #if defined(_at90usb162_) || defined(_atmega32u4_) || defined(_at90usb646_) || defined(_at90usb1286_) // AVR
  139. uint16_t count = 0;
  140. #elif defined(_mk20dx128_) || defined(_mk20dx128vlf5_) || defined(_mk20dx256_) // ARM
  141. uint32_t count = 0;
  142. #endif
  143. // Count characters until NULL character, then send the amount counted
  144. while ( str[count] != '\0' )
  145. count++;
  146. return usb_serial_write( str, count );
  147. }
  148. // Soft Chip Reset
  149. inline void Output_softReset()
  150. {
  151. #if defined(_at90usb162_) || defined(_atmega32u4_) || defined(_at90usb646_) || defined(_at90usb1286_)
  152. usb_debug_software_reset();
  153. #elif defined(_mk20dx128_) || defined(_mk20dx128vlf5_) || defined(_mk20dx256_)
  154. SOFTWARE_RESET();
  155. #endif
  156. }
  157. // ----- CLI Command Functions -----
  158. void cliFunc_kbdProtocol( char* args )
  159. {
  160. print( NL );
  161. info_msg("Keyboard Protocol: ");
  162. printInt8( USBKeys_Protocol );
  163. }
  164. void cliFunc_readLEDs( char* args )
  165. {
  166. print( NL );
  167. info_msg("LED State (This doesn't work yet...): ");
  168. printInt8( USBKeys_LEDs );
  169. }
  170. void cliFunc_sendKeys( char* args )
  171. {
  172. // Copy USBKeys_ArrayCLI to USBKeys_Array
  173. for ( uint8_t key = 0; key < USBKeys_SentCLI; ++key )
  174. {
  175. USBKeys_Array[key] = USBKeys_ArrayCLI[key];
  176. }
  177. USBKeys_Sent = USBKeys_SentCLI;
  178. // Set modifier byte
  179. USBKeys_Modifiers = USBKeys_ModifiersCLI;
  180. }
  181. void cliFunc_setKeys( char* args )
  182. {
  183. char* curArgs;
  184. char* arg1Ptr;
  185. char* arg2Ptr = args;
  186. // Parse up to USBKeys_MaxSize args (whichever is least)
  187. for ( USBKeys_SentCLI = 0; USBKeys_SentCLI < USBKeys_MaxSize; ++USBKeys_SentCLI )
  188. {
  189. curArgs = arg2Ptr;
  190. CLI_argumentIsolation( curArgs, &arg1Ptr, &arg2Ptr );
  191. // Stop processing args if no more are found
  192. if ( *arg1Ptr == '\0' )
  193. break;
  194. // Add the USB code to be sent
  195. USBKeys_ArrayCLI[USBKeys_SentCLI] = decToInt( arg1Ptr );
  196. }
  197. }
  198. void cliFunc_setMod( char* args )
  199. {
  200. // Parse number from argument
  201. // NOTE: Only first argument is used
  202. char* arg1Ptr;
  203. char* arg2Ptr;
  204. CLI_argumentIsolation( args, &arg1Ptr, &arg2Ptr );
  205. USBKeys_ModifiersCLI = decToInt( arg1Ptr );
  206. }