S&B Volcano vaporizer remote control with Pi Pico W
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

console.c 22KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660
  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. int16_t time = volcano_get_auto_shutoff();
  258. println("volcano auto shutoff: %d", time);
  259. r = volcano_get_brightness();
  260. println("volcano brightness: %d", r);
  261. char fw[VOLCANO_FW_LEN + 1] = {0};
  262. r = volcano_get_firmware(fw);
  263. println("volcano firmware: %s", fw);
  264. int32_t rt = volcano_get_runtime();
  265. println("volcano runtime: %ld min", rt);
  266. #ifdef TEST_VOLCANO_AUTO_CONNECT
  267. ble_disconnect();
  268. #endif // TEST_VOLCANO_AUTO_CONNECT
  269. } else if (str_startswith(line, "vwtt ")) {
  270. float val;
  271. int r = sscanf(line, "vwtt %f", &val);
  272. if (r != 1) {
  273. println("invalid input (%d)", r);
  274. } else {
  275. uint16_t v = val * 10.0;
  276. #ifdef TEST_VOLCANO_AUTO_CONNECT
  277. DEV_AUTO_CONNECT(TEST_VOLCANO_AUTO_CONNECT);
  278. #endif // TEST_VOLCANO_AUTO_CONNECT
  279. int8_t r = volcano_set_target_temp(v);
  280. #ifdef TEST_VOLCANO_AUTO_CONNECT
  281. ble_disconnect();
  282. #endif // TEST_VOLCANO_AUTO_CONNECT
  283. if (r < 0) {
  284. println("error writing target temp %d", r);
  285. } else {
  286. println("success");
  287. }
  288. }
  289. } else if (str_startswith(line, "vwh ")) {
  290. int val;
  291. int r = sscanf(line, "vwh %d", &val);
  292. if ((r != 1) || ((val != 0) && (val != 1))) {
  293. println("invalid input (%d %d)", r, val);
  294. } else {
  295. #ifdef TEST_VOLCANO_AUTO_CONNECT
  296. DEV_AUTO_CONNECT(TEST_VOLCANO_AUTO_CONNECT);
  297. #endif // TEST_VOLCANO_AUTO_CONNECT
  298. int8_t r = volcano_set_heater_state(val == 1);
  299. #ifdef TEST_VOLCANO_AUTO_CONNECT
  300. ble_disconnect();
  301. #endif // TEST_VOLCANO_AUTO_CONNECT
  302. if (r < 0) {
  303. println("error writing heater state %d", r);
  304. } else {
  305. println("success");
  306. }
  307. }
  308. } else if (str_startswith(line, "vwp ")) {
  309. int val;
  310. int r = sscanf(line, "vwp %d", &val);
  311. if ((r != 1) || ((val != 0) && (val != 1))) {
  312. println("invalid input (%d %d)", r, val);
  313. } else {
  314. #ifdef TEST_VOLCANO_AUTO_CONNECT
  315. DEV_AUTO_CONNECT(TEST_VOLCANO_AUTO_CONNECT);
  316. #endif // TEST_VOLCANO_AUTO_CONNECT
  317. int8_t r = volcano_set_pump_state(val == 1);
  318. #ifdef TEST_VOLCANO_AUTO_CONNECT
  319. ble_disconnect();
  320. #endif // TEST_VOLCANO_AUTO_CONNECT
  321. if (r < 0) {
  322. println("error writing pump state %d", r);
  323. } else {
  324. println("success");
  325. }
  326. }
  327. } else if (str_startswith(line, "vwu ")) {
  328. char val;
  329. int r = sscanf(line, "vwu %c", &val);
  330. if ((r != 1) || ((val != 'C') && (val != 'F'))) {
  331. println("invalid input (%d %c)", r, val);
  332. } else {
  333. #ifdef TEST_VOLCANO_AUTO_CONNECT
  334. DEV_AUTO_CONNECT(TEST_VOLCANO_AUTO_CONNECT);
  335. #endif // TEST_VOLCANO_AUTO_CONNECT
  336. int8_t r = volcano_set_unit((val == 'C') ? UNIT_C : UNIT_F);
  337. #ifdef TEST_VOLCANO_AUTO_CONNECT
  338. ble_disconnect();
  339. #endif // TEST_VOLCANO_AUTO_CONNECT
  340. if (r < 0) {
  341. println("error writing value %d", r);
  342. } else {
  343. println("success");
  344. }
  345. }
  346. } else if (str_startswith(line, "vwv ")) {
  347. int val;
  348. int r = sscanf(line, "vwv %d", &val);
  349. if ((r != 1) || ((val != 0) && (val != 1))) {
  350. println("invalid input (%d %d)", r, val);
  351. } else {
  352. #ifdef TEST_VOLCANO_AUTO_CONNECT
  353. DEV_AUTO_CONNECT(TEST_VOLCANO_AUTO_CONNECT);
  354. #endif // TEST_VOLCANO_AUTO_CONNECT
  355. int8_t r = volcano_set_vibration(val == 1);
  356. #ifdef TEST_VOLCANO_AUTO_CONNECT
  357. ble_disconnect();
  358. #endif // TEST_VOLCANO_AUTO_CONNECT
  359. if (r < 0) {
  360. println("error writing value %d", r);
  361. } else {
  362. println("success");
  363. }
  364. }
  365. } else if (str_startswith(line, "vwdc ")) {
  366. int val;
  367. int r = sscanf(line, "vwdc %d", &val);
  368. if ((r != 1) || ((val != 0) && (val != 1))) {
  369. println("invalid input (%d %d)", r, val);
  370. } else {
  371. #ifdef TEST_VOLCANO_AUTO_CONNECT
  372. DEV_AUTO_CONNECT(TEST_VOLCANO_AUTO_CONNECT);
  373. #endif // TEST_VOLCANO_AUTO_CONNECT
  374. int8_t r = volcano_set_display_cooling(val == 1);
  375. #ifdef TEST_VOLCANO_AUTO_CONNECT
  376. ble_disconnect();
  377. #endif // TEST_VOLCANO_AUTO_CONNECT
  378. if (r < 0) {
  379. println("error writing value %d", r);
  380. } else {
  381. println("success");
  382. }
  383. }
  384. } else if (strcmp(line, "wfl") == 0) {
  385. println("%d workflows", wf_count());
  386. for (int i = 0; i < wf_count(); i++) {
  387. println(" '%s' by %s", wf_name(i), wf_author(i));
  388. }
  389. } else if (str_startswith(line, "wf ")) {
  390. int wf = -1;
  391. for (int i = 0; i < wf_count(); i++) {
  392. if (strcmp(wf_name(i), line + 3) == 0) {
  393. wf = i;
  394. break;
  395. }
  396. }
  397. if (wf < 0) {
  398. println("unknown workflow");
  399. } else {
  400. struct wf_state s = wf_status();
  401. if (s.status != WF_IDLE) {
  402. println("workflow in progress");
  403. } else {
  404. #ifdef TEST_VOLCANO_AUTO_CONNECT
  405. DEV_AUTO_CONNECT(TEST_VOLCANO_AUTO_CONNECT);
  406. #endif // TEST_VOLCANO_AUTO_CONNECT
  407. println("starting workflow");
  408. wf_start(wf);
  409. s = wf_status();
  410. while (s.status != WF_IDLE) {
  411. main_loop_hw();
  412. wf_run();
  413. s = wf_status();
  414. }
  415. println("done");
  416. #ifdef TEST_VOLCANO_AUTO_CONNECT
  417. ble_disconnect();
  418. #endif // TEST_VOLCANO_AUTO_CONNECT
  419. }
  420. }
  421. } else if (strcmp(line, "crct") == 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_current_temp();
  426. println("crafty current temp: %.1f", r / 10.0);
  427. #ifdef TEST_CRAFTY_AUTO_CONNECT
  428. ble_disconnect();
  429. #endif // TEST_CRAFTY_AUTO_CONNECT
  430. } else if (strcmp(line, "crtt") == 0) {
  431. #ifdef TEST_CRAFTY_AUTO_CONNECT
  432. DEV_AUTO_CONNECT(TEST_CRAFTY_AUTO_CONNECT);
  433. #endif // TEST_CRAFTY_AUTO_CONNECT
  434. int16_t r = crafty_get_target_temp();
  435. println("crafty target temp: %.1f", r / 10.0);
  436. #ifdef TEST_CRAFTY_AUTO_CONNECT
  437. ble_disconnect();
  438. #endif // TEST_CRAFTY_AUTO_CONNECT
  439. } else if (str_startswith(line, "cwtt ")) {
  440. float val;
  441. int r = sscanf(line, "cwtt %f", &val);
  442. if (r != 1) {
  443. println("invalid input (%d)", r);
  444. } else {
  445. uint16_t v = val * 10.0;
  446. #ifdef TEST_CRAFTY_AUTO_CONNECT
  447. DEV_AUTO_CONNECT(TEST_CRAFTY_AUTO_CONNECT);
  448. #endif // TEST_CRAFTY_AUTO_CONNECT
  449. int8_t r = crafty_set_target_temp(v);
  450. #ifdef TEST_CRAFTY_AUTO_CONNECT
  451. ble_disconnect();
  452. #endif // TEST_CRAFTY_AUTO_CONNECT
  453. if (r < 0) {
  454. println("error writing target temp %d", r);
  455. } else {
  456. println("success");
  457. }
  458. }
  459. } else if (str_startswith(line, "cwh ")) {
  460. int val;
  461. int r = sscanf(line, "cwh %d", &val);
  462. if ((r != 1) || ((val != 0) && (val != 1))) {
  463. println("invalid input (%d %d)", r, val);
  464. } else {
  465. #ifdef TEST_CRAFTY_AUTO_CONNECT
  466. DEV_AUTO_CONNECT(TEST_CRAFTY_AUTO_CONNECT);
  467. #endif // TEST_CRAFTY_AUTO_CONNECT
  468. int8_t r = crafty_set_heater_state(val == 1);
  469. #ifdef TEST_CRAFTY_AUTO_CONNECT
  470. ble_disconnect();
  471. #endif // TEST_CRAFTY_AUTO_CONNECT
  472. if (r < 0) {
  473. println("error writing heater state %d", r);
  474. } else {
  475. println("success");
  476. }
  477. }
  478. } else if (strcmp(line, "crb") == 0) {
  479. #ifdef TEST_CRAFTY_AUTO_CONNECT
  480. DEV_AUTO_CONNECT(TEST_CRAFTY_AUTO_CONNECT);
  481. #endif // TEST_CRAFTY_AUTO_CONNECT
  482. int16_t r = crafty_get_battery_state();
  483. println("crafty battery: %d %%", r);
  484. #ifdef TEST_CRAFTY_AUTO_CONNECT
  485. ble_disconnect();
  486. #endif // TEST_CRAFTY_AUTO_CONNECT
  487. } else {
  488. println("unknown command \"%s\"", line);
  489. }
  490. println();
  491. }
  492. void cnsl_init(void) {
  493. cnsl_buff_pos = 0;
  494. for (int i = 0; i < CNSL_BUFF_SIZE + 1; i++) {
  495. cnsl_line_buff[i] = '\0';
  496. cnsl_last_command[i] = '\0';
  497. cnsl_repeated_command[i] = '\0';
  498. }
  499. }
  500. static int32_t cnsl_find_line_end(void) {
  501. for (uint32_t i = 0; i < cnsl_buff_pos; i++) {
  502. if ((cnsl_line_buff[i] == '\r') || (cnsl_line_buff[i] == '\n')) {
  503. return i;
  504. }
  505. }
  506. return -1;
  507. }
  508. void cnsl_run(void) {
  509. if (repeat_command && (strlen(cnsl_repeated_command) > 0)
  510. && (strcmp(cnsl_repeated_command, "repeat") != 0)) {
  511. uint32_t now = to_ms_since_boot(get_absolute_time());
  512. if (now >= (last_repeat_time + CNSL_REPEAT_MS)) {
  513. println("repeating command \"%s\"", cnsl_repeated_command);
  514. cnsl_interpret(cnsl_repeated_command);
  515. println();
  516. last_repeat_time = now;
  517. }
  518. } else {
  519. if (repeat_command) {
  520. println("nothing to repeat");
  521. }
  522. repeat_command = false;
  523. }
  524. }
  525. void cnsl_handle_input(const uint8_t *buf, size_t len) {
  526. if ((cnsl_buff_pos + len) > CNSL_BUFF_SIZE) {
  527. debug("error: console input buffer overflow! %lu > %u", cnsl_buff_pos + len, CNSL_BUFF_SIZE);
  528. cnsl_init();
  529. }
  530. memcpy(cnsl_line_buff + cnsl_buff_pos, buf, len);
  531. cnsl_buff_pos += len;
  532. // handle backspace and local echo
  533. for (ssize_t i = cnsl_buff_pos - len; i < (ssize_t)cnsl_buff_pos; i++) {
  534. if ((cnsl_line_buff[i] == '\b') || (cnsl_line_buff[i] == 0x7F)) {
  535. if (i > 0) {
  536. // overwrite previous character and backspace
  537. for (ssize_t j = i; j < (ssize_t)cnsl_buff_pos - 1; j++) {
  538. cnsl_line_buff[j - 1] = cnsl_line_buff[j + 1];
  539. }
  540. cnsl_buff_pos -= 2;
  541. } else {
  542. // just remove the backspace
  543. for (ssize_t j = i; j < (ssize_t)cnsl_buff_pos - 1; j++) {
  544. cnsl_line_buff[j] = cnsl_line_buff[j + 1];
  545. }
  546. cnsl_buff_pos -= 1;
  547. }
  548. usb_cdc_write((const uint8_t *)"\b \b", 3);
  549. #ifndef NDEBUG
  550. serial_write((const uint8_t *)"\b \b", 3);
  551. #endif
  552. // check for another backspace in this space
  553. i--;
  554. } else {
  555. usb_cdc_write((const uint8_t *)(cnsl_line_buff + i), 1);
  556. #ifndef NDEBUG
  557. serial_write((const uint8_t *)(cnsl_line_buff + i), 1);
  558. #endif
  559. }
  560. }
  561. int32_t line_len = cnsl_find_line_end();
  562. if (line_len < 0) {
  563. // user has not pressed enter yet
  564. return;
  565. }
  566. // convert line to C-style string
  567. cnsl_line_buff[line_len] = '\0';
  568. cnsl_interpret(cnsl_line_buff);
  569. // store command for eventual repeats
  570. strncpy(cnsl_last_command, cnsl_line_buff, CNSL_BUFF_SIZE + 1);
  571. // clear string and move following data over
  572. uint32_t cnt = line_len + 1;
  573. if (cnsl_line_buff[line_len + 1] == '\n') {
  574. cnt++;
  575. }
  576. memset(cnsl_line_buff, '\0', cnt);
  577. memmove(cnsl_line_buff, cnsl_line_buff + cnt, sizeof(cnsl_line_buff) - cnt);
  578. cnsl_buff_pos -= cnt;
  579. }