Przeglądaj źródła

allow setting sensitivity

Thomas Buck 2 lat temu
rodzic
commit
1f482c35bd

+ 15
- 0
firmware/include/config.h Wyświetl plik

@@ -0,0 +1,15 @@
1
+/*
2
+ * config.h
3
+ */
4
+
5
+#ifndef __CONFIG_H__
6
+#define __CONFIG_H__
7
+
8
+#define PMW_MOTION_PIN 20
9
+
10
+#define PMW_PRINT_IDS
11
+#define PMW_IRQ_COUNTERS
12
+//#define PMW_FEATURE_WIRELESS
13
+//#define DISABLE_CDC_DTR_CHECK
14
+
15
+#endif // __CONFIG_H__

+ 14
- 1
firmware/include/pmw3360.h Wyświetl plik

@@ -7,6 +7,19 @@
7 7
 
8 8
 int pmw_init(void);
9 9
 
10
-void print_pmw_status(void);
10
+/*
11
+ * 0x00: 100 cpi (minimum cpi)
12
+ * 0x01: 200 cpi
13
+ * 0x02: 300 cpi
14
+ * ...
15
+ * 0x31: 5000 cpi (default cpi)
16
+ * ...
17
+ * 0x77: 12000 cpi (maximum cpi)
18
+ */
19
+void pmw_set_sensitivity(uint8_t sens);
20
+uint8_t pmw_get_sensitivity(void);
21
+#define PMW_SENSE_TO_CPI(sense) (100 + (sense * 100))
22
+
23
+void pmw_print_status(void);
11 24
 
12 25
 #endif // __PMW3360_H__

+ 2
- 0
firmware/include/util.h Wyświetl plik

@@ -8,6 +8,8 @@
8 8
 void heartbeat_init(void);
9 9
 void heartbeat_run(void);
10 10
 
11
+bool str_startswith(const char *str, const char *start);
12
+
11 13
 void reset_to_bootloader(void);
12 14
 
13 15
 #endif // __UTIL_H__

+ 25
- 4
firmware/src/console.c Wyświetl plik

@@ -2,10 +2,14 @@
2 2
  * console.c
3 3
  */
4 4
 
5
+#include <inttypes.h>
5 6
 #include <string.h>
6 7
 #include "pico/stdlib.h"
8
+
9
+#include "config.h"
7 10
 #include "log.h"
8 11
 #include "pmw3360.h"
12
+#include "util.h"
9 13
 #include "console.h"
10 14
 
11 15
 #define CNSL_BUFF_SIZE 1024
@@ -18,11 +22,28 @@ static void cnsl_interpret(const char *line) {
18 22
             || (strcmp(line, "h") == 0)
19 23
             || (strcmp(line, "?") == 0)) {
20 24
         print("Trackball Firmware Usage:");
21
-        print("    help - print this message");
22
-        print("     pmw - print PMW3360 status");
23
-        print("    \\x18 - reset to bootloader");
25
+        print("    cpi - print current sensitivity");
26
+        print("  cpi N - set sensitivity");
27
+        print("    pmw - print PMW3360 status");
28
+        print("   help - print this message");
29
+        print("   \\x18 - reset to bootloader");
24 30
     } else if (strcmp(line, "pmw") == 0) {
25
-        print_pmw_status();
31
+        pmw_print_status();
32
+    } else if (strcmp(line, "cpi") == 0) {
33
+        uint8_t sense = pmw_get_sensitivity();
34
+        uint16_t cpi = PMW_SENSE_TO_CPI(sense);
35
+        print("current cpi: %u (0x%02X)", cpi, sense);
36
+    } else if (str_startswith(line, "cpi ")) {
37
+        const char *num_str = line + 4;
38
+        uintmax_t num = strtoumax(num_str, NULL, 10);
39
+        if ((num < 100) || (num > 12000)) {
40
+            print("invalid cpi %llu, needs to be %u <= cpi <= %u", num, 100, 12000);
41
+        } else {
42
+            num /= 100;
43
+            num -= 1;
44
+            print("setting cpi to 0x%02llX", num);
45
+            pmw_set_sensitivity(num);
46
+        }
26 47
     } else {
27 48
         print("unknown command \"%s\"", line);
28 49
     }

+ 2
- 0
firmware/src/log.c Wyświetl plik

@@ -5,6 +5,8 @@
5 5
 #include <stdarg.h>
6 6
 #include <stdio.h>
7 7
 #include "pico/stdlib.h"
8
+
9
+#include "config.h"
8 10
 #include "usb_cdc.h"
9 11
 #include "log.h"
10 12
 

+ 1
- 0
firmware/src/main.c Wyświetl plik

@@ -4,6 +4,7 @@
4 4
 
5 5
 #include "pico/stdlib.h"
6 6
 
7
+#include "config.h"
7 8
 #include "util.h"
8 9
 #include "console.h"
9 10
 #include "log.h"

+ 26
- 7
firmware/src/pmw3360.c Wyświetl plik

@@ -21,10 +21,7 @@
21 21
 #include "pico/binary_info.h"
22 22
 #include "hardware/spi.h"
23 23
 
24
-#define PMW_PRINT_IDS
25
-#define PMW_IRQ_COUNTERS
26
-//#define PMW_FEATURE_WIRELESS
27
-
24
+#include "config.h"
28 25
 #include "log.h"
29 26
 #include "pmw3360_registers.h"
30 27
 #include "pmw3360_srom.h"
@@ -34,8 +31,6 @@
34 31
 #error PMW3360 API requires a board with SPI pins
35 32
 #endif
36 33
 
37
-#define PMW_MOTION_PIN 20
38
-
39 34
 #ifdef PMW_IRQ_COUNTERS
