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

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