Skip to content

Commit

Permalink
Complete Button Abstraction (#26581)
Browse files Browse the repository at this point in the history
  • Loading branch information
jepenven-silabs authored and pull[bot] committed Jun 13, 2024
1 parent bf0c381 commit 2384000
Show file tree
Hide file tree
Showing 14 changed files with 46 additions and 63 deletions.
4 changes: 0 additions & 4 deletions examples/chef/efr32/src/AppTask.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -125,9 +125,7 @@ AppTask AppTask::sAppTask;
CHIP_ERROR AppTask::Init()
{
CHIP_ERROR err = CHIP_NO_ERROR;
#ifdef SL_CATALOG_SIMPLE_BUTTON_PRESENT
chip::DeviceLayer::Silabs::GetPlatform().SetButtonsCb(AppTask::ButtonEventHandler);
#endif

err = BaseApplication::Init(&gIdentify);
if (err != CHIP_NO_ERROR)
Expand Down Expand Up @@ -169,7 +167,6 @@ void AppTask::AppTaskMain(void * pvParameter)
}
}

#ifdef SL_CATALOG_SIMPLE_BUTTON_PRESENT
void AppTask::ButtonEventHandler(uint8_t button, uint8_t btnAction)
{
AppEvent button_event = {};
Expand All @@ -182,4 +179,3 @@ void AppTask::ButtonEventHandler(uint8_t button, uint8_t btnAction)
sAppTask.PostEvent(&button_event);
}
}
#endif // SL_CATALOG_SIMPLE_BUTTON_PRESENT
7 changes: 4 additions & 3 deletions examples/light-switch-app/silabs/SiWx917/src/AppTask.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ constexpr chip::EndpointId kGenericSwitchEndpoint = 2;

using namespace chip;
using namespace ::chip::DeviceLayer;
using namespace ::chip::DeviceLayer::Silabs;

