/* * 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(); } }