From 388c27cc112470f7cdeb38413c453a68dbb86c35 Mon Sep 17 00:00:00 2001 From: Alex Tsitsiura Date: Wed, 21 Sep 2022 20:06:41 +0300 Subject: [PATCH] [Telink] TC-OO-2.4 test fix (#22756) * [Telink] TC-OO-2.4 test fix * [Telink] Restyled --- .../telink/include/LightingManager.h | 12 +++++---- examples/lighting-app/telink/src/AppTask.cpp | 27 +++++++++++-------- .../telink/src/LightingManager.cpp | 19 ++++++++----- .../lighting-app/telink/src/ZclCallbacks.cpp | 24 +++++++++++++---- 4 files changed, 55 insertions(+), 27 deletions(-) diff --git a/examples/lighting-app/telink/include/LightingManager.h b/examples/lighting-app/telink/include/LightingManager.h index fdcd37cb4c70d7..2af544ada8625b 100644 --- a/examples/lighting-app/telink/include/LightingManager.h +++ b/examples/lighting-app/telink/include/LightingManager.h @@ -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"); @@ -45,17 +45,20 @@ 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; @@ -63,7 +66,6 @@ class LightingManager LightingCallback_fn mActionInitiated_CB; LightingCallback_fn mActionCompleted_CB; - void Set(bool aOn); void SetLevel(uint8_t aLevel); void UpdateLight(); diff --git a/examples/lighting-app/telink/src/AppTask.cpp b/examples/lighting-app/telink/src/AppTask.cpp index f882c72a749bca..5c537b26a0d39e 100644 --- a/examples/lighting-app/telink/src/AppTask.cpp +++ b/examples/lighting-app/telink/src/AppTask.cpp @@ -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"); @@ -32,6 +32,7 @@ #include #include +#include #include #include #include @@ -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)); @@ -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; @@ -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 @@ -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) { diff --git a/examples/lighting-app/telink/src/LightingManager.cpp b/examples/lighting-app/telink/src/LightingManager.cpp index d0e729f66a3dd5..c89bbf6bf25aa2 100644 --- a/examples/lighting-app/telink/src/LightingManager.cpp +++ b/examples/lighting-app/telink/src/LightingManager.cpp @@ -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"); @@ -20,6 +20,8 @@ #include "AppConfig.h" +#include + #include #include #include @@ -28,13 +30,16 @@ 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; @@ -123,7 +128,9 @@ 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); + constexpr uint32_t kPwmWidthUs = 20000u; + const uint8_t maxEffectiveLevel = mMaxLevel - mMinLevel; + const uint8_t effectiveLevel = mState == kState_On ? chip::min(mLevel - mMinLevel, maxEffectiveLevel) : 0; + + pwm_pin_set_usec(mPwmDevice, mPwmChannel, kPwmWidthUs, kPwmWidthUs * effectiveLevel / maxEffectiveLevel, 0); } diff --git a/examples/lighting-app/telink/src/ZclCallbacks.cpp b/examples/lighting-app/telink/src/ZclCallbacks.cpp index 2c315f8e897b17..83a5d7a0a67ba9 100644 --- a/examples/lighting-app/telink/src/ZclCallbacks.cpp +++ b/examples/lighting-app/telink/src/ZclCallbacks.cpp @@ -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"); @@ -19,6 +19,7 @@ #include "AppTask.h" #include "LightingManager.h" +#include #include #include #include @@ -26,29 +27,31 @@ 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"); } } } @@ -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(); }