Browse Source

added console repeat commands.

Thomas Buck 2 years ago
parent
commit
9097f0f24d

+ 1
- 0
firmware/include/console.h View File

@@ -6,6 +6,7 @@
6 6
 #define __CONSOLE_H__
7 7
 
8 8
 void cnsl_init(void);
9
+void cnsl_run(void);
9 10
 void cnsl_handle_input(const char *buf, uint32_t len);
10 11
 
11 12
 #endif // __CONSOLE_H__

+ 1
- 0
firmware/include/pmw3360.h View File

@@ -12,6 +12,7 @@ struct pmw_motion {
12 12
 };
13 13
 
14 14
 int pmw_init(void);
15
+
15 16
 struct pmw_motion pmw_get(void);
16 17
 
17 18
 /*

+ 1
- 0
firmware/include/util.h View File

@@ -13,5 +13,6 @@ int32_t convert_two_complement(int32_t b);
13 13
 bool str_startswith(const char *str, const char *start);
14 14
 
15 15
 void reset_to_bootloader(void);
16
+void reset_to_main(void);
16 17
 
17 18
 #endif // __UTIL_H__

+ 61
- 4
firmware/src/console.c View File

@@ -17,18 +17,54 @@
17 17
 static char cnsl_line_buff[CNSL_BUFF_SIZE + 1];
18 18
 static uint32_t cnsl_buff_pos = 0;
19 19
 
20
+static char cnsl_last_command[CNSL_BUFF_SIZE + 1];
21
+
22
+static char cnsl_repeated_command[CNSL_BUFF_SIZE + 1];
23
+static bool repeat_command = false;
24
+static uint32_t last_repeat_time = 0;
25
+
20 26
 static void cnsl_interpret(const char *line) {
21
-    if ((strcmp(line, "help") == 0)
27
+    if (strlen(line) == 0) {
28
+        // repeat last command once
29
+        print("repeating command \"%s\"", cnsl_last_command);
30
+        cnsl_interpret(cnsl_last_command);
31
+        print();
32
+        return;
33
+    } else if (strcmp(line, "repeat") == 0) {
34
+        if (!repeat_command) {
35
+            // mark last command to be repeated multiple times
36
+            strncpy(cnsl_repeated_command, cnsl_last_command, CNSL_BUFF_SIZE + 1);
37
+            last_repeat_time = to_ms_since_boot(get_absolute_time()) - 1001;
38
+            repeat_command = true;
39
+        } else {
40
+            // stop repeating
41
+            repeat_command = false;
42
+        }
43
+    } else if ((strcmp(line, "help") == 0)
22 44
             || (strcmp(line, "h") == 0)
23 45
             || (strcmp(line, "?") == 0)) {
24 46
         print("Trackball Firmware Usage:");
25 47
         print("    cpi - print current sensitivity");
26 48
         print("  cpi N - set sensitivity");
27
-        print("    pmw - print PMW3360 status");
28
-        print("   help - print this message");
49
+        print("   pmws - print PMW3360 status");
50
+        print("   pmwr - reset PMW3360");
51
+        print("  reset - reset back into this firmware");
29 52
         print("   \\x18 - reset to bootloader");
30
-    } else if (strcmp(line, "pmw") == 0) {
53
+        print(" repeat - repeat last command once per second");
54
+        print("   help - print this message");
55
+        print("Press Enter with no input to repeat last command.");
56
+        print("Use repeat to continuously execute last command.");
57
+        print("Stop this by calling repeat again.");
58
+    } else if (strcmp(line, "pmws") == 0) {
31 59
         pmw_print_status();
60
+    } else if (strcmp(line, "pmwr") == 0) {
61
+        print("user requests re-initializing of PMW3360");
62
+        int r = pmw_init();
63
+        if (r < 0) {
64
+            print("error initializing PMW3360");
65
+        } else {
66
+            print("PMW3360 re-initialized successfully");
67
+        }
32 68
     } else if (strcmp(line, "cpi") == 0) {
33 69
         uint8_t sense = pmw_get_sensitivity();
34 70
         uint16_t cpi = PMW_SENSE_TO_CPI(sense);
@@ -44,9 +80,14 @@ static void cnsl_interpret(const char *line) {
44 80
             print("setting cpi to 0x%02llX", num);
45 81
             pmw_set_sensitivity(num);
46 82
         }
83
+    } else if (strcmp(line, "reset") == 0) {
84
+        reset_to_main();
47 85
     } else {
48 86
         print("unknown command \"%s\"", line);
87
+        return;
49 88
     }
89
+
90
+    print();
50 91
 }
51 92
 
52 93
 void cnsl_init(void) {
@@ -65,6 +106,19 @@ static int32_t cnsl_find_line_end(void) {
65 106
     return -1;
66 107
 }
67 108
 
109
+void cnsl_run(void) {
110
+    if (repeat_command) {
111
+        uint32_t now = to_ms_since_boot(get_absolute_time());
112
+        if (now >= (last_repeat_time + 1000)) {
113
+            print("repeating command \"%s\"", cnsl_repeated_command);
114
+            cnsl_interpret(cnsl_repeated_command);
115
+            print();
116
+
117
+            last_repeat_time = now;
118
+        }
119
+    }
120
+}
121
+
68 122
 void cnsl_handle_input(const char *buf, uint32_t len) {
69 123
     if ((cnsl_buff_pos + len) > CNSL_BUFF_SIZE) {
70 124
         debug("error: console input buffer overflow! %lu > %u", cnsl_buff_pos + len, CNSL_BUFF_SIZE);
@@ -87,6 +141,9 @@ void cnsl_handle_input(const char *buf, uint32_t len) {
87 141
 
88 142
     cnsl_interpret(cnsl_line_buff);
89 143
 
144
+    // store command for eventual repeats
145
+    strncpy(cnsl_last_command, cnsl_line_buff, CNSL_BUFF_SIZE + 1);
146
+
90 147
     // clear string and move following data over
91 148
     uint32_t cnt = line_len + 1;
92 149
     if (cnsl_line_buff[line_len + 1] == '\n') {

+ 1
- 0
firmware/src/main.c View File

@@ -21,6 +21,7 @@ int main(void) {
21 21
     while (1) {
22 22
         heartbeat_run();
23 23
         usb_run();
24
+        cnsl_run();
24 25
     }
25 26
 
26 27
     return 0;

+ 4
- 0
firmware/src/pmw3360.c View File

@@ -23,6 +23,7 @@
23 23
 
24 24
 #include "config.h"
25 25
 #include "log.h"
26
+#include "util.h"
26 27
 #include "pmw3360_registers.h"
27 28
 #include "pmw3360_srom.h"
28 29
 #include "pmw3360.h"
@@ -358,6 +359,9 @@ int pmw_init(void) {
358 359
     pmw_write_register(REG_CONFIG2, pmw_read_register(REG_CONFIG2) | 0x04);
359 360
     pmw_set_sensitivity(0x31); // default: 5000cpi
360 361
 
362
+    // Set lift-detection threshold to 3mm (max)
363
+    pmw_write_register(REG_LIFT_CONFIG, 0x03);
364
+
361 365
     // setup MOTION pin interrupt to handle reading data
362 366
     gpio_add_raw_irq_handler(PMW_MOTION_PIN, pmw_motion_irq);
363 367
     gpio_set_irq_enabled(PMW_MOTION_PIN, GPIO_IRQ_EDGE_FALL, true);

+ 6
- 6
firmware/src/usb_hid.c View File

@@ -27,6 +27,7 @@
27 27
 #include "tusb.h"
28 28
 
29 29
 #include "config.h"
30
+#include "pmw3360.h"
30 31
 #include "usb_descriptors.h"
31 32
 #include "usb_hid.h"
32 33
 
@@ -56,12 +57,11 @@ static void send_hid_report(uint8_t report_id, uint32_t btn) {
56 57
 
57 58
         case REPORT_ID_MOUSE:
58 59
         {
59
-            // no button, right + down, no scroll, no pan
60
-            tud_hid_mouse_report(REPORT_ID_MOUSE, 0x00, -delta_x, -delta_y, 0, 0);
61
-
62
-            // TODO
63
-            delta_x = 0;
64
-            delta_y = 0;
60
+            struct pmw_motion mouse = pmw_get();
61
+            if (mouse.motion) {
62
+                tud_hid_mouse_report(REPORT_ID_MOUSE, 0x00,
63
+                        -mouse.delta_x, -mouse.delta_y, 0, 0);
64
+            }
65 65
         }
66 66
         break;
67 67
 

+ 9
- 0
firmware/src/util.c View File

@@ -5,6 +5,7 @@
5 5
 #include <string.h>
6 6
 #include "pico/stdlib.h"
7 7
 #include "pico/bootrom.h"
8
+#include "hardware/watchdog.h"
8 9
 
9 10
 #include "config.h"
10 11
 #include "util.h"
@@ -55,3 +56,11 @@ void reset_to_bootloader(void) {
55 56
     reset_usb_boot(0, 0);
56 57
 #endif // PICO_DEFAULT_LED_PIN
57 58
 }
59
+
60
+void reset_to_main(void) {
61
+    watchdog_enable(1, 1);
62
+    while (1) {
63
+        // wait 1ms until watchdog kills us
64
+        asm volatile("nop");
65
+    }
66
+}

Loading…
Cancel
Save