Skip to content

Commit

Permalink
[Bouffalolab] Improve the handle of button jitter (#29366)
Browse files Browse the repository at this point in the history
* enhance button handle

* fix restyle
  • Loading branch information
wy-hh authored and pull[bot] committed Jan 20, 2024
1 parent ba13cfd commit 1855010
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 57 deletions.
100 changes: 48 additions & 52 deletions examples/lighting-app/bouffalolab/common/AppTask.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down Expand Up @@ -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)
Expand All @@ -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;
Expand All @@ -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();
}
Expand Down Expand Up @@ -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
13 changes: 8 additions & 5 deletions examples/lighting-app/bouffalolab/common/AppTask.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand All @@ -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)
Expand Down

0 comments on commit 1855010

Please sign in to comment.