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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561
  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 <unistd.h>
  21. #include <stdio.h>
  22. #include "pico/stdlib.h"
  23. #include "hardware/watchdog.h"
  24. #include "config.h"
  25. #include "log.h"
  26. #include "util.h"
  27. #include "usb_cdc.h"
  28. #include "usb_msc.h"
  29. #include "debug_disk.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. #include "serial.h"
  37. #include "main.h"
  38. #include "models.h"
  39. #include "workflow.h"
  40. #include "crafty.h"
  41. #include "mem.h"
  42. #include "console.h"
  43. #define CNSL_BUFF_SIZE 64
  44. #define CNSL_REPEAT_MS 500
  45. #define DEV_AUTO_CONNECT(s) { \
  46. ble_scan(BLE_SCAN_OFF); \
  47. bd_addr_t addr; \
  48. bd_addr_type_t type; \
  49. const char *foo = s; \
  50. sscanf(foo, "%02hhX:%02hhX:%02hhX:%02hhX:%02hhX:%02hhX %hhu", \
  51. &addr[0], &addr[1], &addr[2], &addr[3], \
  52. &addr[4], &addr[5], &type); \
  53. ble_connect(addr, type); \
  54. while (!ble_is_connected()) { \
  55. sleep_ms(1); \
  56. } \
  57. }
  58. static char cnsl_line_buff[CNSL_BUFF_SIZE + 1];
  59. static uint32_t cnsl_buff_pos = 0;
  60. static char cnsl_last_command[CNSL_BUFF_SIZE + 1];
  61. static char cnsl_repeated_command[CNSL_BUFF_SIZE + 1];
  62. static bool repeat_command = false;
  63. static uint32_t last_repeat_time = 0;
  64. static void cnsl_interpret(const char *line) {
  65. if (strlen(line) == 0) {
  66. if ((strlen(cnsl_last_command) > 0) && (strcmp(cnsl_last_command, "repeat") != 0)) {
  67. // repeat last command once
  68. println("repeating command \"%s\"", cnsl_last_command);
  69. cnsl_interpret(cnsl_last_command);
  70. println();
  71. }
  72. return;
  73. } else if (strcmp(line, "repeat") == 0) {
  74. if (!repeat_command) {
  75. // mark last command to be repeated multiple times
  76. strncpy(cnsl_repeated_command, cnsl_last_command, CNSL_BUFF_SIZE + 1);
  77. last_repeat_time = to_ms_since_boot(get_absolute_time()) - 1001;
  78. repeat_command = true;
  79. } else {
  80. // stop repeating
  81. repeat_command = false;
  82. }
  83. } else if ((strcmp(line, "help") == 0)
  84. || (strcmp(line, "h") == 0)
  85. || (strcmp(line, "?") == 0)) {
  86. println("VolcanoRC Firmware Usage:");
  87. println("");
  88. println(" reset - reset back into this firmware");
  89. println(" \\x18 - reset to bootloader");
  90. println(" repeat - repeat last command every %d milliseconds", CNSL_REPEAT_MS);
  91. println(" help - print this message");
  92. println(" mount - make mass storage medium (un)available");
  93. println(" power - show Lipo battery status");
  94. println(" memr - reset flash memory config");
  95. println("");
  96. println(" scan - start or stop BLE scan");
  97. println("scanres - print list of found BLE devices");
  98. println("con M T - connect to (M)AC and (T)ype");
  99. println(" discon - disconnect from BLE device");
  100. println("");
  101. println(" clear - blank screen");
  102. println(" splash - draw image on screen");
  103. println(" fonts - show font list");
  104. println(" text - draw text on screen");
  105. println(" bat - draw battery indicator");
  106. println("");
  107. println(" vrct - Volcano read current temperature");
  108. println(" vrtt - Volcano read target temperature");
  109. println(" vwtt X - Volcano write target temperature");
  110. println(" vwh X - Set heater to 1 or 0");
  111. println(" vwp X - Set pump to 1 or 0");
  112. println("");
  113. println(" wfl - List available workflows");
  114. println(" wf X - Run workflow");
  115. println("");
  116. println(" crct - Crafty read current temperature");
  117. println(" crtt - Crafty read target temperature");
  118. println(" cwtt X - Crafty write target temperature");
  119. println(" cwh X - Set heater to 1 or 0");
  120. println(" crb - Crafty read battery state");
  121. println("");
  122. println("Press Enter with no input to repeat last command.");
  123. println("Use repeat to continuously execute last command.");
  124. println("Stop this by calling repeat again.");
  125. } else if (strcmp(line, "reset") == 0) {
  126. reset_to_main();
  127. } else if (strcmp(line, "mount") == 0) {
  128. bool state = msc_is_medium_available();
  129. println("Currently %s. %s now.",
  130. state ? "mounted" : "unmounted",
  131. state ? "Unplugging" : "Plugging in");
  132. if (state && msc_is_medium_locked()) {
  133. println("Warning: host has locked medium. Unmounting anyway.");
  134. }
  135. msc_set_medium_available(!state);
  136. } else if (strcmp(line, "power") == 0) {
  137. float volt = lipo_voltage();
  138. println("Battery: %.2fV = %.1f%% @ %s",
  139. volt, lipo_percentage(volt),
  140. lipo_charging() ? "charging" : "draining");
  141. } else if (strcmp(line, "memr") == 0) {
  142. mem_load_defaults();
  143. mem_write();
  144. } else if (strcmp(line, "scan") == 0) {
  145. ble_scan(BLE_SCAN_TOGGLE);
  146. } else if (strcmp(line, "scanres") == 0) {
  147. struct ble_scan_result results[BLE_MAX_SCAN_RESULTS] = {0};
  148. int n = ble_get_scan_results(results, BLE_MAX_SCAN_RESULTS);
  149. if (n < 0) {
  150. println("Error reading results (%d)", n);
  151. } else {
  152. println("%d results", n);
  153. for (int i = 0; i < n; i++) {
  154. char info[32] = "";
  155. enum known_devices dev = models_filter_name(results[i].name);
  156. if (dev != DEV_UNKNOWN) {
  157. models_get_serial(results[i].data, results[i].data_len,
  158. info, sizeof(info));
  159. }
  160. uint32_t age = to_ms_since_boot(get_absolute_time()) - results[i].time;
  161. println("addr=%s type=%d rssi=%d age=%.1fs name='%s' info='%s'",
  162. bd_addr_to_str(results[i].addr),
  163. results[i].type, results[i].rssi,
  164. age / 1000.0, results[i].name, info);
  165. }
  166. }
  167. } else if (str_startswith(line, "con ")) {
  168. bd_addr_t addr;
  169. bd_addr_type_t type;
  170. int r = sscanf(line, "con %02hhX:%02hhX:%02hhX:%02hhX:%02hhX:%02hhX %hhu",
  171. &addr[0], &addr[1], &addr[2], &addr[3],
  172. &addr[4], &addr[5], &type);
  173. if (r == 7) {
  174. debug("connecting");
  175. ble_connect(addr, type);
  176. } else {
  177. debug("invalid input (%d)", r);
  178. }
  179. } else if (strcmp(line, "discon") == 0) {
  180. ble_disconnect();
  181. } else if (strcmp(line, "clear") == 0) {
  182. lcd_clear();
  183. } else if (strcmp(line, "splash") == 0) {
  184. draw_splash();
  185. } else if (strcmp(line, "fonts") == 0) {
  186. const struct mf_font_list_s *f = mf_get_font_list();
  187. debug("Font list:");
  188. while (f) {
  189. debug("full_name: %s", f->font->full_name);
  190. debug("short_name: %s", f->font->short_name);
  191. debug("size: %d %d", f->font->width, f->font->height);
  192. debug("x_advance: %d %d", f->font->min_x_advance, f->font->max_x_advance);
  193. debug("baseline: %d %d", f->font->baseline_x, f->font->baseline_y);
  194. debug("line_height: %d", f->font->line_height);
  195. debug("flags: %d", f->font->flags);
  196. debug("fallback_character: %c", f->font->fallback_character);
  197. debug("character_width: %p", f->font->character_width);
  198. debug("render_character: %p", f->font->render_character);
  199. f = f->next;
  200. if (f) {
  201. debug("");
  202. }
  203. }
  204. } else if (strcmp(line, "text") == 0) {
  205. uint16_t y_off = 0;
  206. const struct mf_font_list_s *f = mf_get_font_list();
  207. while (f) {
  208. struct text_font font = {
  209. .fontname = f->font->short_name,
  210. //.scale = 1,
  211. .font = f->font,
  212. };
  213. text_prepare_font(&font);
  214. struct text_conf text = {
  215. .text = font.fontname,
  216. .x = 0,
  217. .y = y_off,
  218. .justify = false,
  219. .alignment = MF_ALIGN_CENTER,
  220. .width = 240,
  221. .height = 240 - y_off,
  222. .margin = 5,
  223. .fg = RGB_565(0xFF, 0xFF, 0xFF),
  224. .bg = TEXT_BG_NONE,
  225. .font = &font,
  226. };
  227. text_draw(&text);
  228. y_off = text.y;
  229. f = f->next;
  230. }
  231. } else if (strcmp(line, "bat") == 0) {
  232. draw_battery_indicator();
  233. } else if (strcmp(line, "vrct") == 0) {
  234. #ifdef TEST_VOLCANO_AUTO_CONNECT
  235. DEV_AUTO_CONNECT(TEST_VOLCANO_AUTO_CONNECT);
  236. #endif // TEST_VOLCANO_AUTO_CONNECT
  237. int16_t r = volcano_get_current_temp();
  238. println("volcano current temp: %.1f", r / 10.0);
  239. #ifdef TEST_VOLCANO_AUTO_CONNECT
  240. ble_disconnect();
  241. #endif // TEST_VOLCANO_AUTO_CONNECT
  242. } else if (strcmp(line, "vrtt") == 0) {
  243. #ifdef TEST_VOLCANO_AUTO_CONNECT
  244. DEV_AUTO_CONNECT(TEST_VOLCANO_AUTO_CONNECT);
  245. #endif // TEST_VOLCANO_AUTO_CONNECT
  246. int16_t r = volcano_get_target_temp();
  247. println("volcano target temp: %.1f", r / 10.0);
  248. #ifdef TEST_VOLCANO_AUTO_CONNECT
  249. ble_disconnect();
  250. #endif // TEST_VOLCANO_AUTO_CONNECT
  251. } else if (str_startswith(line, "vwtt ")) {
  252. float val;
  253. int r = sscanf(line, "vwtt %f", &val);
  254. if (r != 1) {
  255. println("invalid input (%d)", r);
  256. } else {
  257. uint16_t v = val * 10.0;
  258. #ifdef TEST_VOLCANO_AUTO_CONNECT
  259. DEV_AUTO_CONNECT(TEST_VOLCANO_AUTO_CONNECT);
  260. #endif // TEST_VOLCANO_AUTO_CONNECT
  261. int8_t r = volcano_set_target_temp(v);
  262. #ifdef TEST_VOLCANO_AUTO_CONNECT
  263. ble_disconnect();
  264. #endif // TEST_VOLCANO_AUTO_CONNECT
  265. if (r < 0) {
  266. println("error writing target temp %d", r);
  267. } else {
  268. println("success");
  269. }
  270. }
  271. } else if (str_startswith(line, "vwh ")) {
  272. int val;
  273. int r = sscanf(line, "vwh %d", &val);
  274. if ((r != 1) || ((val != 0) && (val != 1))) {
  275. println("invalid input (%d %d)", r, val);
  276. } else {
  277. #ifdef TEST_VOLCANO_AUTO_CONNECT
  278. DEV_AUTO_CONNECT(TEST_VOLCANO_AUTO_CONNECT);
  279. #endif // TEST_VOLCANO_AUTO_CONNECT
  280. int8_t r = volcano_set_heater_state(val == 1);
  281. #ifdef TEST_VOLCANO_AUTO_CONNECT
  282. ble_disconnect();
  283. #endif // TEST_VOLCANO_AUTO_CONNECT
  284. if (r < 0) {
  285. println("error writing heater state %d", r);
  286. } else {
  287. println("success");
  288. }
  289. }
  290. } else if (str_startswith(line, "vwp ")) {
  291. int val;
  292. int r = sscanf(line, "vwp %d", &val);
  293. if ((r != 1) || ((val != 0) && (val != 1))) {
  294. println("invalid input (%d %d)", r, val);
  295. } else {
  296. #ifdef TEST_VOLCANO_AUTO_CONNECT
  297. DEV_AUTO_CONNECT(TEST_VOLCANO_AUTO_CONNECT);
  298. #endif // TEST_VOLCANO_AUTO_CONNECT
  299. int8_t r = volcano_set_pump_state(val == 1);
  300. #ifdef TEST_VOLCANO_AUTO_CONNECT
  301. ble_disconnect();
  302. #endif // TEST_VOLCANO_AUTO_CONNECT
  303. if (r < 0) {
  304. println("error writing pump state %d", r);
  305. } else {
  306. println("success");
  307. }
  308. }
  309. } else if (strcmp(line, "wfl") == 0) {
  310. println("%d workflows", wf_count());
  311. for (int i = 0; i < wf_count(); i++) {
  312. println(" '%s' by %s", wf_name(i), wf_author(i));
  313. }
  314. } else if (str_startswith(line, "wf ")) {
  315. int wf = -1;
  316. for (int i = 0; i < wf_count(); i++) {
  317. if (strcmp(wf_name(i), line + 3) == 0) {
  318. wf = i;
  319. break;
  320. }
  321. }
  322. if (wf < 0) {
  323. println("unknown workflow");
  324. } else {
  325. struct wf_state s = wf_status();
  326. if (s.status != WF_IDLE) {
  327. println("workflow in progress");
  328. } else {
  329. #ifdef TEST_VOLCANO_AUTO_CONNECT
  330. DEV_AUTO_CONNECT(TEST_VOLCANO_AUTO_CONNECT);
  331. #endif // TEST_VOLCANO_AUTO_CONNECT
  332. println("starting workflow");
  333. wf_start(wf);
  334. s = wf_status();
  335. while (s.status != WF_IDLE) {
  336. main_loop_hw();
  337. wf_run();
  338. s = wf_status();
  339. }
  340. println("done");
  341. #ifdef TEST_VOLCANO_AUTO_CONNECT
  342. ble_disconnect();
  343. #endif // TEST_VOLCANO_AUTO_CONNECT
  344. }
  345. }
  346. } else if (strcmp(line, "crct") == 0) {
  347. #ifdef TEST_CRAFTY_AUTO_CONNECT
  348. DEV_AUTO_CONNECT(TEST_CRAFTY_AUTO_CONNECT);
  349. #endif // TEST_CRAFTY_AUTO_CONNECT
  350. int16_t r = crafty_get_current_temp();
  351. println("crafty current temp: %.1f", r / 10.0);
  352. #ifdef TEST_CRAFTY_AUTO_CONNECT
  353. ble_disconnect();
  354. #endif // TEST_CRAFTY_AUTO_CONNECT
  355. } else if (strcmp(line, "crtt") == 0) {
  356. #ifdef TEST_CRAFTY_AUTO_CONNECT
  357. DEV_AUTO_CONNECT(TEST_CRAFTY_AUTO_CONNECT);
  358. #endif // TEST_CRAFTY_AUTO_CONNECT
  359. int16_t r = crafty_get_target_temp();
  360. println("crafty target temp: %.1f", r / 10.0);
  361. #ifdef TEST_CRAFTY_AUTO_CONNECT
  362. ble_disconnect();
  363. #endif // TEST_CRAFTY_AUTO_CONNECT
  364. } else if (str_startswith(line, "cwtt ")) {
  365. float val;
  366. int r = sscanf(line, "cwtt %f", &val);
  367. if (r != 1) {
  368. println("invalid input (%d)", r);
  369. } else {
  370. uint16_t v = val * 10.0;
  371. #ifdef TEST_CRAFTY_AUTO_CONNECT
  372. DEV_AUTO_CONNECT(TEST_CRAFTY_AUTO_CONNECT);
  373. #endif // TEST_CRAFTY_AUTO_CONNECT
  374. int8_t r = crafty_set_target_temp(v);
  375. #ifdef TEST_CRAFTY_AUTO_CONNECT
  376. ble_disconnect();
  377. #endif // TEST_CRAFTY_AUTO_CONNECT
  378. if (r < 0) {
  379. println("error writing target temp %d", r);
  380. } else {
  381. println("success");
  382. }
  383. }
  384. } else if (str_startswith(line, "cwh ")) {
  385. int val;
  386. int r = sscanf(line, "cwh %d", &val);
  387. if ((r != 1) || ((val != 0) && (val != 1))) {
  388. println("invalid input (%d %d)", r, val);
  389. } else {
  390. #ifdef TEST_CRAFTY_AUTO_CONNECT
  391. DEV_AUTO_CONNECT(TEST_CRAFTY_AUTO_CONNECT);
  392. #endif // TEST_CRAFTY_AUTO_CONNECT
  393. int8_t r = crafty_set_heater_state(val == 1);
  394. #ifdef TEST_CRAFTY_AUTO_CONNECT
  395. ble_disconnect();
  396. #endif // TEST_CRAFTY_AUTO_CONNECT
  397. if (r < 0) {
  398. println("error writing heater state %d", r);
  399. } else {
  400. println("success");
  401. }
  402. }
  403. } else if (strcmp(line, "crb") == 0) {
  404. #ifdef TEST_CRAFTY_AUTO_CONNECT
  405. DEV_AUTO_CONNECT(TEST_CRAFTY_AUTO_CONNECT);
  406. #endif // TEST_CRAFTY_AUTO_CONNECT
  407. int16_t r = crafty_get_battery_state();
  408. println("crafty battery: %d %%", r);
  409. #ifdef TEST_CRAFTY_AUTO_CONNECT
  410. ble_disconnect();
  411. #endif // TEST_CRAFTY_AUTO_CONNECT
  412. } else {
  413. println("unknown command \"%s\"", line);
  414. }
  415. println();
  416. }
  417. void cnsl_init(void) {
  418. cnsl_buff_pos = 0;
  419. for (int i = 0; i < CNSL_BUFF_SIZE + 1; i++) {
  420. cnsl_line_buff[i] = '\0';
  421. cnsl_last_command[i] = '\0';
  422. cnsl_repeated_command[i] = '\0';
  423. }
  424. }
  425. static int32_t cnsl_find_line_end(void) {
  426. for (uint32_t i = 0; i < cnsl_buff_pos; i++) {
  427. if ((cnsl_line_buff[i] == '\r') || (cnsl_line_buff[i] == '\n')) {
  428. return i;
  429. }
  430. }
  431. return -1;
  432. }
  433. void cnsl_run(void) {
  434. if (repeat_command && (strlen(cnsl_repeated_command) > 0)
  435. && (strcmp(cnsl_repeated_command, "repeat") != 0)) {
  436. uint32_t now = to_ms_since_boot(get_absolute_time());
  437. if (now >= (last_repeat_time + CNSL_REPEAT_MS)) {
  438. println("repeating command \"%s\"", cnsl_repeated_command);
  439. cnsl_interpret(cnsl_repeated_command);
  440. println();
  441. last_repeat_time = now;
  442. }
  443. } else {
  444. if (repeat_command) {
  445. println("nothing to repeat");
  446. }
  447. repeat_command = false;
  448. }
  449. }
  450. void cnsl_handle_input(const uint8_t *buf, size_t len) {
  451. if ((cnsl_buff_pos + len) > CNSL_BUFF_SIZE) {
  452. debug("error: console input buffer overflow! %lu > %u", cnsl_buff_pos + len, CNSL_BUFF_SIZE);
  453. cnsl_init();
  454. }
  455. memcpy(cnsl_line_buff + cnsl_buff_pos, buf, len);
  456. cnsl_buff_pos += len;
  457. // handle backspace and local echo
  458. for (ssize_t i = cnsl_buff_pos - len; i < (ssize_t)cnsl_buff_pos; i++) {
  459. if ((cnsl_line_buff[i] == '\b') || (cnsl_line_buff[i] == 0x7F)) {
  460. if (i > 0) {
  461. // overwrite previous character and backspace
  462. for (ssize_t j = i; j < (ssize_t)cnsl_buff_pos - 1; j++) {
  463. cnsl_line_buff[j - 1] = cnsl_line_buff[j + 1];
  464. }
  465. cnsl_buff_pos -= 2;
  466. } else {
  467. // just remove the backspace
  468. for (ssize_t j = i; j < (ssize_t)cnsl_buff_pos - 1; j++) {
  469. cnsl_line_buff[j] = cnsl_line_buff[j + 1];
  470. }
  471. cnsl_buff_pos -= 1;
  472. }
  473. usb_cdc_write((const uint8_t *)"\b \b", 3);
  474. serial_write((const uint8_t *)"\b \b", 3);
  475. // check for another backspace in this space
  476. i--;
  477. } else {
  478. usb_cdc_write((const uint8_t *)(cnsl_line_buff + i), 1);
  479. serial_write((const uint8_t *)(cnsl_line_buff + i), 1);
  480. }
  481. }
  482. int32_t line_len = cnsl_find_line_end();
  483. if (line_len < 0) {
  484. // user has not pressed enter yet
  485. return;
  486. }
  487. // convert line to C-style string
  488. cnsl_line_buff[line_len] = '\0';
  489. cnsl_interpret(cnsl_line_buff);
  490. // store command for eventual repeats
  491. strncpy(cnsl_last_command, cnsl_line_buff, CNSL_BUFF_SIZE + 1);
  492. // clear string and move following data over
  493. uint32_t cnt = line_len + 1;
  494. if (cnsl_line_buff[line_len + 1] == '\n') {
  495. cnt++;
  496. }
  497. memset(cnsl_line_buff, '\0', cnt);
  498. memmove(cnsl_line_buff, cnsl_line_buff + cnt, sizeof(cnsl_line_buff) - cnt);
  499. cnsl_buff_pos -= cnt;
  500. }