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.0KB

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