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.

cli.c 7.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  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 "cli.h"
  26. #include <print.h>
  27. // ----- Variables -----
  28. // Basic command dictionary
  29. CLIDictItem basicCLIDict[] = {
  30. { "help", "You're looking at it :P", cliFunc_help },
  31. { "version", "Version information about this firmware.", cliFunc_version },
  32. { 0, 0, 0 } // Null entry for dictionary end
  33. };
  34. // ----- Functions -----
  35. inline void prompt()
  36. {
  37. print(": ");
  38. }
  39. inline void init_cli()
  40. {
  41. // Reset the Line Buffer
  42. CLILineBufferCurrent = 0;
  43. // Set prompt
  44. prompt();
  45. // Register first dictionary
  46. CLIDictionariesUsed = 0;
  47. registerDictionary_cli( basicCLIDict );
  48. }
  49. void process_cli()
  50. {
  51. // Current buffer position
  52. uint8_t prev_buf_pos = CLILineBufferCurrent;
  53. // Process each character while available
  54. int result = 0;
  55. while ( 1 )
  56. {
  57. // No more characters to process
  58. result = usb_serial_getchar(); // Retrieve from serial module // TODO Make USB agnostic
  59. if ( result == -1 )
  60. break;
  61. char cur_char = (char)result;
  62. // Make sure buffer isn't full
  63. if ( CLILineBufferCurrent >= CLILineBufferMaxSize )
  64. {
  65. print( NL );
  66. erro_print("Serial line buffer is full, dropping character and resetting...");
  67. // Clear buffer
  68. CLILineBufferCurrent = 0;
  69. // Reset the prompt
  70. prompt();
  71. return;
  72. }
  73. // Place into line buffer
  74. CLILineBuffer[CLILineBufferCurrent++] = cur_char;
  75. }
  76. // If buffer has changed, output to screen while there are still characters in the buffer not displayed
  77. while ( CLILineBufferCurrent > prev_buf_pos )
  78. {
  79. // Check for control characters
  80. switch ( CLILineBuffer[prev_buf_pos] )
  81. {
  82. case 0x0D: // Enter
  83. CLILineBufferCurrent--; // Remove the Enter
  84. // Process the current line buffer
  85. commandLookup_cli();
  86. // Reset the buffer
  87. CLILineBufferCurrent = 0;
  88. // Reset the prompt after processing has finished
  89. print( NL );
  90. prompt();
  91. // XXX There is a potential bug here when resetting the buffer (losing valid keypresses)
  92. // Doesn't look like it will happen *that* often, so not handling it for now -HaaTa
  93. return;
  94. case 0x09: // Tab
  95. // Tab completion for the current command
  96. // TODO
  97. return;
  98. case 0x1B: // Esc
  99. // Check for escape sequence
  100. // TODO
  101. return;
  102. case 0x08:
  103. case 0x7F: // Backspace
  104. // TODO - Does not handle case for arrow editing (arrows disabled atm)
  105. CLILineBufferCurrent--; // Remove the backspace
  106. // If there are characters in the buffer
  107. if ( CLILineBufferCurrent > 0 )
  108. {
  109. // Remove character from current position in the line buffer
  110. CLILineBufferCurrent--;
  111. // Remove character from tty
  112. print("\b \b");
  113. }
  114. break;
  115. default:
  116. // Place a null on the end (to use with string print)
  117. CLILineBuffer[CLILineBufferCurrent] = '\0';
  118. // Output buffer to screen
  119. dPrint( &CLILineBuffer[prev_buf_pos] );
  120. // Buffer reset
  121. prev_buf_pos++;
  122. break;
  123. }
  124. /* TODO Enable via option
  125. uint8_t pos = prev_buf_pos;
  126. while ( CLILineBuffer[pos] != 0 )
  127. {
  128. printHex( CLILineBuffer[pos++] );
  129. print(" ");
  130. }
  131. print( NL );
  132. */
  133. }
  134. }
  135. // Takes a string, returns two pointers
  136. // One to the first non-space character
  137. // The second to the next argument (first NULL if there isn't an argument). delimited by a space
  138. // Places a NULL at the first space after the first argument
  139. inline void argumentIsolation_cli( char* string, char** first, char** second )
  140. {
  141. // Mark out the first argument
  142. // This is done by finding the first space after a list of non-spaces and setting it NULL
  143. char* cmdPtr = string - 1;
  144. while ( *++cmdPtr == ' ' ); // Skips leading spaces, and points to first character of cmd
  145. // Locates first space delimiter
  146. char* argPtr = cmdPtr + 1;
  147. while ( *argPtr != ' ' && *argPtr != '\0' )
  148. argPtr++;
  149. // Point to the first character of args or a NULL (no args) and set the space delimiter as a NULL
  150. (++argPtr)[-1] = '\0';
  151. // Set return variables
  152. *first = cmdPtr;
  153. *second = argPtr;
  154. }
  155. void commandLookup_cli()
  156. {
  157. // Ignore command if buffer is 0 length
  158. if ( CLILineBufferCurrent == 0 )
  159. return;
  160. // Set the last+1 character of the buffer to NULL for string processing
  161. CLILineBuffer[CLILineBufferCurrent] = '\0';
  162. // Retrieve pointers to command and beginning of arguments
  163. // Places a NULL at the first space after the command
  164. char* cmdPtr;
  165. char* argPtr;
  166. argumentIsolation_cli( CLILineBuffer, &cmdPtr, &argPtr );
  167. // Scan array of dictionaries for a valid command match
  168. for ( uint8_t dict = 0; dict < CLIDictionariesUsed; dict++ )
  169. {
  170. // Parse each cmd until a null command entry is found, or an argument match
  171. for ( uint8_t cmd = 0; CLIDict[dict][cmd].name != 0; cmd++ )
  172. {
  173. // Compare the first argument and each command entry
  174. if ( eqStr( cmdPtr, CLIDict[dict][cmd].name ) )
  175. {
  176. // Run the specified command function pointer
  177. // argPtr is already pointing at the first character of the arguments
  178. (*CLIDict[dict][cmd].function)( argPtr );
  179. return;
  180. }
  181. }
  182. }
  183. // No match for the command...
  184. print( NL );
  185. erro_dPrint("\"", CLILineBuffer, "\" is not a valid command...type \033[35mhelp\033[0m");
  186. }
  187. inline void registerDictionary_cli( CLIDictItem *cmdDict )
  188. {
  189. // Make sure this max limit of dictionaries hasn't been reached
  190. if ( CLIDictionariesUsed >= CLIMaxDictionaries )
  191. {
  192. erro_print("Max number of dictionaries defined already...");
  193. return;
  194. }
  195. // Add dictionary
  196. CLIDict[CLIDictionariesUsed++] = cmdDict;
  197. }
  198. // ----- CLI Command Functions -----
  199. void cliFunc_help( char* args )
  200. {
  201. // Scan array of dictionaries and print every description
  202. // (no alphabetical here, too much processing/memory to sort...)
  203. for ( uint8_t dict = 0; dict < CLIDictionariesUsed; dict++ )
  204. {
  205. print( NL "\033[1;32mCOMMAND SET\033[0m " );
  206. printInt8( dict + 1 );
  207. print( NL );
  208. // Parse each cmd/description until a null command entry is found
  209. for ( uint8_t cmd = 0; CLIDict[dict][cmd].name != 0; cmd++ )
  210. {
  211. dPrintStrs( " \033[35m", CLIDict[dict][cmd].name, NL, "\033[0m ", CLIDict[dict][cmd].description, NL );
  212. }
  213. }
  214. }
  215. void cliFunc_version( char* args )
  216. {
  217. print( NL );
  218. print("Version!");
  219. dPrint( args );
  220. }