Kiibohd Controller
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  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", "This command :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. void commandLookup_cli()
  136. {
  137. // Ignore command if buffer is 0 length
  138. if ( CLILineBufferCurrent == 0 )
  139. return;
  140. // Set the last+1 character of the buffer to NULL for string processing
  141. CLILineBuffer[CLILineBufferCurrent] = '\0';
  142. // Mark out the first argument
  143. // This is done by finding the first space after a list of non-spaces and setting it NULL
  144. char* cmdPtr = CLILineBuffer - 1;
  145. while ( *++cmdPtr == ' ' ); // Skips leading spaces, and points to first character of cmd
  146. // Locates first space delimiter, and points to first character of args or a NULL (no args)
  147. char* argPtr = cmdPtr;
  148. do {
  149. argPtr++;
  150. } while ( *argPtr != ' ' && *argPtr != '\0' );
  151. // Set the space delimiter as a NULL
  152. argPtr[-1] = '\0';
  153. // Scan array of dictionaries for a valid command match
  154. for ( uint8_t dict = 0; dict < CLIDictionariesUsed; dict++ )
  155. {
  156. // Parse each cmd until a null command entry is found, or an argument match
  157. for ( uint8_t cmd = 0; CLIDict[dict][cmd].name != 0; cmd++ )
  158. {
  159. // Compare the first argument and each command entry
  160. if ( eqStr( cmdPtr, CLIDict[dict][cmd].name ) )
  161. {
  162. // Run the specified command function pointer
  163. // argPtr is already pointing at the first character of the arguments
  164. (*CLIDict[dict][cmd].function)( argPtr );
  165. return;
  166. }
  167. }
  168. }
  169. // No match for the command...
  170. print( NL );
  171. erro_dPrint("\"", CLILineBuffer, "\" is not a valid command...try help");
  172. }
  173. void registerDictionary_cli( CLIDictItem *cmdDict )
  174. {
  175. // Make sure this max limit of dictionaries hasn't been reached
  176. if ( CLIDictionariesUsed >= CLIMaxDictionaries )
  177. {
  178. erro_print("Max number of dictionaries defined already...");
  179. return;
  180. }
  181. // Add dictionary
  182. CLIDict[CLIDictionariesUsed++] = cmdDict;
  183. }
  184. // ----- CLI Command Functions -----
  185. void cliFunc_help( char* args )
  186. {
  187. print( NL );
  188. print("Help!");
  189. dPrint( args );
  190. }
  191. void cliFunc_version( char* args )
  192. {
  193. print( NL );
  194. print("Version!");
  195. dPrint( args );
  196. }