Skip to content

Commit

Permalink
[Telink] TC-OO-2.4 test fix
Browse files Browse the repository at this point in the history
  • Loading branch information
s07641069 committed Sep 16, 2022
1 parent 9989fc1 commit 7ba87cc
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 26 deletions.
12 changes: 7 additions & 5 deletions examples/lighting-app/telink/include/LightingManager.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
*
* Copyright (c) 2021 Project CHIP Authors
* Copyright (c) 2022 Project CHIP Authors
* All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
Expand Down Expand Up @@ -45,25 +45,27 @@ class LightingManager

using LightingCallback_fn = void (*)(Action_t, int32_t);

CHIP_ERROR Init(const device * pwmDevice, uint32_t pwmChannel);
CHIP_ERROR Init(const device * pwmDevice, uint32_t pwmChannel, uint8_t aMinLevel, uint8_t aMaxLevel, uint8_t aDefaultLevel = 0);
void Set(bool aOn);
bool IsTurnedOn() const { return mState == kState_On; }
uint8_t GetLevel() const { return mLevel; }
uint8_t GetMinLevel() const { return mMinLevel; }
uint8_t GetMaxLevel() const { return mMaxLevel; }
bool InitiateAction(Action_t aAction, int32_t aActor, uint8_t size, uint8_t * value);
void SetCallbacks(LightingCallback_fn aActionInitiated_CB, LightingCallback_fn aActionCompleted_CB);

private:
static constexpr uint8_t kMaxLevel = 254;

friend LightingManager & LightingMgr();
State_t mState;
uint8_t mMinLevel;
uint8_t mMaxLevel;
uint8_t mLevel;
const device * mPwmDevice;
uint32_t mPwmChannel;

LightingCallback_fn mActionInitiated_CB;
LightingCallback_fn mActionCompleted_CB;

void Set(bool aOn);
void SetLevel(uint8_t aLevel);
void UpdateLight();

Expand Down
27 changes: 16 additions & 11 deletions examples/lighting-app/telink/src/AppTask.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
*
* Copyright (c) 2021 Project CHIP Authors
* Copyright (c) 2022 Project CHIP Authors
* All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
Expand Down Expand Up @@ -32,6 +32,7 @@

