Skip to content

Commit 71e1c94

Browse files
committed
adding the btn to light switch to control the level via switch
1 parent b536a10 commit 71e1c94

File tree

3 files changed

+83
-28
lines changed

3 files changed

+83
-28
lines changed

examples/light-switch-app/silabs/include/AppEvent.h

+6-4
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,12 @@ struct AppEvent
3333
kEventType_ResetWarning,
3434
kEventType_ResetCanceled,
3535
// Button events
36-
kEventType_UpPressed,
37-
kEventType_UpReleased,
38-
kEventType_DownPressed,
39-
kEventType_DownReleased,
36+
kEventType_ActionButtonPressed,
37+
kEventType_ActionButtonReleased,
38+
kEventType_FunctionButtonPressed,
39+
kEventType_FunctionButtonReleased,
40+
kEventType_TriggerLevelControlAction,
41+
kEventType_TriggerToggle,
4042
};
4143

4244
uint16_t Type;

examples/light-switch-app/silabs/include/LightSwitchMgr.h

+10-3
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,8 @@ class LightSwitchMgr
8484
void TriggerLightSwitchAction(LightSwitchAction action, bool isGroupCommand = false);
8585
void TriggerLevelControlAction(StepModeEnum stepMode, bool isGroupCommand = false);
8686

87+
StepModeEnum getStepMode();
88+
8789
AppEvent CreateNewEvent(AppEvent::AppEventTypes type);
8890

8991
static LightSwitchMgr & GetInstance() { return sSwitch; }
@@ -103,9 +105,14 @@ class LightSwitchMgr
103105
private:
104106
static LightSwitchMgr sSwitch;
105107

106-
Timer * mLongPressTimer = nullptr;
107-
bool mDownPressed = false;
108-
bool mResetWarning = false;
108+
Timer * mLongPressTimer = nullptr;
109+
bool mFunctionButtonPressed = false; // True when button0 is pressed, used to trigger factory reset
110+
bool mActionButtonPressed = false; // True when button1 is pressed, used to initiate toggle or level-up/down
111+
bool mActionButtonSuppressed = false; // True when both button0 and button1 are pressed, used to switch step direction
112+
bool mResetWarning = false;
113+
114+
// Default Step direction for Level control
115+
StepModeEnum stepDirection = StepModeEnum::kUp;
109116

110117
static void OnLongPressTimeout(Timer & timer);
111118
LightSwitchMgr() = default;

examples/light-switch-app/silabs/src/LightSwitchMgr.cpp

+67-21
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ AppEvent LightSwitchMgr::CreateNewEvent(AppEvent::AppEventTypes type)
5656
void LightSwitchMgr::Timer::Start()
5757
{
5858
// Starts or restarts the function timer
59-
if (osTimerStart(mHandler, pdMS_TO_TICKS(100)) != osOK)
59+
if (osTimerStart(mHandler, pdMS_TO_TICKS(LONG_PRESS_TIMEOUT)) != osOK)
6060
{
6161
SILABS_LOG("Timer start() failed");
6262
appError(CHIP_ERROR_INTERNAL);
@@ -80,7 +80,7 @@ void LightSwitchMgr::HandleLongPress()
8080
event.Handler = AppEventHandler;
8181
LightSwitchMgr * lightSwitch = &LightSwitchMgr::GetInstance();
8282
event.LightSwitchEvent.Context = lightSwitch;
83-
if (mDownPressed)
83+
if (mFunctionButtonPressed)
8484
{
8585
if (!mResetWarning)
8686
{
@@ -89,6 +89,13 @@ void LightSwitchMgr::HandleLongPress()
8989
AppTask::GetAppTask().PostEvent(&event);
9090
}
9191
}
92+
else if (mActionButtonPressed)
93+
{
94+
mActionButtonSuppressed = true;
95+
// Long press button up : Trigger Level Control Action
96+
event.Type = AppEvent::kEventType_TriggerLevelControlAction;
97+
AppTask::GetAppTask().PostEvent(&event);
98+
}
9299
}
93100

94101
void LightSwitchMgr::OnLongPressTimeout(LightSwitchMgr::Timer & timer)
@@ -201,6 +208,10 @@ void LightSwitchMgr::GenericSwitchOnShortRelease()
201208
DeviceLayer::PlatformMgr().ScheduleWork(GenericSwitchWorkerFunction, reinterpret_cast<intptr_t>(data));
202209
}
203210

