Explorar el Código

feat: 按钮控制功能 逻辑验证成功

kindring hace 1 año
padre
commit
4093c65279

+ 13 - 1
ble-light.uvprojx

@@ -341,7 +341,7 @@
               <MiscControls>-DADV_NCONN_CFG=0x01   -DADV_CONN_CFG=0x02   -DSCAN_CFG=0x04    -DINIT_CFG=0x08   -DBROADCASTER_CFG=0x01 -DOBSERVER_CFG=0x02   -DPERIPHERAL_CFG=0x04   -DCENTRAL_CFG=0x08   -DHOST_CONFIG=0x4</MiscControls>
               <Define>CFG_CP CFG_QFN32 CFG_SLEEP_MODE=PWR_MODE_NO_SLEEP DEBUG_INFO=1 USE_SYS_TICK HUGE_MODE=0</Define>
               <Undefine></Undefine>
-              <IncludePath>.\src;.\components\inc;.\components\common;.\components\osal\include;.\components\ble\include;.\components\ble\hci;.\components\ble\host;.\components\ble\controller\include;.\components\profiles\DevInfo;.\components\profiles\GATT;.\components\profiles\SimpleProfile;.\components\profiles\Roles;.\components\profiles\ota_app;.\components\profiles\hrs;.\components\driver\common;.\components\driver\log;.\components\driver\gpio;.\components\driver\uart;.\components\driver\i2c;.\components\driver\adc;.\components\driver\clock;.\components\driver\pwm;.\components\driver\pwrmgr;.\components\driver\kscan;.\components\driver\flash;.\components\driver\spi;.\components\driver\i2c;.\components\driver\watchdog;.\components\driver\timer;.\components\libraries\console;.\components\libraries\datetime;.\lib;.\src;.\src\ui;.\src\ble;.\src\pwm_light;.\src\epticore</IncludePath>
+              <IncludePath>.\src;.\components\inc;.\components\common;.\components\osal\include;.\components\ble\include;.\components\ble\hci;.\components\ble\host;.\components\ble\controller\include;.\components\profiles\DevInfo;.\components\profiles\GATT;.\components\profiles\SimpleProfile;.\components\profiles\Roles;.\components\profiles\ota_app;.\components\profiles\hrs;.\components\driver\common;.\components\driver\log;.\components\driver\gpio;.\components\driver\uart;.\components\driver\i2c;.\components\driver\adc;.\components\driver\clock;.\components\driver\pwm;.\components\driver\pwrmgr;.\components\driver\kscan;.\components\driver\flash;.\components\driver\spi;.\components\driver\i2c;.\components\driver\watchdog;.\components\driver\timer;.\components\libraries\console;.\components\libraries\datetime;.\lib;.\src;.\src\ui;.\src\ble;.\src\pwm_light;.\src\epticore;.\src\control;.\src\gpio_key;</IncludePath>
             </VariousControls>
           </Cads>
           <Aads>
@@ -435,6 +435,7 @@
               <FileType>1</FileType>
               <FilePath>.\components\profiles\ota_app\ota_app_service.c</FilePath>
             </File>
+            
           </Files>
         </Group>
         <Group>
@@ -480,6 +481,17 @@
               <FileType>1</FileType>
               <FilePath>.\src\pwm_light\pwm_light.c</FilePath>
             </File>
+            <File>
+              <FileName>gpio_key.c</FileName>
+              <FileType>1</FileType>
+              <FilePath>.\src\gpio_key\gpio_key.c</FilePath>
+            </File>
+            <File>
+              <FileName>btn.c</FileName>
+              <FileType>1</FileType>
+              <FilePath>.\src\control\btn.c</FilePath>
+            </File>
+            
           </Files>
         </Group>
         <Group>

+ 8 - 1
src/OSAL_wrist.c

@@ -25,6 +25,9 @@
 /* Profiles */
 #include "peripheral.h"
 
+/* btn */
+#include "btn.h"
+
 /* Application */
 #include "app_wrist.h"
 
@@ -43,7 +46,8 @@ const pTaskEventHandlerFn tasksArr[] =
   GATT_ProcessEvent,
   GAPRole_ProcessEvent,
   GATTServApp_ProcessEvent,
