Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Silabs]Fixes and cleanup for our window app #30693

Merged
merged 3 commits into from
Nov 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
132 changes: 66 additions & 66 deletions examples/platform/silabs/BaseApplication.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,6 @@

#include <app/server/Server.h>

#if (defined(ENABLE_WSTK_LEDS) && (defined(SL_CATALOG_SIMPLE_LED_LED1_PRESENT) || defined(SIWX_917)))
#include "LEDWidget.h"
#endif // ENABLE_WSTK_LEDS

#ifdef DISPLAY_ENABLED
#include "lcd.h"
#ifdef QR_CODE_ENABLED
Expand Down Expand Up @@ -129,9 +125,6 @@ StaticQueue_t sAppEventQueueStruct;
StackType_t appStack[APP_TASK_STACK_SIZE / sizeof(StackType_t)];
StaticTask_t appTaskStruct;

BaseApplication::Function_t mFunction;
bool mFunctionTimerActive;

#ifdef DISPLAY_ENABLED
SilabsLCD slLCD;
#endif
Expand All @@ -149,7 +142,10 @@ Identify gIdentify = {

#endif // EMBER_AF_PLUGIN_IDENTIFY_SERVER
} // namespace
bool BaseApplication::sIsProvisioned = false;

bool BaseApplication::sIsProvisioned = false;
bool BaseApplication::sIsFactoryResetTriggered = false;
LEDWidget * BaseApplication::sAppActionLed = nullptr;

#ifdef DIC_ENABLE
namespace {
Expand Down Expand Up @@ -280,39 +276,16 @@ void BaseApplication::FunctionTimerEventHandler(TimerHandle_t xTimer)

void BaseApplication::FunctionEventHandler(AppEvent * aEvent)
{
if (aEvent->Type != AppEvent::kEventType_Timer)
{
return;
}

VerifyOrReturn(aEvent->Type == AppEvent::kEventType_Timer);
// If we reached here, the button was held past FACTORY_RESET_TRIGGER_TIMEOUT,
// initiate factory reset
if (mFunctionTimerActive && mFunction == kFunction_StartBleAdv)
if (!sIsFactoryResetTriggered)
{
SILABS_LOG("Factory Reset Triggered. Release button within %ums to cancel.", FACTORY_RESET_CANCEL_WINDOW_TIMEOUT);

// Start timer for FACTORY_RESET_CANCEL_WINDOW_TIMEOUT to allow user to
// cancel, if required.
StartFunctionTimer(FACTORY_RESET_CANCEL_WINDOW_TIMEOUT);

#if CHIP_CONFIG_ENABLE_ICD_SERVER == 1
StartStatusLEDTimer();
#endif // CHIP_CONFIG_ENABLE_ICD_SERVER

mFunction = kFunction_FactoryReset;

#if (defined(ENABLE_WSTK_LEDS) && (defined(SL_CATALOG_SIMPLE_LED_LED1_PRESENT) || defined(SIWX_917)))
// Turn off all LEDs before starting blink to make sure blink is
// co-ordinated.
sStatusLED.Set(false);
sStatusLED.Blink(500);
#endif // ENABLE_WSTK_LEDS
StartFactoryResetSequence();
}
else if (mFunctionTimerActive && mFunction == kFunction_FactoryReset)
else
{
// Actually trigger Factory Reset
mFunction = kFunction_NoneSelected;

// The factory reset sequence was in motion. The cancellation window expired.
// Factory Reset the device now.
#if CHIP_CONFIG_ENABLE_ICD_SERVER == 1
StopStatusLEDTimer();
#endif // CHIP_CONFIG_ENABLE_ICD_SERVER
Expand Down Expand Up @@ -436,7 +409,8 @@ void BaseApplication::LightEventHandler()

#endif // CHIP_CONFIG_ENABLE_ICD_SERVER

#if (defined(ENABLE_WSTK_LEDS) && (defined(SL_CATALOG_SIMPLE_LED_LED1_PRESENT) || defined(SIWX_917)))
#if defined(ENABLE_WSTK_LEDS)
#if (defined(SL_CATALOG_SIMPLE_LED_LED1_PRESENT) || defined(SIWX_917))
// Update the status LED if factory reset has not been initiated.
//
// If system has "full connectivity", keep the LED On constantly.
Expand All @@ -449,13 +423,18 @@ void BaseApplication::LightEventHandler()
// the LEDs at an even rate of 100ms.
//
// Otherwise, blink the LED ON for a very short time.
if (mFunction != kFunction_FactoryReset)
if (!sIsFactoryResetTriggered)
{
ActivateStatusLedPatterns();
}

sStatusLED.Animate();
#endif // ENABLE_WSTK_LEDS && SL_CATALOG_SIMPLE_LED_LED1_PRESENT
#endif // SL_CATALOG_SIMPLE_LED_LED1_PRESENT
if (sAppActionLed)
{
sAppActionLed->Animate();
}
#endif // ENABLE_WSTK_LEDS
}

void BaseApplication::ButtonHandler(AppEvent * aEvent)
Expand All @@ -469,20 +448,22 @@ void BaseApplication::ButtonHandler(AppEvent * aEvent)
// start blinking within the FACTORY_RESET_CANCEL_WINDOW_TIMEOUT
if (aEvent->ButtonEvent.Action == static_cast<uint8_t>(SilabsPlatform::ButtonAction::ButtonPressed))
{
if (!mFunctionTimerActive && mFunction == kFunction_NoneSelected)
{
StartFunctionTimer(FACTORY_RESET_TRIGGER_TIMEOUT);
mFunction = kFunction_StartBleAdv;
}
StartFunctionTimer(FACTORY_RESET_TRIGGER_TIMEOUT);
}
else
{
// If the button was released before factory reset got initiated, open the commissioning window and start BLE advertissement
// in fast mode
if (mFunctionTimerActive && mFunction == kFunction_StartBleAdv)
if (sIsFactoryResetTriggered)
{
CancelFactoryResetSequence();
}
else
{
// The factory reset sequence was not initiated,
// Press and Release:
// - Open the commissioning window and start BLE advertissement in fast mode when not commissioned
// - Output qr code in logs
// - Cycle LCD screen
CancelFunctionTimer();
mFunction = kFunction_NoneSelected;

OutputQrCode(false);
#ifdef DISPLAY_ENABLED
Expand Down Expand Up @@ -515,19 +496,6 @@ void BaseApplication::ButtonHandler(AppEvent * aEvent)
#endif // CHIP_CONFIG_ENABLE_ICD_SERVER
}
}
else if (mFunctionTimerActive && mFunction == kFunction_FactoryReset)
{
CancelFunctionTimer();

#if CHIP_CONFIG_ENABLE_ICD_SERVER == 1
StopStatusLEDTimer();
#endif

// Change the function to none selected since factory reset has been
// canceled.
mFunction = kFunction_NoneSelected;
SILABS_LOG("Factory Reset has been Canceled");
}
}
}

