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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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. #include "lipo.h"
  30. #include "ble.h"
  31. #define CNSL_BUFF_SIZE 1024
  32. #define CNSL_REPEAT_MS 500
  33. //#define CNSL_REPEAT_PMW_STATUS_BY_DEFAULT
  34. static char cnsl_line_buff[CNSL_BUFF_SIZE + 1];
  35. static uint32_t cnsl_buff_pos = 0;
  36. static char cnsl_last_command[CNSL_BUFF_SIZE + 1];
  37. static char cnsl_repeated_command[CNSL_BUFF_SIZE + 1];
  38. static bool repeat_command = false;
  39. static uint32_t last_repeat_time = 0;
  40. static void cnsl_interpret(const char *line) {
  41. if (strlen(line) == 0) {
  42. if ((strlen(cnsl_last_command) > 0) && (strcmp(cnsl_last_command, "repeat") != 0)) {
  43. // repeat last command once
  44. println("repeating command \"%s\"", cnsl_last_command);
  45. cnsl_interpret(cnsl_last_command);
  46. println();
  47. }
  48. return;
  49. } else if (strcmp(line, "repeat") == 0) {
  50. if (!repeat_command) {
  51. // mark last command to be repeated multiple times
  52. strncpy(cnsl_repeated_command, cnsl_last_command, CNSL_BUFF_SIZE + 1);
  53. last_repeat_time = to_ms_since_boot(get_absolute_time()) - 1001;
  54. repeat_command = true;
  55. } else {
  56. // stop repeating
  57. repeat_command = false;
  58. }
  59. } else if ((strcmp(line, "help") == 0)
  60. || (strcmp(line, "h") == 0)
  61. || (strcmp(line, "?") == 0)) {
  62. println("VolcanoRC Firmware Usage:");
  63. println(" reset - reset back into this firmware");
  64. println(" \\x18 - reset to bootloader");
  65. println(" repeat - repeat last command every %d milliseconds", CNSL_REPEAT_MS);
  66. println(" help - print this message");
  67. println(" mount - make mass storage medium (un)available");
  68. println(" power - show Lipo battery status");
  69. println(" scan - start or stop BLE scan");
  70. println("Press Enter with no input to repeat last command.");
  71. println("Use repeat to continuously execute last command.");
  72. println("Stop this by calling repeat again.");
  73. } else if (strcmp(line, "reset") == 0) {
  74. reset_to_main();
  75. } else if (strcmp(line, "mount") == 0) {
  76. bool state = msc_is_medium_available();
  77. println("Currently %s. %s now.",
  78. state ? "mounted" : "unmounted",
  79. state ? "Unplugging" : "Plugging in");
  80. msc_set_medium_available(!state);
  81. } else if (strcmp(line, "power") == 0) {
  82. float volt = lipo_voltage();
  83. println("Battery: %.2fV = %.1f%% @ %s",
  84. volt, lipo_percentage(volt),
  85. lipo_charging() ? "charging" : "draining");
  86. } else if (strcmp(line, "scan") == 0) {
  87. ble_scan(2);
  88. } else {
  89. println("unknown command \"%s\"", line);
  90. }
  91. println();
  92. }
  93. void cnsl_init(void) {
  94. cnsl_buff_pos = 0;
  95. for (int i = 0; i < CNSL_BUFF_SIZE + 1; i++) {
  96. cnsl_line_buff[i] = '\0';
  97. cnsl_last_command[i] = '\0';
  98. cnsl_repeated_command[i] = '\0';
  99. }
  100. #ifdef CNSL_REPEAT_PMW_STATUS_BY_DEFAULT
  101. strcpy(cnsl_repeated_command, "pmws");
  102. repeat_command = true;
  103. #endif // CNSL_REPEAT_PMW_STATUS_BY_DEFAULT
  104. }
  105. static int32_t cnsl_find_line_end(void) {
  106. for (uint32_t i = 0; i < cnsl_buff_pos; i++) {
  107. if ((cnsl_line_buff[i] == '\r') || (cnsl_line_buff[i] == '\n')) {
  108. return i;
  109. }
  110. }
  111. return -1;
  112. }
  113. void cnsl_run(void) {
  114. if (repeat_command && (strlen(cnsl_repeated_command) > 0)
  115. && (strcmp(cnsl_repeated_command, "repeat") != 0)) {
  116. uint32_t now = to_ms_since_boot(get_absolute_time());
  117. if (now >= (last_repeat_time + CNSL_REPEAT_MS)) {
  118. println("repeating command \"%s\"", cnsl_repeated_command);
  119. cnsl_interpret(cnsl_repeated_command);
  120. println();
  121. last_repeat_time = now;
  122. }
  123. } else {
  124. if (repeat_command) {
  125. println("nothing to repeat");
  126. }
  127. repeat_command = false;
  128. }
  129. }
  130. void cnsl_handle_input(const char *buf, uint32_t len) {
  131. if ((cnsl_buff_pos + len) > CNSL_BUFF_SIZE) {
  132. debug("error: console input buffer overflow! %lu > %u", cnsl_buff_pos + len, CNSL_BUFF_SIZE);
  133. cnsl_init();
  134. }
  135. memcpy(cnsl_line_buff + cnsl_buff_pos, buf, len);
  136. cnsl_buff_pos += len;
  137. // handle backspace
  138. for (ssize_t i = cnsl_buff_pos - len; i < (ssize_t)cnsl_buff_pos; i++) {
  139. if ((cnsl_line_buff[i] == '\b') || (cnsl_line_buff[i] == 0x7F)) {
  140. if (i > 0) {
  141. // overwrite previous character and backspace
  142. for (ssize_t j = i; j < (ssize_t)cnsl_buff_pos - 1; j++) {
  143. cnsl_line_buff[j - 1] = cnsl_line_buff[j + 1];
  144. }
  145. cnsl_buff_pos -= 2;
  146. } else {
  147. // just remove the backspace
  148. for (ssize_t j = i; j < (ssize_t)cnsl_buff_pos - 1; j++) {
  149. cnsl_line_buff[j] = cnsl_line_buff[j + 1];
  150. }
  151. cnsl_buff_pos -= 1;
  152. }
  153. usb_cdc_write("\b \b", 3);
  154. // check for another backspace in this space
  155. i--;
  156. } else {
  157. usb_cdc_write(cnsl_line_buff + i, 1);
  158. }
  159. }
  160. int32_t line_len = cnsl_find_line_end();
  161. if (line_len < 0) {
  162. // user has not pressed enter yet
  163. return;
  164. }
  165. // convert line to C-style string
  166. cnsl_line_buff[line_len] = '\0';
  167. cnsl_interpret(cnsl_line_buff);
  168. // store command for eventual repeats
  169. strncpy(cnsl_last_command, cnsl_line_buff, CNSL_BUFF_SIZE + 1);
  170. // clear string and move following data over
  171. uint32_t cnt = line_len + 1;
  172. if (cnsl_line_buff[line_len + 1] == '\n') {
  173. cnt++;
  174. }
  175. memset(cnsl_line_buff, '\0', cnt);
  176. memmove(cnsl_line_buff, cnsl_line_buff + cnt, sizeof(cnsl_line_buff) - cnt);
  177. cnsl_buff_pos -= cnt;
  178. }