211+
StepModeEnum LightSwitchMgr::getStepMode(){
212+
return stepDirection;
213+
}
214+
204215
void LightSwitchMgr::TriggerLightSwitchAction(LightSwitchAction action, bool isGroupCommand)
205216
{
206217
BindingCommandData * data = Platform::New<BindingCommandData>();
@@ -290,11 +301,11 @@ void LightSwitchMgr::ButtonEventHandler(uint8_t button, uint8_t btnAction)
290301
AppEvent event = {};
291302
if (btnAction == to_underlying(SilabsPlatform::ButtonAction::ButtonPressed))
292303
{
293-
event = LightSwitchMgr::GetInstance().CreateNewEvent(button ? AppEvent::kEventType_UpPressed : AppEvent::kEventType_DownPressed);
304+
event = LightSwitchMgr::GetInstance().CreateNewEvent(button ? AppEvent::kEventType_ActionButtonPressed : AppEvent::kEventType_FunctionButtonPressed);
294305
}
295306
else
296307
{
297-
event = LightSwitchMgr::GetInstance().CreateNewEvent(button ? AppEvent::kEventType_UpReleased : AppEvent::kEventType_DownReleased);
308+
event = LightSwitchMgr::GetInstance().CreateNewEvent(button ? AppEvent::kEventType_ActionButtonReleased : AppEvent::kEventType_FunctionButtonReleased);
298309
}
299310
AppTask::GetAppTask().PostEvent(&event);
300311
}
@@ -312,17 +323,21 @@ void LightSwitchMgr::AppEventHandler(AppEvent * aEvent)
312323
lightSwitch->mResetWarning = false;
313324
AppTask::GetAppTask().CancelFactoryResetSequence();
314325
break;
315-
case AppEvent::kEventType_DownPressed:
316-
aEvent->Handler = LightSwitchMgr::SwitchActionEventHandler;
317-
AppTask::GetAppTask().PostEvent(aEvent);
318-
lightSwitch->mDownPressed = true;
326+
case AppEvent::kEventType_FunctionButtonPressed:
327+
lightSwitch->mFunctionButtonPressed = true;
319328
if (lightSwitch->mLongPressTimer)
320329
{
321330
lightSwitch->mLongPressTimer->Start();
322331
}
332+
if (lightSwitch->mActionButtonPressed)
333+
{
334+
lightSwitch->mActionButtonSuppressed = true;
335+
lightSwitch->stepDirection = (lightSwitch->stepDirection == StepModeEnum::kUp) ? StepModeEnum::kDown : StepModeEnum::kUp;
336+
ChipLogProgress(AppServer, "Step direction changed. Current Step Direction : %s", ((lightSwitch->stepDirection == StepModeEnum::kUp) ? "kUp" : "kDown"));
337+
}
323338
break;
324-
case AppEvent::kEventType_DownReleased:
325-
lightSwitch->mDownPressed = false;
339+
case AppEvent::kEventType_FunctionButtonReleased:
340+
lightSwitch->mFunctionButtonPressed = false;
326341
if (lightSwitch->mLongPressTimer)
327342
{
328343
lightSwitch->mLongPressTimer->Stop();
@@ -333,11 +348,44 @@ void LightSwitchMgr::AppEventHandler(AppEvent * aEvent)
333348
AppTask::GetAppTask().PostEvent(aEvent);
334349
}
335350
break;
336-
case AppEvent::kEventType_UpPressed:
337-
case AppEvent::kEventType_UpReleased:
351+
case AppEvent::kEventType_ActionButtonPressed:
352+
lightSwitch->mActionButtonPressed = true;
353+
aEvent->Handler = LightSwitchMgr::SwitchActionEventHandler;
354+
AppTask::GetAppTask().PostEvent(aEvent);
355+
if (lightSwitch->mLongPressTimer)
356+
{
357+
lightSwitch->mLongPressTimer->Start();
358+
}
359+
if (lightSwitch->mFunctionButtonPressed)
360+
{
361+
lightSwitch->mActionButtonSuppressed = true;
362+
lightSwitch->stepDirection = (lightSwitch->stepDirection == StepModeEnum::kUp) ? StepModeEnum::kDown : StepModeEnum::kUp;
363+
ChipLogProgress(AppServer, "Step direction changed. Current Step Direction : %s", ((lightSwitch->stepDirection == StepModeEnum::kUp) ? "kUp" : "kDown"));
364+
}
365+
break;
366+
case AppEvent::kEventType_ActionButtonReleased:
367+
lightSwitch->mActionButtonPressed = false;
368+
if (lightSwitch->mLongPressTimer)
369+
{
370+
lightSwitch->mLongPressTimer->Stop();
371+
}
372+
if (lightSwitch->mActionButtonSuppressed)
373+
{
374+
lightSwitch->mActionButtonSuppressed = false;
375+
}
376+
else
377+
{
378+
aEvent->Type = AppEvent::kEventType_TriggerToggle;
379+
aEvent->Handler = LightSwitchMgr::SwitchActionEventHandler;
380+
AppTask::GetAppTask().PostEvent(aEvent);
381+
}
382+
aEvent->Type = AppEvent::kEventType_ActionButtonReleased;
338383
aEvent->Handler = LightSwitchMgr::SwitchActionEventHandler;
339384
AppTask::GetAppTask().PostEvent(aEvent);
340385
break;
386+
case AppEvent::kEventType_TriggerLevelControlAction:
387+
aEvent->Handler = LightSwitchMgr::SwitchActionEventHandler;
388+
AppTask::GetAppTask().PostEvent(aEvent);
341389
default:
342390
break;
343391
}
@@ -347,20 +395,18 @@ void LightSwitchMgr::SwitchActionEventHandler(AppEvent * aEvent)
347395
{
348396
switch(aEvent->Type)
349397
{
350-
case AppEvent::kEventType_UpPressed: {
351-
LightSwitchMgr::GetInstance().TriggerLightSwitchAction(LightSwitchMgr::LightSwitchAction::Toggle);
398+
case AppEvent::kEventType_ActionButtonPressed:
352399
LightSwitchMgr::GetInstance().GenericSwitchOnInitialPress();
353-
}
354400
break;
355-
case AppEvent::kEventType_UpReleased:
401+
case AppEvent::kEventType_ActionButtonReleased:
356402
LightSwitchMgr::GetInstance().GenericSwitchOnShortRelease();
357403
break;
358-
#if 0
359-
// TODO: Fix the button handling for the btn0 and btn1
360-
case AppEvent::kEventType_DownPressed:
361-
LightSwitchMgr::GetInstance().TriggerLevelControlAction(LevelControl::StepModeEnum::kDown);
404+
case AppEvent::kEventType_TriggerLevelControlAction:
405+
LightSwitchMgr::GetInstance().TriggerLevelControlAction(LightSwitchMgr::GetInstance().getStepMode());
406+
break;
407+
case AppEvent::kEventType_TriggerToggle:
408+
LightSwitchMgr::GetInstance().TriggerLightSwitchAction(LightSwitchMgr::LightSwitchAction::Toggle);
362409
break;
363-
#endif
364410
default:
365411
break;
366412
}

0 commit comments

Comments
 (0)