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

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