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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  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 "pico/stdlib.h"
  21. #include <unistd.h>
  22. #include <stdio.h>
  23. #include "config.h"
  24. #include "log.h"
  25. #include "util.h"
  26. #include "usb_cdc.h"
  27. #include "usb_msc.h"
  28. #include "debug.h"
  29. #include "console.h"
  30. #include "lipo.h"
  31. #include "ble.h"
  32. #include "text.h"
  33. #include "lcd.h"
  34. #include "image.h"
  35. #define CNSL_BUFF_SIZE 1024
  36. #define CNSL_REPEAT_MS 500
  37. static char cnsl_line_buff[CNSL_BUFF_SIZE + 1];
  38. static uint32_t cnsl_buff_pos = 0;
  39. static char cnsl_last_command[CNSL_BUFF_SIZE + 1];
  40. static char cnsl_repeated_command[CNSL_BUFF_SIZE + 1];
  41. static bool repeat_command = false;
  42. static uint32_t last_repeat_time = 0;
  43. static void cnsl_interpret(const char *line) {
  44. if (strlen(line) == 0) {
  45. if ((strlen(cnsl_last_command) > 0) && (strcmp(cnsl_last_command, "repeat") != 0)) {
  46. // repeat last command once
  47. println("repeating command \"%s\"", cnsl_last_command);
  48. cnsl_interpret(cnsl_last_command);
  49. println();
  50. }
  51. return;
  52. } else if (strcmp(line, "repeat") == 0) {
  53. if (!repeat_command) {
  54. // mark last command to be repeated multiple times
  55. strncpy(cnsl_repeated_command, cnsl_last_command, CNSL_BUFF_SIZE + 1);
  56. last_repeat_time = to_ms_since_boot(get_absolute_time()) - 1001;
  57. repeat_command = true;
  58. } else {
  59. // stop repeating
  60. repeat_command = false;
  61. }
  62. } else if ((strcmp(line, "help") == 0)
  63. || (strcmp(line, "h") == 0)
  64. || (strcmp(line, "?") == 0)) {
  65. println("VolcanoRC Firmware Usage:");
  66. println("");
  67. println(" reset - reset back into this firmware");
  68. println(" \\x18 - reset to bootloader");
  69. println(" repeat - repeat last command every %d milliseconds", CNSL_REPEAT_MS);
  70. println(" help - print this message");
  71. println(" mount - make mass storage medium (un)available");
  72. println(" power - show Lipo battery status");
  73. println("");
  74. println(" scan - start or stop BLE scan");
  75. println("scanres - print list of found BLE devices");
  76. println("con M T - connect to (M)AC and (T)ype");
  77. println(" discon - disconnect from BLE device");
  78. println("");
  79. println(" clear - blank screen");
  80. println(" splash - draw image on screen");
  81. println(" fonts - show font list");
  82. println(" text - draw text on screen");
  83. println(" bat - draw battery indicator");
  84. println("");
  85. println("Press Enter with no input to repeat last command.");
  86. println("Use repeat to continuously execute last command.");
  87. println("Stop this by calling repeat again.");
  88. } else if (strcmp(line, "reset") == 0) {
  89. reset_to_main();
  90. } else if (strcmp(line, "mount") == 0) {
  91. bool state = msc_is_medium_available();
  92. println("Currently %s. %s now.",
  93. state ? "mounted" : "unmounted",
  94. state ? "Unplugging" : "Plugging in");
  95. msc_set_medium_available(!state);
  96. } else if (strcmp(line, "power") == 0) {
  97. float volt = lipo_voltage();
  98. println("Battery: %.2fV = %.1f%% @ %s",
  99. volt, lipo_percentage(volt),
  100. lipo_charging() ? "charging" : "draining");
  101. } else if (strcmp(line, "scan") == 0) {
  102. ble_scan(2);
  103. } else if (strcmp(line, "scanres") == 0) {
  104. struct ble_scan_result results[BLE_MAX_SCAN_RESULTS] = {0};
  105. int n = ble_get_scan_results(results, BLE_MAX_SCAN_RESULTS);
  106. if (n < 0) {
  107. println("Error reading results (%d)", n);
  108. } else {
  109. println("%d results", n);
  110. for (int i = 0; i < n; i++) {
  111. uint32_t age = to_ms_since_boot(get_absolute_time()) - results[i].time;
  112. println("addr=%s type=%d rssi=%d age=%.1fs name='%s'",
  113. bd_addr_to_str(results[i].addr),
  114. results[i].type, results[i].rssi,
  115. age / 1000.0, results[i].name);
  116. }
  117. }
  118. } else if (str_startswith(line, "con ")) {
  119. bd_addr_t addr;
  120. bd_addr_type_t type;
  121. int r = sscanf(line, "con %02hhX:%02hhX:%02hhX:%02hhX:%02hhX:%02hhX %hhu",
  122. &addr[0], &addr[1], &addr[2], &addr[3],
  123. &addr[4], &addr[5], &type);
  124. if (r == 7) {
  125. debug("connecting");
  126. ble_connect(addr, type);
  127. } else {
  128. debug("invalid input (%d)", r);
  129. }
  130. } else if (strcmp(line, "discon") == 0) {
  131. ble_disconnect();
  132. } else if (strcmp(line, "clear") == 0) {
  133. lcd_clear();
  134. } else if (strcmp(line, "splash") == 0) {
  135. draw_splash();
  136. } else if (strcmp(line, "fonts") == 0) {
  137. const struct mf_font_list_s *f = mf_get_font_list();
  138. debug("Font list:");
  139. while (f) {
  140. debug("full_name: %s", f->font->full_name);
  141. debug("short_name: %s", f->font->short_name);
  142. debug("size: %d %d", f->font->width, f->font->height);
  143. debug("x_advance: %d %d", f->font->min_x_advance, f->font->max_x_advance);
  144. debug("baseline: %d %d", f->font->baseline_x, f->font->baseline_y);
  145. debug("line_height: %d", f->font->line_height);
  146. debug("flags: %d", f->font->flags);
  147. debug("fallback_character: %c", f->font->fallback_character);
  148. debug("character_width: %p", f->font->character_width);
  149. debug("render_character: %p", f->font->render_character);
  150. f = f->next;
  151. if (f) {
  152. debug("");
  153. }
  154. }
  155. } else if (strcmp(line, "text") == 0) {
  156. uint16_t y_off = 0;
  157. const struct mf_font_list_s *f = mf_get_font_list();
  158. while (f) {
  159. struct text_font font = {
  160. .fontname = f->font->short_name,
  161. //.scale = 1,
  162. .font = f->font,
  163. };
  164. text_prepare_font(&font);
  165. struct text_conf text = {
  166. .text = font.fontname,
  167. .x = 0,
  168. .y = y_off,
  169. .justify = false,
  170. .alignment = MF_ALIGN_CENTER,
  171. .width = 240,
  172. .height = 240 - y_off,
  173. .margin = 5,
  174. .bg = TEXT_BG_NONE,
  175. .font = &font,
  176. };
  177. text_draw(&text);
  178. y_off = text.y;
  179. f = f->next;
  180. }
  181. } else if (strcmp(line, "bat") == 0) {
  182. draw_battery_indicator();
  183. } else {
  184. println("unknown command \"%s\"", line);
  185. }
  186. println();
  187. }
  188. void cnsl_init(void) {
  189. cnsl_buff_pos = 0;
  190. for (int i = 0; i < CNSL_BUFF_SIZE + 1; i++) {
  191. cnsl_line_buff[i] = '\0';
  192. cnsl_last_command[i] = '\0';
  193. cnsl_repeated_command[i] = '\0';
  194. }
  195. }
  196. static int32_t cnsl_find_line_end(void) {
  197. for (uint32_t i = 0; i < cnsl_buff_pos; i++) {
  198. if ((cnsl_line_buff[i] == '\r') || (cnsl_line_buff[i] == '\n')) {
  199. return i;
  200. }
  201. }
  202. return -1;
  203. }
  204. void cnsl_run(void) {
  205. if (repeat_command && (strlen(cnsl_repeated_command) > 0)
  206. && (strcmp(cnsl_repeated_command, "repeat") != 0)) {
  207. uint32_t now = to_ms_since_boot(get_absolute_time());
  208. if (now >= (last_repeat_time + CNSL_REPEAT_MS)) {
  209. println("repeating command \"%s\"", cnsl_repeated_command);
  210. cnsl_interpret(cnsl_repeated_command);
  211. println();
  212. last_repeat_time = now;
  213. }
  214. } else {
  215. if (repeat_command) {
  216. println("nothing to repeat");
  217. }
  218. repeat_command = false;
  219. }
  220. }
  221. void cnsl_handle_input(const char *buf, uint32_t len) {
  222. if ((cnsl_buff_pos + len) > CNSL_BUFF_SIZE) {
  223. debug("error: console input buffer overflow! %lu > %u", cnsl_buff_pos + len, CNSL_BUFF_SIZE);
  224. cnsl_init();
  225. }
  226. memcpy(cnsl_line_buff + cnsl_buff_pos, buf, len);
  227. cnsl_buff_pos += len;
  228. // handle backspace
  229. for (ssize_t i = cnsl_buff_pos - len; i < (ssize_t)cnsl_buff_pos; i++) {
  230. if ((cnsl_line_buff[i] == '\b') || (cnsl_line_buff[i] == 0x7F)) {
  231. if (i > 0) {
  232. // overwrite previous character and backspace
  233. for (ssize_t j = i; j < (ssize_t)cnsl_buff_pos - 1; j++) {
  234. cnsl_line_buff[j - 1] = cnsl_line_buff[j + 1];
  235. }
  236. cnsl_buff_pos -= 2;
  237. } else {
  238. // just remove the backspace
  239. for (ssize_t j = i; j < (ssize_t)cnsl_buff_pos - 1; j++) {
  240. cnsl_line_buff[j] = cnsl_line_buff[j + 1];
  241. }
  242. cnsl_buff_pos -= 1;
  243. }
  244. usb_cdc_write("\b \b", 3);
  245. // check for another backspace in this space
  246. i--;
  247. } else {
  248. usb_cdc_write(cnsl_line_buff + i, 1);
  249. }
  250. }
  251. int32_t line_len = cnsl_find_line_end();
  252. if (line_len < 0) {
  253. // user has not pressed enter yet
  254. return;
  255. }
  256. // convert line to C-style string
  257. cnsl_line_buff[line_len] = '\0';
  258. cnsl_interpret(cnsl_line_buff);
  259. // store command for eventual repeats
  260. strncpy(cnsl_last_command, cnsl_line_buff, CNSL_BUFF_SIZE + 1);
  261. // clear string and move following data over
  262. uint32_t cnt = line_len + 1;
  263. if (cnsl_line_buff[line_len + 1] == '\n') {
  264. cnt++;
  265. }
  266. memset(cnsl_line_buff, '\0', cnt);
  267. memmove(cnsl_line_buff, cnsl_line_buff + cnt, sizeof(cnsl_line_buff) - cnt);
  268. cnsl_buff_pos -= cnt;
  269. }