feat(button): 添加按键模块及初始化按键去皮功能
This commit is contained in:
@@ -0,0 +1,75 @@
|
||||
#include "button.h"
|
||||
#include "sensor.h"
|
||||
|
||||
#include <zephyr/drivers/gpio.h>
|
||||
#include <zephyr/kernel.h>
|
||||
#include <zephyr/logging/log.h>
|
||||
|
||||
LOG_MODULE_REGISTER(button, LOG_LEVEL_INF);
|
||||
|
||||
/* DK Button1 = sw0, LED1 = led0 */
|
||||
static const struct gpio_dt_spec btn1 = GPIO_DT_SPEC_GET(DT_ALIAS(sw0), gpios);
|
||||
static const struct gpio_dt_spec led1 = GPIO_DT_SPEC_GET(DT_ALIAS(led0), gpios);
|
||||
|
||||
static struct gpio_callback btn_cb_data;
|
||||
static struct k_work_delayable led_off_work;
|
||||
|
||||
#define LED_FLASH_MS 200
|
||||
#define LED_BLINK_COUNT 3
|
||||
|
||||
static struct k_work tare_work;
|
||||
static volatile int blink_remaining;
|
||||
|
||||
static void led_off_handler(struct k_work *work) {
|
||||
if (blink_remaining <= 0) return;
|
||||
|
||||
blink_remaining--;
|
||||
/* 奇数次:点亮;偶数次:熄灭 */
|
||||
gpio_pin_set_dt(&led1, blink_remaining & 1);
|
||||
if (blink_remaining > 0) {
|
||||
k_work_schedule(&led_off_work, K_MSEC(LED_FLASH_MS));
|
||||
}
|
||||
}
|
||||
|
||||
static void tare_work_handler(struct k_work *work) {
|
||||
LOG_INF("Button1 pressed → tare");
|
||||
sensor_perform_tare();
|
||||
|
||||
/* LED1 闪烁 3 次:亮-灭-亮-灭-亮-灭 = 6 个状态翻转 */
|
||||
blink_remaining = LED_BLINK_COUNT * 2;
|
||||
gpio_pin_set_dt(&led1, 1);
|
||||
k_work_schedule(&led_off_work, K_MSEC(LED_FLASH_MS));
|
||||
}
|
||||
|
||||
static void btn1_isr(const struct device *dev, struct gpio_callback *cb, uint32_t pins) {
|
||||
k_work_submit(&tare_work);
|
||||
}
|
||||
|
||||
int button_init(void) {
|
||||
if (!gpio_is_ready_dt(&btn1) || !gpio_is_ready_dt(&led1)) {
|
||||
LOG_ERR("GPIO device not ready");
|
||||
return -ENODEV;
|
||||
}
|
||||
|
||||
int err;
|
||||
|
||||
err = gpio_pin_configure_dt(&btn1, GPIO_INPUT);
|
||||
if (err) return err;
|
||||
|
||||
err = gpio_pin_interrupt_configure_dt(&btn1, GPIO_INT_EDGE_TO_ACTIVE);
|
||||
if (err) return err;
|
||||
|
||||
err = gpio_pin_configure_dt(&led1, GPIO_OUTPUT_INACTIVE);
|
||||
if (err) return err;
|
||||
|
||||
gpio_init_callback(&btn_cb_data, btn1_isr, BIT(btn1.pin));
|
||||
|
||||
err = gpio_add_callback(btn1.port, &btn_cb_data);
|
||||
if (err) return err;
|
||||
|
||||
k_work_init(&tare_work, tare_work_handler);
|
||||
k_work_init_delayable(&led_off_work, led_off_handler);
|
||||
|
||||
LOG_INF("Button1 → tare + LED1 flash initialized");
|
||||
return 0;
|
||||
}
|
||||
+10
-1
@@ -8,6 +8,7 @@
|
||||
*/
|
||||
|
||||
#include "ble_transport.h"
|
||||
#include "button.h"
|
||||
#include "sensor.h"
|
||||
|
||||
#include <zephyr/kernel.h>
|
||||
@@ -22,7 +23,15 @@ int main(void) {
|
||||
ble_transport_init();
|
||||
ble_transport_adv_start();
|
||||
|
||||
if (sensor_init()) return 0;
|
||||
if (button_init()) {
|
||||
LOG_ERR("Failed to initialize buttons");
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (sensor_init()) {
|
||||
LOG_ERR("Failed to initialize sensor");
|
||||
return -1;
|
||||
}
|
||||
|
||||
k_sleep(K_FOREVER);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user