Browse Source

add crude debouncer

Ondřej Hruška 1 year ago
parent
commit
98e040905e
Signed by: Ondřej Hruška <ondra@ondrovo.com> GPG key ID: 2C5FD5035250423D
5 changed files with 62 additions and 21 deletions
  1. 1 0
      .gitignore
  2. 1 1
      CMakeLists.txt
  3. 1 1
      main/gui.c
  4. 58 18
      main/knob.c
  5. 1 1
      sdkconfig

+ 1 - 0
.gitignore View File

@@ -2,3 +2,4 @@ cmake-build-debug
2 2
 *.o
3 3
 build/
4 4
 .idea/
5
+sdkconfig.old

+ 1 - 1
CMakeLists.txt View File

@@ -7,4 +7,4 @@ set(CMAKE_CXX_COMPILER xtensa-esp32-elf-g++)
7 7
 set(CMAKE_ASM_COMPILER xtensa-esp32-elf-as)
8 8
 
9 9
 include($ENV{IDF_PATH}/tools/cmake/project.cmake)
10
-project(hello-world)
10
+project(reflower)

+ 1 - 1
main/gui.c View File

@@ -15,7 +15,7 @@ void gui_init() {
15 15
     LCD_setStr("Hello World", 0, 0, 1);
16 16
     LCD_updateDisplay();
17 17
 
18
-    int rv = xTaskCreate(gui_thread, "gui", 4096, NULL, 3, &hGuiThread);
18
+    int rv = xTaskCreate(gui_thread, "gui", 4096, NULL, 6, &hGuiThread);
19 19
     assert (rv == pdPASS);
20 20
 }
21 21
 

+ 58 - 18
main/knob.c View File

@@ -8,38 +8,46 @@ const int pushPin = 5;
8 8
 const int wheelPin1 = 18;
9 9
 const int wheelPin2 = 23;
10 10
 
11
+#define DEBOUNCE_WHEEL_MS 6
12
+#define DEBOUNCE_BTN_MS 20
13
+
11 14
 static void handle_pushbtn(void *arg);
12 15
 static void handle_wheel(void *arg);
13 16
 
14 17
 void knob_init() {
15 18
     printf("Knob init\n");
16 19
 
17
-    gpio_config_t cfgWheel = {
18
-        .pin_bit_mask = (1 << wheelPin1) | (1 << wheelPin2),
19
-        .mode = GPIO_MODE_INPUT,
20
-        .pull_up_en = 1,
21
-        .intr_type = GPIO_INTR_NEGEDGE, // neg means active
22
-    };
23
-    gpio_config(&cfgWheel);
20
+//    gpio_config_t cfgWheel = {
21
+//        .pin_bit_mask = (1 << wheelPin1) /*| (1 << wheelPin2)*/,
22
+//        .mode = GPIO_MODE_INPUT,
23
+//        .pull_up_en = 1,
24
+//        .intr_type = GPIO_INTR_NEGEDGE, // neg means active
25
+//    };
26
+//    gpio_config(&cfgWheel);
24 27
 
25 28
     gpio_config_t cfgPush = {
26
-        .pin_bit_mask = (1 << pushPin),
29
+        .pin_bit_mask = (1 << pushPin) | (1 << wheelPin1),
27 30
         .mode = GPIO_MODE_INPUT,
28 31
         .pull_up_en = 1,
29 32
         .intr_type = GPIO_INTR_ANYEDGE, // neg means active
30 33
     };
31 34
     gpio_config(&cfgPush);
32 35
 
36
+    cfgPush.intr_type = GPIO_INTR_DISABLE;
37
+    cfgPush.pin_bit_mask = (1 << wheelPin2);
38
+    gpio_config(&cfgPush);
39
+
33 40
     gpio_install_isr_service(0);
34 41
     gpio_intr_enable(pushPin);
35 42
     gpio_intr_enable(wheelPin1);
36
-    gpio_intr_enable(wheelPin2);
43
+//    gpio_intr_enable(wheelPin2);
37 44
 
38 45
     gpio_isr_handler_add(pushPin, handle_pushbtn, (void *)0);
39 46
     gpio_isr_handler_add(wheelPin1, handle_wheel, (void *)0);
40
-    gpio_isr_handler_add(wheelPin2, handle_wheel, (void *)1);
47
+    //gpio_isr_handler_add(wheelPin2, handle_wheel, (void *)1);
41 48
 }
