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

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