Browse Source

added console repeat commands.

Thomas Buck 1 year ago
parent
commit
9097f0f24d

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

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

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

12
 };
12
 };
13
 
13
 
14
 int pmw_init(void);
14
 int pmw_init(void);
15
+
15
 struct pmw_motion pmw_get(void);
16
 struct pmw_motion pmw_get(void);
16
 
17
 
17
 /*
18
 /*

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

13
 bool str_startswith(const char *str, const char *start);
13
 bool str_startswith(const char *str, const char *start);
14
 
14
 
15
 void reset_to_bootloader(void);
15
 void reset_to_bootloader(void);
16
+void reset_to_main(void);
16
 
17
 
17
 #endif // __UTIL_H__
18
 #endif // __UTIL_H__

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

17
 static char cnsl_line_buff[CNSL_BUFF_SIZE + 1];
17
 static char cnsl_line_buff[CNSL_BUFF_SIZE + 1];
18
 static uint32_t cnsl_buff_pos = 0;
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
 static void cnsl_interpret(const char *line) {
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
             || (strcmp(line, "h") == 0)
44
             || (strcmp(line, "h") == 0)
23
             || (strcmp(line, "?") == 0)) {
45
             || (strcmp(line, "?") == 0)) {
24
         print("Trackball Firmware Usage:");
46
         print("Trackball Firmware Usage:");
25
         print("    cpi - print current sensitivity");
47
         print("    cpi - print current sensitivity");
26
         print("  cpi N - set sensitivity");
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
         print("   \\x18 - reset to bootloader");
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
         pmw_print_status();
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
     } else if (strcmp(line, "cpi") == 0) {
68
     } else if (strcmp(line, "cpi") == 0) {
33
         uint8_t sense = pmw_get_sensitivity();
69
         uint8_t sense = pmw_get_sensitivity();
34
         uint16_t cpi = PMW_SENSE_TO_CPI(sense);
70
         uint16_t cpi = PMW_SENSE_TO_CPI(sense);
44
             print("setting cpi to 0x%02llX", num);
80
             print("setting cpi to 0x%02llX", num);
45
             pmw_set_sensitivity(num);
81
             pmw_set_sensitivity(num);
46
         }
82
         }
83
+    } else if (strcmp(line, "reset") == 0) {
84
+        reset_to_main();
47
     } else {
85
     } else {
48
         print("unknown command \"%s\"", line);
86
         print("unknown command \"%s\"", line);
87
+        return;
49
     }
88
     }
89
+
90
+    print();
50
 }
91
 }
51
 
92
 
52
 void cnsl_init(void) {
93
 void cnsl_init(void) {
65
     return -1;
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
 void cnsl_handle_input(const char *buf, uint32_t len) {
122
 void cnsl_handle_input(const char *buf, uint32_t len) {
69
     if ((cnsl_buff_pos + len) > CNSL_BUFF_SIZE) {
123
     if ((cnsl_buff_pos + len) > CNSL_BUFF_SIZE) {
70
         debug("error: console input buffer overflow! %lu > %u", cnsl_buff_pos + len, CNSL_BUFF_SIZE);
124
         debug("error: console input buffer overflow! %lu > %u", cnsl_buff_pos + len, CNSL_BUFF_SIZE);
87
 
141
 
88
     cnsl_interpret(cnsl_line_buff);
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
     // clear string and move following data over
147
     // clear string and move following data over
91
     uint32_t cnt = line_len + 1;
148
     uint32_t cnt = line_len + 1;
92
     if (cnsl_line_buff[line_len + 1] == '\n') {
149
     if (cnsl_line_buff[line_len + 1] == '\n') {

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

21
     while (1) {
21
     while (1) {
22
         heartbeat_run();
22
         heartbeat_run();
23
         usb_run();
23
         usb_run();
24
+        cnsl_run();
24
     }
25
     }
25
 
26
 
26
     return 0;
27
     return 0;

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

23
 
23
 
24
 #include "config.h"
24
 #include "config.h"
25
 #include "log.h"
25
 #include "log.h"
26
+#include "util.h"
26
 #include "pmw3360_registers.h"
27
 #include "pmw3360_registers.h"
27
 #include "pmw3360_srom.h"
28
 #include "pmw3360_srom.h"
28
 #include "pmw3360.h"
29
 #include "pmw3360.h"
358
     pmw_write_register(REG_CONFIG2, pmw_read_register(REG_CONFIG2) | 0x04);
359
     pmw_write_register(REG_CONFIG2, pmw_read_register(REG_CONFIG2) | 0x04);
359
     pmw_set_sensitivity(0x31); // default: 5000cpi
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
     // setup MOTION pin interrupt to handle reading data
365
     // setup MOTION pin interrupt to handle reading data
362
     gpio_add_raw_irq_handler(PMW_MOTION_PIN, pmw_motion_irq);
366
     gpio_add_raw_irq_handler(PMW_MOTION_PIN, pmw_motion_irq);
363
     gpio_set_irq_enabled(PMW_MOTION_PIN, GPIO_IRQ_EDGE_FALL, true);
367
     gpio_set_irq_enabled(PMW_MOTION_PIN, GPIO_IRQ_EDGE_FALL, true);

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

27
 #include "tusb.h"
27
 #include "tusb.h"
28
 
28
 
29
 #include "config.h"
29
 #include "config.h"
30
+#include "pmw3360.h"
30
 #include "usb_descriptors.h"
31
 #include "usb_descriptors.h"
31
 #include "usb_hid.h"
32
 #include "usb_hid.h"
32
 
33
 
56
 
57
 
57
         case REPORT_ID_MOUSE:
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
         break;
66
         break;
67
 
67
 

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

5
 #include <string.h>
5
 #include <string.h>
6
 #include "pico/stdlib.h"
6
 #include "pico/stdlib.h"
7
 #include "pico/bootrom.h"
7
 #include "pico/bootrom.h"
8
+#include "hardware/watchdog.h"
8
 
9
 
9
 #include "config.h"
10
 #include "config.h"
10
 #include "util.h"
11
 #include "util.h"
55
     reset_usb_boot(0, 0);
56
     reset_usb_boot(0, 0);
56
 #endif // PICO_DEFAULT_LED_PIN
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