-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathAppTask.cpp
327 lines (293 loc) · 10.1 KB
/
AppTask.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
/*
*
* Copyright (c) 2020 Project CHIP Authors
* Copyright (c) 2019 Google LLC.
* All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**********************************************************
* Includes
*********************************************************/
#include "AppTask.h"
#include "AppConfig.h"
#include "AppEvent.h"
#include "BindingHandler.h"
#include "LEDWidget.h"
#include "LightSwitchMgr.h"
#ifdef DISPLAY_ENABLED
#include "lcd.h"
#ifdef QR_CODE_ENABLED
#include "qrcodegen.h"
#endif // QR_CODE_ENABLED
#endif // DISPLAY_ENABLED
#include <app/server/OnboardingCodesUtil.h>
#include <app/server/Server.h>
#include <app/util/attribute-storage.h>
#include <assert.h>
#include <lib/support/CodeUtils.h>
#include <platform/CHIPDeviceLayer.h>
#include <platform/silabs/platformAbstraction/SilabsPlatform.h>
#include <setup_payload/QRCodeSetupPayloadGenerator.h>
#include <setup_payload/SetupPayload.h>
/**********************************************************
* Defines and Constants
*********************************************************/
#define SYSTEM_STATE_LED &sl_led_led0
namespace {
constexpr chip::EndpointId kLightSwitchEndpoint = 1;
constexpr chip::EndpointId kGenericSwitchEndpoint = 2;
} // namespace
using namespace chip;
using namespace chip::app;
using namespace ::chip::DeviceLayer;
using namespace ::chip::DeviceLayer::Silabs;
using namespace chip::TLV;
using namespace ::chip::DeviceLayer;
/**********************************************************
* AppTask Definitions
*********************************************************/
AppTask AppTask::sAppTask;
bool sFunctionButtonPressed = false; // True when button0 is pressed, used to trigger factory reset
bool sActionButtonPressed = false; // True when button1 is pressed, used to initiate toggle or level-up/down
bool sActionButtonSuppressed = false; // True when both button0 and button1 are pressed, used to switch step direction
bool sIsButtonEventTriggered = false; // True when button0 press event is posted to BaseApplication
CHIP_ERROR AppTask::Init()
{
CHIP_ERROR err = CHIP_NO_ERROR;
chip::DeviceLayer::Silabs::GetPlatform().SetButtonsCb(AppTask::ButtonEventHandler);
#ifdef DISPLAY_ENABLED
GetLCD().Init((uint8_t *) "Light Switch");
#endif
err = BaseApplication::Init();
if (err != CHIP_NO_ERROR)
{
SILABS_LOG("BaseApplication::Init() failed");
appError(err);
}
err = LightSwitchMgr::GetInstance().Init(kLightSwitchEndpoint, kGenericSwitchEndpoint);
if (err != CHIP_NO_ERROR)
{
SILABS_LOG("LightSwitchMgr Init failed!");
appError(err);
}
longPressTimer = new Timer(LONG_PRESS_TIMEOUT, OnLongPressTimeout, this);
return err;
}
void AppTask::Timer::Start()
{
// Starts or restarts the function timer
if (osTimerStart(mHandler, pdMS_TO_TICKS(LONG_PRESS_TIMEOUT)) != osOK)
{
SILABS_LOG("Timer start() failed");
appError(CHIP_ERROR_INTERNAL);
}
mIsActive = true;
}
void AppTask::Timer::Timeout()
{
mIsActive = false;
if (mCallback)
{
mCallback(*this);
}
}
void AppTask::HandleLongPress()
{
AppEvent event;
event.Handler = AppTask::AppEventHandler;
if (sActionButtonPressed)
{
sActionButtonSuppressed = true;
// Long press button up : Trigger Level Control Action
event.Type = AppEvent::kEventType_TriggerLevelControlAction;
AppTask::GetAppTask().PostEvent(&event);
}
}
void AppTask::OnLongPressTimeout(AppTask::Timer & timer)
{
AppTask * app = static_cast<AppTask *>(timer.mContext);
if (app)
{
app->HandleLongPress();
}
}
AppTask::Timer::Timer(uint32_t timeoutInMs, Callback callback, void * context) : mCallback(callback), mContext(context)
{
mHandler = osTimerNew(TimerCallback, // timer callback handler
osTimerOnce, // no timer reload (one-shot timer)
this, // pass the app task obj context
NULL // No osTimerAttr_t to provide.
);
if (mHandler == NULL)
{
SILABS_LOG("Timer create failed");
appError(CHIP_ERROR_INTERNAL);
}
}
AppTask::Timer::~Timer()
{
if (mHandler)
{
osTimerDelete(mHandler);
mHandler = nullptr;
}
}
void AppTask::Timer::Stop()
{
mIsActive = false;
if (osTimerStop(mHandler) == osError)
{
SILABS_LOG("Timer stop() failed");
appError(CHIP_ERROR_INTERNAL);
}
}
void AppTask::Timer::TimerCallback(void * timerCbArg)
{
Timer * timer = reinterpret_cast<Timer *>(timerCbArg);
if (timer)
{
timer->Timeout();
}
}
CHIP_ERROR AppTask::StartAppTask()
{
return BaseApplication::StartAppTask(AppTaskMain);
}
void AppTask::AppTaskMain(void * pvParameter)
{
AppEvent event;
osMessageQueueId_t sAppEventQueue = *(static_cast<osMessageQueueId_t *>(pvParameter));
CHIP_ERROR err = sAppTask.Init();
if (err != CHIP_NO_ERROR)
{
SILABS_LOG("AppTask.Init() failed");
appError(err);
}
#if !(defined(CHIP_CONFIG_ENABLE_ICD_SERVER) && CHIP_CONFIG_ENABLE_ICD_SERVER)
sAppTask.StartStatusLEDTimer();
#endif
SILABS_LOG("App Task started");
while (true)
{
osStatus_t eventReceived = osMessageQueueGet(sAppEventQueue, &event, NULL, osWaitForever);
while (eventReceived == osOK)
{
sAppTask.DispatchEvent(&event);
eventReceived = osMessageQueueGet(sAppEventQueue, &event, NULL, 0);
}
}
}
AppEvent AppTask::CreateNewEvent(AppEvent::AppEventTypes type)
{
AppEvent aEvent;
aEvent.Type = type;
aEvent.Handler = AppTask::AppEventHandler;
return aEvent;
}
void AppTask::ButtonEventHandler(uint8_t button, uint8_t btnAction)
{
AppEvent event = {};
if (btnAction == to_underlying(SilabsPlatform::ButtonAction::ButtonPressed))
{
event = AppTask::GetAppTask().CreateNewEvent(button ? AppEvent::kEventType_ActionButtonPressed
: AppEvent::kEventType_FunctionButtonPressed);
}
else
{
event = AppTask::GetAppTask().CreateNewEvent(button ? AppEvent::kEventType_ActionButtonReleased
: AppEvent::kEventType_FunctionButtonReleased);
}
AppTask::GetAppTask().PostEvent(&event);
}
void AppTask::AppEventHandler(AppEvent * aEvent)
{
switch (aEvent->Type)
{
case AppEvent::kEventType_FunctionButtonPressed:
sFunctionButtonPressed = true;
if (sActionButtonPressed)
{
sActionButtonSuppressed = true;
LightSwitchMgr::GetInstance().changeStepMode();
ChipLogProgress(AppServer, "Step direction changed. Current Step Direction : %s",
((LightSwitchMgr::GetInstance().getStepMode() == StepModeEnum::kUp) ? "kUp" : "kDown"));
}
else
{
sIsButtonEventTriggered = true;
// Post button press event to BaseApplication
AppEvent button_event = {};
button_event.Type = AppEvent::kEventType_Button;
button_event.ButtonEvent.Action = static_cast<uint8_t>(SilabsPlatform::ButtonAction::ButtonPressed);
button_event.Handler = BaseApplication::ButtonHandler;
AppTask::GetAppTask().PostEvent(&button_event);
}
break;
case AppEvent::kEventType_FunctionButtonReleased: {
sFunctionButtonPressed = false;
if (sIsButtonEventTriggered)
{
sIsButtonEventTriggered = false;
// Post button release event to BaseApplication
AppEvent button_event = {};
button_event.Type = AppEvent::kEventType_Button;
button_event.ButtonEvent.Action = static_cast<uint8_t>(SilabsPlatform::ButtonAction::ButtonReleased);
button_event.Handler = BaseApplication::ButtonHandler;
AppTask::GetAppTask().PostEvent(&button_event);
}
break;
}
case AppEvent::kEventType_ActionButtonPressed:
sActionButtonPressed = true;
aEvent->Handler = LightSwitchMgr::SwitchActionEventHandler;
AppTask::GetAppTask().PostEvent(aEvent);
if (sFunctionButtonPressed)
{
sActionButtonSuppressed = true;
LightSwitchMgr::GetInstance().changeStepMode();
ChipLogProgress(AppServer, "Step direction changed. Current Step Direction : %s",
((LightSwitchMgr::GetInstance().getStepMode() == StepModeEnum::kUp) ? "kUp" : "kDown"));
}
else if (sAppTask.longPressTimer)
{
sAppTask.longPressTimer->Start();
}
break;
case AppEvent::kEventType_ActionButtonReleased:
sActionButtonPressed = false;
if (sAppTask.longPressTimer)
{
sAppTask.longPressTimer->Stop();
}
if (sActionButtonSuppressed)
{
sActionButtonSuppressed = false;
}
else
{
aEvent->Type = AppEvent::kEventType_TriggerToggle;
aEvent->Handler = LightSwitchMgr::SwitchActionEventHandler;
AppTask::GetAppTask().PostEvent(aEvent);
}
aEvent->Type = AppEvent::kEventType_ActionButtonReleased;
aEvent->Handler = LightSwitchMgr::SwitchActionEventHandler;
AppTask::GetAppTask().PostEvent(aEvent);
break;
case AppEvent::kEventType_TriggerLevelControlAction:
aEvent->Handler = LightSwitchMgr::SwitchActionEventHandler;
AppTask::GetAppTask().PostEvent(aEvent);
default:
break;
}
}