Skip to content

Commit 3712e87

Browse files
adigieArekBalysNordic
authored andcommitted
samples: matter: light_bulb: Initialize PWM device after Server is started
Matter Server has to be started before reading attributes from persistent storage. PWM device initization depends on values of some attributes. Signed-off-by: Adrian Gielniewski <[email protected]>
1 parent c66f9d6 commit 3712e87

File tree

4 files changed

+27
-17
lines changed

4 files changed

+27
-17
lines changed

samples/matter/light_bulb/src/app_task.cpp

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,29 @@ void AppTask::UpdateClusterState()
264264
});
265265
}
266266

267+
void AppTask::InitPWMDDevice()
268+
{
269+
#if defined(CONFIG_PWM)
270+
/* Initialize lighting device (PWM) */
271+
uint8_t minLightLevel = kDefaultMinLevel;
272+
Clusters::LevelControl::Attributes::MinLevel::Get(kLightEndpointId, &minLightLevel);
273+
274+
uint8_t maxLightLevel = kDefaultMaxLevel;
275+
Clusters::LevelControl::Attributes::MaxLevel::Get(kLightEndpointId, &maxLightLevel);
276+
277+
Clusters::LevelControl::Attributes::CurrentLevel::TypeInfo::Type currentLevel;
278+
Clusters::LevelControl::Attributes::CurrentLevel::Get(kLightEndpointId, currentLevel);
279+
280+
int ret =
281+
mPWMDevice.Init(&sLightPwmDevice, minLightLevel, maxLightLevel, currentLevel.ValueOr(kDefaultMaxLevel));
282+
if (ret != 0) {
283+
LOG_ERR("Failed to initialize PWD device.");
284+
}
285+
286+
mPWMDevice.SetCallbacks(ActionInitiated, ActionCompleted);
287+
#endif
288+
}
289+
267290
CHIP_ERROR AppTask::Init()
268291
{
269292
/* Initialize Matter stack */
@@ -292,22 +315,6 @@ CHIP_ERROR AppTask::Init()
292315
/* Initialize trigger effect timer */
293316
k_timer_init(&sTriggerEffectTimer, &AppTask::TriggerEffectTimerTimeoutCallback, nullptr);
294317

295-
/* Initialize lighting device (PWM) */
296-
uint8_t minLightLevel = kDefaultMinLevel;
297-
Clusters::LevelControl::Attributes::MinLevel::Get(kLightEndpointId, &minLightLevel);
298-
299-
uint8_t maxLightLevel = kDefaultMaxLevel;
300-
Clusters::LevelControl::Attributes::MaxLevel::Get(kLightEndpointId, &maxLightLevel);
301-
302-
#if defined(CONFIG_PWM)
303-
int ret = mPWMDevice.Init(&sLightPwmDevice, minLightLevel, maxLightLevel, maxLightLevel);
304-
if (ret != 0) {
305-
return chip::System::MapErrorZephyr(ret);
306-
}
307-
308-
mPWMDevice.SetCallbacks(ActionInitiated, ActionCompleted);
309-
#endif
310-
311318
return Nrf::Matter::StartServer();
312319
}
313320

samples/matter/light_bulb/src/app_task.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ class AppTask {
3232
CHIP_ERROR StartApp();
3333

3434
void UpdateClusterState();
35+
void InitPWMDDevice();
3536
Nrf::PWMDevice &GetPWMDevice() { return mPWMDevice; }
3637

3738
static void IdentifyStartHandler(Identify *);

samples/matter/light_bulb/src/zcl_callbacks.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,8 @@ void emberAfOnOffClusterInitCallback(EndpointId endpoint)
8484
if (status == Protocols::InteractionModel::Status::Success) {
8585
/* Set actual state to the cluster state that was last persisted */
8686
#if defined(CONFIG_PWM)
87+
AppTask::Instance().InitPWMDDevice();
88+
8789
AppTask::Instance().GetPWMDevice().InitiateAction(
8890
storedValue ? Nrf::PWMDevice::ON_ACTION : Nrf::PWMDevice::OFF_ACTION,
8991
static_cast<int32_t>(LightingActor::Remote), reinterpret_cast<uint8_t *>(&storedValue));

west.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ manifest:
158158
- name: matter
159159
repo-path: sdk-connectedhomeip
160160
path: modules/lib/matter
161-
revision: pull/512/head
161+
revision: 9cfd6e13a239a61288f34b1219e1b41dc70f8457
162162
west-commands: scripts/west/west-commands.yml
163163
submodules:
164164
- name: nlio

0 commit comments

Comments
 (0)