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

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