Files
chill/src/buttons.c
T
2026-05-21 11:43:09 +03:00

51 lines
1.7 KiB
C

#include "buttons.h"
#include <zephyr/kernel.h>
#include <zephyr/device.h>
#include <zephyr/logging/log.h>
LOG_MODULE_REGISTER(buttons, CONFIG_LOG_MAX_LEVEL);
#define DEBOUNCE_TIME_MS 100
/* Forward declarations for internal callbacks */
static void debounce_cb(struct k_work *work);
static void buttons_cb(const struct device *dev, struct gpio_callback *cb, uint32_t pins);
K_WORK_DELAYABLE_DEFINE(debounce_work_btn, debounce_cb);
static const struct gpio_dt_spec buttons[NR_BUTTONS] = {
GPIO_DT_SPEC_GET_OR(DT_ALIAS(int_btn_1), gpios, {0}),
GPIO_DT_SPEC_GET_OR(DT_ALIAS(int_btn_2), gpios, {0}),
};
static struct gpio_callback buttons_cb_data[NR_BUTTONS];
int buttons_init(void)
{
gpio_pin_configure_dt(&buttons[BUTTON1], GPIO_INPUT);
gpio_pin_interrupt_configure_dt(&buttons[BUTTON1], GPIO_INT_EDGE_TO_ACTIVE);
gpio_init_callback(&buttons_cb_data[0], buttons_cb, BIT(buttons[BUTTON1].pin));
gpio_add_callback(buttons[BUTTON1].port, &buttons_cb_data[0]);
gpio_pin_configure_dt(&buttons[BUTTON2], GPIO_INPUT);
gpio_pin_interrupt_configure_dt(&buttons[BUTTON2], GPIO_INT_EDGE_TO_ACTIVE);
gpio_init_callback(&buttons_cb_data[1], buttons_cb, BIT(buttons[BUTTON2].pin));
gpio_add_callback(buttons[BUTTON2].port, &buttons_cb_data[1]);
return 0;
}
static void debounce_cb(struct k_work *work)
{
if (gpio_pin_get_dt(&buttons[BUTTON1]) == GPIO_ACTIVE_HIGH ||
gpio_pin_get_dt(&buttons[BUTTON2]) == GPIO_ACTIVE_HIGH) {
LOG_DBG("Button active after debounce");
}
}
static void buttons_cb(const struct device *dev, struct gpio_callback *cb, uint32_t pins)
{
if (!k_work_delayable_is_pending(&debounce_work_btn)) {
k_work_schedule(&debounce_work_btn, K_MSEC(DEBOUNCE_TIME_MS));
}
}