Expand All @@ -538,8 +506,6 @@ void BaseApplication::CancelFunctionTimer()
SILABS_LOG("app timer stop() failed");
appError(APP_ERROR_STOP_TIMER_FAILED);
}

mFunctionTimerActive = false;
}

void BaseApplication::StartFunctionTimer(uint32_t aTimeoutInMs)
Expand All @@ -558,8 +524,42 @@ void BaseApplication::StartFunctionTimer(uint32_t aTimeoutInMs)
SILABS_LOG("app timer start() failed");
appError(APP_ERROR_START_TIMER_FAILED);
}
}

void BaseApplication::StartFactoryResetSequence()
{
// Initiate the factory reset sequence
SILABS_LOG("Factory Reset Triggered. Release button within %ums to cancel.", FACTORY_RESET_CANCEL_WINDOW_TIMEOUT);

// Start timer for FACTORY_RESET_CANCEL_WINDOW_TIMEOUT to allow user to
// cancel, if required.
StartFunctionTimer(FACTORY_RESET_CANCEL_WINDOW_TIMEOUT);

mFunctionTimerActive = true;
sIsFactoryResetTriggered = true;
#if CHIP_CONFIG_ENABLE_ICD_SERVER == 1
StartStatusLEDTimer();
#endif // CHIP_CONFIG_ENABLE_ICD_SERVER

#if (defined(ENABLE_WSTK_LEDS) && (defined(SL_CATALOG_SIMPLE_LED_LED1_PRESENT) || defined(SIWX_917)))
// Turn off all LEDs before starting blink to make sure blink is
// co-ordinated.
sStatusLED.Set(false);
sStatusLED.Blink(500);
#endif // ENABLE_WSTK_LEDS
}

void BaseApplication::CancelFactoryResetSequence()
{
CancelFunctionTimer();

#if CHIP_CONFIG_ENABLE_ICD_SERVER == 1
StopStatusLEDTimer();
#endif
if (sIsFactoryResetTriggered)
{
sIsFactoryResetTriggered = false;
SILABS_LOG("Factory Reset has been Canceled");
}
}

void BaseApplication::StartStatusLEDTimer()
Expand Down Expand Up @@ -754,7 +754,7 @@ void BaseApplication::OutputQrCode(bool refreshLCD)
}
}

bool BaseApplication::getWifiProvisionStatus()
bool BaseApplication::GetProvisionStatus()
{
return BaseApplication::sIsProvisioned;
}
35 changes: 24 additions & 11 deletions examples/platform/silabs/BaseApplication.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@
#include <platform/CHIPDeviceEvent.h>
#include <platform/CHIPDeviceLayer.h>

