From 1855010bcaff62a85241458ef7628c47a6318609 Mon Sep 17 00:00:00 2001 From: wyhong <30567533+wy-hh@users.noreply.github.com> Date: Fri, 22 Sep 2023 21:45:39 +0800 Subject: [PATCH] [Bouffalolab] Improve the handle of button jitter (#29366) * enhance button handle * fix restyle --- .../bouffalolab/common/AppTask.cpp | 100 +++++++++--------- .../lighting-app/bouffalolab/common/AppTask.h | 13 ++- 2 files changed, 56 insertions(+), 57 deletions(-) diff --git a/examples/lighting-app/bouffalolab/common/AppTask.cpp b/examples/lighting-app/bouffalolab/common/AppTask.cpp index f38ae87a61c340..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(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 +312,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 +335,49 @@ 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 +395,16 @@ 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(); } @@ -436,47 +470,9 @@ bool AppTask::ButtonPressed(void) 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; + 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..bcc9dbaec7a4b5 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, @@ -71,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_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)