Kiibohd Controller
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.
Ce dépôt est archivé. Vous pouvez voir les fichiers et le cloner, mais vous ne pouvez pas pousser ni ouvrir de ticket/demande d'ajout.

cli.c 10.0KB

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