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

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