S&B Volcano vaporizer remote control with Pi Pico W
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552
  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. msc_set_medium_available(!state);
  131. } else if (strcmp(line, "power") == 0) {
  132. float volt = lipo_voltage();
  133. println("Battery: %.2fV = %.1f%% @ %s",
  134. volt, lipo_percentage(volt),
  135. lipo_charging() ? "charging" : "draining");
  136. } else if (strcmp(line, "scan") == 0) {
  137. ble_scan(BLE_SCAN_TOGGLE);
  138. } else if (strcmp(line, "scanres") == 0) {
  139. struct ble_scan_result results[BLE_MAX_SCAN_RESULTS] = {0};
  140. int n = ble_get_scan_results(results, BLE_MAX_SCAN_RESULTS);
  141. if (n < 0) {
  142. println("Error reading results (%d)", n);
  143. } else {
  144. println("%d results", n);
  145. for (int i = 0; i < n; i++) {
  146. char info[32] = "";
  147. enum known_devices dev = models_filter_name(results[i].name);
  148. if (dev != DEV_UNKNOWN) {
  149. models_get_serial(results[i].data, results[i].data_len,
  150. info, sizeof(info));
  151. }
  152. uint32_t age = to_ms_since_boot(get_absolute_time()) - results[i].time;
  153. println("addr=%s type=%d rssi=%d age=%.1fs name='%s' info='%s'",
  154. bd_addr_to_str(results[i].addr),
  155. results[i].type, results[i].rssi,
  156. age / 1000.0, results[i].name, info);
  157. }
  158. }
  159. } else if (str_startswith(line, "con ")) {
  160. bd_addr_t addr;
  161. bd_addr_type_t type;
  162. int r = sscanf(line, "con %02hhX:%02hhX:%02hhX:%02hhX:%02hhX:%02hhX %hhu",
  163. &addr[0], &addr[1], &addr[2], &addr[3],
  164. &addr[4], &addr[5], &type);
  165. if (r == 7) {
  166. debug("connecting");
  167. ble_connect(addr, type);
  168. } else {
  169. debug("invalid input (%d)", r);
  170. }
  171. } else if (strcmp(line, "discon") == 0) {
  172. ble_disconnect();
  173. } else if (strcmp(line, "clear") == 0) {
  174. lcd_clear();
  175. } else if (strcmp(line, "splash") == 0) {
  176. draw_splash();
  177. } else if (strcmp(line, "fonts") == 0) {
  178. const struct mf_font_list_s *f = mf_get_font_list();
  179. debug("Font list:");
  180. while (f) {
  181. debug("full_name: %s", f->font->full_name);
  182. debug("short_name: %s", f->font->short_name);
  183. debug("size: %d %d", f->font->width, f->font->height);
  184. debug("x_advance: %d %d", f->font->min_x_advance, f->font->max_x_advance);
  185. debug("baseline: %d %d", f->font->baseline_x, f->font->baseline_y);
  186. debug("line_height: %d", f->font->line_height);
  187. debug("flags: %d", f->font->flags);
  188. debug("fallback_character: %c", f->font->fallback_character);
  189. debug("character_width: %p", f->font->character_width);
  190. debug("render_character: %p", f->font->render_character);
  191. f = f->next;
  192. if (f) {
  193. debug("");
  194. }
  195. }
  196. } else if (strcmp(line, "text") == 0) {
  197. uint16_t y_off = 0;
  198. const struct mf_font_list_s *f = mf_get_font_list();
  199. while (f) {
  200. struct text_font font = {
  201. .fontname = f->font->short_name,
  202. //.scale = 1,
  203. .font = f->font,
  204. };
  205. text_prepare_font(&font);
  206. struct text_conf text = {
  207. .text = font.fontname,
  208. .x = 0,
  209. .y = y_off,
  210. .justify = false,
  211. .alignment = MF_ALIGN_CENTER,
  212. .width = 240,
  213. .height = 240 - y_off,
  214. .margin = 5,
  215. .bg = TEXT_BG_NONE,
  216. .font = &font,
  217. };
  218. text_draw(&text);
  219. y_off = text.y;
  220. f = f->next;
  221. }
  222. } else if (strcmp(line, "bat") == 0) {
  223. draw_battery_indicator();
  224. } else if (strcmp(line, "vrct") == 0) {
  225. #ifdef TEST_VOLCANO_AUTO_CONNECT
  226. DEV_AUTO_CONNECT(TEST_VOLCANO_AUTO_CONNECT);
  227. #endif // TEST_VOLCANO_AUTO_CONNECT
  228. int16_t r = volcano_get_current_temp();
  229. println("volcano current temp: %.1f", r / 10.0);
  230. #ifdef TEST_VOLCANO_AUTO_CONNECT
  231. ble_disconnect();
  232. #endif // TEST_VOLCANO_AUTO_CONNECT
  233. } else if (strcmp(line, "vrtt") == 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_target_temp();
  238. println("volcano target temp: %.1f", r / 10.0);
  239. #ifdef TEST_VOLCANO_AUTO_CONNECT
  240. ble_disconnect();
  241. #endif // TEST_VOLCANO_AUTO_CONNECT
  242. } else if (str_startswith(line, "vwtt ")) {
  243. float val;
  244. int r = sscanf(line, "vwtt %f", &val);
  245. if (r != 1) {
  246. println("invalid input (%d)", r);
  247. } else {
  248. uint16_t v = val * 10.0;
  249. #ifdef TEST_VOLCANO_AUTO_CONNECT
  250. DEV_AUTO_CONNECT(TEST_VOLCANO_AUTO_CONNECT);
  251. #endif // TEST_VOLCANO_AUTO_CONNECT
  252. int8_t r = volcano_set_target_temp(v);
  253. #ifdef TEST_VOLCANO_AUTO_CONNECT
  254. ble_disconnect();
  255. #endif // TEST_VOLCANO_AUTO_CONNECT
  256. if (r < 0) {
  257. println("error writing target temp %d", r);
  258. } else {
  259. println("success");
  260. }
  261. }
  262. } else if (str_startswith(line, "vwh ")) {
  263. int val;
  264. int r = sscanf(line, "vwh %d", &val);
  265. if ((r != 1) || ((val != 0) && (val != 1))) {
  266. println("invalid input (%d %d)", r, val);
  267. } else {
  268. #ifdef TEST_VOLCANO_AUTO_CONNECT
  269. DEV_AUTO_CONNECT(TEST_VOLCANO_AUTO_CONNECT);
  270. #endif // TEST_VOLCANO_AUTO_CONNECT
  271. int8_t r = volcano_set_heater_state(val == 1);
  272. #ifdef TEST_VOLCANO_AUTO_CONNECT
  273. ble_disconnect();
  274. #endif // TEST_VOLCANO_AUTO_CONNECT
  275. if (r < 0) {
  276. println("error writing heater state %d", r);
  277. } else {
  278. println("success");
  279. }
  280. }
  281. } else if (str_startswith(line, "vwp ")) {
  282. int val;
  283. int r = sscanf(line, "vwp %d", &val);
  284. if ((r != 1) || ((val != 0) && (val != 1))) {
  285. println("invalid input (%d %d)", r, val);
  286. } else {
  287. #ifdef TEST_VOLCANO_AUTO_CONNECT
  288. DEV_AUTO_CONNECT(TEST_VOLCANO_AUTO_CONNECT);
  289. #endif // TEST_VOLCANO_AUTO_CONNECT
  290. int8_t r = volcano_set_pump_state(val == 1);
  291. #ifdef TEST_VOLCANO_AUTO_CONNECT
  292. ble_disconnect();
  293. #endif // TEST_VOLCANO_AUTO_CONNECT
  294. if (r < 0) {
  295. println("error writing pump state %d", r);
  296. } else {
  297. println("success");
  298. }
  299. }
  300. } else if (strcmp(line, "wfl") == 0) {
  301. println("%d workflows", wf_count());
  302. for (int i = 0; i < wf_count(); i++) {
  303. println(" '%s' by %s", wf_name(i), wf_author(i));
  304. }
  305. } else if (str_startswith(line, "wf ")) {
  306. int wf = -1;
  307. for (int i = 0; i < wf_count(); i++) {
  308. if (strcmp(wf_name(i), line + 3) == 0) {
  309. wf = i;
  310. break;
  311. }
  312. }
  313. if (wf < 0) {
  314. println("unknown workflow");
  315. } else {
  316. struct wf_state s = wf_status();
  317. if (s.status != WF_IDLE) {
  318. println("workflow in progress");
  319. } else {
  320. #ifdef TEST_VOLCANO_AUTO_CONNECT
  321. DEV_AUTO_CONNECT(TEST_VOLCANO_AUTO_CONNECT);
  322. #endif // TEST_VOLCANO_AUTO_CONNECT
  323. println("starting workflow");
  324. wf_start(wf);
  325. s = wf_status();
  326. while (s.status != WF_IDLE) {
  327. main_loop_hw();
  328. wf_run();
  329. s = wf_status();
  330. }
  331. println("done");
  332. #ifdef TEST_VOLCANO_AUTO_CONNECT
  333. ble_disconnect();
  334. #endif // TEST_VOLCANO_AUTO_CONNECT
  335. }
  336. }
  337. } else if (strcmp(line, "crct") == 0) {
  338. #ifdef TEST_CRAFTY_AUTO_CONNECT
  339. DEV_AUTO_CONNECT(TEST_CRAFTY_AUTO_CONNECT);
  340. #endif // TEST_CRAFTY_AUTO_CONNECT
  341. int16_t r = crafty_get_current_temp();
  342. println("crafty current temp: %.1f", r / 10.0);
  343. #ifdef TEST_CRAFTY_AUTO_CONNECT
  344. ble_disconnect();
  345. #endif // TEST_CRAFTY_AUTO_CONNECT
  346. } else if (strcmp(line, "crtt") == 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_target_temp();
  351. println("crafty target temp: %.1f", r / 10.0);
  352. #ifdef TEST_CRAFTY_AUTO_CONNECT
  353. ble_disconnect();
  354. #endif // TEST_CRAFTY_AUTO_CONNECT
  355. } else if (str_startswith(line, "cwtt ")) {
  356. float val;
  357. int r = sscanf(line, "cwtt %f", &val);
  358. if (r != 1) {
  359. println("invalid input (%d)", r);
  360. } else {
  361. uint16_t v = val * 10.0;
  362. #ifdef TEST_CRAFTY_AUTO_CONNECT
  363. DEV_AUTO_CONNECT(TEST_CRAFTY_AUTO_CONNECT);
  364. #endif // TEST_CRAFTY_AUTO_CONNECT
  365. int8_t r = crafty_set_target_temp(v);
  366. #ifdef TEST_CRAFTY_AUTO_CONNECT
  367. ble_disconnect();
  368. #endif // TEST_CRAFTY_AUTO_CONNECT
  369. if (r < 0) {
  370. println("error writing target temp %d", r);
  371. } else {
  372. println("success");
  373. }
  374. }
  375. } else if (str_startswith(line, "cwh ")) {
  376. int val;
  377. int r = sscanf(line, "cwh %d", &val);
  378. if ((r != 1) || ((val != 0) && (val != 1))) {
  379. println("invalid input (%d %d)", r, val);
  380. } else {
  381. #ifdef TEST_CRAFTY_AUTO_CONNECT
  382. DEV_AUTO_CONNECT(TEST_CRAFTY_AUTO_CONNECT);
  383. #endif // TEST_CRAFTY_AUTO_CONNECT
  384. int8_t r = crafty_set_heater_state(val == 1);
  385. #ifdef TEST_CRAFTY_AUTO_CONNECT
  386. ble_disconnect();
  387. #endif // TEST_CRAFTY_AUTO_CONNECT
  388. if (r < 0) {
  389. println("error writing heater state %d", r);
  390. } else {
  391. println("success");
  392. }
  393. }
  394. } else if (strcmp(line, "crb") == 0) {
  395. #ifdef TEST_CRAFTY_AUTO_CONNECT
  396. DEV_AUTO_CONNECT(TEST_CRAFTY_AUTO_CONNECT);
  397. #endif // TEST_CRAFTY_AUTO_CONNECT
  398. int16_t r = crafty_get_battery_state();
  399. println("crafty battery: %d %%", r);
  400. #ifdef TEST_CRAFTY_AUTO_CONNECT
  401. ble_disconnect();
  402. #endif // TEST_CRAFTY_AUTO_CONNECT
  403. } else {
  404. println("unknown command \"%s\"", line);
  405. }
  406. println();
  407. }
  408. void cnsl_init(void) {
  409. cnsl_buff_pos = 0;
  410. for (int i = 0; i < CNSL_BUFF_SIZE + 1; i++) {
  411. cnsl_line_buff[i] = '\0';
  412. cnsl_last_command[i] = '\0';
  413. cnsl_repeated_command[i] = '\0';
  414. }
  415. }
  416. static int32_t cnsl_find_line_end(void) {
  417. for (uint32_t i = 0; i < cnsl_buff_pos; i++) {
  418. if ((cnsl_line_buff[i] == '\r') || (cnsl_line_buff[i] == '\n')) {
  419. return i;
  420. }
  421. }
  422. return -1;
  423. }
  424. void cnsl_run(void) {
  425. if (repeat_command && (strlen(cnsl_repeated_command) > 0)
  426. && (strcmp(cnsl_repeated_command, "repeat") != 0)) {
  427. uint32_t now = to_ms_since_boot(get_absolute_time());
  428. if (now >= (last_repeat_time + CNSL_REPEAT_MS)) {
  429. println("repeating command \"%s\"", cnsl_repeated_command);
  430. cnsl_interpret(cnsl_repeated_command);
  431. println();
  432. last_repeat_time = now;
  433. }
  434. } else {
  435. if (repeat_command) {
  436. println("nothing to repeat");
  437. }
  438. repeat_command = false;
  439. }
  440. }
  441. void cnsl_handle_input(const uint8_t *buf, size_t len) {
  442. if ((cnsl_buff_pos + len) > CNSL_BUFF_SIZE) {
  443. debug("error: console input buffer overflow! %lu > %u", cnsl_buff_pos + len, CNSL_BUFF_SIZE);
  444. cnsl_init();
  445. }
  446. memcpy(cnsl_line_buff + cnsl_buff_pos, buf, len);
  447. cnsl_buff_pos += len;
  448. // handle backspace and local echo
  449. for (ssize_t i = cnsl_buff_pos - len; i < (ssize_t)cnsl_buff_pos; i++) {
  450. if ((cnsl_line_buff[i] == '\b') || (cnsl_line_buff[i] == 0x7F)) {
  451. if (i > 0) {
  452. // overwrite previous character and backspace
  453. for (ssize_t j = i; j < (ssize_t)cnsl_buff_pos - 1; j++) {
  454. cnsl_line_buff[j - 1] = cnsl_line_buff[j + 1];
  455. }
  456. cnsl_buff_pos -= 2;
  457. } else {
  458. // just remove the backspace
  459. for (ssize_t j = i; j < (ssize_t)cnsl_buff_pos - 1; j++) {
  460. cnsl_line_buff[j] = cnsl_line_buff[j + 1];
  461. }
  462. cnsl_buff_pos -= 1;
  463. }
  464. usb_cdc_write((const uint8_t *)"\b \b", 3);
  465. serial_write((const uint8_t *)"\b \b", 3);
  466. // check for another backspace in this space
  467. i--;
  468. } else {
  469. usb_cdc_write((const uint8_t *)(cnsl_line_buff + i), 1);
  470. serial_write((const uint8_t *)(cnsl_line_buff + i), 1);
  471. }
  472. }
  473. int32_t line_len = cnsl_find_line_end();
  474. if (line_len < 0) {
  475. // user has not pressed enter yet
  476. return;
  477. }
  478. // convert line to C-style string
  479. cnsl_line_buff[line_len] = '\0';
  480. cnsl_interpret(cnsl_line_buff);
  481. // store command for eventual repeats
  482. strncpy(cnsl_last_command, cnsl_line_buff, CNSL_BUFF_SIZE + 1);
  483. // clear string and move following data over
  484. uint32_t cnt = line_len + 1;
  485. if (cnsl_line_buff[line_len + 1] == '\n') {
  486. cnt++;
  487. }
  488. memset(cnsl_line_buff, '\0', cnt);
  489. memmove(cnsl_line_buff, cnsl_line_buff + cnt, sizeof(cnsl_line_buff) - cnt);
  490. cnsl_buff_pos -= cnt;
  491. }