BLE_TYQ_BJQ_CH32V303/bsp/src/bsp_button.c

177 lines
4.8 KiB
C
Raw Normal View History

2024-12-01 13:49:43 +08:00
#include <rtthread.h>
#include "flexible_button.h"
#include "bsp_button.h"
2024-12-30 17:51:13 +08:00
#include "bsp_flash.h"
2024-12-01 13:49:43 +08:00
#include "bsp_led.h"
#include "bsp_beep.h"
2024-12-30 17:51:13 +08:00
#include "bsp_bt.h"
2024-12-01 13:49:43 +08:00
#include "user_sys.h"
2025-02-21 15:33:29 +08:00
#include "bsp_mq.h"
2024-12-01 13:49:43 +08:00
#define LOG_TAG "bsp_button"
#define LOG_LVL LOG_LVL_DBG
#include <ulog.h>
#ifdef BUTTON_USE_THREAD
ALIGN(RT_ALIGN_SIZE)
static char button_thread_stack[BUTTON_THREAD_STACK_SIZE];
static struct rt_thread button_thread;
#else
static rt_timer_t button_timer;
#endif // !BUTTON_USE_THREAD
typedef enum
{
USER_BUTTON_0 = 0,
2024-12-30 17:51:13 +08:00
USER_BUTTON_1,
2024-12-01 13:49:43 +08:00
USER_BUTTON_MAX
} user_button_t;
static flex_button_t user_button[USER_BUTTON_MAX];
static uint8_t _CommonBtnRead(void *arg)
{
uint8_t value = 0;
flex_button_t *btn = (flex_button_t *)arg;
switch (btn->id)
{
case USER_BUTTON_0:
value = rt_pin_read(BUTTON_0);
break;
2024-12-13 19:07:17 +08:00
case USER_BUTTON_1:
value = rt_pin_read(BUTTON_1);
break;
2024-12-01 13:49:43 +08:00
default:
RT_ASSERT(0);
}
return value;
}
static void _CommonBtnEvtCb(void *arg)
{
// static rt_uint8_t button_cnt = 0;
2024-12-04 10:31:57 +08:00
2025-02-21 15:33:29 +08:00
if (flex_button_event_read(&user_button[USER_BUTTON_1]) == FLEX_BTN_PRESS_DOUBLE_CLICK)
2024-12-01 13:49:43 +08:00
{
2025-04-08 14:40:25 +08:00
if ( SysControl.status > kPreheatingEvent)
{
Send_Laser_Alarm_Event(kSelfCheckEvent);
}
2024-12-01 13:49:43 +08:00
}
2025-02-21 15:33:29 +08:00
else if (flex_button_event_read(&user_button[USER_BUTTON_0]) == FLEX_BTN_PRESS_CLICK)
{
if (Sensor_device.detection_flag == kSensorAlarm)
{
Send_Laser_Alarm_Event(KMuteEvent);
}
}
else if ((flex_button_event_read(&user_button[USER_BUTTON_0]) == FLEX_BTN_PRESS_REPEAT_CLICK) && (flex_button_event_read(&user_button[USER_BUTTON_1]) == FLEX_BTN_PRESS_LONG_HOLD))
2024-12-30 17:51:13 +08:00
{
LOG_I("本地清除阀门信息");
rt_uint8_t mac_addr[6] = {0};
if(Flash_Get_Valve_Num() == 0)
2024-12-30 17:51:13 +08:00
{
LOG_I("本地没有阀门信息");
}
else
{
//清空本地阀门信息
if (Flash_Set_Mac_Addr(mac_addr, 1) != RESET)
2024-12-30 17:51:13 +08:00
{
LOG_E("本地阀门信息清空完成");
2025-01-03 17:01:35 +08:00
Flash_Set_Valve_Num(0);
2024-12-30 17:51:13 +08:00
}
}
Bt_Valve_Handler(kValveCmdRemAll, RT_NULL, RT_NULL);
if (rt_sem_take(&bt_rem_sem, 10000) == RT_EOK) // 如果没刷掉数据,则黄灯会亮一秒,如果刷成功了,则红灯闪一下黄灯不会亮
{
2025-01-03 17:01:35 +08:00
LED_OFF(g);
LED_OFF(r);
LED_OFF(y);
LED_ON(r);
rt_thread_mdelay(100);
LED_OFF(r);
LOG_I("本地清除阀门信息成功");
}
2024-12-30 17:51:13 +08:00
}
2024-12-01 13:49:43 +08:00
}
static void _BUTTON_Process(void *param)
{
#ifdef BUTTON_USE_THREAD
LOG_D("BSP_BUTTON_thread");
while (1)
{
flex_button_scan();
rt_thread_mdelay(20);
}
#else
flex_button_scan();
// LOG_D("BSP_BUTTON_timer");
#endif // !BUTTON_USE_THREAD
}
int BSP_BUTTON_Init(void)
{
2024-12-04 10:31:57 +08:00
rt_uint8_t ret = 0;
2024-12-01 13:49:43 +08:00
rt_memset(&user_button[0], 0x0, sizeof(user_button));
rt_pin_mode(BUTTON_0, PIN_MODE_INPUT_PULLUP); /* set KEY pin mode to input */
2024-12-13 19:07:17 +08:00
rt_pin_mode(BUTTON_1, PIN_MODE_INPUT_PULLUP); /* set KEY pin mode to input */
2024-12-30 17:51:13 +08:00
2024-12-01 13:49:43 +08:00
for (uint8_t i = 0; i < USER_BUTTON_MAX; i++)
{
user_button[i].id = i;
user_button[i].usr_button_read = _CommonBtnRead;
user_button[i].cb = _CommonBtnEvtCb;
user_button[i].pressed_logic_level = PIN_LOW;
user_button[i].short_press_start_tick = FLEX_MS_TO_SCAN_CNT(1000);
user_button[i].long_press_start_tick = FLEX_MS_TO_SCAN_CNT(3000);
user_button[i].long_hold_start_tick = FLEX_MS_TO_SCAN_CNT(5000);
flex_button_register(&user_button[i]);
}
#ifdef BUTTON_USE_THREAD
2024-12-04 10:31:57 +08:00
ret = rt_thread_init(&button_thread,
"button_thread",
_BUTTON_Process,
RT_NULL,
&button_thread_stack[0],
sizeof(button_thread_stack),
BUTTON_THREAD_PRIORITY,
BUTTON_THREAD_TICKS);
if (ret == RT_EOK)
{
rt_thread_startup(&button_thread);
return RT_EOK;
}
else
{
LOG_E("button_thread create failed");
return -RT_ERROR;
}
2024-12-01 13:49:43 +08:00
#else
button_timer = rt_timer_create("button_timer", _BUTTON_Process,
RT_NULL, 20,
RT_TIMER_FLAG_PERIODIC);
if (button_timer != RT_NULL)
{
rt_timer_start(button_timer);
}
else
{
LOG_E("create button_timer fail");
return -RT_ERROR;
}
#endif // !BUTTON_USE_THREAD
LOG_I("BSP_BUTTON_Init!");
return -RT_ERROR;
}
#ifdef FINSH_USING_MSH
// INIT_DEVICE_EXPORT(BSP_BUTTON_Init);
2024-12-01 13:49:43 +08:00
#endif