From 1c45d611001ee26fd5bc217c833f5f46b7605602 Mon Sep 17 00:00:00 2001 From: wyhong Date: Mon, 18 Sep 2023 19:56:54 +0800 Subject: [PATCH 1/2] enhance button handle --- .../bouffalolab/common/AppTask.cpp | 101 ++++++++---------- .../lighting-app/bouffalolab/common/AppTask.h | 11 +- 2 files changed, 52 insertions(+), 60 deletions(-) diff --git a/examples/lighting-app/bouffalolab/common/AppTask.cpp b/examples/lighting-app/bouffalolab/common/AppTask.cpp index f38ae87a61c340..f251d1982d443d 100644 --- a/examples/lighting-app/bouffalolab/common/AppTask.cpp +++ b/examples/lighting-app/bouffalolab/common/AppTask.cpp @@ -163,7 +163,7 @@ void AppTask::AppTaskMain(void * pvParameter) ef_set_env_blob(APP_REBOOT_RESET_COUNT_KEY, &resetCnt, sizeof(resetCnt)); #endif - GetAppTask().sTimer = xTimerCreate("lightTmr", pdMS_TO_TICKS(1000), false, NULL, AppTask::TimerCallback); + GetAppTask().sTimer = xTimerCreate("lightTmr", pdMS_TO_TICKS(APP_TIMER_EVENT_DEFAULT_ITVL), false, NULL, AppTask::TimerCallback); if (GetAppTask().sTimer == NULL) { ChipLogError(NotSpecified, "Failed to create timer task"); @@ -311,7 +311,7 @@ bool AppTask::StartTimer(void) if (GetAppTask().mTimerIntvl == 0) { - GetAppTask().mTimerIntvl = 1000; + GetAppTask().mTimerIntvl = APP_TIMER_EVENT_DEFAULT_ITVL; } if (xTimerChangePeriod(GetAppTask().sTimer, pdMS_TO_TICKS(GetAppTask().mTimerIntvl), pdMS_TO_TICKS(100)) != pdPASS) @@ -334,26 +334,46 @@ void AppTask::TimerCallback(TimerHandle_t xTimer) void AppTask::TimerEventHandler(app_event_t event) { + uint32_t pressedTime = 0; + if (GetAppTask().mButtonPressedTime) { + pressedTime = System::SystemClock().GetMonotonicMilliseconds64().count() - GetAppTask().mButtonPressedTime; #ifdef BOOT_PIN_RESET - if (System::SystemClock().GetMonotonicMilliseconds64().count() - GetAppTask().mButtonPressedTime > APP_BUTTON_PRESS_LONG) - { - GetAppTask().PostEvent(APP_EVENT_BTN_LONG); - } - else if (System::SystemClock().GetMonotonicMilliseconds64().count() - GetAppTask().mButtonPressedTime >= - APP_BUTTON_PRESS_SHORT) - { + if (ButtonPressed()) { + if (pressedTime > APP_BUTTON_PRESS_LONG) + { + GetAppTask().PostEvent(APP_EVENT_BTN_LONG); + } + else if (pressedTime >= APP_BUTTON_PRESS_SHORT) + { #if defined(BL602_NIGHT_LIGHT) || defined(BL706_NIGHT_LIGHT) - /** change color to indicate to wait factory reset confirm */ - sLightLED.SetColor(254, 0, 210); + /** change color to indicate to wait factory reset confirm */ + sLightLED.SetColor(254, 0, 210); #else - /** toggle led to indicate to wait factory reset confirm */ - sLightLED.Toggle(); + /** toggle led to indicate to wait factory reset confirm */ + sLightLED.Toggle(); #endif + } + } + else { + if (pressedTime >= APP_BUTTON_PRESS_LONG) + { + GetAppTask().PostEvent(APP_EVENT_FACTORY_RESET); + } + else if (APP_BUTTON_PRESS_SHORT >= pressedTime && pressedTime >= APP_BUTTON_PRESS_JITTER) + { + GetAppTask().PostEvent(APP_EVENT_BTN_SHORT); + } + else { + GetAppTask().PostEvent(APP_EVENT_LIGHTING_MASK); + } + + GetAppTask().mTimerIntvl = APP_BUTTON_PRESSED_ITVL; + GetAppTask().mButtonPressedTime = 0; } #else - if (System::SystemClock().GetMonotonicMilliseconds64().count() - GetAppTask().mButtonPressedTime > APP_BUTTON_PRESS_LONG) + if (pressedTime > APP_BUTTON_PRESS_LONG) { /** factory reset confirm timeout */ GetAppTask().mButtonPressedTime = 0; @@ -371,6 +391,14 @@ void AppTask::TimerEventHandler(app_event_t event) } #endif } +#ifdef BOOT_PIN_RESET + else { + if (ButtonPressed()) { + GetAppTask().mTimerIntvl = APP_BUTTON_PRESSED_ITVL; + GetAppTask().mButtonPressedTime = System::SystemClock().GetMonotonicMilliseconds64().count(); + } + } +#endif StartTimer(); } @@ -434,49 +462,10 @@ bool AppTask::ButtonPressed(void) return val == 1; } -void AppTask::ButtonEventHandler(void * arg) +void AppTask::ButtonEventHandler(void * arg) { - uint32_t presstime; - - if (ButtonPressed()) - { -#if BL702L_ENABLE - bl_set_gpio_intmod(gpio_key.port, HOSAL_IRQ_TRIG_NEG_LEVEL); -#else - bl_set_gpio_intmod(gpio_key.port, 1, HOSAL_IRQ_TRIG_NEG_LEVEL); -#endif - GetAppTask().mButtonPressedTime = System::SystemClock().GetMonotonicMilliseconds64().count(); - GetAppTask().mTimerIntvl = APP_BUTTON_PRESS_JITTER; - GetAppTask().PostEvent(APP_EVENT_TIMER); - } - else - { -#if BL702L_ENABLE - bl_set_gpio_intmod(gpio_key.port, HOSAL_IRQ_TRIG_POS_PULSE); -#else - bl_set_gpio_intmod(gpio_key.port, 1, HOSAL_IRQ_TRIG_POS_PULSE); -#endif - - if (GetAppTask().mButtonPressedTime) - { - presstime = System::SystemClock().GetMonotonicMilliseconds64().count() - GetAppTask().mButtonPressedTime; - if (presstime >= APP_BUTTON_PRESS_LONG) - { - GetAppTask().PostEvent(APP_EVENT_FACTORY_RESET); - } - else if (presstime <= APP_BUTTON_PRESS_SHORT && presstime >= APP_BUTTON_PRESS_JITTER) - { - GetAppTask().mTimerIntvl = 1000; - GetAppTask().PostEvent(APP_EVENT_BTN_SHORT); - } - else - { - GetAppTask().mTimerIntvl = 1000; - GetAppTask().PostEvent(APP_EVENT_LIGHTING_MASK); - } - } - - GetAppTask().mButtonPressedTime = 0; + if (ButtonPressed()) { + GetAppTask().PostEvent(APP_EVENT_BTN_ISR); } } #endif diff --git a/examples/lighting-app/bouffalolab/common/AppTask.h b/examples/lighting-app/bouffalolab/common/AppTask.h index 638ad9e9f71b58..8c4dadfc74aa3e 100644 --- a/examples/lighting-app/bouffalolab/common/AppTask.h +++ b/examples/lighting-app/bouffalolab/common/AppTask.h @@ -29,9 +29,11 @@ using namespace ::chip; using namespace ::chip::DeviceLayer; -#define APP_BUTTON_PRESS_JITTER 50 -#define APP_BUTTON_PRESS_SHORT 1500 -#define APP_BUTTON_PRESS_LONG 5000 +#define APP_BUTTON_PRESSED_ITVL 50 +#define APP_BUTTON_PRESS_JITTER 100 +#define APP_BUTTON_PRESS_SHORT 1000 +#define APP_BUTTON_PRESS_LONG 4000 +#define APP_TIMER_EVENT_DEFAULT_ITVL 1000 #define APP_LIGHT_ENDPOINT_ID 1 #define APP_REBOOT_RESET_COUNT 3 @@ -60,6 +62,7 @@ class AppTask APP_EVENT_BTN_SHORT = 0x00000020, APP_EVENT_FACTORY_RESET = 0x00000040, APP_EVENT_BTN_LONG = 0x00000080, + APP_EVENT_BTN_ISR = 0x00000100, APP_EVENT_LIGHTING_ONOFF = 0x00010000, APP_EVENT_LIGHTING_LEVEL = 0x00020000, @@ -72,7 +75,7 @@ class AppTask APP_EVENT_IDENTIFY_MASK = APP_EVENT_IDENTIFY_START | APP_EVENT_IDENTIFY_IDENTIFY | APP_EVENT_IDENTIFY_STOP, APP_EVENT_ALL_MASK = - APP_EVENT_LIGHTING_MASK | APP_EVENT_TIMER | APP_EVENT_BTN_SHORT | APP_EVENT_BTN_LONG | APP_EVENT_IDENTIFY_MASK, + APP_EVENT_LIGHTING_MASK | APP_EVENT_TIMER | APP_EVENT_BTN_SHORT | APP_EVENT_BTN_LONG | APP_EVENT_BTN_ISR | APP_EVENT_IDENTIFY_MASK, }; void SetEndpointId(EndpointId endpointId) From 8a350b77722dea22ae55d9eb34661029518a22cf Mon Sep 17 00:00:00 2001 From: wyhong Date: Thu, 21 Sep 2023 17:25:29 +0800 Subject: [PATCH 2/2] fix restyle --- .../bouffalolab/common/AppTask.cpp | 23 ++++++++++++------- .../lighting-app/bouffalolab/common/AppTask.h | 4 ++-- 2 files changed, 17 insertions(+), 10 deletions(-) diff --git a/examples/lighting-app/bouffalolab/common/AppTask.cpp b/examples/lighting-app/bouffalolab/common/AppTask.cpp index f251d1982d443d..ee0ead05c6e8c6 100644 --- a/examples/lighting-app/bouffalolab/common/AppTask.cpp +++ b/examples/lighting-app/bouffalolab/common/AppTask.cpp @@ -163,7 +163,8 @@ void AppTask::AppTaskMain(void * pvParameter) ef_set_env_blob(APP_REBOOT_RESET_COUNT_KEY, &resetCnt, sizeof(resetCnt)); #endif - GetAppTask().sTimer = xTimerCreate("lightTmr", pdMS_TO_TICKS(APP_TIMER_EVENT_DEFAULT_ITVL), false, NULL, AppTask::TimerCallback); + GetAppTask().sTimer = + xTimerCreate("lightTmr", pdMS_TO_TICKS(APP_TIMER_EVENT_DEFAULT_ITVL), false, NULL, AppTask::TimerCallback); if (GetAppTask().sTimer == NULL) { ChipLogError(NotSpecified, "Failed to create timer task"); @@ -340,7 +341,8 @@ void AppTask::TimerEventHandler(app_event_t event) { pressedTime = System::SystemClock().GetMonotonicMilliseconds64().count() - GetAppTask().mButtonPressedTime; #ifdef BOOT_PIN_RESET - if (ButtonPressed()) { + if (ButtonPressed()) + { if (pressedTime > APP_BUTTON_PRESS_LONG) { GetAppTask().PostEvent(APP_EVENT_BTN_LONG); @@ -356,7 +358,8 @@ void AppTask::TimerEventHandler(app_event_t event) #endif } } - else { + else + { if (pressedTime >= APP_BUTTON_PRESS_LONG) { GetAppTask().PostEvent(APP_EVENT_FACTORY_RESET); @@ -365,7 +368,8 @@ void AppTask::TimerEventHandler(app_event_t event) { GetAppTask().PostEvent(APP_EVENT_BTN_SHORT); } - else { + else + { GetAppTask().PostEvent(APP_EVENT_LIGHTING_MASK); } @@ -392,8 +396,10 @@ void AppTask::TimerEventHandler(app_event_t event) #endif } #ifdef BOOT_PIN_RESET - else { - if (ButtonPressed()) { + else + { + if (ButtonPressed()) + { GetAppTask().mTimerIntvl = APP_BUTTON_PRESSED_ITVL; GetAppTask().mButtonPressedTime = System::SystemClock().GetMonotonicMilliseconds64().count(); } @@ -462,9 +468,10 @@ bool AppTask::ButtonPressed(void) return val == 1; } -void AppTask::ButtonEventHandler(void * arg) +void AppTask::ButtonEventHandler(void * arg) { - if (ButtonPressed()) { + if (ButtonPressed()) + { GetAppTask().PostEvent(APP_EVENT_BTN_ISR); } } diff --git a/examples/lighting-app/bouffalolab/common/AppTask.h b/examples/lighting-app/bouffalolab/common/AppTask.h index 8c4dadfc74aa3e..bcc9dbaec7a4b5 100644 --- a/examples/lighting-app/bouffalolab/common/AppTask.h +++ b/examples/lighting-app/bouffalolab/common/AppTask.h @@ -74,8 +74,8 @@ class AppTask APP_EVENT_IDENTIFY_STOP = 0x04000000, APP_EVENT_IDENTIFY_MASK = APP_EVENT_IDENTIFY_START | APP_EVENT_IDENTIFY_IDENTIFY | APP_EVENT_IDENTIFY_STOP, - APP_EVENT_ALL_MASK = - APP_EVENT_LIGHTING_MASK | APP_EVENT_TIMER | APP_EVENT_BTN_SHORT | APP_EVENT_BTN_LONG | APP_EVENT_BTN_ISR | APP_EVENT_IDENTIFY_MASK, + APP_EVENT_ALL_MASK = APP_EVENT_LIGHTING_MASK | APP_EVENT_TIMER | APP_EVENT_BTN_SHORT | APP_EVENT_BTN_LONG | + APP_EVENT_BTN_ISR | APP_EVENT_IDENTIFY_MASK, }; void SetEndpointId(EndpointId endpointId)