namespace {

Expand Down Expand Up @@ -239,7 +240,7 @@ void AppTask::SwitchActionEventHandler(AppEvent * aEvent)

static bool mCurrentButtonState = false;

if (aEvent->ButtonEvent.Action == SL_SIMPLE_BUTTON_PRESSED)
if (aEvent->ButtonEvent.Action == static_cast<uint8_t>(SilabsPlatform::ButtonAction::ButtonPressed))
{
mCurrentButtonState = !mCurrentButtonState;
LightSwitchMgr::LightSwitchAction action =
Expand All @@ -252,7 +253,7 @@ void AppTask::SwitchActionEventHandler(AppEvent * aEvent)
sAppTask.GetLCD().WriteDemoUI(mCurrentButtonState);
#endif
}
else if (aEvent->ButtonEvent.Action == SL_SIMPLE_BUTTON_RELEASED)
else if (aEvent->ButtonEvent.Action == static_cast<uint8_t>(SilabsPlatform::ButtonAction::ButtonReleased))
{
LightSwitchMgr::GetInstance().GenericSwitchOnShortRelease();
}
Expand All @@ -268,7 +269,7 @@ void AppTask::ButtonEventHandler(uint8_t button, uint8_t btnAction)
button_event.Handler = SwitchActionEventHandler;
sAppTask.PostEvent(&button_event);
}
else if (button == SIWx917_BTN0 && btnAction == SL_SIMPLE_BUTTON_PRESSED)
else if (button == SIWx917_BTN0 && btnAction == static_cast<uint8_t>(SilabsPlatform::ButtonAction::ButtonPressed))
{
button_event.Handler = BaseApplication::ButtonHandler;
sAppTask.PostEvent(&button_event);
Expand Down
9 changes: 3 additions & 6 deletions examples/light-switch-app/silabs/efr32/src/AppTask.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ constexpr chip::EndpointId kGenericSwitchEndpoint = 2;

using namespace chip;
using namespace ::chip::DeviceLayer;
using namespace ::chip::DeviceLayer::Silabs;

namespace {

Expand Down Expand Up @@ -153,9 +154,7 @@ AppTask AppTask::sAppTask;
CHIP_ERROR AppTask::Init()
{
CHIP_ERROR err = CHIP_NO_ERROR;
#ifdef SL_CATALOG_SIMPLE_BUTTON_PRESENT
chip::DeviceLayer::Silabs::GetPlatform().SetButtonsCb(AppTask::ButtonEventHandler);
#endif

#ifdef DISPLAY_ENABLED
GetLCD().Init((uint8_t *) "Light Switch");
Expand Down Expand Up @@ -229,14 +228,13 @@ void AppTask::OnIdentifyStop(Identify * identify)
#endif
}

#ifdef SL_CATALOG_SIMPLE_BUTTON_PRESENT
void AppTask::SwitchActionEventHandler(AppEvent * aEvent)
{
VerifyOrReturn(aEvent->Type == AppEvent::kEventType_Button);

static bool mCurrentButtonState = false;

if (aEvent->ButtonEvent.Action == SL_SIMPLE_BUTTON_PRESSED)
if (aEvent->ButtonEvent.Action == static_cast<uint8_t>(SilabsPlatform::ButtonAction::ButtonPressed))
{
mCurrentButtonState = !mCurrentButtonState;
LightSwitchMgr::LightSwitchAction action =
Expand All @@ -249,7 +247,7 @@ void AppTask::SwitchActionEventHandler(AppEvent * aEvent)
sAppTask.GetLCD().WriteDemoUI(mCurrentButtonState);
#endif
}
else if (aEvent->ButtonEvent.Action == SL_SIMPLE_BUTTON_RELEASED)
else if (aEvent->ButtonEvent.Action == static_cast<uint8_t>(SilabsPlatform::ButtonAction::ButtonReleased))
{
LightSwitchMgr::GetInstance().GenericSwitchOnShortRelease();
}
Expand All @@ -272,4 +270,3 @@ void AppTask::ButtonEventHandler(uint8_t button, uint8_t btnAction)
sAppTask.PostEvent(&button_event);
}
}
#endif // SL_CATALOG_SIMPLE_BUTTON_PRESENT
5 changes: 3 additions & 2 deletions examples/lighting-app/silabs/SiWx917/src/AppTask.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@

using namespace chip;
using namespace ::chip::DeviceLayer;
using namespace ::chip::DeviceLayer::Silabs;

namespace {

Expand Down Expand Up @@ -240,12 +241,12 @@ void AppTask::ButtonEventHandler(uint8_t button, uint8_t btnAction)
AppEvent button_event = {};
button_event.Type = AppEvent::kEventType_Button;
button_event.ButtonEvent.Action = btnAction;
if (button == SIWx917_BTN1 && btnAction == SL_SIMPLE_BUTTON_PRESSED)
if (button == SIWx917_BTN1 && btnAction == static_cast<uint8_t>(SilabsPlatform::ButtonAction::ButtonPressed))
{
button_event.Handler = LightActionEventHandler;
sAppTask.PostEvent(&button_event);
}
else if (button == SIWx917_BTN0 && btnAction == SL_SIMPLE_BUTTON_PRESSED)
else if (button == SIWx917_BTN0 && btnAction == static_cast<uint8_t>(SilabsPlatform::ButtonAction::ButtonPressed))
{
button_event.Handler = BaseApplication::ButtonHandler;
sAppTask.PostEvent(&button_event);
Expand Down
4 changes: 2 additions & 2 deletions examples/lighting-app/silabs/efr32/include/AppTask.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ class AppTask : public BaseApplication
static void AppTaskMain(void * pvParameter);

CHIP_ERROR StartAppTask();
#ifdef SL_CATALOG_SIMPLE_BUTTON_PRESENT

/**
* @brief Event handler when a button is pressed
* Function posts an event for button processing
Expand All @@ -78,7 +78,7 @@ class AppTask : public BaseApplication
* SL_SIMPLE_BUTTON_RELEASED or SL_SIMPLE_BUTTON_DISABLED
*/
static void ButtonEventHandler(uint8_t button, uint8_t btnAction);
#endif

/**
* @brief Callback called by the identify-server when an identify command is received
*
Expand Down
19 changes: 5 additions & 14 deletions examples/lighting-app/silabs/efr32/src/AppTask.cpp
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,12 @@
#define LIGHT_LED 0
#endif

#ifdef SL_CATALOG_SIMPLE_BUTTON_PRESENT

#define APP_FUNCTION_BUTTON 0
#define APP_LIGHT_SWITCH 1
#endif

using namespace chip;
using namespace ::chip::DeviceLayer;
using namespace ::chip::DeviceLayer::Silabs;

namespace {

Expand Down Expand Up @@ -132,9 +130,7 @@ AppTask AppTask::sAppTask;
CHIP_ERROR AppTask::Init()
{
CHIP_ERROR err = CHIP_NO_ERROR;
#ifdef SL_CATALOG_SIMPLE_BUTTON_PRESENT
chip::DeviceLayer::Silabs::GetPlatform().SetButtonsCb(AppTask::ButtonEventHandler);
#endif

#ifdef DISPLAY_ENABLED
GetLCD().Init((uint8_t *) "Lighting-App");
Expand Down Expand Up @@ -241,13 +237,11 @@ void AppTask::LightActionEventHandler(AppEvent * aEvent)
action = static_cast<LightingManager::Action_t>(aEvent->LightEvent.Action);
actor = aEvent->LightEvent.Actor;
}
#ifdef SL_CATALOG_SIMPLE_BUTTON_PRESENT
else if (aEvent->Type == AppEvent::kEventType_Button)
{
action = (LightMgr().IsLightOn()) ? LightingManager::OFF_ACTION : LightingManager::ON_ACTION;
actor = AppEvent::kEventType_Button;
}
#endif
else
{
err = APP_ERROR_UNHANDLED_EVENT;
Expand All @@ -263,14 +257,14 @@ void AppTask::LightActionEventHandler(AppEvent * aEvent)
}
}
}
#ifdef SL_CATALOG_SIMPLE_BUTTON_PRESENT

void AppTask::ButtonEventHandler(uint8_t button, uint8_t btnAction)
{
AppEvent button_event = {};
button_event.Type = AppEvent::kEventType_Button;
button_event.ButtonEvent.Action = btnAction;

if (button == APP_LIGHT_SWITCH && btnAction == SL_SIMPLE_BUTTON_PRESSED)
if (button == APP_LIGHT_SWITCH && btnAction == static_cast<uint8_t>(SilabsPlatform::ButtonAction::ButtonPressed))
{
button_event.Handler = LightActionEventHandler;
AppTask::GetAppTask().PostEvent(&button_event);
Expand All @@ -281,7 +275,6 @@ void AppTask::ButtonEventHandler(uint8_t button, uint8_t btnAction)
AppTask::GetAppTask().PostEvent(&button_event);
}
}
#endif

void AppTask::ActionInitiated(LightingManager::Action_t aAction, int32_t aActor)
{
Expand All @@ -294,12 +287,11 @@ void AppTask::ActionInitiated(LightingManager::Action_t aAction, int32_t aActor)
#ifdef DISPLAY_ENABLED
sAppTask.GetLCD().WriteDemoUI(lightOn);
#endif
#ifdef SL_CATALOG_SIMPLE_BUTTON_PRESENT

if (aActor == AppEvent::kEventType_Button)
{
sAppTask.mSyncClusterToButtonAction = true;
}
#endif
}

void AppTask::ActionCompleted(LightingManager::Action_t aAction)
Expand All @@ -313,13 +305,12 @@ void AppTask::ActionCompleted(LightingManager::Action_t aAction)
{
SILABS_LOG("Light OFF")
}
#ifdef SL_CATALOG_SIMPLE_BUTTON_PRESENT

if (sAppTask.mSyncClusterToButtonAction)
{
chip::DeviceLayer::PlatformMgr().ScheduleWork(UpdateClusterState, reinterpret_cast<intptr_t>(nullptr));
sAppTask.mSyncClusterToButtonAction = false;
}
#endif
}

void AppTask::PostLightActionRequest(int32_t aActor, LightingManager::Action_t aAction)
Expand Down
5 changes: 3 additions & 2 deletions examples/lock-app/silabs/SiWx917/src/AppTask.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ using chip::app::Clusters::DoorLock::OperationSourceEnum;

using namespace chip;
using namespace ::chip::DeviceLayer;
using namespace ::chip::DeviceLayer::Silabs;
using namespace ::chip::DeviceLayer::Internal;
using namespace SI917DoorLock::LockInitParams;

Expand Down Expand Up @@ -348,12 +349,12 @@ void AppTask::ButtonEventHandler(uint8_t button, uint8_t btnAction)
button_event.Type = AppEvent::kEventType_Button;
button_event.ButtonEvent.Action = btnAction;

if (button == SIWx917_BTN1 && btnAction == SL_SIMPLE_BUTTON_PRESSED)
if (button == SIWx917_BTN1 && btnAction == static_cast<uint8_t>(SilabsPlatform::ButtonAction::ButtonPressed))
{
button_event.Handler = LockActionEventHandler;
sAppTask.PostEvent(&button_event);
}
else if (button == SIWx917_BTN0 && btnAction == SL_SIMPLE_BUTTON_PRESSED)
else if (button == SIWx917_BTN0 && btnAction == static_cast<uint8_t>(SilabsPlatform::ButtonAction::ButtonPressed))
{
button_event.Handler = BaseApplication::ButtonHandler;
sAppTask.PostEvent(&button_event);
Expand Down
7 changes: 2 additions & 5 deletions examples/lock-app/silabs/efr32/src/AppTask.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ using chip::app::Clusters::DoorLock::OperationSourceEnum;

using namespace chip;
using namespace ::chip::DeviceLayer;
using namespace ::chip::DeviceLayer::Silabs;
using namespace ::chip::DeviceLayer::Internal;
using namespace EFR32DoorLock::LockInitParams;

Expand Down Expand Up @@ -143,9 +144,7 @@ CHIP_ERROR AppTask::Init()
{
CHIP_ERROR err = CHIP_NO_ERROR;

#ifdef SL_CATALOG_SIMPLE_BUTTON_PRESENT
chip::DeviceLayer::Silabs::GetPlatform().SetButtonsCb(AppTask::ButtonEventHandler);
#endif // SL_CATALOG_SIMPLE_BUTTON_PRESENT

#ifdef DISPLAY_ENABLED
GetLCD().Init((uint8_t *) "Lock-App", true);
Expand Down Expand Up @@ -361,14 +360,13 @@ void AppTask::LockActionEventHandler(AppEvent * aEvent)
}
}

#ifdef SL_CATALOG_SIMPLE_BUTTON_PRESENT
void AppTask::ButtonEventHandler(uint8_t button, uint8_t btnAction)
{
AppEvent button_event = {};
button_event.Type = AppEvent::kEventType_Button;
button_event.ButtonEvent.Action = btnAction;

if (button == APP_LOCK_SWITCH && btnAction == SL_SIMPLE_BUTTON_PRESSED)
if (button == APP_LOCK_SWITCH && btnAction == static_cast<uint8_t>(SilabsPlatform::ButtonAction::ButtonPressed))
{
button_event.Handler = LockActionEventHandler;
sAppTask.PostEvent(&button_event);
Expand All @@ -379,7 +377,6 @@ void AppTask::ButtonEventHandler(uint8_t button, uint8_t btnAction)
sAppTask.PostEvent(&button_event);
}
}
#endif // SL_CATALOG_SIMPLE_BUTTON_PRESENT

void AppTask::ActionInitiated(LockManager::Action_t aAction, int32_t aActor)
{
Expand Down
5 changes: 4 additions & 1 deletion examples/platform/silabs/SiWx917/BaseApplication.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@
#include <setup_payload/QRCodeSetupPayloadGenerator.h>
#include <setup_payload/SetupPayload.h>

#include <platform/silabs/platformAbstraction/SilabsPlatform.h>

#ifdef SL_WIFI
#include "wfx_host_events.h"
#include <app/clusters/network-commissioning/network-commissioning.h>
Expand All @@ -70,6 +72,7 @@

using namespace chip;
using namespace ::chip::DeviceLayer;
using namespace ::chip::DeviceLayer::Silabs;

namespace {

Expand Down Expand Up @@ -355,7 +358,7 @@ void BaseApplication::ButtonHandler(AppEvent * aEvent)
// FACTORY_RESET_TRIGGER_TIMEOUT to signal factory reset has been initiated.
// To cancel factory reset: release the APP_FUNCTION_BUTTON once all LEDs
// start blinking within the FACTORY_RESET_CANCEL_WINDOW_TIMEOUT
if (aEvent->ButtonEvent.Action == SL_SIMPLE_BUTTON_PRESSED)
if (aEvent->ButtonEvent.Action == static_cast<uint8_t>(SilabsPlatform::ButtonAction::ButtonPressed))
{
if ((!mFunctionTimerActive) && (mFunction == kFunction_NoneSelected))
{
Expand Down
16 changes: 7 additions & 9 deletions examples/platform/silabs/efr32/BaseApplication.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@
#include <platform/silabs/ThreadStackManagerImpl.h>
#endif // CHIP_ENABLE_OPENTHREAD

#include <platform/silabs/platformAbstraction/SilabsPlatform.h>

#ifdef SL_WIFI
#include "wfx_host_events.h"
#include <app/clusters/network-commissioning/network-commissioning.h>
Expand All @@ -75,12 +77,11 @@
#if defined(ENABLE_WSTK_LEDS) && defined(SL_CATALOG_SIMPLE_LED_LED1_PRESENT)
#define SYSTEM_STATE_LED 0
#endif // ENABLE_WSTK_LEDS
#ifdef SL_CATALOG_SIMPLE_BUTTON_PRESENT
#define APP_FUNCTION_BUTTON 0
#endif

using namespace chip;
using namespace ::chip::DeviceLayer;
using namespace ::chip::DeviceLayer::Silabs;

namespace {

Expand Down Expand Up @@ -397,7 +398,7 @@ void BaseApplication::LightEventHandler()
sStatusLED.Animate();
#endif // ENABLE_WSTK_LEDS
}
#ifdef SL_CATALOG_SIMPLE_BUTTON_PRESENT

void BaseApplication::ButtonHandler(AppEvent * aEvent)
{
// To trigger software update: press the APP_FUNCTION_BUTTON button briefly (<
Expand All @@ -407,7 +408,7 @@ void BaseApplication::ButtonHandler(AppEvent * aEvent)
// FACTORY_RESET_TRIGGER_TIMEOUT to signal factory reset has been initiated.
// To cancel factory reset: release the APP_FUNCTION_BUTTON once all LEDs
// start blinking within the FACTORY_RESET_CANCEL_WINDOW_TIMEOUT
if (aEvent->ButtonEvent.Action == SL_SIMPLE_BUTTON_PRESSED)
if (aEvent->ButtonEvent.Action == static_cast<uint8_t>(SilabsPlatform::ButtonAction::ButtonPressed))
{
if (!mFunctionTimerActive && mFunction == kFunction_NoneSelected)
{
Expand Down Expand Up @@ -444,10 +445,7 @@ void BaseApplication::ButtonHandler(AppEvent * aEvent)
SILABS_LOG("Failed to open the Basic Commissioning Window");
}
}
else
{
SILABS_LOG("Network is already provisioned, Ble advertissement not enabled");
}
else { SILABS_LOG("Network is already provisioned, Ble advertissement not enabled"); }
}
else if (mFunctionTimerActive && mFunction == kFunction_FactoryReset)
{
Expand All @@ -464,7 +462,7 @@ void BaseApplication::ButtonHandler(AppEvent * aEvent)
}
}
}
#endif

void BaseApplication::CancelFunctionTimer()
{
if (xTimerStop(sFunctionTimer, pdMS_TO_TICKS(0)) == pdFAIL)
Expand Down
Loading

0 comments on commit 2384000

Please sign in to comment.