Нема описа
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.

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