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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  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 <stdarg.h>
  24. // Project Includes
  25. #include <buildvars.h>
  26. #include "cli.h"
  27. #include <led.h>
  28. #include <print.h>
  29. // ----- Variables -----
  30. // Basic command dictionary
  31. CLIDictItem basicCLIDict[] = {
  32. { "cliDebug", "Enables/Disables hex output of the most recent cli input.", cliFunc_cliDebug },
  33. { "help", "You're looking at it :P", cliFunc_help },
  34. { "led", "Enables/Disables indicator LED. Try a couple times just in case the LED is in an odd state.\r\n\t\t\033[33mWarning\033[0m: May adversely affect some modules...", cliFunc_led },
  35. { "reload", "Signals microcontroller to reflash/reload.", cliFunc_reload },
  36. { "reset", "Sends a software reset, should be similar to powering on the device.", cliFunc_reset },
  37. { "version", "Version information about this firmware.", cliFunc_version },
  38. { 0, 0, 0 } // Null entry for dictionary end
  39. };
  40. // ----- Functions -----
  41. inline void prompt()
  42. {
  43. print(": ");
  44. }
  45. inline void init_cli()
  46. {
  47. // Reset the Line Buffer
  48. CLILineBufferCurrent = 0;
  49. // Set prompt
  50. prompt();
  51. // Register first dictionary
  52. CLIDictionariesUsed = 0;
  53. registerDictionary_cli( basicCLIDict );
  54. // Initialize main LED
  55. init_errorLED();
  56. CLILEDState = 0;
  57. }
  58. void process_cli()
  59. {
  60. // Current buffer position
  61. uint8_t prev_buf_pos = CLILineBufferCurrent;
  62. // Process each character while available
  63. int result = 0;
  64. while ( 1 )
  65. {
  66. // No more characters to process
  67. result = usb_serial_getchar(); // Retrieve from serial module // TODO Make USB agnostic
  68. if ( result == -1 )
  69. break;
  70. char cur_char = (char)result;
  71. // Make sure buffer isn't full
  72. if ( CLILineBufferCurrent >= CLILineBufferMaxSize )
  73. {
  74. print( NL );
  75. erro_print("Serial line buffer is full, dropping character and resetting...");
  76. // Clear buffer
  77. CLILineBufferCurrent = 0;
  78. // Reset the prompt
  79. prompt();
  80. return;
  81. }
  82. // Place into line buffer
  83. CLILineBuffer[CLILineBufferCurrent++] = cur_char;
  84. }
  85. // If buffer has changed, output to screen while there are still characters in the buffer not displayed
  86. while ( CLILineBufferCurrent > prev_buf_pos )
  87. {
  88. // Check for control characters
  89. switch ( CLILineBuffer[prev_buf_pos] )
  90. {
  91. case 0x0D: // Enter
  92. CLILineBufferCurrent--; // Remove the Enter
  93. // Process the current line buffer
  94. commandLookup_cli();
  95. // Reset the buffer
  96. CLILineBufferCurrent = 0;
  97. // Reset the prompt after processing has finished
  98. print( NL );
  99. prompt();
  100. // XXX There is a potential bug here when resetting the buffer (losing valid keypresses)
  101. // Doesn't look like it will happen *that* often, so not handling it for now -HaaTa
  102. return;
  103. case 0x09: // Tab
  104. // Tab completion for the current command
  105. // TODO
  106. return;
  107. case 0x1B: // Esc
  108. // Check for escape sequence
  109. // TODO
  110. return;
  111. case 0x08:
  112. case 0x7F: // Backspace
  113. // TODO - Does not handle case for arrow editing (arrows disabled atm)
  114. CLILineBufferCurrent--; // Remove the backspace
  115. // If there are characters in the buffer
  116. if ( CLILineBufferCurrent > 0 )
  117. {
  118. // Remove character from current position in the line buffer
  119. CLILineBufferCurrent--;
  120. // Remove character from tty
  121. print("\b \b");
  122. }
  123. break;
  124. default:
  125. // Place a null on the end (to use with string print)
  126. CLILineBuffer[CLILineBufferCurrent] = '\0';
  127. // Output buffer to screen
  128. dPrint( &CLILineBuffer[prev_buf_pos] );
  129. // Buffer reset
  130. prev_buf_pos++;
  131. break;
  132. }
  133. /* TODO Enable via option
  134. uint8_t pos = prev_buf_pos;
  135. while ( CLILineBuffer[pos] != 0 )
  136. {
  137. printHex( CLILineBuffer[pos++] );
  138. print(" ");
  139. }
  140. print( NL );
  141. */
  142. }
  143. }
  144. // Takes a string, returns two pointers
  145. // One to the first non-space character
  146. // The second to the next argument (first NULL if there isn't an argument). delimited by a space
  147. // Places a NULL at the first space after the first argument
  148. inline void argumentIsolation_cli( char* string, char** first, char** second )
  149. {
  150. // Mark out the first argument
  151. // This is done by finding the first space after a list of non-spaces and setting it NULL
  152. char* cmdPtr = string - 1;
  153. while ( *++cmdPtr == ' ' ); // Skips leading spaces, and points to first character of cmd
  154. // Locates first space delimiter
  155. char* argPtr = cmdPtr + 1;
  156. while ( *argPtr != ' ' && *argPtr != '\0' )
  157. argPtr++;
  158. // Point to the first character of args or a NULL (no args) and set the space delimiter as a NULL
  159. (++argPtr)[-1] = '\0';
  160. // Set return variables
  161. *first = cmdPtr;
  162. *second = argPtr;
  163. }
  164. void commandLookup_cli()
  165. {
  166. // Ignore command if buffer is 0 length
  167. if ( CLILineBufferCurrent == 0 )
  168. return;
  169. // Set the last+1 character of the buffer to NULL for string processing
  170. CLILineBuffer[CLILineBufferCurrent] = '\0';
  171. // Retrieve pointers to command and beginning of arguments
  172. // Places a NULL at the first space after the command
  173. char* cmdPtr;
  174. char* argPtr;
  175. argumentIsolation_cli( CLILineBuffer, &cmdPtr, &argPtr );
  176. // Scan array of dictionaries for a valid command match
  177. for ( uint8_t dict = 0; dict < CLIDictionariesUsed; dict++ )
  178. {
  179. // Parse each cmd until a null command entry is found, or an argument match
  180. for ( uint8_t cmd = 0; CLIDict[dict][cmd].name != 0; cmd++ )
  181. {
  182. // Compare the first argument and each command entry
  183. if ( eqStr( cmdPtr, CLIDict[dict][cmd].name ) )
  184. {
  185. // Run the specified command function pointer
  186. // argPtr is already pointing at the first character of the arguments
  187. (*CLIDict[dict][cmd].function)( argPtr );
  188. return;
  189. }
  190. }
  191. }
  192. // No match for the command...
  193. print( NL );
  194. erro_dPrint("\"", CLILineBuffer, "\" is not a valid command...type \033[35mhelp\033[0m");
  195. }
  196. inline void registerDictionary_cli( CLIDictItem *cmdDict )
  197. {
  198. // Make sure this max limit of dictionaries hasn't been reached
  199. if ( CLIDictionariesUsed >= CLIMaxDictionaries )
  200. {
  201. erro_print("Max number of dictionaries defined already...");
  202. return;
  203. }
  204. // Add dictionary
  205. CLIDict[CLIDictionariesUsed++] = cmdDict;
  206. }
  207. // ----- CLI Command Functions -----
  208. void cliFunc_cliDebug( char* args )
  209. {
  210. }
  211. void cliFunc_help( char* args )
  212. {
  213. // Scan array of dictionaries and print every description
  214. // (no alphabetical here, too much processing/memory to sort...)
  215. for ( uint8_t dict = 0; dict < CLIDictionariesUsed; dict++ )
  216. {
  217. print( NL "\033[1;32mCOMMAND SET\033[0m " );
  218. printInt8( dict + 1 );
  219. print( NL );
  220. // Parse each cmd/description until a null command entry is found
  221. for ( uint8_t cmd = 0; CLIDict[dict][cmd].name != 0; cmd++ )
  222. {
  223. dPrintStrs(" \033[35m", CLIDict[dict][cmd].name, "\033[0m");
  224. // Determine number of spaces to tab by the length of the command and TabAlign
  225. uint8_t padLength = CLIEntryTabAlign - lenStr( CLIDict[dict][cmd].name );
  226. while ( padLength-- > 0 )
  227. print(" ");
  228. dPrintStrNL( CLIDict[dict][cmd].description );
  229. }
  230. }
  231. }
  232. void cliFunc_led( char* args )
  233. {
  234. CLILEDState ^= 1 << 1; // Toggle between 0 and 1
  235. errorLED( CLILEDState ); // Enable/Disable error LED
  236. }
  237. void cliFunc_reload( char* args )
  238. {
  239. // Request to output module to be set into firmware reload mode
  240. output_firmwareReload();
  241. }
  242. void cliFunc_reset( char* args )
  243. {
  244. // Trigger an overall software reset
  245. SOFTWARE_RESET();
  246. }
  247. void cliFunc_version( char* args )
  248. {
  249. print( NL );
  250. print( " \033[1mRevision:\033[0m " CLI_Revision NL );
  251. print( " \033[1mBranch:\033[0m " CLI_Branch NL );
  252. print( " \033[1mTree Status:\033[0m " CLI_ModifiedStatus NL );
  253. print( " \033[1mRepo Origin:\033[0m " CLI_RepoOrigin NL );
  254. print( " \033[1mCommit Date:\033[0m " CLI_CommitDate NL );
  255. print( " \033[1mCommit Author:\033[0m " CLI_CommitAuthor NL );
  256. print( " \033[1mBuild Date:\033[0m " CLI_BuildDate NL );
  257. print( " \033[1mBuild OS:\033[0m " CLI_BuildOS NL );
  258. print( " \033[1mArchitecture:\033[0m " CLI_Arch NL );
  259. print( " \033[1mChip:\033[0m " CLI_Chip NL );
  260. print( " \033[1mCPU:\033[0m " CLI_CPU NL );
  261. print( " \033[1mDevice:\033[0m " CLI_Device NL );
  262. print( " \033[1mModules:\033[0m " CLI_Modules NL );
  263. }