add crude debouncer

gui-framework
Ondřej Hruška 5 years ago
parent 6021778498
commit 98e040905e
Signed by: MightyPork
GPG Key ID: 2C5FD5035250423D
  1. 1
      .gitignore
  2. 2
      CMakeLists.txt
  3. 2
      main/gui.c
  4. 76
      main/knob.c
  5. 2
      sdkconfig

1
.gitignore vendored

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

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

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

@ -8,38 +8,46 @@ const int pushPin = 5;
const int wheelPin1 = 18; const int wheelPin1 = 18;
const int wheelPin2 = 23; const int wheelPin2 = 23;
#define DEBOUNCE_WHEEL_MS 6
#define DEBOUNCE_BTN_MS 20
static void handle_pushbtn(void *arg); static void handle_pushbtn(void *arg);
static void handle_wheel(void *arg); static void handle_wheel(void *arg);
void knob_init() { void knob_init() {
printf("Knob init\n"); printf("Knob init\n");
gpio_config_t cfgWheel = { // gpio_config_t cfgWheel = {
.pin_bit_mask = (1 << wheelPin1) | (1 << wheelPin2), // .pin_bit_mask = (1 << wheelPin1) /*| (1 << wheelPin2)*/,
.mode = GPIO_MODE_INPUT, // .mode = GPIO_MODE_INPUT,
.pull_up_en = 1, // .pull_up_en = 1,
.intr_type = GPIO_INTR_NEGEDGE, // neg means active // .intr_type = GPIO_INTR_NEGEDGE, // neg means active
}; // };
gpio_config(&cfgWheel); // gpio_config(&cfgWheel);
gpio_config_t cfgPush = { gpio_config_t cfgPush = {
.pin_bit_mask = (1 << pushPin), .pin_bit_mask = (1 << pushPin) | (1 << wheelPin1),
.mode = GPIO_MODE_INPUT, .mode = GPIO_MODE_INPUT,
.pull_up_en = 1, .pull_up_en = 1,
.intr_type = GPIO_INTR_ANYEDGE, // neg means active .intr_type = GPIO_INTR_ANYEDGE, // neg means active
}; };
gpio_config(&cfgPush); gpio_config(&cfgPush);
cfgPush.intr_type = GPIO_INTR_DISABLE;
cfgPush.pin_bit_mask = (1 << wheelPin2);
gpio_config(&cfgPush);
gpio_install_isr_service(0); gpio_install_isr_service(0);
gpio_intr_enable(pushPin); gpio_intr_enable(pushPin);
gpio_intr_enable(wheelPin1); gpio_intr_enable(wheelPin1);
gpio_intr_enable(wheelPin2); // gpio_intr_enable(wheelPin2);
gpio_isr_handler_add(pushPin, handle_pushbtn, (void *)0); gpio_isr_handler_add(pushPin, handle_pushbtn, (void *)0);
gpio_isr_handler_add(wheelPin1, handle_wheel, (void *)0); gpio_isr_handler_add(wheelPin1, handle_wheel, (void *)0);
gpio_isr_handler_add(wheelPin2, handle_wheel, (void *)1); //gpio_isr_handler_add(wheelPin2, handle_wheel, (void *)1);
} }
#if 0
enum state { enum state {
S_11 = 0, S_11 = 0,
@ -104,9 +112,37 @@ const uint8_t switch_table[28] = {
}; };
static uint8_t OldEnc = 0b00; static uint8_t OldEnc = 0b00;
#endif
static void handle_wheel(void *arg) { static void handle_wheel(void *arg) {
static uint32_t negedge_time = 0;
int dir = 0; int dir = 0;
const uint32_t inputs = gpio_input_get();
const bool a = 0 != (inputs & (1 << wheelPin1)); // neg = active
const bool b = 0 != (inputs & (1 << wheelPin2));
const uint32_t time = xTaskGetTickCount();
if (!a) {
// negedge
negedge_time = time;
} else {
// posedge
if (time - negedge_time > pdMS_TO_TICKS(DEBOUNCE_WHEEL_MS)) {
if (b) {
dir = -1;
} else {
dir = 1;
}
} else {
// reset the timer
negedge_time = time;
}
}
#if 0 #if 0
@ -151,7 +187,7 @@ static void handle_wheel(void *arg) {
}; // end if encoder value changed. }; // end if encoder value changed.
#endif #endif
#if 1 #if 0
// Omron version // Omron version
int which = (int)arg; int which = (int)arg;
@ -232,20 +268,24 @@ static void handle_wheel(void *arg) {
if (dir != 0) { if (dir != 0) {
BaseType_t higherWoken = 0; BaseType_t higherWoken = 0;
xTaskNotifyFromISR(hGuiThread, dir == 1 ? 0b10 : 0b01, eSetBits, &higherWoken); xTaskNotifyFromISR(hGuiThread, dir == 1 ? 0b10 : 0b01, eSetBits, &higherWoken);
if (higherWoken) { if (higherWoken) portYIELD_FROM_ISR();
portYIELD_FROM_ISR();
}
} }
} }
static void handle_pushbtn(void *arg) { static void handle_pushbtn(void *arg) {
static uint32_t negedge_time = 0;
const uint32_t inputs = gpio_input_get(); const uint32_t inputs = gpio_input_get();
BaseType_t higherWoken = 0; BaseType_t higherWoken = 0;
bool pushed = (inputs & (1 << pushPin)) == 0; bool pushed = (inputs & (1 << pushPin)) == 0;
xTaskNotifyFromISR(hGuiThread, pushed ? 0b1000 : 0b0100, eSetBits, &higherWoken);
if (higherWoken) { const uint32_t time = xTaskGetTickCount();
portYIELD_FROM_ISR(); if (pushed) {
negedge_time = time;
} else {
if (time - negedge_time > pdMS_TO_TICKS(DEBOUNCE_BTN_MS)) {
xTaskNotifyFromISR(hGuiThread, pushed ? 0b1000 : 0b0100, eSetBits, &higherWoken);
if (higherWoken) portYIELD_FROM_ISR();
}
} }
} }

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

Loading…
Cancel
Save