Browse Source

properly handle backspace in console

Thomas Buck 1 year ago
parent
commit
a0d4792552
2 changed files with 27 additions and 5 deletions
  1. 27
    2
      firmware/src/console.c
  2. 0
    3
      firmware/src/usb_cdc.c

+ 27
- 2
firmware/src/console.c View File

@@ -10,6 +10,7 @@
10 10
 #include "log.h"
11 11
 #include "pmw3360.h"
12 12
 #include "util.h"
13
+#include "usb_cdc.h"
13 14
 #include "usb_msc.h"
14 15
 #include "debug.h"
15 16
 #include "console.h"
@@ -214,6 +215,32 @@ void cnsl_handle_input(const char *buf, uint32_t len) {
214 215
     memcpy(cnsl_line_buff + cnsl_buff_pos, buf, len);
215 216
     cnsl_buff_pos += len;
216 217
 
218
+    // handle backspace
219
+    for (ssize_t i = cnsl_buff_pos - len; i < (ssize_t)cnsl_buff_pos; i++) {
220
+        if ((cnsl_line_buff[i] == '\b') || (cnsl_line_buff[i] == 0x7F)) {
221
+            if (i > 0) {
222
+                // overwrite previous character and backspace
223
+                for (ssize_t j = i; j < (ssize_t)cnsl_buff_pos - 1; j++) {
224
+                    cnsl_line_buff[j - 1] = cnsl_line_buff[j + 1];
225
+                }
226
+                cnsl_buff_pos -= 2;
227
+            } else {
228
+                // just remove the backspace
229
+                for (ssize_t j = i; j < (ssize_t)cnsl_buff_pos - 1; j++) {
230
+                    cnsl_line_buff[j] = cnsl_line_buff[j + 1];
231
+                }
232
+                cnsl_buff_pos -= 1;
233
+            }
234
+
235
+            usb_cdc_write("\b \b", 3);
236
+
237
+            // check for another backspace in this space
238
+            i--;
239
+        } else {
240
+            usb_cdc_write(cnsl_line_buff + i, 1);
241
+        }
242
+    }
243
+
217 244
     int32_t line_len = cnsl_find_line_end();
218 245
     if (line_len < 0) {
219 246
         // user has not pressed enter yet
@@ -223,8 +250,6 @@ void cnsl_handle_input(const char *buf, uint32_t len) {
223 250
     // convert line to C-style string
224 251
     cnsl_line_buff[line_len] = '\0';
225 252
 
226
-    // TODO handle backspace and other terminal commands?
227
-
228 253
     cnsl_interpret(cnsl_line_buff);
229 254
 
230 255
     // store command for eventual repeats

+ 0
- 3
firmware/src/usb_cdc.c View File

@@ -79,9 +79,6 @@ void cdc_task(void) {
79 79
         } else if (reroute_cdc_debug) {
80 80
             debug_handle_input(buf, count);
81 81
         } else {
82
-            // echo back
83
-            usb_cdc_write(buf, count);
84
-
85 82
             cnsl_handle_input(buf, count);
86 83
         }
87 84
     }

Loading…
Cancel
Save