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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. /* Copyright (C) 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. #elif defined(_mk20dx128_) || defined(_mk20dx128vlf5_) || defined(_mk20dx256_)
  31. #include "arm/uart_serial.h"
  32. #endif
  33. // Local Includes
  34. #include "output_com.h"
  35. // ----- Function Declarations -----
  36. void cliFunc_kbdProtocol( char* args );
  37. void cliFunc_readLEDs ( char* args );
  38. void cliFunc_sendKeys ( char* args );
  39. void cliFunc_setKeys ( char* args );
  40. void cliFunc_setMod ( char* args );
  41. // ----- Variables -----
  42. // Output Module command dictionary
  43. const char outputCLIDictName[] = "USB Module Commands - NOT WORKING";
  44. const CLIDictItem outputCLIDict[] = {
  45. { "kbdProtocol", "Keyboard Protocol Mode: 0 - Boot, 1 - OS/NKRO Mode", cliFunc_kbdProtocol },
  46. { "readLEDs", "Read LED byte:" NL "\t\t1 NumLck, 2 CapsLck, 4 ScrlLck, 16 Kana, etc.", cliFunc_readLEDs },
  47. { "sendKeys", "Send the prepared list of USB codes and modifier byte.", cliFunc_sendKeys },
  48. { "setKeys", "Prepare a space separated list of USB codes (decimal). Waits until \033[35msendKeys\033[0m.", cliFunc_setKeys },
  49. { "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 },
  50. { 0, 0, 0 } // Null entry for dictionary end
  51. };
  52. // Which modifier keys are currently pressed
  53. // 1=left ctrl, 2=left shift, 4=left alt, 8=left gui
  54. // 16=right ctrl, 32=right shift, 64=right alt, 128=right gui
  55. uint8_t USBKeys_Modifiers = 0;
  56. uint8_t USBKeys_ModifiersCLI = 0; // Separate CLI send buffer
  57. // Currently pressed keys, max is defined by USB_MAX_KEY_SEND
  58. uint8_t USBKeys_Array [USB_MAX_KEY_SEND];
  59. uint8_t USBKeys_ArrayCLI[USB_MAX_KEY_SEND]; // Separate CLI send buffer
  60. // The number of keys sent to the usb in the array
  61. uint8_t USBKeys_Sent = 0;
  62. uint8_t USBKeys_SentCLI = 0;
  63. // 1=num lock, 2=caps lock, 4=scroll lock, 8=compose, 16=kana
  64. volatile uint8_t USBKeys_LEDs = 0;
  65. // Protocol setting from the host.
  66. // 0 - Boot Mode (Default, until set by the host)
  67. // 1 - NKRO Mode
  68. volatile uint8_t USBKeys_Protocol = 1;
  69. // the idle configuration, how often we send the report to the
  70. // host (ms * 4) even when it hasn't changed
  71. uint8_t USBKeys_Idle_Config = 125;
  72. // count until idle timeout
  73. uint8_t USBKeys_Idle_Count = 0;
  74. // ----- Functions -----
  75. // USB Module Setup
  76. inline void Output_setup()
  77. {
  78. // Setup UART
  79. uart_serial_setup();
  80. // Register USB Output CLI dictionary
  81. CLI_registerDictionary( outputCLIDict, outputCLIDictName );
  82. }
  83. // USB Data Send
  84. inline void Output_send(void)
  85. {
  86. // TODO
  87. }
  88. // Sets the device into firmware reload mode
  89. inline void Output_firmwareReload()
  90. {
  91. uart_device_reload();
  92. }
  93. // USB Input buffer available
  94. inline unsigned int Output_availablechar()
  95. {
  96. return uart_serial_available();
  97. }
  98. // USB Get Character from input buffer
  99. inline int Output_getchar()
  100. {
  101. // XXX Make sure to check output_availablechar() first! Information is lost with the cast (error codes) (AVR)
  102. return (int)uart_serial_getchar();
  103. }
  104. // USB Send Character to output buffer
  105. inline int Output_putchar( char c )
  106. {
  107. return uart_serial_putchar( c );
  108. }
  109. // USB Send String to output buffer, null terminated
  110. inline int Output_putstr( char* str )
  111. {
  112. #if defined(_at90usb162_) || defined(_atmega32u4_) || defined(_at90usb646_) || defined(_at90usb1286_) // AVR
  113. uint16_t count = 0;
  114. #elif defined(_mk20dx128_) || defined(_mk20dx128vlf5_) || defined(_mk20dx256_) // ARM
  115. uint32_t count = 0;
  116. #endif
  117. // Count characters until NULL character, then send the amount counted
  118. while ( str[count] != '\0' )
  119. count++;
  120. return uart_serial_write( str, count );
  121. }
  122. // Soft Chip Reset
  123. inline void Output_softReset()
  124. {
  125. usb_device_software_reset();
  126. }
  127. // ----- CLI Command Functions -----
  128. void cliFunc_kbdProtocol( char* args )
  129. {
  130. print( NL );
  131. info_msg("Keyboard Protocol: ");
  132. printInt8( USBKeys_Protocol );
  133. }
  134. void cliFunc_readLEDs( char* args )
  135. {
  136. print( NL );
  137. info_msg("LED State (This doesn't work yet...): ");
  138. printInt8( USBKeys_LEDs );
  139. }
  140. void cliFunc_sendKeys( char* args )
  141. {
  142. // Copy USBKeys_ArrayCLI to USBKeys_Array
  143. for ( uint8_t key = 0; key < USBKeys_SentCLI; ++key )
  144. {
  145. USBKeys_Array[key] = USBKeys_ArrayCLI[key];
  146. }
  147. USBKeys_Sent = USBKeys_SentCLI;
  148. // Set modifier byte
  149. USBKeys_Modifiers = USBKeys_ModifiersCLI;
  150. }
  151. void cliFunc_setKeys( char* args )
  152. {
  153. char* curArgs;
  154. char* arg1Ptr;
  155. char* arg2Ptr = args;
  156. // Parse up to USBKeys_MaxSize args (whichever is least)
  157. for ( USBKeys_SentCLI = 0; USBKeys_SentCLI < USBKeys_MaxSize; ++USBKeys_SentCLI )
  158. {
  159. curArgs = arg2Ptr;
  160. CLI_argumentIsolation( curArgs, &arg1Ptr, &arg2Ptr );
  161. // Stop processing args if no more are found
  162. if ( *arg1Ptr == '\0' )
  163. break;
  164. // Add the USB code to be sent
  165. USBKeys_ArrayCLI[USBKeys_SentCLI] = decToInt( arg1Ptr );
  166. }
  167. }
  168. void cliFunc_setMod( char* args )
  169. {
  170. // Parse number from argument
  171. // NOTE: Only first argument is used
  172. char* arg1Ptr;
  173. char* arg2Ptr;
  174. CLI_argumentIsolation( args, &arg1Ptr, &arg2Ptr );
  175. USBKeys_ModifiersCLI = decToInt( arg1Ptr );
  176. }