Browse Source

add rudimentary gpio button reading

Thomas Buck 2 years ago
parent
commit
0e7e1b54e7

+ 2
- 0
firmware/CMakeLists.txt View File

@@ -46,6 +46,8 @@ target_sources(trackball PUBLIC
46 46
     src/usb_msc.c
47 47
     src/fat_disk.c
48 48
     src/debug.c
49
+    src/buttons.c
50
+    src/controls.c
49 51
     ${CMAKE_CURRENT_BINARY_DIR}/fatfs/ff.c
50 52
     ${CMAKE_CURRENT_BINARY_DIR}/fatfs/ffunicode.c
51 53
 )

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

@@ -0,0 +1,12 @@
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,4 +16,6 @@
16 16
 #define INVERT_MOUSE_Y_AXIS true
17 17
 #define DEFAULT_MOUSE_SENSITIVITY PMW_CPI_TO_SENSE(500)
18 18
 
19
+#define DEBOUNCE_DELAY_MS 5
20
+
19 21
 #endif // __CONFIG_H__

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

@@ -0,0 +1,12 @@
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

@@ -0,0 +1,45 @@
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

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

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

@@ -28,6 +28,7 @@
28 28
 
29 29
 #include "config.h"
30 30
 #include "pmw3360.h"
31
+#include "controls.h"
31 32
 #include "usb_descriptors.h"
32 33
 #include "usb_hid.h"
33 34
 
@@ -129,7 +130,7 @@ void hid_task(void) {
129 130
     if ( board_millis() - start_ms < interval_ms) return; // not enough time
130 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 135
     // Remote wakeup
135 136
     if ( tud_suspended() && btn ) {

Loading…
Cancel
Save