diff --git a/README.md b/README.md index 5a8045c..9484c9b 100644 --- a/README.md +++ b/README.md @@ -10,8 +10,7 @@ Networked bedside clock utilising an ESP32-SOLO-1. - [x] Console for configuration - [ ] TCP API for configuration - [x] SNTP synchronisation -- [ ] Dismiss button -- [ ] Snooze button +- [x] Snooze and dismiss buttons - [ ] Multiple alarms - [ ] Alarm conditions (e.g. weekday / weekend) - [ ] IPv6 support diff --git a/components/buttons/CMakeLists.txt b/components/buttons/CMakeLists.txt new file mode 100644 index 0000000..926ada3 --- /dev/null +++ b/components/buttons/CMakeLists.txt @@ -0,0 +1,5 @@ +idf_component_register( + SRCS "buttons.c" + INCLUDE_DIRS "." + REQUIRES driver esp_timer fatal +) diff --git a/components/buttons/buttons.c b/components/buttons/buttons.c new file mode 100644 index 0000000..14e802f --- /dev/null +++ b/components/buttons/buttons.c @@ -0,0 +1,103 @@ +/* + * SPDX-License-Identifier: AGPL-3.0-only + * Copyright (c) Camden Dixie O'Brien + */ + +#include "buttons.h" + +#include "fatal.h" + +#include "driver/gpio.h" +#include "esp_log.h" +#include "esp_timer.h" + +#define TAG "Buttons" + +#define PIN_BITMASK(n) ((uint64_t)1 << n) + +#define SNOOZE_PIN GPIO_NUM_34 +#define DISMISS_PIN GPIO_NUM_35 + +#define PRESS_HANDLE_PERIOD_US 300000UL + +static bool snooze_pressed; +static bool dismiss_pressed; + +static void handle_snooze_interrupt(void *arg) +{ + (void)arg; + snooze_pressed = true; +} + +static void handle_dismiss_interrupt(void *arg) +{ + (void)arg; + dismiss_pressed = true; +} + +static void handle_presses(void *arg) +{ + (void)arg; + if (snooze_pressed) { + ESP_LOGI(TAG, "Snooze pressed"); + snooze_pressed = false; + } + if (dismiss_pressed) { + ESP_LOGI(TAG, "Dismiss pressed"); + dismiss_pressed = false; + } +} + +void buttons_init() +{ + esp_err_t error; + + snooze_pressed = false; + dismiss_pressed = false; + + const gpio_config_t pin_config = { + .pin_bit_mask = PIN_BITMASK(SNOOZE_PIN) | PIN_BITMASK(DISMISS_PIN), + .mode = GPIO_MODE_INPUT, + .pull_down_en = GPIO_PULLDOWN_ENABLE, + .intr_type = GPIO_INTR_POSEDGE, + }; + error = gpio_config(&pin_config); + if (error != ESP_OK) { + ESP_LOGE(TAG, "Error configuring GPIO: %04x", error); + FATAL(); + } + + error = gpio_install_isr_service(0); + if (error != ESP_OK) { + ESP_LOGE( + TAG, "Error installing GPIO interrupt service: %04x", error); + FATAL(); + } + error = gpio_isr_handler_add(SNOOZE_PIN, &handle_snooze_interrupt, NULL); + if (error != ESP_OK) { + ESP_LOGE(TAG, "Error adding snooze interrupt handler: %04x", error); + } + error + = gpio_isr_handler_add(DISMISS_PIN, &handle_dismiss_interrupt, NULL); + if (error != ESP_OK) { + ESP_LOGE(TAG, "Error adding snooze interrupt handler: %04x", error); + FATAL(); + } + + const esp_timer_create_args_t timer_config = { + .callback = &handle_presses, + .name = "Buttons press handler", + }; + esp_timer_handle_t timer; + error = esp_timer_create(&timer_config, &timer); + if (error != ESP_OK) { + ESP_LOGE(TAG, "Error creating timer: %04x", error); + FATAL(); + } + + error = esp_timer_start_periodic(timer, PRESS_HANDLE_PERIOD_US); + if (error != ESP_OK) { + ESP_LOGE(TAG, "Error starting timer: %04x", error); + FATAL(); + } +} diff --git a/components/buttons/buttons.h b/components/buttons/buttons.h new file mode 100644 index 0000000..2cdfa8c --- /dev/null +++ b/components/buttons/buttons.h @@ -0,0 +1,16 @@ +/* + * SPDX-License-Identifier: AGPL-3.0-only + * Copyright (c) Camden Dixie O'Brien + */ + +#ifndef BUTTONS_H +#define BUTTONS_H + +typedef void (*ButtonCallback)(void); + +/** + * Initialize the buttons module. + */ +void buttons_init(void); + +#endif diff --git a/main/CMakeLists.txt b/main/CMakeLists.txt index 4a475cb..2ad0e8b 100644 --- a/main/CMakeLists.txt +++ b/main/CMakeLists.txt @@ -1,5 +1,5 @@ idf_component_register( SRCS "main.c" INCLUDE_DIRS "." - REQUIRES console_wrapper display settings sound time wifi + REQUIRES buttons console_wrapper display settings sound time wifi ) diff --git a/main/main.c b/main/main.c index 73cda53..957bc5d 100644 --- a/main/main.c +++ b/main/main.c @@ -3,6 +3,7 @@ * Copyright (c) Camden Dixie O'Brien */ +#include "buttons.h" #include "console.h" #include "display.h" #include "settings.h" @@ -18,4 +19,5 @@ void app_main(void) wifi_init(); time_manager_init(); sound_init(); + buttons_init(); }