Browse Source

add rudimentary gpio button reading

Thomas Buck 1 year ago
parent
commit
0e7e1b54e7

+ 2
- 0
firmware/CMakeLists.txt View File

46
     src/usb_msc.c
46
     src/usb_msc.c
47
     src/fat_disk.c
47
     src/fat_disk.c
48
     src/debug.c
48
     src/debug.c
49
+    src/buttons.c
50
+    src/controls.c
49
     ${CMAKE_CURRENT_BINARY_DIR}/fatfs/ff.c
51
     ${CMAKE_CURRENT_BINARY_DIR}/fatfs/ff.c
50
     ${CMAKE_CURRENT_BINARY_DIR}/fatfs/ffunicode.c
52
     ${CMAKE_CURRENT_BINARY_DIR}/fatfs/ffunicode.c
51
 )
53
 )

+ 12
- 0
firmware/include/buttons.h View File

1
+/*
2
+ * buttons.h
3
+ */
4
+
5
+#ifndef __BUTTONS_H__
6
+#define __BUTTONS_H__
7
+
8
+void buttons_init(void);
9
+void buttons_run(void);
10
+
11
+#endif // __BUTTONS_H__
12
+

+ 2
- 0
firmware/include/config.h View File

16
 #define INVERT_MOUSE_Y_AXIS true
16
 #define INVERT_MOUSE_Y_AXIS true
17
 #define DEFAULT_MOUSE_SENSITIVITY PMW_CPI_TO_SENSE(500)
17
 #define DEFAULT_MOUSE_SENSITIVITY PMW_CPI_TO_SENSE(500)
18
 
18
 
19
+#define DEBOUNCE_DELAY_MS 5
20
+
19
 #endif // __CONFIG_H__
21
 #endif // __CONFIG_H__

+ 12
- 0
firmware/include/controls.h View File

1
+/*
2
+ * controls.h
3
+ */
4
+
5
+#ifndef __CONTROLS_H__
6
+#define __CONTROLS_H__
7
+
8
+void controls_new(int id, bool state);
9
+bool controls_button_read(void);
10
+
11
+#endif // __CONTROLS_H__
12
+

+ 45
- 0
firmware/src/buttons.c View File

1
+/*
2
+ * buttons.c
3
+ */
4
+
5
+#include "pico/stdlib.h"
6
+
7
+#include "config.h"
8
+#include "controls.h"
9
+#include "buttons.h"
10
+
11
+#define BUTTONS_COUNT 4
12
+uint gpio_num[BUTTONS_COUNT] = { 21, 22, 26, 27 };
13
+
14
+struct button_state {
15
+    uint32_t last_time;
16
+    bool last_state;
17
+};
18
+
19
+struct button_state buttons[BUTTONS_COUNT];
20
+
21
+void buttons_init(void) {
22
+    for (int i = 0; i < BUTTONS_COUNT; i++) {
23
+        gpio_init(gpio_num[i]);
24
+        gpio_pull_up(gpio_num[i]);
25
+    }
26
+}
27
+
28
+void buttons_run(void) {
29
+    for (int i = 0; i < BUTTONS_COUNT; i++) {
30
+        bool state = gpio_get(gpio_num[i]);
31
+        uint32_t now = to_ms_since_boot(get_absolute_time());
32
+
33
+        if (state != buttons[i].last_state) {
34
+            buttons[i].last_time = now;
35
+        }
36
+
37
+        if ((now - buttons[i].last_time) > DEBOUNCE_DELAY_MS) {
38
+            if (state != buttons[i].last_state) {
39
+                buttons[i].last_state = state;
40
+                controls_new(i, state);
41
+            }
42
+        }
43
+    }
44
+}
45
+

+ 20
- 0
firmware/src/controls.c View File

1
+/*
2
+ * controls.c
3
+ */
4
+
5
+#include "pico/stdlib.h"
6
+
7
+#include "config.h"
8
+#include "controls.h"
9
+
10
+static bool button_state = false;
11
+
12
+void controls_new(int id, bool state) {
13
+    (void)id;
14
+    button_state = state;
15
+}
16
+
17
+bool controls_button_read(void) {
18
+    return button_state;
19
+}
20
+

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

12
 #include "usb.h"
12
 #include "usb.h"
13
 #include "pmw3360.h"
13
 #include "pmw3360.h"
14
 #include "fat_disk.h"
14
 #include "fat_disk.h"
15
+#include "buttons.h"
15
 
16
 
16
 int main(void) {
17
 int main(void) {
17
     heartbeat_init();
18
     heartbeat_init();
19
+    buttons_init();
18
 
20
 
19
     cnsl_init();
21
     cnsl_init();
20
     usb_init();
22
     usb_init();
33
 
35
 
34
     // trigger after 500ms
36
     // trigger after 500ms
35
     // (PMW3360 initialization takes ~160ms)
37
     // (PMW3360 initialization takes ~160ms)
36
-    watchdog_enable(500, 1);
38
+    //watchdog_enable(500, 1);
37
 
39
 
38
     debug("init done");
40
     debug("init done");
39
 
41
 
41
         watchdog_update();
43
         watchdog_update();
42
 
44
 
43
         heartbeat_run();
45
         heartbeat_run();
46
+        buttons_run();
44
         usb_run();
47
         usb_run();
45
         cnsl_run();
48
         cnsl_run();
46
         pmw_run();
49
         pmw_run();

+ 2
- 1
firmware/src/usb_hid.c View File

28
 
28
 
29
 #include "config.h"
29
 #include "config.h"
30
 #include "pmw3360.h"
30
 #include "pmw3360.h"
31
+#include "controls.h"
31
 #include "usb_descriptors.h"
32
 #include "usb_descriptors.h"
32
 #include "usb_hid.h"
33
 #include "usb_hid.h"
33
 
34
 
129
     if ( board_millis() - start_ms < interval_ms) return; // not enough time
130
     if ( board_millis() - start_ms < interval_ms) return; // not enough time
130
     start_ms += interval_ms;
131
     start_ms += interval_ms;
131
 
132
 
132
-    uint32_t const btn = board_button_read();
133
+    uint32_t const btn = controls_button_read();
133
 
134
 
134
     // Remote wakeup
135
     // Remote wakeup
135
     if ( tud_suspended() && btn ) {
136
     if ( tud_suspended() && btn ) {

Loading…
Cancel
Save