-  appWristProcEvt
+  Key_ProcessEvent,
+  appWristProcEvt,
 };
 
 const uint8 tasksCnt = sizeof( tasksArr ) / sizeof( tasksArr[0] );
@@ -92,6 +96,9 @@ void osalInitTasks( void )
 
   GATTServApp_Init( taskID++ );
 
+  /** 初始化按钮 事件*/
+  btn_init(taskID++);
+
   /* Application */
   appWristInit( taskID );
 }

+ 51 - 1
src/app_wrist.c

@@ -191,6 +191,50 @@ static void wristCB(uint8 event, uint8 param_size, uint8* param)
 }
 
 
+static void user_key_1_handler(GPIO_Pin_e pin, IO_Wakeup_Pol_e type)
+{
+	LOG("user key 1 handler:%d %d\n", pin, type);
+} 
+static void user_key_2_handler(GPIO_Pin_e pin, IO_Wakeup_Pol_e type)
+{
+	LOG("user key 2 handler:%d %d\n", pin, type);
+}
+static void user_key_3_handler(GPIO_Pin_e pin, IO_Wakeup_Pol_e type)
+{
+	LOG("user key 3 handler:%d %d\n", pin, type);
+}
+static void user_key_4_handler(GPIO_Pin_e pin, IO_Wakeup_Pol_e type)
+{
+	LOG("user key 4 handler:%d %d\n", pin, type);
+}
+
+static void user_key_1_handler_ppp(GPIO_Pin_e pin, IO_Wakeup_Pol_e type)
+{
+	LOG("+++ 1 handler ppp:%d %d\n", pin, type);
+} 
+static void user_key_2_handler_ppp(GPIO_Pin_e pin, IO_Wakeup_Pol_e type)
+{
+	LOG("+++ 2 handler ppp:%d %d\n", pin, type);
+}
+static void user_key_3_handler_ppp(GPIO_Pin_e pin, IO_Wakeup_Pol_e type)
+{
+	LOG("+++ 3 handler ppp:%d %d\n", pin, type);
+}
+static void user_key_4_handler_ppp(GPIO_Pin_e pin, IO_Wakeup_Pol_e type)
+{
+	LOG("+++ 4 handler ppp:%d %d\n", pin, type);
+}
+
+static void getPinState(){
+	LOG("---------------------------------\n\n\n");
+	LOG("P01:%d\n",hal_gpio_read(GPIO_P01));
+	LOG("P02:%d\n",hal_gpio_read(GPIO_P02));
+	LOG("P03:%d\n",hal_gpio_read(GPIO_P03));
+	LOG("P04:%d\n",hal_gpio_read(GPIO_P04));
+	LOG("P05:%d\n",hal_gpio_read(GPIO_P05));
+	LOG("---------------------------------\n");
+}
+
 void appWristInit( uint8 task_id)
 {
     AppWrist_TaskID = task_id;
@@ -271,7 +315,6 @@ void appWristInit( uint8 task_id)
 
     LOG("appWristInit end\n");
 
-
 }
 
 // 事件处理器
@@ -304,5 +347,12 @@ uint16 appWristProcEvt( uint8 task_id, uint16 events )
         
         return ( events ^ START_DEVICE_EVT );
     }
+
+    if ( events & TIMER_LIGHT_EVT )
+    {
+        LOG("TIMER_LIGHT_EVT\n");
+        getPinState();
+        return ( events ^ TIMER_LIGHT_EVT );
+    }
      return 0;
 }

+ 116 - 0
src/control/btn.c