42 49
 
50
+#if 0
43 51
 enum state {
44 52
     S_11 = 0,
45 53
 
@@ -104,9 +112,37 @@ const uint8_t switch_table[28] = {
104 112
 };
105 113
 
106 114
 static uint8_t OldEnc = 0b00;
115
+#endif
107 116
 
108 117
 static void handle_wheel(void *arg) {
118
+    static uint32_t negedge_time = 0;
109 119
     int dir = 0;
120
+    const uint32_t inputs = gpio_input_get();
121
+    const bool a = 0 != (inputs & (1 << wheelPin1)); // neg = active
122
+    const bool b = 0 != (inputs & (1 << wheelPin2));
123
+
124
+    const uint32_t time = xTaskGetTickCount();
125
+
126
+    if (!a) {
127
+        // negedge
128
+        negedge_time = time;
129
+    } else {
130
+        // posedge
131
+        if (time - negedge_time > pdMS_TO_TICKS(DEBOUNCE_WHEEL_MS)) {
132
+            if (b) {
133
+                dir = -1;
134
+            } else {
135
+                dir = 1;
136
+            }
137
+        } else {
138
+            // reset the timer
139
+            negedge_time = time;
140
+        }
141
+    }
142
+
143
+
144
+
145
+
110 146
 
111 147
 
112 148
 #if 0
@@ -151,7 +187,7 @@ static void handle_wheel(void *arg) {
151 187
     }; // end if encoder value changed.
152 188
 #endif
153 189
 
154
-#if 1
190
+#if 0
155 191
     // Omron version
156 192
     int which = (int)arg;
157 193
 
@@ -232,20 +268,24 @@ static void handle_wheel(void *arg) {
232 268
     if (dir != 0) {
233 269
         BaseType_t higherWoken = 0;
234 270
         xTaskNotifyFromISR(hGuiThread, dir == 1 ? 0b10 : 0b01, eSetBits, &higherWoken);
235
-        if (higherWoken) {
236
-            portYIELD_FROM_ISR();
237
-        }
271
+        if (higherWoken) portYIELD_FROM_ISR();
238 272
     }
239 273
 }
240 274
 
241 275
 static void handle_pushbtn(void *arg) {
276
+    static uint32_t negedge_time = 0;
277
+
242 278
     const uint32_t inputs = gpio_input_get();
243 279
     BaseType_t higherWoken = 0;
244
-
245 280
     bool pushed = (inputs & (1 << pushPin)) == 0;
246
-    xTaskNotifyFromISR(hGuiThread, pushed ? 0b1000 : 0b0100, eSetBits, &higherWoken);
247 281
 
248
-    if (higherWoken) {
249
-        portYIELD_FROM_ISR();
282
+    const uint32_t time = xTaskGetTickCount();
283
+    if (pushed) {
284
+        negedge_time = time;
285
+    } else {
286
+        if (time - negedge_time > pdMS_TO_TICKS(DEBOUNCE_BTN_MS)) {
287
+            xTaskNotifyFromISR(hGuiThread, pushed ? 0b1000 : 0b0100, eSetBits, &higherWoken);
288
+            if (higherWoken) portYIELD_FROM_ISR();
289
+        }
250 290
     }
251 291
 }

+ 1 - 1
sdkconfig View File

@@ -281,7 +281,7 @@ CONFIG_MB_TIMER_INDEX=0
281 281
 CONFIG_FREERTOS_NO_AFFINITY=0x7FFFFFFF
282 282
 CONFIG_FREERTOS_CORETIMER_0=y
283 283
 # CONFIG_FREERTOS_CORETIMER_1 is not set
284
-CONFIG_FREERTOS_HZ=100
284
+CONFIG_FREERTOS_HZ=1000
285 285
 CONFIG_FREERTOS_ASSERT_ON_UNTESTED_FUNCTION=y
286 286
 # CONFIG_FREERTOS_CHECK_STACKOVERFLOW_NONE is not set
287 287
 # CONFIG_FREERTOS_CHECK_STACKOVERFLOW_PTRVAL is not set