Browse Source

按钮功能验证

kindring 1 year ago
parent
commit
77b57e2b2f
3 changed files with 149 additions and 12 deletions
  1. 140 11
      src/control/btn.c
  2. 8 0
      src/control/btn.h
  3. 1 1
      src/control/key_config.h

+ 140 - 11
src/control/btn.c

@@ -12,19 +12,64 @@ uint8 task_btn_id;
 #define KEY_DEMO_CYCLE_TIMER     0x0002
 
 
+// btn 0 add
+// btn 1 sub
 
-// 按钮调节模式,默认为亮度调节
-bool btn_mode = Light_MODE;
+// 是否在调节亮度
+bool btn_mode = false;
+// 断案按钮抬起事件 btn
+int btn_ind = -1;
+int next_btn_ind ;
 
-// 同时按下的按键数组, 用于判断是否同时按下
-int all_pressed_len = 1;
+int temp = 2300;
+// int temp_speed = 50;
+int light = 0;
+// int light_speed = 5;
+void temp_add(int temp_speed){
+	int next_temp = temp + temp_speed;
+	if(next_temp > 6500){
+		next_temp = 6500;
+	}
+	LOG("temp %d ===>> %d\n", temp, next_temp);
+	temp = next_temp;
+}
+void temp_sub(int temp_speed){
+	int next_temp = temp - temp_speed;
+	if(next_temp < 2300){
+		next_temp = 2300;
+	}
+	LOG("temp %d ===>> %d\n", temp, next_temp);
+	temp = next_temp;
+}
+
+void light_add(int light_speed){
+	int next_light = light + light_speed;
+	if(next_light > 100){
+		next_light = 100;
+	}
+	LOG("light %d ===>> %d\n", light, next_light);
+	light = next_light;
+	light_set(0, light);
+	light_set(1, light);
+}
+
+void light_sub(int light_speed){
+	int next_light = light - light_speed;
+	if(next_light < 0){
+		next_light = 0;
+	}
+	LOG("light %d ===>> %d\n", light, next_light);
+	light = next_light;
+	light_set(0, light);
+	light_set(1, light);
+}
 
 void btn_mode_switch(){
 	btn_mode = !btn_mode;
 	LOG("btn_mode change to: %s\n",btn_mode?"light":"temp");
 }
 
-btn_all_pressed_t btn_all_pressed[all_pressed_len] = {
+btn_all_pressed_t btn_all_pressed[ALL_PRESSED_LEN] = {
 	{
 		.len = 2,
 		.btns = (uint8_t[]){0, 1},
@@ -32,11 +77,23 @@ btn_all_pressed_t btn_all_pressed[all_pressed_len] = {
 	}
 };
 
+void evt_press_release(int i){
+	btn_ind = i;
+	if(btn_mode)
+	{
+		btn_mode = false;
+		btn_ind = -1;
+	}
+
+	// 短按抬起 
+	osal_start_timerEx(task_btn_id, EVT_TIME_CHECK, TIME_CHECK_TEMP);
+}
+
 // 判断按键是否同时按下
 bool btn_is_all_pressed(){
 	bool is_all_pressed = false;
 	// 遍历所有的同时按下的按键
-	for (int i = 0; i < all_pressed_len; i++)
+	for (int i = 0; i < ALL_PRESSED_LEN; i++)
 	{
 		// 遍历每个同时按下的按键的按键数组
 		for (int j = 0; j < btn_all_pressed[i].len; j++)
@@ -70,12 +127,29 @@ 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);
+			switch (i)
+			{
+			case 0:
+				light_add(10);
+				break;
+			case 1:
+				light_sub(10);
+				break;
+			default:
+				break;
+			}
 			break;
 
 #ifdef HAL_KEY_SUPPORT_LONG_PRESS		
 		case HAL_KEY_EVT_LONG_RELEASE:
-			hal_pwrmgr_unlock(MOD_USR1);
 			LOG("key(long press release)\n");
+			// 检测是否有同时按下的按键, 只需要在长按事件中检测即可
+			// btn_is_all_pressed();
+			if(btn_mode){
+				btn_mode = false;
+				btn_ind = -1;
+			}
 			break;
 #endif
 		
@@ -127,12 +201,15 @@ uint16 Key_ProcessEvent( uint8 task_id, uint16 events )
 	}
 	
 	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)){
 				gpio_key_timer_handler(i);
 			}
+
+			
+			
 		}
 		return (events ^ HAL_KEY_EVENT);
 	}
@@ -147,13 +224,65 @@ uint16 Key_ProcessEvent( uint8 task_id, uint16 events )
 				LOG("key(long press down)\n");
 				osal_start_timerEx(task_btn_id, KEY_DEMO_LONG_PRESS_EVT, HAL_KEY_LONG_PRESS_TIME);
 				//user app code long press down process 
-
-				// 检测是否有同时按下的按键, 只需要在长按事件中检测即可
-				btn_is_all_pressed();
+				if (btn_mode && i != btn_ind && btn_ind != -1){
+					// 色温调节
+					switch (i)
+					{
+					case 0:
+						temp_add(50);
+						break;
+					case 1:
+						temp_sub(50);
+						break;
+					default:
+						break;
+					}
+				} else {
+				// 长按 但是没有在调节
+				switch (i)
+				{
+				case 0:
+					light_add(1);
+					break;
+				case 1:
+					light_sub(1);
+					break;
+				default:
+					break;
+				}
 			}
 		}
 		return (events ^ KEY_DEMO_LONG_PRESS_EVT);
 	}
 #endif
 
+	if( events & EVT_TIME_CHECK){
+		LOG("EVT_TIME_CHECK\n");
+		
+		switch (btn_ind)
+		{
+		case 0:
+			// 如果另一个按钮已经按下,则切换模式
+			next_btn_ind = 1;
+			break;
+		case 1:
+			// 如果另一个按钮已经按下,则切换模式
+			next_btn_ind = 0;
+			break;
+		default:
+			next_btn_ind = -1;
+			break;
+		}
+		// 如果另一个按钮已经按下,则切换模式
+		if(
+			next_btn_ind != -1 && (key_state.key[next_btn_ind].state == HAL_KEY_EVT_PRESS || key_state.key[next_btn_ind].state == HAL_KEY_EVT_LONG_PRESS)
+		){
+			btn_mode = true;
+		}
+		return (events ^ EVT_TIME_CHECK);
+	}
+	
+	return 0;
+
+}
 }

+ 8 - 0
src/control/btn.h

@@ -8,6 +8,13 @@
 
 #define Light_MODE TRUE
 #define Temp_MODE FALSE
+
+// 按下按钮后,1000ms后检测另一个按钮是否按下,如果按下则切换模式
+#define TIME_CHECK_TEMP 1000
+
+// 事件定义
+#define EVT_TIME_CHECK 0x0008 // 检测另一个按钮是否已经按下
+
 extern uint8 task_btn_id;
 // 按钮调整模式  true: 亮度调节  false: 色温调节
 extern bool btn_mode;
@@ -21,6 +28,7 @@ typedef struct {
 } btn_all_pressed_t;
 
 // 同时按下的按键数组, 用于判断是否同时按下
+#define ALL_PRESSED_LEN 1
 extern btn_all_pressed_t btn_all_pressed[];
 
 // 按钮状态

+ 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      700    //2s
+#define HAL_KEY_LONG_PRESS_TIME      300    //2s
 #define KEY_DEMO_LONG_PRESS_EVT   0x0200 
 #endif