@@ -0,0 +1,116 @@
+
+#include "clock.h"
+#include "log.h"
+#include "gpio.h"
+#include "light.h"
+
+#include "btn.h"
+
+
+uint8 task_btn_id;
+// GPIO_Pin_e btn_pins[HAL_KEY_NUM] = {
+//     GPIO_BTN_PLUS, 
+//     GPIO_BTN_MINUS
+// };
+#define KEY_DEMO_ONCE_TIMER      0x0001
+#define KEY_DEMO_CYCLE_TIMER     0x0002
+
+static void key_press_evt(uint8_t i,key_evt_t key_evt)
+{
+	LOG("\nkey index:%d gpio:%d ",i,key_state.key[i].pin);
+	switch(key_evt)
+	{
+		case HAL_KEY_EVT_PRESS:
+			LOG("key(press down)\n");		
+#ifdef HAL_KEY_SUPPORT_LONG_PRESS
+			osal_start_timerEx(task_btn_id, KEY_DEMO_LONG_PRESS_EVT, HAL_KEY_LONG_PRESS_TIME);
+#endif
+			
+			break;
+		
+		case HAL_KEY_EVT_RELEASE:
+			LOG("key(press release)\n");
+			break;
+
+#ifdef HAL_KEY_SUPPORT_LONG_PRESS		
+		case HAL_KEY_EVT_LONG_RELEASE:
+			hal_pwrmgr_unlock(MOD_USR1);
+			LOG("key(long press release)\n");
+			break;
+#endif
+		
+		default:
+			LOG("unexpect\n");
+			break;
+	}
+}
+
+// key_state 引入
+
+// 初始化按钮
+int btn_init(task_id){
+    LOG("[btn_init]\n");
+    task_btn_id = task_id;
+    // 初始化pwm
+    for (int i = 0; i < HAL_KEY_NUM; i++)
+    {
+		hal_gpio_pin_init(btn_pins[i], IE);
+        key_state.key[i].pin = btn_pins[i];
+        key_state.key[i].state = HAL_KEY_EVT_PRESS;
+        key_state.key[i].idle_level = HAL_HIGH_IDLE;
+    }
+    
+    key_state.task_id = task_btn_id;
+	key_state.key_callbank = key_press_evt;
+	key_init();
+
+    return 1;
+}
+
+// 按钮事件处理函数
+uint16 Key_ProcessEvent( uint8 task_id, uint16 events )
+{
+	// LOG("Key_ProcessEvent\n");
+	// LOG("key event:%04x\n",events);
+	if(task_id != task_btn_id){
+		return 0;
+	}
+	if( events & KEY_DEMO_ONCE_TIMER){		
+		//LOG("once timer\n\n");
+		osal_start_timerEx( task_btn_id, KEY_DEMO_ONCE_TIMER , 5000);
+		return (events ^ KEY_DEMO_ONCE_TIMER);
+	}
+	if( events & KEY_DEMO_CYCLE_TIMER){		
+		//LOG("recycle timer\n\n");
+		return (events ^ KEY_DEMO_CYCLE_TIMER);
+	}
+	
+	if( events & HAL_KEY_EVENT){	
+		LOG("HAL_KEY_EVENT \n");											//do not modify,key will use it
+		for (uint8 i = 0; i < HAL_KEY_NUM; ++i){
+			if ((key_state.temp[i].in_enable == TRUE)||
+			(key_state.key[i].state == HAL_STATE_KEY_RELEASE_DEBOUNCE)){
+				gpio_key_timer_handler(i);
+			}
+		}
+		return (events ^ HAL_KEY_EVENT);
+	}
+
+#ifdef HAL_KEY_SUPPORT_LONG_PRESS	
+
+	if( events & KEY_DEMO_LONG_PRESS_EVT){
+		LOG("KEY_DEMO_LONG_PRESS_EVT\n");
+		for (int i = 0; i < HAL_KEY_NUM; ++i){
+			if(key_state.key[i].state == HAL_KEY_EVT_PRESS){
+				LOG("key:%d gpio:%d	",i,key_state.key[i].pin);
+				LOG("key(long press down)\n");
+				osal_start_timerEx(task_btn_id,KEY_DEMO_LONG_PRESS_EVT,HAL_KEY_LONG_PRESS_TIME);//2s
+				
+				//user app code long press down process 	
+			}
+		}
+		return (events ^ KEY_DEMO_LONG_PRESS_EVT);
+	}
+#endif
+
+}

+ 23 - 0
src/control/btn.h

@@ -0,0 +1,23 @@
+#ifndef __BTN_H__
+#define __BTN_H__
+
+#include "types.h"
+#include "gpio.h"
+
+#include "gpio_key.h"
+
+extern uint8 task_btn_id;
+
+
+
+
+// 按钮状态
+extern int btn_init();
+
+
+uint16 Key_ProcessEvent( uint8 task_id, uint16 events );
+
+// 按钮表格
+// extern GPIO_Pin_e btn_pins[];
+
+#endif

