Create buttons module

Temporarily just logging button presses, as alarms module doesn't yet
exist.
This commit is contained in:
Camden Dixie O'Brien 2023-05-16 21:11:41 +01:00
parent 82334be492
commit c82af79d52
6 changed files with 128 additions and 3 deletions

View File

@ -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

View File

@ -0,0 +1,5 @@
idf_component_register(
SRCS "buttons.c"
INCLUDE_DIRS "."
REQUIRES driver esp_timer fatal
)

View File

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

View File

@ -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

View File

@ -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
)

View File

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