diff options
author | Maël Gassmann <mael.gassmann@students.bfh.ch> | 2024-10-11 02:18:54 +0200 |
---|---|---|
committer | Maël Gassmann <mael.gassmann@students.bfh.ch> | 2024-10-11 02:19:31 +0200 |
commit | 02dfa1385e25875659eec20313b8914ccbf60954 (patch) | |
tree | b8df240517543f2e4a55a9188ea5856abec932cf | |
parent | 62f508b029c4cc6d0819eb5b3e06353c902be2be (diff) |
[+] Button module with long / short press and release detectionmaster
-rw-r--r-- | .gitignore | 1 | ||||
-rw-r--r-- | components/button/CMakeLists.txt | 3 | ||||
-rw-r--r-- | components/button/button_handler.c | 76 | ||||
-rw-r--r-- | components/button/button_handler.h | 33 | ||||
-rw-r--r-- | components/screen/screen.c | 2 | ||||
-rw-r--r-- | main/Kconfig.projbuild | 16 | ||||
-rw-r--r-- | main/main.c | 22 |
7 files changed, 138 insertions, 15 deletions
@@ -3,6 +3,7 @@ lib-examples examples managed_components build +dependencies.lock .cache .vscode .devcontainer
\ No newline at end of file diff --git a/components/button/CMakeLists.txt b/components/button/CMakeLists.txt new file mode 100644 index 0000000..1497865 --- /dev/null +++ b/components/button/CMakeLists.txt @@ -0,0 +1,3 @@ +idf_component_register(SRCS "button_handler.c" + INCLUDE_DIRS "." + PRIV_REQUIRES esp_timer esp_driver_gpio) diff --git a/components/button/button_handler.c b/components/button/button_handler.c new file mode 100644 index 0000000..eb89212 --- /dev/null +++ b/components/button/button_handler.c @@ -0,0 +1,76 @@ +#include "button_handler.h" + +bool is_pressed(struct Button* b) +{ + return b->time > b->p_time; +} + +bool was_pressed(struct Button* b, int64_t since) +{ + return b->time >= b->p_time && b->time > since; +} + +bool was_pressed_and_released(struct Button* b, int64_t since) +{ + return b->time == b->p_time && b->time > since; +} + +bool was_short_pressed(struct Button* b, int64_t since) +{ + return (is_pressed(b) && (esp_timer_get_time() - b->time)/1000 < LONG_PRESS) || (was_pressed_and_released(b, since) && b->dt < LONG_PRESS); +} + +bool was_long_pressed(struct Button* b, int64_t since) +{ + return (is_pressed(b) && (esp_timer_get_time() - b->time)/1000 >= LONG_PRESS) || (was_pressed_and_released(b, since) && b->dt >= LONG_PRESS); +} + +bool was_short_pressed_and_released(struct Button* b, int64_t since) +{ + return was_pressed_and_released(b, since) && b->dt < LONG_PRESS; +} + +bool was_long_pressed_and_released(struct Button* b, int64_t since) +{ + return was_pressed_and_released(b, since) && b->dt >= LONG_PRESS; +} + +static void IRAM_ATTR button_handler(void* arg) { + struct Button* b = (struct Button*)arg; + xQueueSendFromISR(uinput_evt_queue, &b, NULL); +} + +void button_task(void* arg) { + struct Button* b; + while (1) { + if (xQueueReceive(uinput_evt_queue, &b, portMAX_DELAY)) { + uint8_t lvl = gpio_get_level(b->pin); + if (lvl == 0 && (b->time == b->p_time || b->time == 0)) { // Button pressed + b->time = esp_timer_get_time(); + printf("Button pressed\n"); + } else if (lvl == 1 && b->time != b->p_time) { // Button released + b->dt = (esp_timer_get_time() - b->time) / 1000; + printf("Button released after %d ms\n", b->dt); + b->p_time = b->time; + } + } + } +} + + +void setup_button(struct Button* b) { + b->pin = CONFIG_BUTTON_PIN; // Assume CONFIG_BUTTON_PIN is defined + + gpio_config_t io_conf = { + .intr_type = GPIO_INTR_ANYEDGE, + .pin_bit_mask = (1ULL << b->pin), + .mode = GPIO_MODE_INPUT, + .pull_up_en = GPIO_PULLUP_ENABLE, + }; + gpio_config(&io_conf); + + uinput_evt_queue = xQueueCreate(10, sizeof(struct Button*)); + xTaskCreate(button_task, "button_task", 2048, NULL, 10, NULL); + gpio_install_isr_service(ESP_INTR_FLAG_DEFAULT); + gpio_isr_handler_add(b->pin, button_handler, (void*)b); +}
\ No newline at end of file diff --git a/components/button/button_handler.h b/components/button/button_handler.h new file mode 100644 index 0000000..a980d5b --- /dev/null +++ b/components/button/button_handler.h @@ -0,0 +1,33 @@ +#ifndef BUTTON_HANDLER_H +#define BUTTON_HANDLER_H + +#include <stdio.h> +#include <stdint.h> +#include "freertos/FreeRTOS.h" +#include "freertos/task.h" +#include "driver/gpio.h" +#include "esp_timer.h" + +#define LONG_PRESS 500 +#define BUTTON_PIN CONFIG_BUTTON_PIN +#define ESP_INTR_FLAG_DEFAULT 0 + +struct Button { + uint8_t pin; + int64_t time; + int64_t p_time; + uint16_t dt; +}; +static QueueHandle_t uinput_evt_queue = NULL; + +void setup_button(struct Button* b); +void button_task(void* arg); +bool is_pressed(struct Button* b); +bool was_pressed(struct Button* b, int64_t since); +bool was_pressed_and_released(struct Button* b, int64_t since); +bool was_short_pressed(struct Button* b, int64_t since); +bool was_long_pressed(struct Button* b, int64_t since); +bool was_short_pressed_and_released(struct Button* b, int64_t since); +bool was_long_pressed_and_released(struct Button* b, int64_t since); + +#endif // BUTTON_HANDLER_H
\ No newline at end of file diff --git a/components/screen/screen.c b/components/screen/screen.c index 92f55de..bb4fbcc 100644 --- a/components/screen/screen.c +++ b/components/screen/screen.c @@ -180,7 +180,7 @@ void display(struct Screen* screen, char digits[4]) { for (uint8_t i = 0; i < 4; ++i) { uint8_t digit = digits[i] - '0'; - printf("%i", digit); + //printf("%i", digit); if (screen->nixies[i].digit != digit){ screen->nixies[i].digit = digit; set_digit(screen->mcp, &(screen->nixies[i])); // Call set_digit function diff --git a/main/Kconfig.projbuild b/main/Kconfig.projbuild index 4ff3093..85eb1ee 100644 --- a/main/Kconfig.projbuild +++ b/main/Kconfig.projbuild @@ -144,6 +144,12 @@ menu "Configuration of the Nixie Screen" default 3 help Pin number for N3_P3. + + config BUTTON_PIN + int "Pin number for the button" + default 9 + help + Pin number for the button. config ESP_DPP_LISTEN_CHANNEL_LIST string "DPP Listen channel list" @@ -166,14 +172,4 @@ menu "Configuration of the Nixie Screen" default "pool.ntp.org" help Hostname of the main SNTP server. - - choice SNTP_TIME_SYNC_METHOD - prompt "Time synchronization method" - default SNTP_TIME_SYNC_METHOD_IMMED - help - Time synchronization method. - - config SNTP_TIME_SYNC_METHOD_IMMED - bool "update time immediately when received" - endchoice endmenu
\ No newline at end of file diff --git a/main/main.c b/main/main.c index 81a51b1..16e1ca6 100644 --- a/main/main.c +++ b/main/main.c @@ -1,11 +1,13 @@ -#include <unistd.h> +#include <stdio.h> #include "freertos/FreeRTOS.h" #include "freertos/task.h" #include "freertos/event_groups.h" #include "screen.h" #include "dpp_wifi.h" #include "time_sntp.h" +#include "button_handler.h" +struct Button* button; struct Screen screen; char t[4]; @@ -25,26 +27,38 @@ void setup_nvs() ESP_ERROR_CHECK(ret); } + void app_main(void) { + printf("Starting up...\n"); + configure_screen(&screen); display(&screen, "aaaa"); printf("Screen Configured!\n"); + setup_nvs(); display(&screen, "0aaa"); printf("NVS Flash Configured!\n"); - launch_dpp(); + + button = (struct Button*)malloc(sizeof(struct Button)); + button->time = 0; + setup_button(button); display(&screen, "00aa"); + printf("User Inputs Configured!\n"); + + launch_dpp(); + display(&screen, "000a"); printf("WiFi Configured!\n"); + set_tz("CET-1CEST,M3.5.0/2,M10.5.0/2"); - display(&screen, "000a"); sync_time(); display(&screen, "0000"); printf("SNTP Configured!\n"); + while(1){ refresh_screen(); - sleep(2); + vTaskDelay(2000 / portTICK_PERIOD_MS); } }
\ No newline at end of file |