+ 32 - 0
src/control/key_config.h

@@ -0,0 +1,32 @@
+
+// 最大按钮数量
+#define HAL_KEY_NUM   2 
+
+// 启用长按
+#define HAL_KEY_SUPPORT_LONG_PRESS
+// 是否启用按钮长按. 启用则设定长按时间
+#ifdef HAL_KEY_SUPPORT_LONG_PRESS
+#define HAL_KEY_LONG_PRESS_TIME      700    //2s
+#define KEY_DEMO_LONG_PRESS_EVT   0x0200 
+#endif
+
+// 按钮消抖时间
+#define HAL_KEY_DEBOUNCD              20      //20ms
+
+
+// 按钮定义
+// btn +
+#define GPIO_BTN_PLUS GPIO_P01
+// btn -
+#define GPIO_BTN_MINUS GPIO_P02
+
+
+// 防止重复定义
+#ifndef BTN_PINS_DEFINED
+#define BTN_PINS_DEFINED
+static const GPIO_Pin_e btn_pins[HAL_KEY_NUM] = {
+    GPIO_BTN_PLUS, 
+    GPIO_BTN_MINUS
+};
+#endif
+

+ 180 - 0
src/gpio_key/gpio_key.c

@@ -0,0 +1,180 @@
+
+#include "log.h"
+#include "OSAL.h"
+#include "pwrmgr.h"
+#include "common.h"
+#include "error.h"
+
+#include "gpio_key.h"
+
+key_contex_t key_state;
+
+extern uint32 getMcuPrecisionCount(void);
+
+static int key_timer_start(uint32 intval_ms)
+{
+	osal_start_timerEx(key_state.task_id, HAL_KEY_EVENT, intval_ms);	
+	return 0;
+}
+
+static void key_idle_handler(uint8 i,IO_Wakeup_Pol_e type)
+{
+  if(((type == NEGEDGE) && (key_state.key[i].idle_level == HAL_HIGH_IDLE)) || 
+     ((type == POSEDGE) && (key_state.key[i].idle_level == HAL_LOW_IDLE))){
+		hal_pwrmgr_lock(MOD_USR1);
+		key_state.key[i].state = HAL_STATE_KEY_PRESS_DEBOUNCE;
+		key_state.temp[i].in_enable = TRUE;
+		key_timer_start(HAL_KEY_DEBOUNCD);
+  }
+}
+
+static void key_press_debonce_handler(uint8 i,IO_Wakeup_Pol_e type)
+{
+	  if(((type == NEGEDGE) && (key_state.key[i].idle_level == HAL_HIGH_IDLE)) || 
+     ((type == POSEDGE) && (key_state.key[i].idle_level == HAL_LOW_IDLE))){
+    key_timer_start(HAL_KEY_DEBOUNCD);
+  } 
+}
+
+static void key_press_handler(uint8 i,IO_Wakeup_Pol_e type)
+{
+	  if(((type == POSEDGE) && (key_state.key[i].idle_level == HAL_HIGH_IDLE)) || 
+     ((type == NEGEDGE) && (key_state.key[i].idle_level == HAL_LOW_IDLE))){
+			hal_pwrmgr_lock(MOD_USR1);
+			key_state.key[i].state = HAL_STATE_KEY_RELEASE_DEBOUNCE;
+			key_timer_start(HAL_KEY_DEBOUNCD);
+  } 
+}
+
+static void key_release_debonce_handler(uint8 i,IO_Wakeup_Pol_e type)
+{
+		 if(((type == POSEDGE) && (key_state.key[i].idle_level == HAL_HIGH_IDLE)) || 
+     ((type == NEGEDGE) && (key_state.key[i].idle_level == HAL_LOW_IDLE))){
+    key_timer_start(HAL_KEY_DEBOUNCD);
+  } 
+}
+
+static void pin_event_handler(GPIO_Pin_e pin,IO_Wakeup_Pol_e type)
+{
+	uint8 i;
+	// LOG("pin_event_handler pin:%d type:%d\n",pin,type);
+	for(i = 0;i < HAL_KEY_NUM;i++){
+		if(pin == key_state.key[i].pin)
+		break;
+	}
+		
+	if(i < HAL_KEY_NUM){
+		switch(key_state.key[i].state)
+		{             
+			case HAL_STATE_KEY_IDLE:
+				key_idle_handler(i,type);
+				break;
+			
+			case HAL_STATE_KEY_PRESS_DEBOUNCE:
+				key_press_debonce_handler(i,type);
+				break;
+			
+			case HAL_STATE_KEY_PRESS:
+				key_press_handler(i,type);
+				break;
+			
+			case HAL_STATE_KEY_RELEASE_DEBOUNCE:
+				key_release_debonce_handler(i,type);
+				break;
+			
+			default:
+				break;    
+		}
+	}
+}
+
+void key_init(void)
+{
+	uint8 i;
+	
+	for(i = 0; i < HAL_KEY_NUM; ++i){
+		if(key_state.key[i].idle_level == HAL_LOW_IDLE){
+			hal_gpio_pull_set(key_state.key[i].pin,PULL_DOWN);
+		}
+		else{
+			hal_gpio_pull_set(key_state.key[i].pin,WEAK_PULL_UP);
+		}
+		
+		key_state.temp[i].timer_tick = 0;
+		(void)hal_gpioin_register(key_state.key[i].pin, pin_event_handler, pin_event_handler);
+	}
+	
+	hal_pwrmgr_register(MOD_USR1, NULL, NULL);
+}
+
+static void key_press_debonce_timer_handler(uint8 i)
+{
+	if(((hal_gpio_read(key_state.key[i].pin) == FALSE) && (key_state.key[i].idle_level == HAL_HIGH_IDLE)) || 
+	((hal_gpio_read(key_state.key[i].pin) == TRUE) && (key_state.key[i].idle_level == HAL_LOW_IDLE)))
+	{
+		#ifndef HAL_KEY_SUPPORT_LONG_PRESS
+		hal_pwrmgr_unlock(MOD_USR1);
+		#endif
+		
+		key_state.key[i].state = HAL_STATE_KEY_PRESS;
+		key_state.temp[i].timer_tick = getMcuPrecisionCount();
+				
+		if(key_state.key_callbank != NULL)
+		{ 
+			key_state.key_callbank(i,HAL_KEY_EVT_PRESS);
+		}
+	}
+	else
+	{
+		key_state.key[i].state = HAL_STATE_KEY_IDLE;
+		key_state.temp[i].in_enable = FALSE;
+	} 
+}
+
+static void key_release_debonce_timer_handler(uint8 i)
+{
+	if(key_state.key[i].idle_level == hal_gpio_read(key_state.key[i].pin))
+	{
+		osal_stop_timerEx(key_state.task_id,HAL_KEY_EVT_LONG_PRESS);
+		uint32_t hold_tick = (getMcuPrecisionCount() - key_state.temp[i].timer_tick)*625;
+		hal_pwrmgr_unlock(MOD_USR1);
+
+		if(key_state.key_callbank != NULL)
+		{ 
+#ifdef HAL_KEY_SUPPORT_LONG_PRESS			
+			if(hold_tick >= (HAL_KEY_LONG_PRESS_TIME * 1000))// 微秒
+			{
+				key_state.key_callbank(i,HAL_KEY_EVT_LONG_RELEASE);
+				key_state.key[i].state = HAL_STATE_KEY_IDLE;
+			}
+			else
+#endif				
+			{
+				key_state.key_callbank(i,HAL_KEY_EVT_RELEASE);
+				key_state.key[i].state = HAL_STATE_KEY_IDLE;				
+			}
+		}
+	}
+	else
+	{
+		key_state.key[i].state = HAL_STATE_KEY_PRESS;
+	} 
+}
+
+void gpio_key_timer_handler(uint8 i)
+{
+	switch(key_state.key[i].state)
+	{
+		case HAL_STATE_KEY_PRESS_DEBOUNCE:
+			key_press_debonce_timer_handler(i);
+			break;
+
+		case HAL_STATE_KEY_RELEASE_DEBOUNCE:
+			key_release_debonce_timer_handler(i);
+			break;
+
+		default:
+			break;
+	}      
+}
+

