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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  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 <stdio.h>
  23. #include "config.h"
  24. #include "log.h"
  25. #include "util.h"
  26. #include "usb_cdc.h"
  27. #include "usb_msc.h"
  28. #include "debug.h"
  29. #include "console.h"
  30. #include "lipo.h"
  31. #include "ble.h"
  32. #include "text.h"
  33. #include "lcd.h"
  34. #include "image.h"
  35. #include "volcano.h"
  36. #define CNSL_BUFF_SIZE 1024
  37. #define CNSL_REPEAT_MS 500
  38. //#define TEST_VOLCANO_AUTO_CONNECT "xx:xx:xx:xx:xx:xx 1"
  39. #define VOLCANO_AUTO_CONNECT { \
  40. ble_scan(BLE_SCAN_OFF); \
  41. bd_addr_t addr; \
  42. bd_addr_type_t type; \
  43. const char *foo = TEST_VOLCANO_AUTO_CONNECT; \
  44. sscanf(foo, "%02hhX:%02hhX:%02hhX:%02hhX:%02hhX:%02hhX %hhu", \
  45. &addr[0], &addr[1], &addr[2], &addr[3], \
  46. &addr[4], &addr[5], &type); \
  47. ble_connect(addr, type); \
  48. while (!ble_is_connected()) { \
  49. sleep_ms(1); \
  50. } \
  51. }
  52. static char cnsl_line_buff[CNSL_BUFF_SIZE + 1];
  53. static uint32_t cnsl_buff_pos = 0;
  54. static char cnsl_last_command[CNSL_BUFF_SIZE + 1];
  55. static char cnsl_repeated_command[CNSL_BUFF_SIZE + 1];
  56. static bool repeat_command = false;
  57. static uint32_t last_repeat_time = 0;
  58. static void cnsl_interpret(const char *line) {
  59. if (strlen(line) == 0) {
  60. if ((strlen(cnsl_last_command) > 0) && (strcmp(cnsl_last_command, "repeat") != 0)) {
  61. // repeat last command once
  62. println("repeating command \"%s\"", cnsl_last_command);
  63. cnsl_interpret(cnsl_last_command);
  64. println();
  65. }
  66. return;
  67. } else if (strcmp(line, "repeat") == 0) {
  68. if (!repeat_command) {
  69. // mark last command to be repeated multiple times
  70. strncpy(cnsl_repeated_command, cnsl_last_command, CNSL_BUFF_SIZE + 1);
  71. last_repeat_time = to_ms_since_boot(get_absolute_time()) - 1001;
  72. repeat_command = true;
  73. } else {
  74. // stop repeating
  75. repeat_command = false;
  76. }
  77. } else if ((strcmp(line, "help") == 0)
  78. || (strcmp(line, "h") == 0)
  79. || (strcmp(line, "?") == 0)) {
  80. println("VolcanoRC Firmware Usage:");
  81. println("");
  82. println(" reset - reset back into this firmware");
  83. println(" \\x18 - reset to bootloader");
  84. println(" repeat - repeat last command every %d milliseconds", CNSL_REPEAT_MS);
  85. println(" help - print this message");
  86. println(" mount - make mass storage medium (un)available");
  87. println(" power - show Lipo battery status");
  88. println("");
  89. println(" scan - start or stop BLE scan");
  90. println("scanres - print list of found BLE devices");
  91. println("con M T - connect to (M)AC and (T)ype");
  92. println(" discon - disconnect from BLE device");
  93. println("");
  94. println(" clear - blank screen");
  95. println(" splash - draw image on screen");
  96. println(" fonts - show font list");
  97. println(" text - draw text on screen");
  98. println(" bat - draw battery indicator");
  99. println("");
  100. println(" vrct - Volcano read current temperature");
  101. println(" vrtt - Volcano read target temperature");
  102. println("");
  103. println("Press Enter with no input to repeat last command.");
  104. println("Use repeat to continuously execute last command.");
  105. println("Stop this by calling repeat again.");
  106. } else if (strcmp(line, "reset") == 0) {
  107. reset_to_main();
  108. } else if (strcmp(line, "mount") == 0) {
  109. bool state = msc_is_medium_available();
  110. println("Currently %s. %s now.",
  111. state ? "mounted" : "unmounted",
  112. state ? "Unplugging" : "Plugging in");
  113. msc_set_medium_available(!state);
  114. } else if (strcmp(line, "power") == 0) {
  115. float volt = lipo_voltage();
  116. println("Battery: %.2fV = %.1f%% @ %s",
  117. volt, lipo_percentage(volt),
  118. lipo_charging() ? "charging" : "draining");
  119. } else if (strcmp(line, "scan") == 0) {
  120. ble_scan(BLE_SCAN_TOGGLE);
  121. } else if (strcmp(line, "scanres") == 0) {
  122. struct ble_scan_result results[BLE_MAX_SCAN_RESULTS] = {0};
  123. int n = ble_get_scan_results(results, BLE_MAX_SCAN_RESULTS);
  124. if (n < 0) {
  125. println("Error reading results (%d)", n);
  126. } else {
  127. println("%d results", n);
  128. for (int i = 0; i < n; i++) {
  129. uint32_t age = to_ms_since_boot(get_absolute_time()) - results[i].time;
  130. println("addr=%s type=%d rssi=%d age=%.1fs name='%s'",
  131. bd_addr_to_str(results[i].addr),
  132. results[i].type, results[i].rssi,
  133. age / 1000.0, results[i].name);
  134. }
  135. }
  136. } else if (str_startswith(line, "con ")) {
  137. bd_addr_t addr;
  138. bd_addr_type_t type;
  139. int r = sscanf(line, "con %02hhX:%02hhX:%02hhX:%02hhX:%02hhX:%02hhX %hhu",
  140. &addr[0], &addr[1], &addr[2], &addr[3],
  141. &addr[4], &addr[5], &type);
  142. if (r == 7) {
  143. debug("connecting");
  144. ble_connect(addr, type);
  145. } else {
  146. debug("invalid input (%d)", r);
  147. }
  148. } else if (strcmp(line, "discon") == 0) {
  149. ble_disconnect();
  150. } else if (strcmp(line, "clear") == 0) {
  151. lcd_clear();
  152. } else if (strcmp(line, "splash") == 0) {
  153. draw_splash();
  154. } else if (strcmp(line, "fonts") == 0) {
  155. const struct mf_font_list_s *f = mf_get_font_list();
  156. debug("Font list:");
  157. while (f) {
  158. debug("full_name: %s", f->font->full_name);
  159. debug("short_name: %s", f->font->short_name);
  160. debug("size: %d %d", f->font->width, f->font->height);
  161. debug("x_advance: %d %d", f->font->min_x_advance, f->font->max_x_advance);
  162. debug("baseline: %d %d", f->font->baseline_x, f->font->baseline_y);
  163. debug("line_height: %d", f->font->line_height);
  164. debug("flags: %d", f->font->flags);
  165. debug("fallback_character: %c", f->font->fallback_character);
  166. debug("character_width: %p", f->font->character_width);
  167. debug("render_character: %p", f->font->render_character);
  168. f = f->next;
  169. if (f) {
  170. debug("");
  171. }
  172. }
  173. } else if (strcmp(line, "text") == 0) {
  174. uint16_t y_off = 0;
  175. const struct mf_font_list_s *f = mf_get_font_list();
  176. while (f) {
  177. struct text_font font = {
  178. .fontname = f->font->short_name,
  179. //.scale = 1,
  180. .font = f->font,
  181. };
  182. text_prepare_font(&font);
  183. struct text_conf text = {
  184. .text = font.fontname,
  185. .x = 0,
  186. .y = y_off,
  187. .justify = false,
  188. .alignment = MF_ALIGN_CENTER,
  189. .width = 240,
  190. .height = 240 - y_off,
  191. .margin = 5,
  192. .bg = TEXT_BG_NONE,
  193. .font = &font,
  194. };
  195. text_draw(&text);
  196. y_off = text.y;
  197. f = f->next;
  198. }
  199. } else if (strcmp(line, "bat") == 0) {
  200. draw_battery_indicator();
  201. } else if (strcmp(line, "vrct") == 0) {
  202. #ifdef TEST_VOLCANO_AUTO_CONNECT
  203. VOLCANO_AUTO_CONNECT
  204. #endif // TEST_VOLCANO_AUTO_CONNECT
  205. int16_t r = volcano_get_current_temp();
  206. println("volcano current temp: %.1f", r / 10.0);
  207. #ifdef TEST_VOLCANO_AUTO_CONNECT
  208. ble_disconnect();
  209. #endif // TEST_VOLCANO_AUTO_CONNECT
  210. } else if (strcmp(line, "vrtt") == 0) {
  211. #ifdef TEST_VOLCANO_AUTO_CONNECT
  212. VOLCANO_AUTO_CONNECT
  213. #endif // TEST_VOLCANO_AUTO_CONNECT
  214. int16_t r = volcano_get_target_temp();
  215. println("volcano target temp: %.1f", r / 10.0);
  216. #ifdef TEST_VOLCANO_AUTO_CONNECT
  217. ble_disconnect();
  218. #endif // TEST_VOLCANO_AUTO_CONNECT
  219. } else {
  220. println("unknown command \"%s\"", line);
  221. }
  222. println();
  223. }
  224. void cnsl_init(void) {
  225. cnsl_buff_pos = 0;
  226. for (int i = 0; i < CNSL_BUFF_SIZE + 1; i++) {
  227. cnsl_line_buff[i] = '\0';
  228. cnsl_last_command[i] = '\0';
  229. cnsl_repeated_command[i] = '\0';
  230. }
  231. }
  232. static int32_t cnsl_find_line_end(void) {
  233. for (uint32_t i = 0; i < cnsl_buff_pos; i++) {
  234. if ((cnsl_line_buff[i] == '\r') || (cnsl_line_buff[i] == '\n')) {
  235. return i;
  236. }
  237. }
  238. return -1;
  239. }
  240. void cnsl_run(void) {
  241. if (repeat_command && (strlen(cnsl_repeated_command) > 0)
  242. && (strcmp(cnsl_repeated_command, "repeat") != 0)) {
  243. uint32_t now = to_ms_since_boot(get_absolute_time());
  244. if (now >= (last_repeat_time + CNSL_REPEAT_MS)) {
  245. println("repeating command \"%s\"", cnsl_repeated_command);
  246. cnsl_interpret(cnsl_repeated_command);
  247. println();
  248. last_repeat_time = now;
  249. }
  250. } else {
  251. if (repeat_command) {
  252. println("nothing to repeat");
  253. }
  254. repeat_command = false;
  255. }
  256. }
  257. void cnsl_handle_input(const char *buf, uint32_t len) {
  258. if ((cnsl_buff_pos + len) > CNSL_BUFF_SIZE) {
  259. debug("error: console input buffer overflow! %lu > %u", cnsl_buff_pos + len, CNSL_BUFF_SIZE);
  260. cnsl_init();
  261. }
  262. memcpy(cnsl_line_buff + cnsl_buff_pos, buf, len);
  263. cnsl_buff_pos += len;
  264. // handle backspace
  265. for (ssize_t i = cnsl_buff_pos - len; i < (ssize_t)cnsl_buff_pos; i++) {
  266. if ((cnsl_line_buff[i] == '\b') || (cnsl_line_buff[i] == 0x7F)) {
  267. if (i > 0) {
  268. // overwrite previous character and backspace
  269. for (ssize_t j = i; j < (ssize_t)cnsl_buff_pos - 1; j++) {
  270. cnsl_line_buff[j - 1] = cnsl_line_buff[j + 1];
  271. }
  272. cnsl_buff_pos -= 2;
  273. } else {
  274. // just remove the backspace
  275. for (ssize_t j = i; j < (ssize_t)cnsl_buff_pos - 1; j++) {
  276. cnsl_line_buff[j] = cnsl_line_buff[j + 1];
  277. }
  278. cnsl_buff_pos -= 1;
  279. }
  280. usb_cdc_write("\b \b", 3);
  281. // check for another backspace in this space
  282. i--;
  283. } else {
  284. usb_cdc_write(cnsl_line_buff + i, 1);
  285. }
  286. }
  287. int32_t line_len = cnsl_find_line_end();
  288. if (line_len < 0) {
  289. // user has not pressed enter yet
  290. return;
  291. }
  292. // convert line to C-style string
  293. cnsl_line_buff[line_len] = '\0';
  294. cnsl_interpret(cnsl_line_buff);
  295. // store command for eventual repeats
  296. strncpy(cnsl_last_command, cnsl_line_buff, CNSL_BUFF_SIZE + 1);
  297. // clear string and move following data over
  298. uint32_t cnt = line_len + 1;
  299. if (cnsl_line_buff[line_len + 1] == '\n') {
  300. cnt++;
  301. }
  302. memset(cnsl_line_buff, '\0', cnt);
  303. memmove(cnsl_line_buff, cnsl_line_buff + cnt, sizeof(cnsl_line_buff) - cnt);
  304. cnsl_buff_pos -= cnt;
  305. }