#include <app-common/zap-generated/attribute-id.h>
#include <app-common/zap-generated/attribute-type.h>
#include <app-common/zap-generated/attributes/Accessors.h>
#include <app-common/zap-generated/cluster-id.h>
#include <app/clusters/identify-server/identify-server.h>
#include <app/util/attribute-storage.h>
Expand Down Expand Up @@ -62,6 +63,8 @@ namespace {
constexpr int kAppEventQueueSize = 10;
constexpr uint8_t kButtonPushEvent = 1;
constexpr uint8_t kButtonReleaseEvent = 0;
constexpr uint8_t kDefaultMinLevel = 0;
constexpr uint8_t kDefaultMaxLevel = 254;

K_MSGQ_DEFINE(sAppEventQueue, sizeof(AppEvent), kAppEventQueueSize, alignof(AppEvent));

Expand Down Expand Up @@ -112,6 +115,8 @@ Identify sIdentify = {

} // namespace

using namespace ::chip;
using namespace ::chip::app;
using namespace ::chip::Credentials;
using namespace ::chip::DeviceLayer;
using namespace ::chip::DeviceLayer::Internal;
Expand All @@ -134,13 +139,18 @@ CHIP_ERROR AppTask::Init()
InitButtons();

// Init lighting manager
ret = LightingMgr().Init(LIGHTING_PWM_DEVICE, LIGHTING_PWM_CHANNEL);
uint8_t minLightLevel = kDefaultMinLevel;
Clusters::LevelControl::Attributes::MinLevel::Get(1, &minLightLevel);

uint8_t maxLightLevel = kDefaultMaxLevel;
Clusters::LevelControl::Attributes::MaxLevel::Get(1, &maxLightLevel);

ret = LightingMgr().Init(LIGHTING_PWM_DEVICE, LIGHTING_PWM_CHANNEL, minLightLevel, maxLightLevel, maxLightLevel);
if (ret != CHIP_NO_ERROR)
{
LOG_ERR("Failed to int lighting manager");
return ret;
}

LightingMgr().SetCallbacks(ActionInitiated, ActionCompleted);

// Init ZCL Data Model and start server
Expand Down Expand Up @@ -422,20 +432,15 @@ void AppTask::DispatchEvent(AppEvent * aEvent)

void AppTask::UpdateClusterState()
{
uint8_t onoff = LightingMgr().IsTurnedOn();

// write the new on/off value
EmberAfStatus status =
emberAfWriteAttribute(1, ZCL_ON_OFF_CLUSTER_ID, ZCL_ON_OFF_ATTRIBUTE_ID, &onoff, ZCL_BOOLEAN_ATTRIBUTE_TYPE);
EmberAfStatus status = Clusters::OnOff::Attributes::OnOff::Set(1, LightingMgr().IsTurnedOn());

if (status != EMBER_ZCL_STATUS_SUCCESS)
{
LOG_ERR("Updating on/off cluster failed: %x", status);
}

uint8_t level = LightingMgr().GetLevel();

status =
emberAfWriteAttribute(1, ZCL_LEVEL_CONTROL_CLUSTER_ID, ZCL_CURRENT_LEVEL_ATTRIBUTE_ID, &level, ZCL_INT8U_ATTRIBUTE_TYPE);
status = Clusters::LevelControl::Attributes::CurrentLevel::Set(1, LightingMgr().GetLevel());

if (status != EMBER_ZCL_STATUS_SUCCESS)
{
Expand Down
16 changes: 11 additions & 5 deletions examples/lighting-app/telink/src/LightingManager.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
*
* Copyright (c) 2021 Project CHIP Authors
* Copyright (c) 2022 Project CHIP Authors
* All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
Expand All @@ -20,6 +20,8 @@

#include "AppConfig.h"

#include <lib/support/CodeUtils.h>

#include <drivers/pwm.h>
#include <logging/log.h>
#include <zephyr.h>
Expand All @@ -28,13 +30,15 @@ LOG_MODULE_DECLARE(app);

LightingManager LightingManager::sLight;

CHIP_ERROR LightingManager::Init(const device * pwmDevice, uint32_t pwmChannel)
CHIP_ERROR LightingManager::Init(const device * pwmDevice, uint32_t pwmChannel, uint8_t aMinLevel, uint8_t aMaxLevel, uint8_t aDefaultLevel)
{
// We use a gpioPin instead of a LEDWidget here because we want to use PWM
// and other features instead of just on/off.

mState = kState_On;
mLevel = kMaxLevel;
mMinLevel = aMinLevel;
mMaxLevel = aMaxLevel;
mLevel = aDefaultLevel;
mPwmDevice = pwmDevice;
mPwmChannel = pwmChannel;

Expand Down Expand Up @@ -124,6 +128,8 @@ void LightingManager::Set(bool aOn)
void LightingManager::UpdateLight()
{
constexpr uint32_t kPwmWidthUs = 20000u;
const uint8_t level = mState == kState_On ? mLevel : 0;
pwm_pin_set_usec(mPwmDevice, mPwmChannel, kPwmWidthUs, kPwmWidthUs * level / kMaxLevel, 0);
const uint8_t maxEffectiveLevel = mMaxLevel - mMinLevel;
const uint8_t effectiveLevel = mState == kState_On ? chip::min<uint8_t>(mLevel - mMinLevel, maxEffectiveLevel) : 0;

pwm_pin_set_usec(mPwmDevice, mPwmChannel, kPwmWidthUs, kPwmWidthUs * effectiveLevel / maxEffectiveLevel, 0);
}
24 changes: 19 additions & 5 deletions examples/lighting-app/telink/src/ZclCallbacks.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
*
* Copyright (c) 2021 Project CHIP Authors
* Copyright (c) 2022 Project CHIP Authors
* All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
Expand All @@ -19,36 +19,39 @@
#include "AppTask.h"
#include "LightingManager.h"

#include <app-common/zap-generated/attributes/Accessors.h>
#include <app-common/zap-generated/ids/Attributes.h>
#include <app-common/zap-generated/ids/Clusters.h>
#include <app/ConcreteAttributePath.h>
#include <lib/support/logging/CHIPLogging.h>

using namespace chip;
using namespace chip::app::Clusters;
using namespace chip::app::Clusters::OnOff;

void MatterPostAttributeChangeCallback(const chip::app::ConcreteAttributePath & attributePath, uint8_t type, uint16_t size,
uint8_t * value)
{
ClusterId clusterId = attributePath.mClusterId;
AttributeId attributeId = attributePath.mAttributeId;
ChipLogProgress(Zcl, "Cluster callback: " ChipLogFormatMEI, ChipLogValueMEI(clusterId));

if (clusterId == OnOff::Id && attributeId == OnOff::Attributes::OnOff::Id)
{
ChipLogProgress(Zcl, "Cluster OnOff: attribute OnOff set to %u", *value);
LightingMgr().InitiateAction(*value ? LightingManager::ON_ACTION : LightingManager::OFF_ACTION,
AppEvent::kEventType_Lighting, size, value);
}
else if (clusterId == LevelControl::Id && attributeId == LevelControl::Attributes::CurrentLevel::Id)
{
ChipLogProgress(Zcl, "Value: %u, length %u", *value, size);
if (size == 1)
ChipLogProgress(Zcl, "Cluster LevelControl: attribute CurrentLevel set to %u", *value);

if (LightingMgr().IsTurnedOn())
{
LightingMgr().InitiateAction(LightingManager::LEVEL_ACTION, AppEvent::kEventType_Lighting, size, value);
}
else
{
ChipLogError(Zcl, "wrong length for level: %d", size);
ChipLogDetail(Zcl, "LED is off. Try to use move-to-level-with-on-off instead of move-to-level");
}
}
}
Expand All @@ -64,5 +67,16 @@ void MatterPostAttributeChangeCallback(const chip::app::ConcreteAttributePath &
*/
void emberAfOnOffClusterInitCallback(EndpointId endpoint)
{
EmberAfStatus status;
bool storedValue;

// Read storedValue on/off value
status = Attributes::OnOff::Get(1, &storedValue);
if (status == EMBER_ZCL_STATUS_SUCCESS)
{
// Set actual state to stored before reboot
LightingMgr().Set(storedValue);
}

GetAppTask().UpdateClusterState();
}

0 comments on commit 7ba87cc

Please sign in to comment.