+ 61 - 0
src/gpio_key/gpio_key.h

@@ -0,0 +1,61 @@
+
+#ifndef __GPIO_KEY_H__
+#define __GPIO_KEY_H__
+
+#include "types.h"
+#include "gpio.h"
+#include "key_config.h"
+// #define HAL_KEY_NUM 						      2      //config key's number
+#define HAL_KEY_EVENT						      0x0100 //assign short key event in your app event process
+
+// #define HAL_KEY_SUPPORT_LONG_PRESS           //if use long key,please enable it
+
+
+typedef enum{
+	HAL_STATE_KEY_IDLE = 0x00,
+	HAL_STATE_KEY_PRESS_DEBOUNCE = 0x01,
+	HAL_STATE_KEY_PRESS = 0x02,
+	HAL_STATE_KEY_RELEASE_DEBOUNCE = 0x03,
+}key_state_e;
+
+typedef enum{
+	HAL_KEY_EVT_IDLE = 0x0000,
+	HAL_KEY_EVT_PRESS   = 0x0002,
+	HAL_KEY_EVT_RELEASE = 0x0004,
+	HAL_KEY_EVT_LONG_PRESS = 0x0010,
+	HAL_KEY_EVT_LONG_RELEASE = 0x0020,
+} key_evt_t;
+
+typedef enum{
+	HAL_LOW_IDLE = 0x00,
+	HAL_HIGH_IDLE = 0x01,
+}idle_level_e;
+
+typedef void (* key_callbank_hdl_t)(uint8_t,key_evt_t);
+
+typedef struct gpio_key_t{
+	GPIO_Pin_e 			pin;
+	key_state_e    	state;
+	idle_level_e 		idle_level;
+}gpio_key;
+
+typedef struct gpio_internal_t{
+	uint32_t 	timer_tick;	
+	bool     	in_enable;
+	
+}gpio_internal;
+
+typedef struct key_state{
+	gpio_key 						key[HAL_KEY_NUM];
+	gpio_internal 			temp[HAL_KEY_NUM];
+	uint8_t  						task_id;
+	key_callbank_hdl_t  key_callbank;
+	
+}key_contex_t;
+
+
+void key_init(void);
+void gpio_key_timer_handler(uint8 index);
+extern key_contex_t key_state;
+#endif
+

