Third
This commit is contained in:
@@ -0,0 +1,44 @@
|
||||
#include "buttons.h"
|
||||
#include <zephyr/kernel.h>
|
||||
#include <zephyr/device.h>
|
||||
#include <zephyr/logging/log.h>
|
||||
|
||||
LOG_MODULE_REGISTER(buttons, CONFIG_LOG_MAX_LEVEL);
|
||||
K_WORK_DELAYABLE_DEFINE(debounce_work_btn, debounce_cb);
|
||||
#define DEBOUNCE_TIME_MS 100
|
||||
|
||||
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) {
|
||||
// BUTTON 1
|
||||
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]);
|
||||
|
||||
// BUTTON 2
|
||||
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;
|
||||
}
|
||||
|
||||
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("Callback");
|
||||
}
|
||||
}
|
||||
|
||||
void buttons_cb(const struct device *dev, struct gpio_callback *cb, uint32_t pins) {
|
||||
LOG_DBG("Button:");
|
||||
if (!k_work_delayable_is_pending(&debounce_work_btn)) {
|
||||
k_work_schedule(&debounce_work_btn, K_MSEC(DEBOUNCE_TIME_MS));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user