40 35
 static uint64_t pmw_irq_count_all = 0;
41 36
 static uint64_t pmw_irq_count_motion = 0;
@@ -48,7 +43,7 @@ static uint64_t pmw_irq_count_rest2 = 0;
48 43
 static uint64_t pmw_irq_count_rest3 = 0;
49 44
 #endif // PMW_IRQ_COUNTERS
50 45
 
51
-void print_pmw_status(void) {
46
+void pmw_print_status(void) {
52 47
 #ifdef PMW_IRQ_COUNTERS
53 48
     print("    pmw_irq_cnt_all = %llu", pmw_irq_count_all);
54 49
     print(" pmw_irq_cnt_motion = %llu", pmw_irq_count_motion);
@@ -281,6 +276,26 @@ static void pmw_motion_irq(void) {
281 276
     }
282 277
 }
283 278
 
279
+void pmw_set_sensitivity(uint8_t sens) {
280
+    if (sens > 0x77) {
281
+        debug("invalid sense, clamping (0x%X > 0x77)", sens);
282
+        sens = 0x77;
283
+    }
284
+
285
+    pmw_write_register(REG_CONFIG1, sens);
286
+    pmw_write_register(REG_CONFIG5, sens);
287
+}
288
+
289
+uint8_t pmw_get_sensitivity(void) {
290
+    uint8_t sense_y = pmw_read_register(REG_CONFIG1);
291
+    uint8_t sense_x = pmw_read_register(REG_CONFIG5);
292
+    if (sense_y != sense_x) {
293
+        debug("sensitivity differs for x (0x%02X) and y (0x%02X). resetting.", sense_x, sense_y);
294
+        pmw_write_register(REG_CONFIG5, sense_y);
295
+    }
296
+    return sense_y;
297
+}
298
+
284 299
 int pmw_init(void) {
285 300
     pmw_spi_init();
286 301
 
@@ -322,6 +337,10 @@ int pmw_init(void) {
322 337
     pmw_write_register(REG_CONFIG2, 0x00);
323 338
 #endif // PMW_FEATURE_WIRELESS
324 339
 
340
+    // Set sensitivity for each axis
341
+    pmw_write_register(REG_CONFIG2, pmw_read_register(REG_CONFIG2) | 0x04);
342
+    pmw_set_sensitivity(0x31); // default: 5000cpi
343
+
325 344
     // setup MOTION pin interrupt to handle reading data
326 345
     gpio_add_raw_irq_handler(PMW_MOTION_PIN, pmw_motion_irq);
327 346
     gpio_set_irq_enabled(PMW_MOTION_PIN, GPIO_IRQ_EDGE_FALL, true);

+ 1
- 0
firmware/src/usb.c Wyświetl plik

@@ -26,6 +26,7 @@
26 26
 #include "bsp/board.h"
27 27
 #include "tusb.h"
28 28
 
29
+#include "config.h"
29 30
 #include "usb_descriptors.h"
30 31
 #include "usb_cdc.h"
31 32
 #include "usb_hid.h"

+ 1
- 0
firmware/src/usb_cdc.c Wyświetl plik

@@ -26,6 +26,7 @@
26 26
 #include "bsp/board.h"
27 27
 #include "tusb.h"
28 28
 
29
+#include "config.h"
29 30
 #include "console.h"
30 31
 #include "log.h"
31 32
 #include "util.h"

+ 2
- 0
firmware/src/usb_descriptors.c Wyświetl plik

@@ -25,6 +25,8 @@
25 25
 
26 26
 #include "pico/unique_id.h"
27 27
 #include "tusb.h"
28
+
29
+#include "config.h"
28 30
 #include "usb_descriptors.h"
29 31
 
30 32
 /*

+ 2
- 1
firmware/src/usb_hid.c Wyświetl plik

@@ -26,6 +26,7 @@
26 26
 #include "bsp/board.h"
27 27
 #include "tusb.h"
28 28
 
29
+#include "config.h"
29 30
 #include "usb_descriptors.h"
30 31
 #include "usb_hid.h"
31 32
 
@@ -59,7 +60,7 @@ static void send_hid_report(uint8_t report_id, uint32_t btn) {
59 60
         case REPORT_ID_MOUSE:
60 61
         {
61 62
             // no button, right + down, no scroll, no pan
62
-            tud_hid_mouse_report(REPORT_ID_MOUSE, 0x00, delta_x, delta_y, 0, 0);
63
+            tud_hid_mouse_report(REPORT_ID_MOUSE, 0x00, -delta_x, -delta_y, 0, 0);
63 64
 
64 65
             // TODO
65 66
             delta_x = 0;

+ 11
- 0
firmware/src/util.c Wyświetl plik

@@ -2,8 +2,11 @@
2 2
  * util.c
3 3
  */
4 4
 
5
+#include <string.h>
5 6
 #include "pico/stdlib.h"
6 7
 #include "pico/bootrom.h"
8
+
9
+#include "config.h"
7 10
 #include "util.h"
8 11
 
9 12
 #define HEARTBEAT_INTERVAL_MS 500
@@ -30,6 +33,14 @@ void heartbeat_run(void) {
30 33
 #endif // PICO_DEFAULT_LED_PIN
31 34
 }
32 35
 
36
+bool str_startswith(const char *str, const char *start) {
37
+    size_t l = strlen(start);
38
+    if (l > strlen(str)) {
39
+        return false;
40
+    }
41
+    return (strncmp(str, start, l) == 0);
42
+}
43
+
33 44
 void reset_to_bootloader(void) {
34 45
 #ifdef PICO_DEFAULT_LED_PIN
35 46
     reset_usb_boot(1 << PICO_DEFAULT_LED_PIN, 0);

Ładowanie…
Anuluj
Zapisz