No Description
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.

console.c 8.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. /*
  2. * console.c
  3. */
  4. #include <inttypes.h>
  5. #include <string.h>
  6. #include "pico/stdlib.h"
  7. #include "config.h"
  8. #include "log.h"
  9. #include "pmw3360.h"
  10. #include "util.h"
  11. #include "usb_cdc.h"
  12. #include "usb_msc.h"
  13. #include "debug.h"
  14. #include "console.h"
  15. #define CNSL_BUFF_SIZE 1024
  16. #define CNSL_REPEAT_MS 500
  17. //#define CNSL_REPEAT_PMW_STATUS_BY_DEFAULT
  18. static char cnsl_line_buff[CNSL_BUFF_SIZE + 1];
  19. static uint32_t cnsl_buff_pos = 0;
  20. static char cnsl_last_command[CNSL_BUFF_SIZE + 1];
  21. static char cnsl_repeated_command[CNSL_BUFF_SIZE + 1];
  22. static bool repeat_command = false;
  23. static uint32_t last_repeat_time = 0;
  24. static void cnsl_interpret(const char *line) {
  25. if (strlen(line) == 0) {
  26. if ((strlen(cnsl_last_command) > 0) && (strcmp(cnsl_last_command, "repeat") != 0)) {
  27. // repeat last command once
  28. println("repeating command \"%s\"", cnsl_last_command);
  29. cnsl_interpret(cnsl_last_command);
  30. println();
  31. }
  32. return;
  33. } else if (strcmp(line, "repeat") == 0) {
  34. if (!repeat_command) {
  35. // mark last command to be repeated multiple times
  36. strncpy(cnsl_repeated_command, cnsl_last_command, CNSL_BUFF_SIZE + 1);
  37. last_repeat_time = to_ms_since_boot(get_absolute_time()) - 1001;
  38. repeat_command = true;
  39. } else {
  40. // stop repeating
  41. repeat_command = false;
  42. }
  43. } else if ((strcmp(line, "help") == 0)
  44. || (strcmp(line, "h") == 0)
  45. || (strcmp(line, "?") == 0)) {
  46. println("Trackball Firmware Usage:");
  47. println(" cpi - print current sensitivity");
  48. println(" cpi N - set sensitivity");
  49. println(" angle - print current angle");
  50. println("angle N - set angle");
  51. println(" pmws - print PMW3360 status");
  52. println(" pmwf - print PMW3360 frame capture");
  53. println(" pmwd - print PMW3360 data dump");
  54. println(" pmwr - reset PMW3360");
  55. println(" reset - reset back into this firmware");
  56. println(" \\x18 - reset to bootloader");
  57. println(" repeat - repeat last command every %d milliseconds", CNSL_REPEAT_MS);
  58. println(" help - print this message");
  59. println(" stats - put statistics on mass storage medium");
  60. println(" data - put PMW3360 data on mass storage medium");
  61. println(" mount - make mass storage medium (un)available");
  62. println("Press Enter with no input to repeat last command.");
  63. println("Use repeat to continuously execute last command.");
  64. println("Stop this by calling repeat again.");
  65. } else if (strcmp(line, "pmws") == 0) {
  66. char status_buff[1024];
  67. pmw_print_status(status_buff, sizeof(status_buff));
  68. print("%s", status_buff);
  69. } else if (strcmp(line, "pmwd") == 0) {
  70. pmw_dump_data(true);
  71. } else if (strcmp(line, "pmwf") == 0) {
  72. uint8_t frame[PMW_FRAME_CAPTURE_LEN];
  73. ssize_t r = pmw_frame_capture(frame, PMW_FRAME_CAPTURE_LEN);
  74. if (r == PMW_FRAME_CAPTURE_LEN) {
  75. println("PMW3360 frame capture:");
  76. hexdump(frame, PMW_FRAME_CAPTURE_LEN);
  77. println("Re-Initializing PMW3360");
  78. pmw_init();
  79. } else {
  80. println("error capturing frame (%d)", r);
  81. }
  82. } else if (strcmp(line, "pmwr") == 0) {
  83. println("user requests re-initializing of PMW3360");
  84. int r = pmw_init();
  85. if (r < 0) {
  86. println("error initializing PMW3360");
  87. } else {
  88. println("PMW3360 re-initialized successfully");
  89. }
  90. } else if (strcmp(line, "cpi") == 0) {
  91. uint8_t sense = pmw_get_sensitivity();
  92. uint16_t cpi = PMW_SENSE_TO_CPI(sense);
  93. println("current cpi: %u (0x%02X)", cpi, sense);
  94. } else if (str_startswith(line, "cpi ")) {
  95. const char *num_str = line + 4;
  96. uintmax_t num = strtoumax(num_str, NULL, 10);
  97. if ((num < 100) || (num > 12000)) {
  98. println("invalid cpi %llu, needs to be %u <= cpi <= %u", num, 100, 12000);
  99. } else {
  100. num = PMW_CPI_TO_SENSE(num);
  101. println("setting cpi to 0x%02llX", num);
  102. pmw_set_sensitivity(num);
  103. }
  104. } else if (strcmp(line, "angle") == 0) {
  105. int8_t angle = pmw_get_angle();
  106. println("current angle: %d", angle);
  107. } else if (str_startswith(line, "angle ")) {
  108. const char *num_str = line + 6;
  109. intmax_t num = strtoimax(num_str, NULL, 10);
  110. if ((num < -128) || (num > 127)) {
  111. println("invalid angle %lld, needs to be %d <= cpi <= %d", num, -128, 127);
  112. } else {
  113. int8_t tmp = num;
  114. println("setting angle to %d", tmp);
  115. pmw_set_angle(num);
  116. }
  117. } else if (strcmp(line, "reset") == 0) {
  118. reset_to_main();
  119. } else if ((strcmp(line, "stats") == 0) || (strcmp(line, "data") == 0)) {
  120. if (msc_is_medium_available()) {
  121. println("Currently mounted. Unplugging now.");
  122. msc_set_medium_available(false);
  123. }
  124. if (debug_msc_mount() != 0) {
  125. println("Error mounting file system.");
  126. println();
  127. return;
  128. }
  129. println("Writing data to file system");
  130. if (strcmp(line, "data") == 0) {
  131. debug_msc_pmw3360();
  132. }
  133. debug_msc_stats();
  134. if (debug_msc_unmount() != 0) {
  135. println("Error unmounting file system.");
  136. }
  137. println("Done. Plugging in now.");
  138. msc_set_medium_available(true);
  139. } else if (strcmp(line, "mount") == 0) {
  140. bool state = msc_is_medium_available();
  141. println("Currently %s. %s now.",
  142. state ? "mounted" : "unmounted",
  143. state ? "Unplugging" : "Plugging in");
  144. msc_set_medium_available(!state);
  145. } else {
  146. println("unknown command \"%s\"", line);
  147. }
  148. println();
  149. }
  150. void cnsl_init(void) {
  151. cnsl_buff_pos = 0;
  152. for (int i = 0; i < CNSL_BUFF_SIZE + 1; i++) {
  153. cnsl_line_buff[i] = '\0';
  154. cnsl_last_command[i] = '\0';
  155. cnsl_repeated_command[i] = '\0';
  156. }
  157. #ifdef CNSL_REPEAT_PMW_STATUS_BY_DEFAULT
  158. strcpy(cnsl_repeated_command, "pmws");
  159. repeat_command = true;
  160. #endif // CNSL_REPEAT_PMW_STATUS_BY_DEFAULT
  161. }
  162. static int32_t cnsl_find_line_end(void) {
  163. for (uint32_t i = 0; i < cnsl_buff_pos; i++) {
  164. if ((cnsl_line_buff[i] == '\r') || (cnsl_line_buff[i] == '\n')) {
  165. return i;
  166. }
  167. }
  168. return -1;
  169. }
  170. void cnsl_run(void) {
  171. if (repeat_command && (strlen(cnsl_repeated_command) > 0)
  172. && (strcmp(cnsl_repeated_command, "repeat") != 0)) {
  173. uint32_t now = to_ms_since_boot(get_absolute_time());
  174. if (now >= (last_repeat_time + CNSL_REPEAT_MS)) {
  175. println("repeating command \"%s\"", cnsl_repeated_command);
  176. cnsl_interpret(cnsl_repeated_command);
  177. println();
  178. last_repeat_time = now;
  179. }
  180. } else {
  181. if (repeat_command) {
  182. println("nothing to repeat");
  183. }
  184. repeat_command = false;
  185. }
  186. }
  187. void cnsl_handle_input(const char *buf, uint32_t len) {
  188. if ((cnsl_buff_pos + len) > CNSL_BUFF_SIZE) {
  189. debug("error: console input buffer overflow! %lu > %u", cnsl_buff_pos + len, CNSL_BUFF_SIZE);
  190. cnsl_init();
  191. }
  192. memcpy(cnsl_line_buff + cnsl_buff_pos, buf, len);
  193. cnsl_buff_pos += len;
  194. // handle backspace
  195. for (ssize_t i = cnsl_buff_pos - len; i < (ssize_t)cnsl_buff_pos; i++) {
  196. if ((cnsl_line_buff[i] == '\b') || (cnsl_line_buff[i] == 0x7F)) {
  197. if (i > 0) {
  198. // overwrite previous character and backspace
  199. for (ssize_t j = i; j < (ssize_t)cnsl_buff_pos - 1; j++) {
  200. cnsl_line_buff[j - 1] = cnsl_line_buff[j + 1];
  201. }
  202. cnsl_buff_pos -= 2;
  203. } else {
  204. // just remove the backspace
  205. for (ssize_t j = i; j < (ssize_t)cnsl_buff_pos - 1; j++) {
  206. cnsl_line_buff[j] = cnsl_line_buff[j + 1];
  207. }
  208. cnsl_buff_pos -= 1;
  209. }
  210. usb_cdc_write("\b \b", 3);
  211. // check for another backspace in this space
  212. i--;
  213. } else {
  214. usb_cdc_write(cnsl_line_buff + i, 1);
  215. }
  216. }
  217. int32_t line_len = cnsl_find_line_end();
  218. if (line_len < 0) {
  219. // user has not pressed enter yet
  220. return;
  221. }
  222. // convert line to C-style string
  223. cnsl_line_buff[line_len] = '\0';
  224. cnsl_interpret(cnsl_line_buff);
  225. // store command for eventual repeats
  226. strncpy(cnsl_last_command, cnsl_line_buff, CNSL_BUFF_SIZE + 1);
  227. // clear string and move following data over
  228. uint32_t cnt = line_len + 1;
  229. if (cnsl_line_buff[line_len + 1] == '\n') {
  230. cnt++;
  231. }
  232. memset(cnsl_line_buff, '\0', cnt);
  233. memmove(cnsl_line_buff, cnsl_line_buff + cnt, sizeof(cnsl_line_buff) - cnt);
  234. cnsl_buff_pos -= cnt;
  235. }