add crude debouncer

This commit is contained in:
2020-01-03 10:41:27 +01:00
parent 6021778498
commit 98e040905e
5 changed files with 62 additions and 21 deletions
+1 -1
View File
@@ -15,7 +15,7 @@ void gui_init() {
LCD_setStr("Hello World", 0, 0, 1);
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);
}
+58 -18
View File
@@ -8,38 +8,46 @@ const int pushPin = 5;
const int wheelPin1 = 18;
const int wheelPin2 = 23;
#define DEBOUNCE_WHEEL_MS 6
#define DEBOUNCE_BTN_MS 20
static void handle_pushbtn(void *arg);
static void handle_wheel(void *arg);
void knob_init() {
printf("Knob init\n");
gpio_config_t cfgWheel = {
.pin_bit_mask = (1 << wheelPin1) | (1 << wheelPin2),
.mode = GPIO_MODE_INPUT,
.pull_up_en = 1,
.intr_type = GPIO_INTR_NEGEDGE, // neg means active
};
gpio_config(&cfgWheel);
// gpio_config_t cfgWheel = {
// .pin_bit_mask = (1 << wheelPin1) /*| (1 << wheelPin2)*/,
// .mode = GPIO_MODE_INPUT,
// .pull_up_en = 1,
// .intr_type = GPIO_INTR_NEGEDGE, // neg means active
// };
// gpio_config(&cfgWheel);
gpio_config_t cfgPush = {
.pin_bit_mask = (1 << pushPin),
.pin_bit_mask = (1 << pushPin) | (1 << wheelPin1),
.mode = GPIO_MODE_INPUT,
.pull_up_en = 1,
.intr_type = GPIO_INTR_ANYEDGE, // neg means active
};
gpio_config(&cfgPush);
cfgPush.intr_type = GPIO_INTR_DISABLE;
cfgPush.pin_bit_mask = (1 << wheelPin2);
gpio_config(&cfgPush);
gpio_install_isr_service(0);
gpio_intr_enable(pushPin);
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(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 {
S_11 = 0,
@@ -104,9 +112,37 @@ const uint8_t switch_table[28] = {
};
static uint8_t OldEnc = 0b00;
#endif
static void handle_wheel(void *arg) {
static uint32_t negedge_time = 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
@@ -151,7 +187,7 @@ static void handle_wheel(void *arg) {
}; // end if encoder value changed.
#endif
#if 1
#if 0
// Omron version
int which = (int)arg;
@@ -232,20 +268,24 @@ static void handle_wheel(void *arg) {
if (dir != 0) {
BaseType_t higherWoken = 0;
xTaskNotifyFromISR(hGuiThread, dir == 1 ? 0b10 : 0b01, eSetBits, &higherWoken);
if (higherWoken) {
portYIELD_FROM_ISR();
}
if (higherWoken) portYIELD_FROM_ISR();
}
}
static void handle_pushbtn(void *arg) {
static uint32_t negedge_time = 0;
const uint32_t inputs = gpio_input_get();
BaseType_t higherWoken = 0;
bool pushed = (inputs & (1 << pushPin)) == 0;
xTaskNotifyFromISR(hGuiThread, pushed ? 0b1000 : 0b0100, eSetBits, &higherWoken);
if (higherWoken) {
portYIELD_FROM_ISR();
const uint32_t time = xTaskGetTickCount();
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();
}
}
}