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 7.4KB

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