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.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. /* Copyright (C) 2014-2015 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. uint8_t Output_Available = 0;
  89. // ----- Capabilities -----
  90. // Adds a single USB Code to the USB Output buffer
  91. // Argument #1: USB Code
  92. void Output_usbCodeSend_capability( uint8_t state, uint8_t stateType, uint8_t *args )
  93. {
  94. // Display capability name
  95. if ( stateType == 0xFF && state == 0xFF )
  96. {
  97. print("Output_usbCodeSend(usbCode)");
  98. print("Not used in uartOut...");
  99. return;
  100. }
  101. }
  102. // ----- Functions -----
  103. // USB Module Setup
  104. inline void Output_setup()
  105. {
  106. // Setup UART
  107. uart_serial_setup();
  108. // Register USB Output CLI dictionary
  109. CLI_registerDictionary( outputCLIDict, outputCLIDictName );
  110. }
  111. // USB Data Send
  112. inline void Output_send(void)
  113. {
  114. // TODO
  115. }
  116. // Sets the device into firmware reload mode
  117. inline void Output_firmwareReload()
  118. {
  119. uart_device_reload();
  120. }
  121. // USB Input buffer available
  122. inline unsigned int Output_availablechar()
  123. {
  124. return uart_serial_available();
  125. }
  126. // USB Get Character from input buffer
  127. inline int Output_getchar()
  128. {
  129. // XXX Make sure to check output_availablechar() first! Information is lost with the cast (error codes) (AVR)
  130. return (int)uart_serial_getchar();
  131. }
  132. // USB Send Character to output buffer
  133. inline int Output_putchar( char c )
  134. {
  135. return uart_serial_putchar( c );
  136. }
  137. // USB Send String to output buffer, null terminated
  138. inline int Output_putstr( char* str )
  139. {
  140. #if defined(_at90usb162_) || defined(_atmega32u4_) || defined(_at90usb646_) || defined(_at90usb1286_) // AVR
  141. uint16_t count = 0;
  142. #elif defined(_mk20dx128_) || defined(_mk20dx128vlf5_) || defined(_mk20dx256_) || defined(_mk20dx256vlh7_) // ARM
  143. uint32_t count = 0;
  144. #endif
  145. // Count characters until NULL character, then send the amount counted
  146. while ( str[count] != '\0' )
  147. count++;
  148. return uart_serial_write( str, count );
  149. }
  150. // Soft Chip Reset
  151. inline void Output_softReset()
  152. {
  153. #if defined(_at90usb162_) || defined(_atmega32u4_) || defined(_at90usb646_) || defined(_at90usb1286_) // AVR
  154. #elif defined(_mk20dx128_) || defined(_mk20dx128vlf5_) || defined(_mk20dx256_) || defined(_mk20dx256vlh7_) // ARM
  155. SOFTWARE_RESET();
  156. #endif
  157. }
  158. // ----- CLI Command Functions -----
  159. void cliFunc_kbdProtocol( char* args )
  160. {
  161. print( NL );
  162. info_msg("Keyboard Protocol: ");
  163. printInt8( USBKeys_Protocol );
  164. }
  165. void cliFunc_readLEDs( char* args )
  166. {
  167. print( NL );
  168. info_msg("LED State: ");
  169. printInt8( USBKeys_LEDs );
  170. }
  171. void cliFunc_sendKeys( char* args )
  172. {
  173. // Copy USBKeys_KeysCLI to USBKeys_Keys
  174. for ( uint8_t key = 0; key < USBKeys_SentCLI; ++key )
  175. {
  176. // TODO
  177. //USBKeys_Keys[key] = USBKeys_KeysCLI[key];
  178. }
  179. USBKeys_Sent = USBKeys_SentCLI;
  180. // Set modifier byte
  181. USBKeys_Modifiers = USBKeys_ModifiersCLI;
  182. }
  183. void cliFunc_setKeys( char* args )
  184. {
  185. char* curArgs;
  186. char* arg1Ptr;
  187. char* arg2Ptr = args;
  188. // Parse up to USBKeys_MaxSize args (whichever is least)
  189. for ( USBKeys_SentCLI = 0; USBKeys_SentCLI < USB_BOOT_MAX_KEYS; ++USBKeys_SentCLI )
  190. {
  191. curArgs = arg2Ptr;
  192. CLI_argumentIsolation( curArgs, &arg1Ptr, &arg2Ptr );
  193. // Stop processing args if no more are found
  194. if ( *arg1Ptr == '\0' )
  195. break;
  196. // Add the USB code to be sent
  197. // TODO
  198. //USBKeys_KeysCLI[USBKeys_SentCLI] = numToInt( arg1Ptr );
  199. }
  200. }
  201. void cliFunc_setMod( char* args )
  202. {
  203. // Parse number from argument
  204. // NOTE: Only first argument is used
  205. char* arg1Ptr;
  206. char* arg2Ptr;
  207. CLI_argumentIsolation( args, &arg1Ptr, &arg2Ptr );
  208. USBKeys_ModifiersCLI = numToInt( arg1Ptr );
  209. }