Kaynağa Gözat

allow setting sensitivity

Thomas Buck 2 yıl önce
ebeveyn
işleme
1f482c35bd

+ 15
- 0
firmware/include/config.h Dosyayı Görüntüle

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 Dosyayı Görüntüle

7
 
7
 
8
 int pmw_init(void);
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
 #endif // __PMW3360_H__
25
 #endif // __PMW3360_H__

+ 2
- 0
firmware/include/util.h Dosyayı Görüntüle

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

+ 25
- 4
firmware/src/console.c Dosyayı Görüntüle

2
  * console.c
2
  * console.c
3
  */
3
  */
4
 
4
 
5
+#include <inttypes.h>
5
 #include <string.h>
6
 #include <string.h>
6
 #include "pico/stdlib.h"
7
 #include "pico/stdlib.h"
8
+
9
+#include "config.h"
7
 #include "log.h"
10
 #include "log.h"
8
 #include "pmw3360.h"
11
 #include "pmw3360.h"
12
+#include "util.h"
9
 #include "console.h"
13
 #include "console.h"
10
 
14
 
11
 #define CNSL_BUFF_SIZE 1024
15
 #define CNSL_BUFF_SIZE 1024
18
             || (strcmp(line, "h") == 0)
22
             || (strcmp(line, "h") == 0)
19
             || (strcmp(line, "?") == 0)) {
23
             || (strcmp(line, "?") == 0)) {
20
         print("Trackball Firmware Usage:");
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
     } else if (strcmp(line, "pmw") == 0) {
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
     } else {
47
     } else {
27
         print("unknown command \"%s\"", line);
48
         print("unknown command \"%s\"", line);
28
     }
49
     }

+ 2
- 0
firmware/src/log.c Dosyayı Görüntüle

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

+ 1
- 0
firmware/src/main.c Dosyayı Görüntüle

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

+ 26
- 7
firmware/src/pmw3360.c Dosyayı Görüntüle

21
 #include "pico/binary_info.h"
21
 #include "pico/binary_info.h"
22
 #include "hardware/spi.h"
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
 #include "log.h"
25
 #include "log.h"
29
 #include "pmw3360_registers.h"
26
 #include "pmw3360_registers.h"
30
 #include "pmw3360_srom.h"
27
 #include "pmw3360_srom.h"
34
 #error PMW3360 API requires a board with SPI pins
31
 #error PMW3360 API requires a board with SPI pins
35
 #endif
32
 #endif
36
 
33
 
37
-#define PMW_MOTION_PIN 20
38
-
39
 #ifdef PMW_IRQ_COUNTERS
34
 #ifdef PMW_IRQ_COUNTERS
40
 static uint64_t pmw_irq_count_all = 0;
35
 static uint64_t pmw_irq_count_all = 0;
41
 static uint64_t pmw_irq_count_motion = 0;
36
 static uint64_t pmw_irq_count_motion = 0;
48
 static uint64_t pmw_irq_count_rest3 = 0;
43
 static uint64_t pmw_irq_count_rest3 = 0;
49
 #endif // PMW_IRQ_COUNTERS
44
 #endif // PMW_IRQ_COUNTERS
50
 
45
 
51
-void print_pmw_status(void) {
46
+void pmw_print_status(void) {
52
 #ifdef PMW_IRQ_COUNTERS
47
 #ifdef PMW_IRQ_COUNTERS
53
     print("    pmw_irq_cnt_all = %llu", pmw_irq_count_all);
48
     print("    pmw_irq_cnt_all = %llu", pmw_irq_count_all);
54
     print(" pmw_irq_cnt_motion = %llu", pmw_irq_count_motion);
49
     print(" pmw_irq_cnt_motion = %llu", pmw_irq_count_motion);
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
 int pmw_init(void) {
299
 int pmw_init(void) {
285
     pmw_spi_init();
300
     pmw_spi_init();
286
 
301
 
322
     pmw_write_register(REG_CONFIG2, 0x00);
337
     pmw_write_register(REG_CONFIG2, 0x00);
323
 #endif // PMW_FEATURE_WIRELESS
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
     // setup MOTION pin interrupt to handle reading data
344
     // setup MOTION pin interrupt to handle reading data
326
     gpio_add_raw_irq_handler(PMW_MOTION_PIN, pmw_motion_irq);
345
     gpio_add_raw_irq_handler(PMW_MOTION_PIN, pmw_motion_irq);
327
     gpio_set_irq_enabled(PMW_MOTION_PIN, GPIO_IRQ_EDGE_FALL, true);
346
     gpio_set_irq_enabled(PMW_MOTION_PIN, GPIO_IRQ_EDGE_FALL, true);

+ 1
- 0
firmware/src/usb.c Dosyayı Görüntüle

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

+ 1
- 0
firmware/src/usb_cdc.c Dosyayı Görüntüle

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

+ 2
- 0
firmware/src/usb_descriptors.c Dosyayı Görüntüle

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

+ 2
- 1
firmware/src/usb_hid.c Dosyayı Görüntüle

26
 #include "bsp/board.h"
26
 #include "bsp/board.h"
27
 #include "tusb.h"
27
 #include "tusb.h"
28
 
28
 
29
+#include "config.h"
29
 #include "usb_descriptors.h"
30
 #include "usb_descriptors.h"
30
 #include "usb_hid.h"
31
 #include "usb_hid.h"
31
 
32
 
59
         case REPORT_ID_MOUSE:
60
         case REPORT_ID_MOUSE:
60
         {
61
         {
61
             // no button, right + down, no scroll, no pan
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
             // TODO
65
             // TODO
65
             delta_x = 0;
66
             delta_x = 0;

+ 11
- 0
firmware/src/util.c Dosyayı Görüntüle

2
  * util.c
2
  * util.c
3
  */
3
  */
4
 
4
 
5
+#include <string.h>
5
 #include "pico/stdlib.h"
6
 #include "pico/stdlib.h"
6
 #include "pico/bootrom.h"
7
 #include "pico/bootrom.h"
8
+
9
+#include "config.h"
7
 #include "util.h"
10
 #include "util.h"
8
 
11
 
9
 #define HEARTBEAT_INTERVAL_MS 500
12
 #define HEARTBEAT_INTERVAL_MS 500
30
 #endif // PICO_DEFAULT_LED_PIN
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
 void reset_to_bootloader(void) {
44
 void reset_to_bootloader(void) {
34
 #ifdef PICO_DEFAULT_LED_PIN
45
 #ifdef PICO_DEFAULT_LED_PIN
35
     reset_usb_boot(1 << PICO_DEFAULT_LED_PIN, 0);
46
     reset_usb_boot(1 << PICO_DEFAULT_LED_PIN, 0);

Loading…
İptal
Kaydet