Keine Beschreibung
Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

console.c 5.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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 "console.h"
  12. #define CNSL_BUFF_SIZE 1024
  13. #define CNSL_REPEAT_MS 500
  14. //#define CNSL_REPEAT_PMW_STATUS_BY_DEFAULT
  15. static char cnsl_line_buff[CNSL_BUFF_SIZE + 1];
  16. static uint32_t cnsl_buff_pos = 0;
  17. static char cnsl_last_command[CNSL_BUFF_SIZE + 1];
  18. static char cnsl_repeated_command[CNSL_BUFF_SIZE + 1];
  19. static bool repeat_command = false;
  20. static uint32_t last_repeat_time = 0;
  21. static void cnsl_interpret(const char *line) {
  22. if (strlen(line) == 0) {
  23. if ((strlen(cnsl_last_command) > 0) && (strcmp(cnsl_last_command, "repeat") != 0)) {
  24. // repeat last command once
  25. print("repeating command \"%s\"", cnsl_last_command);
  26. cnsl_interpret(cnsl_last_command);
  27. print();
  28. }
  29. return;
  30. } else if (strcmp(line, "repeat") == 0) {
  31. if (!repeat_command) {
  32. // mark last command to be repeated multiple times
  33. strncpy(cnsl_repeated_command, cnsl_last_command, CNSL_BUFF_SIZE + 1);
  34. last_repeat_time = to_ms_since_boot(get_absolute_time()) - 1001;
  35. repeat_command = true;
  36. } else {
  37. // stop repeating
  38. repeat_command = false;
  39. }
  40. } else if ((strcmp(line, "help") == 0)
  41. || (strcmp(line, "h") == 0)
  42. || (strcmp(line, "?") == 0)) {
  43. print("Trackball Firmware Usage:");
  44. print(" cpi - print current sensitivity");
  45. print(" cpi N - set sensitivity");
  46. print(" pmws - print PMW3360 status");
  47. print(" pmwr - reset PMW3360");
  48. print(" reset - reset back into this firmware");
  49. print(" \\x18 - reset to bootloader");
  50. print(" repeat - repeat last command every %d milliseconds", CNSL_REPEAT_MS);
  51. print(" help - print this message");
  52. print("Press Enter with no input to repeat last command.");
  53. print("Use repeat to continuously execute last command.");
  54. print("Stop this by calling repeat again.");
  55. } else if (strcmp(line, "pmws") == 0) {
  56. pmw_print_status();
  57. } else if (strcmp(line, "pmwr") == 0) {
  58. print("user requests re-initializing of PMW3360");
  59. int r = pmw_init();
  60. if (r < 0) {
  61. print("error initializing PMW3360");
  62. } else {
  63. print("PMW3360 re-initialized successfully");
  64. }
  65. } else if (strcmp(line, "cpi") == 0) {
  66. uint8_t sense = pmw_get_sensitivity();
  67. uint16_t cpi = PMW_SENSE_TO_CPI(sense);
  68. print("current cpi: %u (0x%02X)", cpi, sense);
  69. } else if (str_startswith(line, "cpi ")) {
  70. const char *num_str = line + 4;
  71. uintmax_t num = strtoumax(num_str, NULL, 10);
  72. if ((num < 100) || (num > 12000)) {
  73. print("invalid cpi %llu, needs to be %u <= cpi <= %u", num, 100, 12000);
  74. } else {
  75. num = PMW_CPI_TO_SENSE(num);
  76. print("setting cpi to 0x%02llX", num);
  77. pmw_set_sensitivity(num);
  78. }
  79. } else if (strcmp(line, "reset") == 0) {
  80. reset_to_main();
  81. } else {
  82. print("unknown command \"%s\"", line);
  83. return;
  84. }
  85. print();
  86. }
  87. void cnsl_init(void) {
  88. cnsl_buff_pos = 0;
  89. for (int i = 0; i < CNSL_BUFF_SIZE + 1; i++) {
  90. cnsl_line_buff[i] = '\0';
  91. cnsl_last_command[i] = '\0';
  92. cnsl_repeated_command[i] = '\0';
  93. }
  94. #ifdef CNSL_REPEAT_PMW_STATUS_BY_DEFAULT
  95. strcpy(cnsl_repeated_command, "pmws");
  96. repeat_command = true;
  97. #endif // CNSL_REPEAT_PMW_STATUS_BY_DEFAULT
  98. }
  99. static int32_t cnsl_find_line_end(void) {
  100. for (int32_t i = 0; i < cnsl_buff_pos; i++) {
  101. if ((cnsl_line_buff[i] == '\r') || (cnsl_line_buff[i] == '\n')) {
  102. return i;
  103. }
  104. }
  105. return -1;
  106. }
  107. void cnsl_run(void) {
  108. if (repeat_command && (strlen(cnsl_repeated_command) > 0)
  109. && (strcmp(cnsl_repeated_command, "repeat") != 0)) {
  110. uint32_t now = to_ms_since_boot(get_absolute_time());
  111. if (now >= (last_repeat_time + CNSL_REPEAT_MS)) {
  112. print("repeating command \"%s\"", cnsl_repeated_command);
  113. cnsl_interpret(cnsl_repeated_command);
  114. print();
  115. last_repeat_time = now;
  116. }
  117. } else {
  118. if (repeat_command) {
  119. print("nothing to repeat");
  120. }
  121. repeat_command = false;
  122. }
  123. }
  124. void cnsl_handle_input(const char *buf, uint32_t len) {
  125. if ((cnsl_buff_pos + len) > CNSL_BUFF_SIZE) {
  126. debug("error: console input buffer overflow! %lu > %u", cnsl_buff_pos + len, CNSL_BUFF_SIZE);
  127. cnsl_init();
  128. }
  129. memcpy(cnsl_line_buff + cnsl_buff_pos, buf, len);
  130. cnsl_buff_pos += len;
  131. int32_t line_len = cnsl_find_line_end();
  132. if (line_len < 0) {
  133. // user has not pressed enter yet
  134. return;
  135. }
  136. // convert line to C-style string
  137. cnsl_line_buff[line_len] = '\0';
  138. // TODO handle backspace and other terminal commands?
  139. cnsl_interpret(cnsl_line_buff);
  140. // store command for eventual repeats
  141. strncpy(cnsl_last_command, cnsl_line_buff, CNSL_BUFF_SIZE + 1);
  142. // clear string and move following data over
  143. uint32_t cnt = line_len + 1;
  144. if (cnsl_line_buff[line_len + 1] == '\n') {
  145. cnt++;
  146. }
  147. memset(cnsl_line_buff, '\0', cnt);
  148. memmove(cnsl_line_buff, cnsl_line_buff + cnt, sizeof(cnsl_line_buff) - cnt);
  149. cnsl_buff_pos -= cnt;
  150. }