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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. /* Copyright (C) 2014-2016 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. #elif defined(_mk20dx128_) || defined(_mk20dx128vlf5_) || defined(_mk20dx256_) || defined(_mk20dx256vlh7_)
  32. #include "arm/uart_serial.h"
  33. #endif
  34. // Local Includes
  35. #include "output_com.h"
  36. // ----- Function Declarations -----
  37. void cliFunc_kbdProtocol( char* args );
  38. void cliFunc_readLEDs ( char* args );
  39. void cliFunc_sendKeys ( char* args );
  40. void cliFunc_setKeys ( char* args );
  41. void cliFunc_setMod ( char* args );
  42. // ----- Variables -----
  43. // Output Module command dictionary
  44. CLIDict_Entry( kbdProtocol, "Keyboard Protocol Mode: 0 - Boot, 1 - OS/NKRO Mode" );
  45. CLIDict_Entry( readLEDs, "Read LED byte:" NL "\t\t1 NumLck, 2 CapsLck, 4 ScrlLck, 16 Kana, etc." );
  46. CLIDict_Entry( sendKeys, "Send the prepared list of USB codes and modifier byte." );
  47. CLIDict_Entry( setKeys, "Prepare a space separated list of USB codes (decimal). Waits until \033[35msendKeys\033[0m." );
  48. CLIDict_Entry( setMod, "Set the modfier byte:" NL "\t\t1 LCtrl, 2 LShft, 4 LAlt, 8 LGUI, 16 RCtrl, 32 RShft, 64 RAlt, 128 RGUI" );
  49. CLIDict_Def( outputCLIDict, "USB Module Commands" ) = {
  50. CLIDict_Item( kbdProtocol ),
  51. CLIDict_Item( readLEDs ),
  52. CLIDict_Item( sendKeys ),
  53. CLIDict_Item( setKeys ),
  54. CLIDict_Item( setMod ),
  55. { 0, 0, 0 } // Null entry for dictionary end
  56. };
  57. // Which modifier keys are currently pressed
  58. // 1=left ctrl, 2=left shift, 4=left alt, 8=left gui
  59. // 16=right ctrl, 32=right shift, 64=right alt, 128=right gui
  60. uint8_t USBKeys_Modifiers = 0;
  61. uint8_t USBKeys_ModifiersCLI = 0; // Separate CLI send buffer
  62. // Currently pressed keys, max is defined by USB_MAX_KEY_SEND
  63. uint8_t USBKeys_Keys [USB_NKRO_BITFIELD_SIZE_KEYS];
  64. uint8_t USBKeys_KeysCLI[USB_NKRO_BITFIELD_SIZE_KEYS]; // Separate CLI send buffer
  65. // System Control and Consumer Control 1KRO containers
  66. uint8_t USBKeys_SysCtrl;
  67. uint16_t USBKeys_ConsCtrl;
  68. // The number of keys sent to the usb in the array
  69. uint8_t USBKeys_Sent = 0;
  70. uint8_t USBKeys_SentCLI = 0;
  71. // 1=num lock, 2=caps lock, 4=scroll lock, 8=compose, 16=kana
  72. volatile uint8_t USBKeys_LEDs = 0;
  73. // Protocol setting from the host.
  74. // 0 - Boot Mode
  75. // 1 - NKRO Mode (Default, unless set by a BIOS or boot interface)
  76. volatile uint8_t USBKeys_Protocol = 0;
  77. // Indicate if USB should send update
  78. // OS only needs update if there has been a change in state
  79. USBKeyChangeState USBKeys_Changed = USBKeyChangeState_None;
  80. // the idle configuration, how often we send the report to the
  81. // host (ms * 4) even when it hasn't changed
  82. uint8_t USBKeys_Idle_Config = 125;
  83. // count until idle timeout
  84. uint8_t USBKeys_Idle_Count = 0;
  85. // Indicates whether the Output module is fully functional
  86. // 0 - Not fully functional, 1 - Fully functional
  87. // 0 is often used to show that a USB cable is not plugged in (but has power)
  88. volatile uint8_t Output_Available = 0;
  89. // ----- Capabilities -----
  90. // Ignored capabilities
  91. void Output_kbdProtocolBoot_capability( uint8_t state, uint8_t stateType, uint8_t *args ) {}
  92. void Output_kbdProtocolNKRO_capability( uint8_t state, uint8_t stateType, uint8_t *args ) {}
  93. void Output_toggleKbdProtocol_capability( uint8_t state, uint8_t stateType, uint8_t *args ) {}
  94. void Output_consCtrlSend_capability( uint8_t state, uint8_t stateType, uint8_t *args ) {}
  95. void Output_noneSend_capability( uint8_t state, uint8_t stateType, uint8_t *args ) {}
  96. void Output_sysCtrlSend_capability( uint8_t state, uint8_t stateType, uint8_t *args ) {}
  97. void Output_usbCodeSend_capability( uint8_t state, uint8_t stateType, uint8_t *args ) {}
  98. void Output_usbMouse_capability( uint8_t state, uint8_t stateType, uint8_t *args ) {}
  99. void Output_flashMode_capability( uint8_t state, uint8_t stateType, uint8_t *args )
  100. {
  101. // Display capability name
  102. if ( stateType == 0xFF && state == 0xFF )
  103. {
  104. print("Output_flashMode(usbCode)");
  105. return;
  106. }
  107. // Start flash mode
  108. Output_firmwareReload();
  109. }
  110. // ----- Functions -----
  111. // USB Module Setup
  112. inline void Output_setup()
  113. {
  114. // Setup UART
  115. uart_serial_setup();
  116. // Register USB Output CLI dictionary
  117. CLI_registerDictionary( outputCLIDict, outputCLIDictName );
  118. }
  119. // USB Data Send
  120. inline void Output_send(void)
  121. {
  122. // TODO
  123. }
  124. // Sets the device into firmware reload mode
  125. inline void Output_firmwareReload()
  126. {
  127. uart_device_reload();
  128. }
  129. // USB Input buffer available
  130. inline unsigned int Output_availablechar()
  131. {
  132. return uart_serial_available();
  133. }
  134. // USB Get Character from input buffer
  135. inline int Output_getchar()
  136. {
  137. // XXX Make sure to check output_availablechar() first! Information is lost with the cast (error codes) (AVR)
  138. return (int)uart_serial_getchar();
  139. }
  140. // USB Send Character to output buffer
  141. inline int Output_putchar( char c )
  142. {
  143. return uart_serial_putchar( c );
  144. }
  145. // USB Send String to output buffer, null terminated
  146. inline int Output_putstr( char* str )
  147. {
  148. #if defined(_at90usb162_) || defined(_atmega32u4_) || defined(_at90usb646_) || defined(_at90usb1286_) // AVR
  149. uint16_t count = 0;
  150. #elif defined(_mk20dx128_) || defined(_mk20dx128vlf5_) || defined(_mk20dx256_) || defined(_mk20dx256vlh7_) // ARM
  151. uint32_t count = 0;
  152. #endif
  153. // Count characters until NULL character, then send the amount counted
  154. while ( str[count] != '\0' )
  155. count++;
  156. return uart_serial_write( str, count );
  157. }
  158. // Soft Chip Reset
  159. inline void Output_softReset()
  160. {
  161. #if defined(_at90usb162_) || defined(_atmega32u4_) || defined(_at90usb646_) || defined(_at90usb1286_) // AVR
  162. #elif defined(_mk20dx128_) || defined(_mk20dx128vlf5_) || defined(_mk20dx256_) || defined(_mk20dx256vlh7_) // ARM
  163. SOFTWARE_RESET();
  164. #endif
  165. }
  166. // ----- CLI Command Functions -----
  167. void cliFunc_kbdProtocol( char* args )
  168. {
  169. print( NL );
  170. info_msg("Keyboard Protocol: ");
  171. printInt8( USBKeys_Protocol );
  172. }
  173. void cliFunc_readLEDs( char* args )
  174. {
  175. print( NL );
  176. info_msg("LED State: ");
  177. printInt8( USBKeys_LEDs );
  178. }
  179. void cliFunc_sendKeys( char* args )
  180. {
  181. // Copy USBKeys_KeysCLI to USBKeys_Keys
  182. for ( uint8_t key = 0; key < USBKeys_SentCLI; ++key )
  183. {
  184. // TODO
  185. //USBKeys_Keys[key] = USBKeys_KeysCLI[key];
  186. }
  187. USBKeys_Sent = USBKeys_SentCLI;
  188. // Set modifier byte
  189. USBKeys_Modifiers = USBKeys_ModifiersCLI;
  190. }
  191. void cliFunc_setKeys( char* args )
  192. {
  193. char* curArgs;
  194. char* arg1Ptr;
  195. char* arg2Ptr = args;
  196. // Parse up to USBKeys_MaxSize args (whichever is least)
  197. for ( USBKeys_SentCLI = 0; USBKeys_SentCLI < USB_BOOT_MAX_KEYS; ++USBKeys_SentCLI )
  198. {
  199. curArgs = arg2Ptr;
  200. CLI_argumentIsolation( curArgs, &arg1Ptr, &arg2Ptr );
  201. // Stop processing args if no more are found
  202. if ( *arg1Ptr == '\0' )
  203. break;
  204. // Add the USB code to be sent
  205. // TODO
  206. //USBKeys_KeysCLI[USBKeys_SentCLI] = numToInt( arg1Ptr );
  207. }
  208. }
  209. void cliFunc_setMod( char* args )
  210. {
  211. // Parse number from argument
  212. // NOTE: Only first argument is used
  213. char* arg1Ptr;
  214. char* arg2Ptr;
  215. CLI_argumentIsolation( args, &arg1Ptr, &arg2Ptr );
  216. USBKeys_ModifiersCLI = numToInt( arg1Ptr );
  217. }