Browse Source

修复按钮长按异常

kindring 1 year ago
parent
commit
03a08a6ec5
5 changed files with 33 additions and 23 deletions
  1. 1 1
      src/ble/wrist_service.c
  2. 25 17
      src/control/btn.c
  3. 5 3
      src/control/btn.h
  4. 1 1
      src/control/key_config.h
  5. 1 1
      src/pwm_light/pwm_light.c

+ 1 - 1
src/ble/wrist_service.c

@@ -385,7 +385,7 @@ int on_recieved_cmd_packet(const uint8* data, uint16 len)
   // cmdParse();
   LOG("light ctrl %d\n", data[0]);
   
-  light_set(0, data[0]);
+  light_set( data[0] );
 
   LOG("response all data");
   cmd_response(data, len );

+ 25 - 17
src/control/btn.c

@@ -11,12 +11,13 @@ uint8 task_btn_id;
 #define KEY_DEMO_ONCE_TIMER      0x0001
 #define KEY_DEMO_CYCLE_TIMER     0x0002
 
-
+int last_release_btn_ind = -1;
 
 void evt_press_release(int i){
 	// 短按抬起 
-	osal_start_timerEx(task_btn_id, EVT_TIME_CHECK, TIME_CHECK_TEMP);
-	// 短按抬起后,1000ms 后检测该按键是否为长按,是则 调整色温
+	osal_start_timerEx(task_btn_id, BTN_EVT_TIME_CHECK, TIME_CHECK_TEMP * 3);
+	// 500ms 后检测是否
+	last_release_btn_ind = i;
 }
 
 
@@ -26,7 +27,17 @@ static void key_press_evt(uint8_t i,key_evt_t key_evt)
 	switch(key_evt)
 	{
 		case HAL_KEY_EVT_PRESS:
-			LOG("key(press down)\n");		
+			LOG("key(press down)\n");	
+			if (last_release_btn_ind != -1)
+			{
+				if (last_release_btn_ind == i)	
+				{
+					// 按下后再次按下
+					
+				}
+			}else {
+				osal_stop_timeEx(task_btn_id, BTN_EVT_TIME_CHECK)
+			}
 			
 #ifdef HAL_KEY_SUPPORT_LONG_PRESS
 			osal_start_timerEx(task_btn_id, KEY_DEMO_LONG_PRESS_EVT, HAL_KEY_LONG_PRESS_TIME);
@@ -35,7 +46,7 @@ static void key_press_evt(uint8_t i,key_evt_t key_evt)
 		
 		case HAL_KEY_EVT_RELEASE:
 			LOG("key(press release)\n");
-			// evt_press_release(i);
+			evt_press_release(i);
 			break;
 
 #ifdef HAL_KEY_SUPPORT_LONG_PRESS		
@@ -65,7 +76,6 @@ int btn_init(task_id){
         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();
@@ -76,23 +86,21 @@ int btn_init(task_id){
 // 按钮事件处理函数
 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");
+		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");
+		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
+		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)){
@@ -105,12 +113,13 @@ uint16 Key_ProcessEvent( uint8 task_id, uint16 events )
 #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");
+		// LOG("KEY_DEMO_LONG_PRESS_EVT\n");
+		for (int i = 0; i <= HAL_KEY_NUM; i++){
+			// LOG("%d --- state [%d]",i, key_state.key[i].state);
+			if(key_state.key[i].state == HAL_KEY_EVT_PRESS)
+			{
 				osal_start_timerEx(task_btn_id, KEY_DEMO_LONG_PRESS_EVT, HAL_KEY_LONG_PRESS_TIME);
+			}
 		}
 		return (events ^ KEY_DEMO_LONG_PRESS_EVT);
 	}
@@ -124,4 +133,3 @@ uint16 Key_ProcessEvent( uint8 task_id, uint16 events )
 	return 0;
 
 }
-}

+ 5 - 3
src/control/btn.h

@@ -9,11 +9,13 @@
 #define Light_MODE TRUE
 #define Temp_MODE FALSE
 
-// 按下按钮后,1000ms后检测另一个按钮是否按下,如果按下则切换模式
-#define TIME_CHECK_TEMP 1000
+// 按下按钮后,如果在
+#define TIME_CHECK_TEMP 100
 
 // 事件定义
-#define EVT_TIME_CHECK 0x0008 // 检测另一个按钮是否已经按下
+#define BTN_EVT_TIME_CHECK 0x0008 // 检测是否连续长按
+#define BTN_EVT_TEMP_CHECK 0x0010 // 色温检测
+
 
 extern uint8 task_btn_id;
 // 按钮调整模式  true: 亮度调节  false: 色温调节

+ 1 - 1
src/control/key_config.h

@@ -6,7 +6,7 @@
 #define HAL_KEY_SUPPORT_LONG_PRESS
 // 是否启用按钮长按. 启用则设定长按时间
 #ifdef HAL_KEY_SUPPORT_LONG_PRESS
-#define HAL_KEY_LONG_PRESS_TIME      300    //2s
+#define HAL_KEY_LONG_PRESS_TIME      700    //2s
 #define KEY_DEMO_LONG_PRESS_EVT   0x0200 
 #endif
 

+ 1 - 1
src/pwm_light/pwm_light.c

@@ -48,7 +48,7 @@ void pwm_reflash_all(){
     // 遍历 pwm_light_chann_list
     for (int i = 0; i < max_pwm_light; i++)
     {
-        pwm_light_reflash(i)
+        pwm_light_reflash(i);
     }
 }