+ 5 - 1
src/light.c

@@ -1,3 +1,4 @@
+
 #include "pwm.h"
 #include "gpio.h"
 
@@ -8,6 +9,7 @@
 
 #include "light.h"
 
+
 // 配置
 
 int light_init(){
@@ -36,4 +38,6 @@ int light_set(uint8_t ch, uint8_t val){
     LOG("[light_set] set ch%d val to %d \n", ch, val);
     int ret = pwm_light_set_val(ch, val);
     return ret;
-}
+}
+
+// 按钮控制

+ 2 - 13
src/light.h

@@ -1,12 +1,3 @@
-/*
- * @Author: kindring 2460131004@qq.com
- * @Date: 2023-11-20 23:37:29
- * @LastEditors: kindring 2460131004@qq.com
- * @LastEditTime: 2023-11-25 21:37:47
- * @FilePath: \ble-light\src\light.h
- * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
- */
-
 #ifndef __LIGHT_H__
 #define __LIGHT_H__
 
@@ -17,10 +8,8 @@
 
 // 风扇 pwm
 #define GPIO_FAN P23
-// btn +
-#define GPIO_BTN_PLUS P26
-// btn -
-#define GPIO_BTN_MINUS P27
+
+
 
 
 // adc 电源监测脚

La diferencia del archivo ha sido suprimido porque es demasiado grande
+ 8 - 0
参考资料/SCH_Schematic3_2023-12-02.pdf


+ 2 - 2
蓝牙灯光设计.md

@@ -26,10 +26,10 @@
 通知起始码 0x66
 
 ### 格式定义
-下发命令格式: `起始码` `命令码` `sn码` `数据1` `数据2`
+下发命令格式: `起始码` `长度` `命令码` `sn码` `数据1` `数据2`
 下发起始码: 0x6c
 
-响应命令格式: `起始码` `命令码` `sn码` `数据1` `数据2`
+响应命令格式: `起始码` `长度` `命令码` `sn码` `数据1` `数据2`
 响应: 0xc6
 
 

Algunos archivos no se mostraron porque demasiados archivos cambiaron en este cambio