S&B Volcano vaporizer remote control with Pi Pico W
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 6.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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 <unistd.h>
  22. #include "config.h"
  23. #include "log.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(" reset - reset back into this firmware");
  62. println(" \\x18 - reset to bootloader");
  63. println(" repeat - repeat last command every %d milliseconds", CNSL_REPEAT_MS);
  64. println(" help - print this message");
  65. println(" mount - make mass storage medium (un)available");
  66. //println(" foo - bar");
  67. println("Press Enter with no input to repeat last command.");
  68. println("Use repeat to continuously execute last command.");
  69. println("Stop this by calling repeat again.");
  70. } else if (strcmp(line, "reset") == 0) {
  71. reset_to_main();
  72. } else if (strcmp(line, "mount") == 0) {
  73. bool state = msc_is_medium_available();
  74. println("Currently %s. %s now.",
  75. state ? "mounted" : "unmounted",
  76. state ? "Unplugging" : "Plugging in");
  77. msc_set_medium_available(!state);
  78. } else {
  79. println("unknown command \"%s\"", line);
  80. }
  81. println();
  82. }
  83. void cnsl_init(void) {
  84. cnsl_buff_pos = 0;
  85. for (int i = 0; i < CNSL_BUFF_SIZE + 1; i++) {
  86. cnsl_line_buff[i] = '\0';
  87. cnsl_last_command[i] = '\0';
  88. cnsl_repeated_command[i] = '\0';
  89. }
  90. #ifdef CNSL_REPEAT_PMW_STATUS_BY_DEFAULT
  91. strcpy(cnsl_repeated_command, "pmws");
  92. repeat_command = true;
  93. #endif // CNSL_REPEAT_PMW_STATUS_BY_DEFAULT
  94. }
  95. static int32_t cnsl_find_line_end(void) {
  96. for (uint32_t i = 0; i < cnsl_buff_pos; i++) {
  97. if ((cnsl_line_buff[i] == '\r') || (cnsl_line_buff[i] == '\n')) {
  98. return i;
  99. }
  100. }
  101. return -1;
  102. }
  103. void cnsl_run(void) {
  104. if (repeat_command && (strlen(cnsl_repeated_command) > 0)
  105. && (strcmp(cnsl_repeated_command, "repeat") != 0)) {
  106. uint32_t now = to_ms_since_boot(get_absolute_time());
  107. if (now >= (last_repeat_time + CNSL_REPEAT_MS)) {
  108. println("repeating command \"%s\"", cnsl_repeated_command);
  109. cnsl_interpret(cnsl_repeated_command);
  110. println();
  111. last_repeat_time = now;
  112. }
  113. } else {
  114. if (repeat_command) {
  115. println("nothing to repeat");
  116. }
  117. repeat_command = false;
  118. }
  119. }
  120. void cnsl_handle_input(const char *buf, uint32_t len) {
  121. if ((cnsl_buff_pos + len) > CNSL_BUFF_SIZE) {
  122. debug("error: console input buffer overflow! %lu > %u", cnsl_buff_pos + len, CNSL_BUFF_SIZE);
  123. cnsl_init();
  124. }
  125. memcpy(cnsl_line_buff + cnsl_buff_pos, buf, len);
  126. cnsl_buff_pos += len;
  127. // handle backspace
  128. for (ssize_t i = cnsl_buff_pos - len; i < (ssize_t)cnsl_buff_pos; i++) {
  129. if ((cnsl_line_buff[i] == '\b') || (cnsl_line_buff[i] == 0x7F)) {
  130. if (i > 0) {
  131. // overwrite previous character and backspace
  132. for (ssize_t j = i; j < (ssize_t)cnsl_buff_pos - 1; j++) {
  133. cnsl_line_buff[j - 1] = cnsl_line_buff[j + 1];
  134. }
  135. cnsl_buff_pos -= 2;
  136. } else {
  137. // just remove the backspace
  138. for (ssize_t j = i; j < (ssize_t)cnsl_buff_pos - 1; j++) {
  139. cnsl_line_buff[j] = cnsl_line_buff[j + 1];
  140. }
  141. cnsl_buff_pos -= 1;
  142. }
  143. usb_cdc_write("\b \b", 3);
  144. // check for another backspace in this space
  145. i--;
  146. } else {
  147. usb_cdc_write(cnsl_line_buff + i, 1);
  148. }
  149. }
  150. int32_t line_len = cnsl_find_line_end();
  151. if (line_len < 0) {
  152. // user has not pressed enter yet
  153. return;
  154. }
  155. // convert line to C-style string
  156. cnsl_line_buff[line_len] = '\0';
  157. cnsl_interpret(cnsl_line_buff);
  158. // store command for eventual repeats
  159. strncpy(cnsl_last_command, cnsl_line_buff, CNSL_BUFF_SIZE + 1);
  160. // clear string and move following data over
  161. uint32_t cnt = line_len + 1;
  162. if (cnsl_line_buff[line_len + 1] == '\n') {
  163. cnt++;
  164. }
  165. memset(cnsl_line_buff, '\0', cnt);
  166. memmove(cnsl_line_buff, cnsl_line_buff + cnt, sizeof(cnsl_line_buff) - cnt);
  167. cnsl_buff_pos -= cnt;
  168. }