diff --git a/examples/air-quality-sensor-app/telink/CMakeLists.txt b/examples/air-quality-sensor-app/telink/CMakeLists.txt index a16d4299d35484..e78760f34e681a 100644 --- a/examples/air-quality-sensor-app/telink/CMakeLists.txt +++ b/examples/air-quality-sensor-app/telink/CMakeLists.txt @@ -100,6 +100,7 @@ target_sources(app PRIVATE ${TELINK_COMMON}/util/src/ButtonManager.cpp ${TELINK_COMMON}/util/src/ThreadUtil.cpp ${TELINK_COMMON}/util/src/PWMDevice.cpp + ${TELINK_COMMON}/util/src/WS2812Device.cpp ${CHIP_ROOT}/examples/air-quality-sensor-app/air-quality-sensor-common/src/air-quality-sensor-manager.cpp) chip_configure_data_model(app diff --git a/examples/lighting-app/telink/CMakeLists.txt b/examples/lighting-app/telink/CMakeLists.txt index e4dd1a4adc4094..061721a71dbe14 100644 --- a/examples/lighting-app/telink/CMakeLists.txt +++ b/examples/lighting-app/telink/CMakeLists.txt @@ -21,6 +21,21 @@ get_filename_component(GEN_DIR ${CHIP_ROOT}/zzz_generated/ REALPATH) set(ignoreMe "${TLNK_USB_DONGLE}") +if(${TLNK_MARS_BOARD} MATCHES y) + if(EXISTS "${CHIP_ROOT}/src/platform/telink/${BOARD}_mars.overlay") + set(MARS_DTC_OVERLAY_FILE "${CHIP_ROOT}/src/platform/telink/${BOARD}_mars.overlay") + else() + unset(MARS_DTC_OVERLAY_FILE) + endif() + if(EXISTS "${CHIP_ROOT}/src/platform/telink/${BOARD}_mars.conf") + set(MARS_CONF_OVERLAY_FILE "${CHIP_ROOT}/src/platform/telink/${BOARD}_mars.conf") + else() + unset(MARS_CONF_OVERLAY_FILE) + endif() +else() + unset(MARS_CONF_OVERLAY_FILE) +endif() + if(${TLNK_USB_DONGLE} MATCHES y) if(EXISTS "${CHIP_ROOT}/src/platform/telink/${BOARD}_usb.overlay") set(USB_DTC_OVERLAY_FILE "${CHIP_ROOT}/src/platform/telink/${BOARD}_usb.overlay") @@ -58,14 +73,14 @@ endif() if(DTC_OVERLAY_FILE) set(DTC_OVERLAY_FILE - "${DTC_OVERLAY_FILE} ${GLOBAL_DTC_OVERLAY_FILE} ${USB_DTC_OVERLAY_FILE} ${FLASH_DTC_OVERLAY_FILE} ${LOCAL_DTC_OVERLAY_FILE}" + "${DTC_OVERLAY_FILE} ${GLOBAL_DTC_OVERLAY_FILE} ${USB_DTC_OVERLAY_FILE} ${MARS_DTC_OVERLAY_FILE} ${FLASH_DTC_OVERLAY_FILE} ${LOCAL_DTC_OVERLAY_FILE}" CACHE STRING "" FORCE ) else() - set(DTC_OVERLAY_FILE ${GLOBAL_DTC_OVERLAY_FILE} ${USB_DTC_OVERLAY_FILE} ${FLASH_DTC_OVERLAY_FILE} ${LOCAL_DTC_OVERLAY_FILE}) + set(DTC_OVERLAY_FILE ${GLOBAL_DTC_OVERLAY_FILE} ${USB_DTC_OVERLAY_FILE} ${MARS_DTC_OVERLAY_FILE} ${FLASH_DTC_OVERLAY_FILE} ${LOCAL_DTC_OVERLAY_FILE}) endif() -set(CONF_FILE ${USB_CONF_OVERLAY_FILE} prj.conf) +set(CONF_FILE ${USB_CONF_OVERLAY_FILE} prj.conf ${MARS_CONF_OVERLAY_FILE}) # Load NCS/Zephyr build system list(APPEND ZEPHYR_EXTRA_MODULES ${CHIP_ROOT}/config/telink/chip-module) @@ -99,6 +114,7 @@ target_sources(app PRIVATE ${TELINK_COMMON}/util/src/ButtonManager.cpp ${TELINK_COMMON}/util/src/ThreadUtil.cpp ${TELINK_COMMON}/util/src/PWMDevice.cpp + ${TELINK_COMMON}/util/src/WS2812Device.cpp ${TELINK_COMMON}/util/src/ColorFormat.cpp) chip_configure_data_model(app diff --git a/examples/lighting-app/telink/include/AppTask.h b/examples/lighting-app/telink/include/AppTask.h index a9d9ab3712ef94..764cb209505a82 100644 --- a/examples/lighting-app/telink/include/AppTask.h +++ b/examples/lighting-app/telink/include/AppTask.h @@ -28,7 +28,12 @@ class AppTask : public AppTaskCommon #endif /* CONFIG_CHIP_ENABLE_POWER_ON_FACTORY_RESET */ void SetInitiateAction(PWMDevice::Action_t aAction, int32_t aActor, uint8_t * value); void UpdateClusterState(void); - PWMDevice & GetPWMDevice(void) { return mPwmRgbBlueLed; } + +#ifdef CONFIG_WS2812_STRIP + WS2812Device & GetLightingDevice(void) { return mWS2812Device; } +#else + PWMDevice & GetLightingDevice(void) { return mPwmRgbBlueLed; } +#endif /* CONFIG_WS2812_STRIP */ private: friend AppTask & GetAppTask(void); @@ -47,11 +52,16 @@ class AppTask : public AppTaskCommon static unsigned int sPowerOnFactoryResetTimerCnt; static k_timer sPowerOnFactoryResetTimer; #endif /* CONFIG_CHIP_ENABLE_POWER_ON_FACTORY_RESET */ + +#ifdef CONFIG_WS2812_STRIP + WS2812Device mWS2812Device; +#else PWMDevice mPwmRgbBlueLed; #if USE_RGB_PWM PWMDevice mPwmRgbGreenLed; PWMDevice mPwmRgbRedLed; #endif +#endif /* CONFIG_WS2812_STRIP */ static AppTask sAppTask; }; diff --git a/examples/lighting-app/telink/src/AppTask.cpp b/examples/lighting-app/telink/src/AppTask.cpp index 94f0f9dd571ed1..f9c7f989628554 100644 --- a/examples/lighting-app/telink/src/AppTask.cpp +++ b/examples/lighting-app/telink/src/AppTask.cpp @@ -27,17 +27,23 @@ LOG_MODULE_DECLARE(app, CONFIG_CHIP_APP_LOG_LEVEL); namespace { +#ifdef CONFIG_WS2812_STRIP +const struct device *const ws2812_dev = DEVICE_DT_GET(DT_ALIAS(led_strip)); +#else const struct pwm_dt_spec sPwmRgbSpecBlueLed = PWM_DT_SPEC_GET(DT_ALIAS(pwm_led0)); #if USE_RGB_PWM const struct pwm_dt_spec sPwmRgbSpecGreenLed = PWM_DT_SPEC_GET(DT_ALIAS(pwm_led1)); const struct pwm_dt_spec sPwmRgbSpecRedLed = PWM_DT_SPEC_GET(DT_ALIAS(pwm_led2)); +#endif +#endif // CONFIG_WS2812_STRIP +#if defined(CONFIG_WS2812_STRIP) || USE_RGB_PWM uint8_t sBrightness; PWMDevice::Action_t sColorAction = PWMDevice::INVALID_ACTION; XyColor_t sXY; HsvColor_t sHSV; CtColor_t sCT; -#endif +#endif // CONFIG_WS2812_STRIP || USE_RGB_PWM } // namespace AppTask AppTask::sAppTask; @@ -55,6 +61,8 @@ void AppTask::PowerOnFactoryReset(void) CHIP_ERROR AppTask::Init(void) { + CHIP_ERROR err; + // Init lighting manager uint8_t minLightLevel = kDefaultMinLevel; Clusters::LevelControl::Attributes::MinLevel::Get(kExampleEndpointId, &minLightLevel); @@ -62,7 +70,15 @@ CHIP_ERROR AppTask::Init(void) uint8_t maxLightLevel = kDefaultMaxLevel; Clusters::LevelControl::Attributes::MaxLevel::Get(kExampleEndpointId, &maxLightLevel); - CHIP_ERROR err = sAppTask.mPwmRgbBlueLed.Init(&sPwmRgbSpecBlueLed, minLightLevel, maxLightLevel, maxLightLevel); +#ifdef CONFIG_WS2812_STRIP + err = sAppTask.mWS2812Device.Init(ws2812_dev, STRIP_NUM_PIXELS(led_strip)); + if (err != CHIP_NO_ERROR) + { + LOG_ERR("WS2812 Device Init fail"); + return err; + } +#else + err = sAppTask.mPwmRgbBlueLed.Init(&sPwmRgbSpecBlueLed, minLightLevel, maxLightLevel, maxLightLevel); if (err != CHIP_NO_ERROR) { LOG_ERR("Blue RGB PWM Device Init fail"); @@ -84,6 +100,7 @@ CHIP_ERROR AppTask::Init(void) } #endif sAppTask.mPwmRgbBlueLed.SetCallbacks(ActionInitiated, ActionCompleted, nullptr); +#endif // CONFIG_WS2812_STRIP #if APP_USE_EXAMPLE_START_BUTTON SetExampleButtonCallbacks(LightingActionEventHandler); @@ -95,6 +112,21 @@ CHIP_ERROR AppTask::Init(void) void AppTask::LightingActionEventHandler(AppEvent * aEvent) { +#ifdef CONFIG_WS2812_STRIP + if (aEvent->Type == AppEvent::kEventType_Button) + { + if (sAppTask.mWS2812Device.IsTurnedOn()) + { + sAppTask.mWS2812Device.Set(SET_RGB_TURN_OFF); + } + else + { + sAppTask.mWS2812Device.Set(SET_RGB_TURN_ON); + } + + sAppTask.UpdateClusterState(); + } +#else PWMDevice::Action_t action = PWMDevice::INVALID_ACTION; int32_t actor = 0; @@ -120,8 +152,7 @@ void AppTask::LightingActionEventHandler(AppEvent * aEvent) actor = AppEvent::kEventType_Button; } - if (action != PWMDevice::INVALID_ACTION && - ( + if (action != PWMDevice::INVALID_ACTION && ( #if USE_RGB_PWM !sAppTask.mPwmRgbRedLed.InitiateAction(action, actor, NULL) || !sAppTask.mPwmRgbGreenLed.InitiateAction(action, actor, NULL) || @@ -130,6 +161,7 @@ void AppTask::LightingActionEventHandler(AppEvent * aEvent) { LOG_INF("Action is in progress or active"); } +#endif // CONFIG_WS2812_STRIP } void AppTask::ActionInitiated(PWMDevice::Action_t aAction, int32_t aActor) @@ -171,22 +203,19 @@ void AppTask::ActionCompleted(PWMDevice::Action_t aAction, int32_t aActor) void AppTask::UpdateClusterState(void) { -#if USE_RGB_PWM - bool isTurnedOn = - sAppTask.mPwmRgbRedLed.IsTurnedOn() || sAppTask.mPwmRgbGreenLed.IsTurnedOn() || sAppTask.mPwmRgbBlueLed.IsTurnedOn(); -#else - bool isTurnedOn = sAppTask.mPwmRgbBlueLed.IsTurnedOn(); -#endif - // write the new on/off value - EmberAfStatus status = Clusters::OnOff::Attributes::OnOff::Set(kExampleEndpointId, isTurnedOn); + EmberAfStatus status; + bool isTurnedOn; + uint8_t setLevel; - if (status != EMBER_ZCL_STATUS_SUCCESS) - { - LOG_ERR("Update OnOff fail: %x", status); - } +#if defined(CONFIG_WS2812_STRIP) || USE_RGB_PWM +#ifdef CONFIG_WS2812_STRIP + isTurnedOn = sAppTask.mWS2812Device.IsTurnedOn(); +#else + isTurnedOn = sAppTask.mPwmRgbRedLed.IsTurnedOn() + || sAppTask.mPwmRgbGreenLed.IsTurnedOn() + || sAppTask.mPwmRgbBlueLed.IsTurnedOn(); +#endif // CONFIG_WS2812_STRIP -#if USE_RGB_PWM - uint8_t setLevel; if (sColorAction == PWMDevice::COLOR_ACTION_XY || sColorAction == PWMDevice::COLOR_ACTION_HSV || sColorAction == PWMDevice::COLOR_ACTION_CT) { @@ -194,11 +223,28 @@ void AppTask::UpdateClusterState(void) } else { +#ifdef CONFIG_WS2812_STRIP + setLevel = sAppTask.mWS2812Device.GetBlueLevel(); + if (setLevel > kDefaultMaxLevel) + { + setLevel = kDefaultMaxLevel; + } +#else setLevel = sAppTask.mPwmRgbBlueLed.GetLevel(); +#endif // CONFIG_WS2812_STRIP } #else - uint8_t setLevel = sAppTask.mPwmRgbBlueLed.GetLevel(); -#endif + isTurnedOn = sAppTask.mPwmRgbBlueLed.IsTurnedOn(); + setLevel = sAppTask.mPwmRgbBlueLed.GetLevel(); +#endif // CONFIG_WS2812_STRIP || USE_RGB_PWM + + // write the new on/off value + status = Clusters::OnOff::Attributes::OnOff::Set(kExampleEndpointId, isTurnedOn); + if (status != EMBER_ZCL_STATUS_SUCCESS) + { + LOG_ERR("Update OnOff fail: %x", status); + } + status = Clusters::LevelControl::Attributes::CurrentLevel::Set(kExampleEndpointId, setLevel); if (status != EMBER_ZCL_STATUS_SUCCESS) { @@ -208,22 +254,33 @@ void AppTask::UpdateClusterState(void) void AppTask::SetInitiateAction(PWMDevice::Action_t aAction, int32_t aActor, uint8_t * value) { -#if USE_RGB_PWM +#if defined(CONFIG_WS2812_STRIP) || USE_RGB_PWM bool setRgbAction = false; RgbColor_t rgb; -#endif +#endif // CONFIG_WS2812_STRIP || USE_RGB_PWM if (aAction == PWMDevice::ON_ACTION || aAction == PWMDevice::OFF_ACTION) { +#ifdef CONFIG_WS2812_STRIP + if (aAction == PWMDevice::ON_ACTION) + { + sAppTask.mWS2812Device.Set(SET_RGB_TURN_ON); + } + else if (aAction == PWMDevice::OFF_ACTION) + { + sAppTask.mWS2812Device.Set(SET_RGB_TURN_OFF); + } +#else sAppTask.mPwmRgbBlueLed.InitiateAction(aAction, aActor, value); #if USE_RGB_PWM sAppTask.mPwmRgbRedLed.InitiateAction(aAction, aActor, value); sAppTask.mPwmRgbGreenLed.InitiateAction(aAction, aActor, value); #endif +#endif // CONFIG_WS2812_STRIP } else if (aAction == PWMDevice::LEVEL_ACTION) { -#if USE_RGB_PWM +#if defined(CONFIG_WS2812_STRIP) || USE_RGB_PWM // Save a new brightness for ColorControl sBrightness = *value; @@ -238,19 +295,16 @@ void AppTask::SetInitiateAction(PWMDevice::Action_t aAction, int32_t aActor, uin } else { - rgb.r = sBrightness; - rgb.g = sBrightness; - rgb.b = sBrightness; + memset(&rgb, sBrightness, sizeof(RgbColor_t)); } ChipLogProgress(Zcl, "New brightness: %u | R: %u, G: %u, B: %u", sBrightness, rgb.r, rgb.g, rgb.b); setRgbAction = true; #else sAppTask.mPwmRgbBlueLed.InitiateAction(aAction, aActor, value); -#endif +#endif // CONFIG_WS2812_STRIP || USE_RGB_PWM } - -#if USE_RGB_PWM +#if defined(CONFIG_WS2812_STRIP) || USE_RGB_PWM else if (aAction == PWMDevice::COLOR_ACTION_XY) { sXY = *reinterpret_cast(value); @@ -283,11 +337,15 @@ void AppTask::SetInitiateAction(PWMDevice::Action_t aAction, int32_t aActor, uin if (setRgbAction) { +#ifdef CONFIG_WS2812_STRIP + sAppTask.mWS2812Device.SetLevel(&rgb); +#else sAppTask.mPwmRgbRedLed.InitiateAction(aAction, aActor, &rgb.r); sAppTask.mPwmRgbGreenLed.InitiateAction(aAction, aActor, &rgb.g); sAppTask.mPwmRgbBlueLed.InitiateAction(aAction, aActor, &rgb.b); +#endif // CONFIG_WS2812_STRIP } -#endif +#endif // CONFIG_WS2812_STRIP || USE_RGB_PWM } #ifdef CONFIG_CHIP_ENABLE_POWER_ON_FACTORY_RESET @@ -301,11 +359,15 @@ void AppTask::PowerOnFactoryResetEventHandler(AppEvent * aEvent) { LOG_INF("Lighting App Power On Factory Reset Handler"); sPowerOnFactoryResetTimerCnt = 1; +#ifdef CONFIG_WS2812_STRIP + sAppTask.mWS2812Device.Set(sPowerOnFactoryResetTimerCnt % 2); +#else sAppTask.mPwmRgbBlueLed.Set(sPowerOnFactoryResetTimerCnt % 2); #if USE_RGB_PWM sAppTask.mPwmRgbRedLed.Set(sPowerOnFactoryResetTimerCnt % 2); sAppTask.mPwmRgbGreenLed.Set(sPowerOnFactoryResetTimerCnt % 2); #endif +#endif // CONFIG_WS2812_STRIP k_timer_init(&sPowerOnFactoryResetTimer, PowerOnFactoryResetTimerEvent, nullptr); k_timer_start(&sPowerOnFactoryResetTimer, K_MSEC(kPowerOnFactoryResetIndicationTimeMs), K_MSEC(kPowerOnFactoryResetIndicationTimeMs)); @@ -315,11 +377,15 @@ void AppTask::PowerOnFactoryResetTimerEvent(struct k_timer * timer) { sPowerOnFactoryResetTimerCnt++; LOG_INF("Lighting App Power On Factory Reset Handler %u", sPowerOnFactoryResetTimerCnt); +#ifdef CONFIG_WS2812_STRIP + sAppTask.mWS2812Device.Set(sPowerOnFactoryResetTimerCnt % 2); +#else sAppTask.mPwmRgbBlueLed.Set(sPowerOnFactoryResetTimerCnt % 2); #if USE_RGB_PWM sAppTask.mPwmRgbRedLed.Set(sPowerOnFactoryResetTimerCnt % 2); sAppTask.mPwmRgbGreenLed.Set(sPowerOnFactoryResetTimerCnt % 2); #endif +#endif // CONFIG_WS2812_STRIP if (sPowerOnFactoryResetTimerCnt > kPowerOnFactoryResetIndicationMax) { k_timer_stop(timer); diff --git a/examples/lighting-app/telink/src/ZclCallbacks.cpp b/examples/lighting-app/telink/src/ZclCallbacks.cpp index 86e57809b67e11..53649639507142 100644 --- a/examples/lighting-app/telink/src/ZclCallbacks.cpp +++ b/examples/lighting-app/telink/src/ZclCallbacks.cpp @@ -48,7 +48,7 @@ void MatterPostAttributeChangeCallback(const chip::app::ConcreteAttributePath & } else if (clusterId == LevelControl::Id && attributeId == LevelControl::Attributes::CurrentLevel::Id) { - if (GetAppTask().GetPWMDevice().IsTurnedOn()) + if (GetAppTask().GetLightingDevice().IsTurnedOn()) { ChipLogDetail(Zcl, "Cluster LevelControl: attribute CurrentLevel set to %u", *value); GetAppTask().SetInitiateAction(PWMDevice::LEVEL_ACTION, static_cast(AppEvent::kEventType_Lighting), value); @@ -138,7 +138,7 @@ void emberAfOnOffClusterInitCallback(EndpointId endpoint) if (status == EMBER_ZCL_STATUS_SUCCESS) { // Set actual state to stored before reboot - GetAppTask().GetPWMDevice().Set(storedValue); + GetAppTask().GetLightingDevice().Set(storedValue); } GetAppTask().UpdateClusterState(); diff --git a/examples/platform/telink/common/include/AppTaskCommon.h b/examples/platform/telink/common/include/AppTaskCommon.h index e0b344df5b6ec1..8cb25d4e8cbede 100644 --- a/examples/platform/telink/common/include/AppTaskCommon.h +++ b/examples/platform/telink/common/include/AppTaskCommon.h @@ -29,6 +29,10 @@ #include "PWMDevice.h" #endif +#ifdef CONFIG_WS2812_STRIP +#include "WS2812Device.h" +#endif + #include #include #include diff --git a/examples/platform/telink/common/include/SensorManagerCommon.h b/examples/platform/telink/common/include/SensorManagerCommon.h index 4b804b5ecf0723..8420391000472d 100644 --- a/examples/platform/telink/common/include/SensorManagerCommon.h +++ b/examples/platform/telink/common/include/SensorManagerCommon.h @@ -21,12 +21,12 @@ #include #include -#include "AppEventCommon.h" +#include "AppTaskCommon.h" #include #include -#if defined(CONFIG_CHIP_USE_MARS_SENSOR) && !defined(CONFIG_PM) +#if defined(CONFIG_CHIP_USE_MARS_SENSOR) && defined(CONFIG_WS2812_STRIP) && !defined(CONFIG_PM) #define USE_COLOR_TEMPERATURE_LIGHT #endif @@ -46,7 +46,8 @@ class SensorManager static void SensorBanForNextMeasurTimerTimeoutCallback(k_timer * timer); #ifdef USE_COLOR_TEMPERATURE_LIGHT - int SetColorTemperatureLight(int8_t temp); + WS2812Device mWS2812Device; + void SetColorTemperatureLight(int8_t temp); #endif // USE_COLOR_TEMPERATURE_LIGHT // SHT30 operating range −40…125°C diff --git a/examples/platform/telink/common/src/SensorManagerCommon.cpp b/examples/platform/telink/common/src/SensorManagerCommon.cpp index e3510340630d0d..1c3b8a221919d8 100644 --- a/examples/platform/telink/common/src/SensorManagerCommon.cpp +++ b/examples/platform/telink/common/src/SensorManagerCommon.cpp @@ -19,10 +19,6 @@ #include "SensorManagerCommon.h" #ifdef CONFIG_CHIP_USE_MARS_SENSOR #include - -#ifdef USE_COLOR_TEMPERATURE_LIGHT -#include -#endif // CONFIG_PM #endif // CONFIG_CHIP_USE_MARS_SENSOR LOG_MODULE_DECLARE(app, CONFIG_CHIP_APP_LOG_LEVEL); @@ -42,10 +38,6 @@ const struct device *const sht3xd_dev = DEVICE_DT_GET_ONE(sensirion_sht3xd); #ifdef USE_COLOR_TEMPERATURE_LIGHT const struct device *const ws2812_dev = DEVICE_DT_GET(DT_ALIAS(led_strip)); -#define STRIP_NUM_PIXELS DT_PROP(DT_ALIAS(led_strip), chain_length) - -#define RGB_MAX_VALUE 255 - #define TEMP_LOW_LIM 0 // °C #define TEMP_HIGH_LIM 40 // °C #endif // USE_COLOR_TEMPERATURE_LIGHT @@ -67,17 +59,17 @@ CHIP_ERROR SensorManager::Init() } #ifdef USE_COLOR_TEMPERATURE_LIGHT - if (!device_is_ready(ws2812_dev)) - { - LOG_ERR("Device %s is not ready", ws2812_dev->name); - return CHIP_ERROR_INCORRECT_STATE; - } + RgbColor_t rgb = {0}; - int status = SetColorTemperatureLight(mMinMeasuredTempCelsius); - if (status) { - LOG_ERR("Couldn't update strip: %d", status); - return System::MapErrorZephyr(status); + CHIP_ERROR err = sSensorManager.mWS2812Device.Init(ws2812_dev, STRIP_NUM_PIXELS(led_strip)); + if (err != CHIP_NO_ERROR) + { + LOG_ERR("WS2812 Device Init fail"); + return err; } + + sSensorManager.mWS2812Device.SetLevel(&rgb); + sSensorManager.mWS2812Device.Set(SET_RGB_TURN_ON); #endif // USE_COLOR_TEMPERATURE_LIGHT // Initialise the timer to ban sensor measurement @@ -97,11 +89,10 @@ CHIP_ERROR SensorManager::GetTempAndHumMeasurValue(int16_t *pTempMeasured, uint1 #ifdef CONFIG_CHIP_USE_MARS_SENSOR static struct sensor_value sensorTemp = {0}; static struct sensor_value sensorHum = {0}; - int status; if (!mSensorBanForNextMeasurFlag) { - status = sensor_sample_fetch(sht3xd_dev); + int status = sensor_sample_fetch(sht3xd_dev); if (status) { LOG_ERR("Device %s is not ready to fetch the sensor samples (status: %d)", sht3xd_dev->name, status); @@ -132,11 +123,7 @@ CHIP_ERROR SensorManager::GetTempAndHumMeasurValue(int16_t *pTempMeasured, uint1 hum = (float)sensor_value_to_double(&sensorHum); #ifdef USE_COLOR_TEMPERATURE_LIGHT - status = SetColorTemperatureLight(temp); - if (status) { - LOG_ERR("Couldn't update strip: %d", status); - return System::MapErrorZephyr(status); - } + SetColorTemperatureLight(temp); #endif // USE_COLOR_TEMPERATURE_LIGHT #else /* Temperature simulation is used */ @@ -206,10 +193,9 @@ void SensorManager::SensorBanForNextMeasurTimerTimeoutCallback(k_timer * timer) } #ifdef USE_COLOR_TEMPERATURE_LIGHT -int SensorManager::SetColorTemperatureLight(int8_t temp) +void SensorManager::SetColorTemperatureLight(int8_t temp) { - int status; - struct led_rgb rgb = {0}; + RgbColor_t rgb = {0}; if (temp >= mMinMeasuredTempCelsius && temp <= TEMP_LOW_LIM) { @@ -259,8 +245,7 @@ int SensorManager::SetColorTemperatureLight(int8_t temp) LOG_ERR("Couldn't set the Color Temperature Light"); } - status = led_strip_update_rgb(ws2812_dev, &rgb, STRIP_NUM_PIXELS); - return status; + sSensorManager.mWS2812Device.SetLevel(&rgb); } #endif // USE_COLOR_TEMPERATURE_LIGHT #endif // CONFIG_CHIP_USE_MARS_SENSOR diff --git a/examples/platform/telink/util/include/WS2812Device.h b/examples/platform/telink/util/include/WS2812Device.h new file mode 100644 index 00000000000000..24351db7836115 --- /dev/null +++ b/examples/platform/telink/util/include/WS2812Device.h @@ -0,0 +1,57 @@ +/* + * Copyright (c) 2023 Project CHIP Authors + * 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. + */ + +#pragma once + +#include + +#include + +#include + +#define STRIP_NUM_PIXELS(LED_STRIP) DT_PROP(DT_ALIAS(LED_STRIP), chain_length) +#define RGB_MIN_VALUE 0 +#define RGB_MAX_VALUE 255 + +#define SET_RGB_TURN_OFF 0 +#define SET_RGB_TURN_ON 1 + +class WS2812Device +{ +public: + CHIP_ERROR Init(const struct device * ws2812Device, uint32_t aChainLength); + void SetLevel(RgbColor_t * pRgb); + void Set(bool aTurnOn); + bool IsTurnedOn(void); + uint8_t GetBlueLevel(void) const { return mLedRgb.b; } + uint8_t GetGreenLevel(void) const { return mLedRgb.g; } + uint8_t GetRedLevel(void) const { return mLedRgb.r; } + +private: + enum WS2812State_t : uint8_t + { + kRgbState_On = 0, + kRgbState_Off, + }; + + void UpdateRgbLight(); + + const struct device * mWs2812Device; + uint32_t mChainLength; + RgbColor_t mLedRgb; + WS2812State_t mState; +}; diff --git a/examples/platform/telink/util/src/PWMDevice.cpp b/examples/platform/telink/util/src/PWMDevice.cpp index b77126b2b7b802..8456d5482a3e6e 100644 --- a/examples/platform/telink/util/src/PWMDevice.cpp +++ b/examples/platform/telink/util/src/PWMDevice.cpp @@ -25,7 +25,7 @@ #include #include -LOG_MODULE_DECLARE(app); +LOG_MODULE_REGISTER(PWMDevice); constexpr uint32_t kBreatheStepTimeMS = 10; diff --git a/examples/platform/telink/util/src/WS2812Device.cpp b/examples/platform/telink/util/src/WS2812Device.cpp new file mode 100644 index 00000000000000..3ff00a6ba85ab7 --- /dev/null +++ b/examples/platform/telink/util/src/WS2812Device.cpp @@ -0,0 +1,85 @@ +/* + * Copyright (c) 2023 Project CHIP Authors + * 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. + */ + +#include "WS2812Device.h" + +#include + +#include +#include + +LOG_MODULE_REGISTER(WS2812Device); + +using namespace chip; + +CHIP_ERROR WS2812Device::Init(const struct device * ws2812Device, uint32_t aChainLength) +{ + mWs2812Device = ws2812Device; + mChainLength = aChainLength; + mState = kRgbState_Off; + memset(&mLedRgb, RGB_MAX_VALUE, sizeof(RgbColor_t)); + + if (!device_is_ready(mWs2812Device)) + { + LOG_ERR("Device %s is not ready", mWs2812Device->name); + return CHIP_ERROR_INCORRECT_STATE; + } + + UpdateRgbLight(); + + return CHIP_NO_ERROR; +} + +void WS2812Device::UpdateRgbLight() +{ + int status; + led_rgb setRgb = {0}; + + if (mState == kRgbState_On) + { + setRgb.r = mLedRgb.r; + setRgb.g = mLedRgb.g; + setRgb.b = mLedRgb.b; + } + + status = led_strip_update_rgb(mWs2812Device, &setRgb, mChainLength); + if (status) + { + LOG_ERR("Couldn't update strip: %d", status); + } +} + +void WS2812Device::SetLevel(RgbColor_t * pRgb) +{ + if (pRgb != NULL) + { + memcpy(&mLedRgb, pRgb, sizeof(RgbColor_t)); + } + + UpdateRgbLight(); +} + +void WS2812Device::Set(bool aTurnOn) +{ + mState = aTurnOn ? kRgbState_On : kRgbState_Off; + UpdateRgbLight(); +} + +bool WS2812Device::IsTurnedOn() +{ + return mState == kRgbState_On; +} diff --git a/examples/temperature-measurement-app/telink/CMakeLists.txt b/examples/temperature-measurement-app/telink/CMakeLists.txt index 00c77fd368dcf8..aa2daa1946e655 100644 --- a/examples/temperature-measurement-app/telink/CMakeLists.txt +++ b/examples/temperature-measurement-app/telink/CMakeLists.txt @@ -98,7 +98,7 @@ target_sources(app PRIVATE ${TELINK_COMMON}/util/src/ButtonManager.cpp ${TELINK_COMMON}/util/src/ThreadUtil.cpp ${TELINK_COMMON}/util/src/PWMDevice.cpp - ) + ${TELINK_COMMON}/util/src/WS2812Device.cpp) chip_configure_data_model(app INCLUDE_SERVER diff --git a/examples/thermostat/telink/CMakeLists.txt b/examples/thermostat/telink/CMakeLists.txt index c3938949508c33..05a5fc6ca6c02d 100755 --- a/examples/thermostat/telink/CMakeLists.txt +++ b/examples/thermostat/telink/CMakeLists.txt @@ -98,7 +98,8 @@ target_sources(app PRIVATE ${TELINK_COMMON}/util/src/LEDWidget.cpp ${TELINK_COMMON}/util/src/ButtonManager.cpp ${TELINK_COMMON}/util/src/ThreadUtil.cpp - ${TELINK_COMMON}/util/src/PWMDevice.cpp) + ${TELINK_COMMON}/util/src/PWMDevice.cpp + ${TELINK_COMMON}/util/src/WS2812Device.cpp) chip_configure_data_model(app INCLUDE_SERVER