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

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