#if defined(ENABLE_WSTK_LEDS)
#include "LEDWidget.h"
#endif // ENABLE_WSTK_LEDS

#ifdef EMBER_AF_PLUGIN_IDENTIFY_SERVER
#include <app/clusters/identify-server/identify-server.h>
#endif
Expand Down Expand Up @@ -71,6 +75,8 @@ class BaseApplication
BaseApplication() = default;
virtual ~BaseApplication(){};
static bool sIsProvisioned;
static bool sIsFactoryResetTriggered;
static LEDWidget * sAppActionLed;

/**
* @brief Create AppTask task and Event Queue
Expand All @@ -80,6 +86,20 @@ class BaseApplication
*/
CHIP_ERROR StartAppTask(TaskFunction_t taskFunction);

/**
* @brief Links the application specific led to the baseApplication context
* in order to synchronize both LED animations.
* Some apps may not have an application led or no animation patterns.
*
* @param appLed Pointer to the configure LEDWidget for the application defined LED
*/
void LinkAppLed(LEDWidget * appLed) { sAppActionLed = appLed; }

/**
* @brief Remove the app Led linkage form the baseApplication context
*/
void UnlinkAppLed() { sAppActionLed = nullptr; }

/**
* @brief PostEvent function that add event to AppTask queue for processing
*
Expand All @@ -104,7 +124,10 @@ class BaseApplication
* Turns off Status LED before stopping timer
*/
static void StopStatusLEDTimer(void);
static bool getWifiProvisionStatus(void);
static bool GetProvisionStatus(void);

static void StartFactoryResetSequence(void);
static void CancelFactoryResetSequence(void);

#ifdef EMBER_AF_PLUGIN_IDENTIFY_SERVER
// Idenfiy server command callbacks.
Expand All @@ -114,16 +137,6 @@ class BaseApplication
static void OnTriggerIdentifyEffect(Identify * identify);
#endif

enum Function_t
{
kFunction_NoneSelected = 0,
kFunction_SoftwareUpdate = 0,
kFunction_StartBleAdv = 1,
kFunction_FactoryReset = 2,

kFunction_Invalid
} Function;

protected:
CHIP_ERROR Init();

Expand Down
15 changes: 0 additions & 15 deletions examples/window-app/silabs/include/AppEvent.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,6 @@ struct AppEvent
{
kEventType_Button = 0,
kEventType_Timer,
kEventType_Light,
kEventType_Install,
kEventType_ButtonDown,
kEventType_ButtonUp,

kEventType_None,
kEventType_Reset,
kEventType_ResetPressed,
kEventType_ResetWarning,
kEventType_ResetCanceled,
// Button events
Expand All @@ -54,13 +46,6 @@ struct AppEvent

// Cover Attribute update events
kEventType_AttributeChange,

// Provisioning events
kEventType_ProvisionedStateChanged,
kEventType_ConnectivityStateChanged,
kEventType_BLEConnectionsChanged,
kEventType_WinkOff,
kEventType_WinkOn,
};

uint16_t Type;
Expand Down
29 changes: 7 additions & 22 deletions examples/window-app/silabs/include/WindowManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -116,40 +116,26 @@ class WindowManager
void PostAttributeChange(chip::EndpointId endpoint, chip::AttributeId attributeId);

static void ButtonEventHandler(uint8_t button, uint8_t btnAction);
void UpdateLEDs();
void UpdateLED();
void UpdateLCD();

static void GeneralEventHandler(AppEvent * aEvent);

static void OnIconTimeout(WindowManager::Timer & timer);

protected:
struct StateFlags
{
#if CHIP_ENABLE_OPENTHREAD
bool isThreadProvisioned = false;
bool isThreadEnabled = false;
#else
bool isWiFiProvisioned = false;
bool isWiFiEnabled = false;
#endif
bool haveBLEConnections = false;
bool isWinking = false;
};

Cover & GetCover();
Cover * GetCover(chip::EndpointId endpoint);

static void OnLongPressTimeout(Timer & timer);

Timer * mLongPressTimer = nullptr;
StateFlags mState;
bool mTiltMode = false;
bool mUpPressed = false;
bool mDownPressed = false;
bool mUpSuppressed = false;
bool mDownSuppressed = false;
bool mResetWarning = false;
bool mTiltMode = false;
bool mUpPressed = false;
bool mDownPressed = false;
bool mUpSuppressed = false;
bool mDownSuppressed = false;
bool mResetWarning = false;

private:
void HandleLongPress();
Expand All @@ -158,7 +144,6 @@ class WindowManager
Cover mCoverList[WINDOW_COVER_COUNT];
uint8_t mCurrentCover = 0;

LEDWidget mStatusLED;
LEDWidget mActionLED;
#ifdef DISPLAY_ENABLED
Timer mIconTimer;
Expand Down
Loading
Loading