Skip to content

Commit e1557d8

Browse files
committed
Fixed button handling in dimmer-switch app
1 parent c77667f commit e1557d8

File tree

4 files changed

+273
-267
lines changed

4 files changed

+273
-267
lines changed

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

+52
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,29 @@ class AppTask : public BaseApplication
5757

5858
static AppTask & GetAppTask() { return sAppTask; }
5959

60+
struct Timer
61+
{
62+
typedef void (*Callback)(Timer & timer);
63+
64+
Timer(uint32_t timeoutInMs, Callback callback, void * context);
65+
~Timer();
66+
67+
void Start();
68+
void Stop();
69+
void Timeout();
70+
71+
Callback mCallback = nullptr;
72+
void * mContext = nullptr;
73+
bool mIsActive = false;
74+
75+
osTimerId_t mHandler = nullptr;
76+
77+
private:
78+
static void TimerCallback(void * timerCbArg);
79+
};
80+
81+
Timer * longPressTimer = nullptr;
82+
6083
/**
6184
* @brief AppTask task main loop function
6285
*
@@ -65,6 +88,19 @@ class AppTask : public BaseApplication
6588
static void AppTaskMain(void * pvParameter);
6689

6790
CHIP_ERROR StartAppTask();
91+
/**
92+
* @brief Event handler when a button is pressed
93+
* Function posts an event for button processing
94+
*
95+
* @param buttonHandle BTN0 or BTN1
96+
* @param btnAction button action - SL_SIMPLE_BUTTON_PRESSED,
97+
* SL_SIMPLE_BUTTON_RELEASED or SL_SIMPLE_BUTTON_DISABLED
98+
*/
99+
static void ButtonEventHandler(uint8_t button, uint8_t btnAction);
100+
101+
AppEvent CreateNewEvent(AppEvent::AppEventTypes type);
102+
103+
static void AppEventHandler(AppEvent * aEvent);
68104

69105
private:
70106
static AppTask sAppTask;
@@ -75,4 +111,20 @@ class AppTask : public BaseApplication
75111
* @return CHIP_ERROR
76112
*/
77113
CHIP_ERROR Init();
114+
115+
/**
116+
* @brief PB1 Button event processing function
117+
* Function triggers a switch action sent to the CHIP task
118+
*
119+
* @param aEvent button event being processed
120+
*/
121+
static void SwitchActionEventHandler(AppEvent * aEvent);
122+
123+
static void OnLongPressTimeout(Timer & timer);
124+
125+
/**
126+
* @brief This function will be called when PB1 is
127+
* long-pressed to trigger the level-control action
128+
*/
129+
void HandleLongPress();
78130
};

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

+9-56
Original file line numberDiff line numberDiff line change
@@ -52,84 +52,37 @@ class LightSwitchMgr
5252
.stepSize = 1, .transitionTime = 0, .optionsMask = 0, .optionsOverride = 0
5353
};
5454

55-
struct Timer
56-
{
57-
typedef void (*Callback)(Timer & timer);
58-
59-
Timer(uint32_t timeoutInMs, Callback callback, void * context);
60-
~Timer();
61-
62-
void Start();
63-
void Stop();
64-
void Timeout();
65-
66-
Callback mCallback = nullptr;
67-
void * mContext = nullptr;
68-
bool mIsActive = false;
69-
70-
osTimerId_t mHandler = nullptr;
71-
72-
private:
73-
static void TimerCallback(void * timerCbArg);
74-
};
75-
7655
CHIP_ERROR Init(chip::EndpointId lightSwitchEndpoint, chip::EndpointId genericSwitchEndpoint);
7756

7857
void GenericSwitchOnInitialPress();
7958
void GenericSwitchOnShortRelease();
8059

60+
/**
61+
* @brief Button event processing function
62+
* Function triggers a switch action sent to the CHIP task
63+
*
64+
* @param aEvent button event being processed
65+
*/
66+
static void SwitchActionEventHandler(AppEvent * aEvent);
67+
8168
void TriggerLightSwitchAction(LightSwitchAction action, bool isGroupCommand = false);
8269
void TriggerLevelControlAction(StepModeEnum stepMode, bool isGroupCommand = false);
8370

8471
StepModeEnum getStepMode();
85-
86-
AppEvent CreateNewEvent(AppEvent::AppEventTypes type);
72+
void changeStepMode();
8773

8874
static LightSwitchMgr & GetInstance() { return sSwitch; }
8975

90-
/**
91-
* @brief Event handler when a button is pressed
92-
* Function posts an event for button processing
93-
*
94-
* @param button BUTTON0 or BUTTON1
95-
* @param btnAction button action - SL_SIMPLE_BUTTON_PRESSED,
96-
* SL_SIMPLE_BUTTON_RELEASED
97-
*/
98-
static void ButtonEventHandler(uint8_t button, uint8_t btnAction);
99-
100-
static void AppEventHandler(AppEvent * aEvent);
101-
10276
private:
10377
static LightSwitchMgr sSwitch;
10478

105-
Timer * mLongPressTimer = nullptr;
106-
bool mFunctionButtonPressed = false; // True when button0 is pressed, used to trigger factory reset
107-
bool mActionButtonPressed = false; // True when button1 is pressed, used to initiate toggle or level-up/down
108-
bool mActionButtonSuppressed = false; // True when both button0 and button1 are pressed, used to switch step direction
109-
bool mResetWarning = false;
110-
11179
// Default Step direction for Level control
11280
StepModeEnum stepDirection = StepModeEnum::kUp;
113-
114-
static void OnLongPressTimeout(Timer & timer);
11581
LightSwitchMgr() = default;
11682

117-
/**
118-
* @brief This function will be called when PB0 is
119-
* long-pressed to trigger the factory-reset
120-
*/
121-
void HandleLongPress();
122-
12383
static void GenericSwitchWorkerFunction(intptr_t context);
12484

12585
chip::EndpointId mLightSwitchEndpoint = chip::kInvalidEndpointId;
12686
chip::EndpointId mGenericSwitchEndpoint = chip::kInvalidEndpointId;
12787

128-
/**
129-
* @brief Button event processing function
130-
* Function triggers a switch action sent to the CHIP task
131-
*
132-
* @param aEvent button event being processed
133-
*/
134-
static void SwitchActionEventHandler(AppEvent * aEvent);
13588
};

0 commit comments

Comments
 (0)