No Description
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 6.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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 "lcd.h"
  29. #include "main.h"
  30. #include "console.h"
  31. #define CNSL_BUFF_SIZE 64
  32. #define CNSL_REPEAT_MS 500
  33. #define DEV_AUTO_CONNECT(s) { \
  34. ble_scan(BLE_SCAN_OFF); \
  35. bd_addr_t addr; \
  36. bd_addr_type_t type; \
  37. const char *foo = s; \
  38. sscanf(foo, "%02hhX:%02hhX:%02hhX:%02hhX:%02hhX:%02hhX %hhu", \
  39. &addr[0], &addr[1], &addr[2], &addr[3], \
  40. &addr[4], &addr[5], &type); \
  41. ble_connect(addr, type); \
  42. while (!ble_is_connected()) { \
  43. sleep_ms(1); \
  44. } \
  45. }
  46. static char cnsl_line_buff[CNSL_BUFF_SIZE + 1];
  47. static uint32_t cnsl_buff_pos = 0;
  48. static char cnsl_last_command[CNSL_BUFF_SIZE + 1];
  49. static char cnsl_repeated_command[CNSL_BUFF_SIZE + 1];
  50. static bool repeat_command = false;
  51. static uint32_t last_repeat_time = 0;
  52. static void cnsl_interpret(const char *line) {
  53. if (strlen(line) == 0) {
  54. if ((strlen(cnsl_last_command) > 0) && (strcmp(cnsl_last_command, "repeat") != 0)) {
  55. // repeat last command once
  56. println("repeating command \"%s\"", cnsl_last_command);
  57. cnsl_interpret(cnsl_last_command);
  58. println();
  59. }
  60. return;
  61. } else if (strcmp(line, "repeat") == 0) {
  62. if (!repeat_command) {
  63. // mark last command to be repeated multiple times
  64. strncpy(cnsl_repeated_command, cnsl_last_command, CNSL_BUFF_SIZE + 1);
  65. last_repeat_time = to_ms_since_boot(get_absolute_time()) - 1001;
  66. repeat_command = true;
  67. } else {
  68. // stop repeating
  69. repeat_command = false;
  70. }
  71. } else if ((strcmp(line, "help") == 0)
  72. || (strcmp(line, "h") == 0)
  73. || (strcmp(line, "?") == 0)) {
  74. println("Dispensy Firmware Usage:");
  75. println("");
  76. println(" reset - reset back into this firmware");
  77. println(" \\x18 - reset to bootloader");
  78. println("");
  79. println("Press Enter with no input to repeat last command.");
  80. println("Use repeat to continuously execute last command.");
  81. println("Stop this by calling repeat again.");
  82. } else if (strcmp(line, "reset") == 0) {
  83. reset_to_main();
  84. } else {
  85. println("unknown command \"%s\"", line);
  86. }
  87. println();
  88. }
  89. void cnsl_init(void) {
  90. cnsl_buff_pos = 0;
  91. for (int i = 0; i < CNSL_BUFF_SIZE + 1; i++) {
  92. cnsl_line_buff[i] = '\0';
  93. cnsl_last_command[i] = '\0';
  94. cnsl_repeated_command[i] = '\0';
  95. }
  96. }
  97. static int32_t cnsl_find_line_end(void) {
  98. for (uint32_t i = 0; i < cnsl_buff_pos; i++) {
  99. if ((cnsl_line_buff[i] == '\r') || (cnsl_line_buff[i] == '\n')) {
  100. return i;
  101. }
  102. }
  103. return -1;
  104. }
  105. void cnsl_run(void) {
  106. if (repeat_command && (strlen(cnsl_repeated_command) > 0)
  107. && (strcmp(cnsl_repeated_command, "repeat") != 0)) {
  108. uint32_t now = to_ms_since_boot(get_absolute_time());
  109. if (now >= (last_repeat_time + CNSL_REPEAT_MS)) {
  110. println("repeating command \"%s\"", cnsl_repeated_command);
  111. cnsl_interpret(cnsl_repeated_command);
  112. println();
  113. last_repeat_time = now;
  114. }
  115. } else {
  116. if (repeat_command) {
  117. println("nothing to repeat");
  118. }
  119. repeat_command = false;
  120. }
  121. }
  122. void cnsl_handle_input(const void *buf, size_t len) {
  123. if ((cnsl_buff_pos + len) > CNSL_BUFF_SIZE) {
  124. debug("error: console input buffer overflow! %lu > %u", cnsl_buff_pos + len, CNSL_BUFF_SIZE);
  125. cnsl_init();
  126. }
  127. memcpy(cnsl_line_buff + cnsl_buff_pos, buf, len);
  128. cnsl_buff_pos += len;
  129. // handle backspace and local echo
  130. for (ssize_t i = cnsl_buff_pos - len; i < (ssize_t)cnsl_buff_pos; i++) {
  131. if ((cnsl_line_buff[i] == '\b') || (cnsl_line_buff[i] == 0x7F)) {
  132. if (i > 0) {
  133. // overwrite previous character and backspace
  134. for (ssize_t j = i; j < (ssize_t)cnsl_buff_pos - 1; j++) {
  135. cnsl_line_buff[j - 1] = cnsl_line_buff[j + 1];
  136. }
  137. cnsl_buff_pos -= 2;
  138. } else {
  139. // just remove the backspace
  140. for (ssize_t j = i; j < (ssize_t)cnsl_buff_pos - 1; j++) {
  141. cnsl_line_buff[j] = cnsl_line_buff[j + 1];
  142. }
  143. cnsl_buff_pos -= 1;
  144. }
  145. usb_cdc_write((const uint8_t *)"\b \b", 3);
  146. #ifndef NDEBUG
  147. serial_write((const uint8_t *)"\b \b", 3);
  148. #endif
  149. // check for another backspace in this space
  150. i--;
  151. } else {
  152. usb_cdc_write((const uint8_t *)(cnsl_line_buff + i), 1);
  153. #ifndef NDEBUG
  154. serial_write((const uint8_t *)(cnsl_line_buff + i), 1);
  155. #endif
  156. }
  157. }
  158. int32_t line_len = cnsl_find_line_end();
  159. if (line_len < 0) {
  160. // user has not pressed enter yet
  161. return;
  162. }
  163. // convert line to C-style string
  164. cnsl_line_buff[line_len] = '\0';
  165. cnsl_interpret(cnsl_line_buff);
  166. // store command for eventual repeats
  167. strncpy(cnsl_last_command, cnsl_line_buff, CNSL_BUFF_SIZE + 1);
  168. // clear string and move following data over
  169. uint32_t cnt = line_len + 1;
  170. if (cnsl_line_buff[line_len + 1] == '\n') {
  171. cnt++;
  172. }
  173. memset(cnsl_line_buff, '\0', cnt);
  174. memmove(cnsl_line_buff, cnsl_line_buff + cnt, sizeof(cnsl_line_buff) - cnt);
  175. cnsl_buff_pos -= cnt;
  176. }