From 3995ad22c9bcb350c1beb2c921b214706e89702c Mon Sep 17 00:00:00 2001 From: Serhii Salamakha Date: Tue, 14 Nov 2023 17:01:17 +0200 Subject: [PATCH 1/7] [Telink] Add Mars board overlay and real measurment of sht3xd sensor --- config/telink/chip-module/Kconfig | 5 + .../telink/CMakeLists.txt | 24 +++- .../telink/include/AppTask.h | 4 +- .../telink/include/SensorManager.h | 44 +++++++ .../air-quality-sensor-app/telink/prj.conf | 2 +- .../telink/src/AppTask.cpp | 95 ++++++++++++-- .../telink/src/SensorManager.cpp | 120 ++++++++++++++++++ src/platform/telink/tlsr9518adk80d_mars.conf | 4 + .../telink/tlsr9518adk80d_mars.overlay | 62 +++++++++ 9 files changed, 345 insertions(+), 15 deletions(-) create mode 100644 examples/air-quality-sensor-app/telink/include/SensorManager.h create mode 100644 examples/air-quality-sensor-app/telink/src/SensorManager.cpp create mode 100644 src/platform/telink/tlsr9518adk80d_mars.conf create mode 100644 src/platform/telink/tlsr9518adk80d_mars.overlay diff --git a/config/telink/chip-module/Kconfig b/config/telink/chip-module/Kconfig index b1bd07660937bd..93ebc31a0f426e 100644 --- a/config/telink/chip-module/Kconfig +++ b/config/telink/chip-module/Kconfig @@ -202,3 +202,8 @@ config CHIP_ENABLE_POWER_ON_FACTORY_RESET Enable power on factory reset sequence. If device power triggered off during first 5 seconds after power on and this sequence repeated 5 times - factory reset will be involved. + +config CHIP_USE_MARS_SENSOR + bool "Use Mars board sensor" + depends on SOC_SERIES_RISCV_TELINK_B9X && (BOARD_TLSR9518ADK80D || BOARD_TLSR9518ADK80D_RETENTION) + default n diff --git a/examples/air-quality-sensor-app/telink/CMakeLists.txt b/examples/air-quality-sensor-app/telink/CMakeLists.txt index fd5a5b14256426..a6148fe2933e2d 100644 --- a/examples/air-quality-sensor-app/telink/CMakeLists.txt +++ b/examples/air-quality-sensor-app/telink/CMakeLists.txt @@ -19,6 +19,23 @@ get_filename_component(CHIP_ROOT ${CMAKE_CURRENT_SOURCE_DIR}/third_party/connect get_filename_component(TELINK_COMMON ${CHIP_ROOT}/examples/platform/telink REALPATH) get_filename_component(GEN_DIR ${CHIP_ROOT}/zzz_generated/ REALPATH) +set(ignoreMe "${TLNK_MARS_BOARD}") + +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(EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/boards/${BOARD}.overlay") set(LOCAL_DTC_OVERLAY_FILE "${CMAKE_CURRENT_SOURCE_DIR}/boards/${BOARD}.overlay") else() @@ -41,14 +58,14 @@ endif() if(DTC_OVERLAY_FILE) set(DTC_OVERLAY_FILE - "${DTC_OVERLAY_FILE} ${GLOBAL_DTC_OVERLAY_FILE} ${FLASH_DTC_OVERLAY_FILE} ${LOCAL_DTC_OVERLAY_FILE}" + "${DTC_OVERLAY_FILE} ${GLOBAL_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} ${FLASH_DTC_OVERLAY_FILE} ${LOCAL_DTC_OVERLAY_FILE}) + set(DTC_OVERLAY_FILE ${GLOBAL_DTC_OVERLAY_FILE} ${MARS_DTC_OVERLAY_FILE} ${FLASH_DTC_OVERLAY_FILE} ${LOCAL_DTC_OVERLAY_FILE}) endif() -set(CONF_FILE prj.conf) +set(CONF_FILE ${MARS_CONF_OVERLAY_FILE} prj.conf) # Load NCS/Zephyr build system list(APPEND ZEPHYR_EXTRA_MODULES ${CHIP_ROOT}/config/telink/chip-module) @@ -75,6 +92,7 @@ add_definitions( target_sources(app PRIVATE src/AppTask.cpp + src/SensorManager.cpp src/ZclCallbacks.cpp ${TELINK_COMMON}/common/src/mainCommon.cpp ${TELINK_COMMON}/common/src/AppTaskCommon.cpp diff --git a/examples/air-quality-sensor-app/telink/include/AppTask.h b/examples/air-quality-sensor-app/telink/include/AppTask.h index a1b55a9b83108f..e76897cdd81d99 100644 --- a/examples/air-quality-sensor-app/telink/include/AppTask.h +++ b/examples/air-quality-sensor-app/telink/include/AppTask.h @@ -31,7 +31,9 @@ class AppTask : public AppTaskCommon CHIP_ERROR Init(void); - static void AirQualityActionEventHandler(AppEvent * aEvent); + static void AirQualitySensorMeasurTimerTimeoutCallback(k_timer * timer); + static void AirQualitySensorBanForNextMeasurTimerTimeoutCallback(k_timer * timer); + static void AirQualitySensorMeasurActionEventHandler(AppEvent * aEvent); static AppTask sAppTask; }; diff --git a/examples/air-quality-sensor-app/telink/include/SensorManager.h b/examples/air-quality-sensor-app/telink/include/SensorManager.h new file mode 100644 index 00000000000000..f545120df238ab --- /dev/null +++ b/examples/air-quality-sensor-app/telink/include/SensorManager.h @@ -0,0 +1,44 @@ +/* + * + * 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 "AppEventCommon.h" + +#include +#include + +class SensorManager +{ +public: + CHIP_ERROR Init(); + CHIP_ERROR GetTempAndHumMeasurValue(int16_t *pTempMeasured, uint16_t *pHumMeasured); + +private: + friend SensorManager & SensorMgr(); + + static SensorManager sSensorManager; +}; + +inline SensorManager & SensorMgr() +{ + return SensorManager::sSensorManager; +} diff --git a/examples/air-quality-sensor-app/telink/prj.conf b/examples/air-quality-sensor-app/telink/prj.conf index b43fbe9636d9aa..f55000798a16cb 100644 --- a/examples/air-quality-sensor-app/telink/prj.conf +++ b/examples/air-quality-sensor-app/telink/prj.conf @@ -48,4 +48,4 @@ CONFIG_CHIP_FACTORY_DATA_BUILD=n CONFIG_CHIP_FACTORY_DATA_MERGE_WITH_FIRMWARE=n # Enable Power Management -CONFIG_PM=y +CONFIG_PM=n diff --git a/examples/air-quality-sensor-app/telink/src/AppTask.cpp b/examples/air-quality-sensor-app/telink/src/AppTask.cpp index 94988ea8bbe5a0..5e841faa8d5623 100644 --- a/examples/air-quality-sensor-app/telink/src/AppTask.cpp +++ b/examples/air-quality-sensor-app/telink/src/AppTask.cpp @@ -17,50 +17,125 @@ */ #include "AppTask.h" +#include "SensorManager.h" #include LOG_MODULE_DECLARE(app, CONFIG_CHIP_APP_LOG_LEVEL); +using namespace chip; using namespace ::chip::app::Clusters; using namespace ::chip::app::Clusters::AirQuality; -AppTask AppTask::sAppTask; +namespace { +volatile bool mSensorBanForNextMeasur = false; +k_timer sSensorMeasurTimer; +k_timer sSensorBanForNextMeasurTimer; +constexpr uint16_t kSensorMeasurTimerPeriodMs = 10000; // 10s timer period +constexpr uint16_t kSensorBanForNextMeasurTimeoutMs = 1000; // 1s timeout +} // namespace -constexpr EndpointId kAirQualityEndpoint = 1; +AppTask AppTask::sAppTask; CHIP_ERROR AppTask::Init(void) { + CHIP_ERROR ret; + #if APP_USE_EXAMPLE_START_BUTTON - SetExampleButtonCallbacks(AirQualityActionEventHandler); + SetExampleButtonCallbacks(AirQualitySensorMeasurActionEventHandler); #endif InitCommonParts(); - AirQualitySensorManager::InitInstance(kAirQualityEndpoint); + ret = SensorMgr().Init(); + if (ret != CHIP_NO_ERROR) + { + LOG_ERR("Init of the Sensor Manager failed"); + return ret; + } + + // Initialize sensor measurement timer + k_timer_init(&sSensorMeasurTimer, &AppTask::AirQualitySensorMeasurTimerTimeoutCallback, nullptr); + k_timer_user_data_set(&sSensorMeasurTimer, this); + k_timer_start(&sSensorMeasurTimer, K_MSEC(kSensorMeasurTimerPeriodMs), K_NO_WAIT); + + // Initialise the timer to ban sensor measurement + k_timer_init(&sSensorBanForNextMeasurTimer, &AppTask::AirQualitySensorBanForNextMeasurTimerTimeoutCallback, nullptr); + k_timer_user_data_set(&sSensorBanForNextMeasurTimer, this); + + AirQualitySensorManager::InitInstance(kExampleEndpointId); return CHIP_NO_ERROR; } void AppTask::UpdateClusterState(void) { + CHIP_ERROR ret; AirQualitySensorManager * mInstance = AirQualitySensorManager::GetInstance(); + static int16_t temperature = 0; + static uint16_t humidity = 0; + + if (!mSensorBanForNextMeasur) + { + ret = SensorMgr().GetTempAndHumMeasurValue(&temperature, &humidity); + if (ret != CHIP_NO_ERROR) + { + LOG_ERR("Update of the Air Quality clusters failed"); + return; + } + + mSensorBanForNextMeasur = true; + + // Start next timer to measurement the air quality sensor + k_timer_start(&sSensorBanForNextMeasurTimer, K_MSEC(kSensorBanForNextMeasurTimeoutMs), K_NO_WAIT); + } // Update AirQuality value - mInstance->OnAirQualityChangeHandler(AirQualityEnum::kModerate); + mInstance->OnAirQualityChangeHandler(AirQualityEnum::kUnknown); // Update Carbon Dioxide - mInstance->OnCarbonDioxideMeasurementChangeHandler(400); + mInstance->OnCarbonDioxideMeasurementChangeHandler(0); // Update Temperature value - mInstance->OnTemperatureMeasurementChangeHandler(18); + mInstance->OnTemperatureMeasurementChangeHandler(temperature); // Update Humidity value - mInstance->OnHumidityMeasurementChangeHandler(60); + mInstance->OnHumidityMeasurementChangeHandler(humidity); + + LOG_INF("Update Air Quality: temperature is %d*0.01°C, humidity is %d", temperature, humidity); } -void AppTask::AirQualityActionEventHandler(AppEvent * aEvent) +void AppTask::AirQualitySensorMeasurTimerTimeoutCallback(k_timer * timer) { - if (aEvent->Type == AppEvent::kEventType_Button) + if (!timer) + { + return; + } + + AppEvent event; + event.Type = AppEvent::kEventType_Timer; + event.Handler = AirQualitySensorMeasurActionEventHandler; + sAppTask.PostEvent(&event); +} + +void AppTask::AirQualitySensorBanForNextMeasurTimerTimeoutCallback(k_timer * timer) +{ + if (!timer) + { + return; + } + + mSensorBanForNextMeasur = false; +} + +void AppTask::AirQualitySensorMeasurActionEventHandler(AppEvent * aEvent) +{ + if ((aEvent->Type == AppEvent::kEventType_Button) || (aEvent->Type == AppEvent::kEventType_Timer)) { sAppTask.UpdateClusterState(); } + + if (aEvent->Type == AppEvent::kEventType_Timer) + { + // Start next timer to measurement the air quality sensor + k_timer_start(&sSensorMeasurTimer, K_MSEC(kSensorMeasurTimerPeriodMs), K_NO_WAIT); + } } diff --git a/examples/air-quality-sensor-app/telink/src/SensorManager.cpp b/examples/air-quality-sensor-app/telink/src/SensorManager.cpp new file mode 100644 index 00000000000000..de580ed9ba00fb --- /dev/null +++ b/examples/air-quality-sensor-app/telink/src/SensorManager.cpp @@ -0,0 +1,120 @@ +/* + * + * 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 "SensorManager.h" +#include "AppConfig.h" +#include "AppTask.h" +#include + +LOG_MODULE_DECLARE(app, CONFIG_CHIP_APP_LOG_LEVEL); + +using namespace chip; +using namespace ::chip::DeviceLayer; + +constexpr float kMinTempDelta = 0.5; // 0.5 degree Celsius + +#ifdef CONFIG_CHIP_USE_MARS_SENSOR +const struct device *const sht3xd_dev = DEVICE_DT_GET_ONE(sensirion_sht3xd); +#elif +constexpr float kSimulatedHum = 55.5; // percents +constexpr uint16_t kSimulatedReadingFrequency = 5; // change Simulated number +static float mSimulatedTemp[] = { 23.01, 24.02, 28.03, 25.50, 22.05, 21.25, 21.07, 26.08, 18.09, 27.11 }; +#endif // CONFIG_CHIP_USE_MARS_SENSOR + +SensorManager SensorManager::sSensorManager; + +CHIP_ERROR SensorManager::Init() +{ +#ifdef CONFIG_CHIP_USE_MARS_SENSOR + if (!device_is_ready(sht3xd_dev)) + { + LOG_ERR("Device %s is not ready", sht3xd_dev->name); + return CHIP_ERROR_INCORRECT_STATE; + } +#endif // CONFIG_CHIP_USE_MARS_SENSOR + + return CHIP_NO_ERROR; +} + +CHIP_ERROR SensorManager::GetTempAndHumMeasurValue(int16_t *pTempMeasured, uint16_t *pHumMeasured) +{ + static float lastTemp = 0.0; + float temp = 0.0; + float hum = 0.0; + +#ifdef CONFIG_CHIP_USE_MARS_SENSOR + struct sensor_value sensorTemp = {0}; + struct sensor_value sensorHum = {0}; + + int status = sensor_sample_fetch(sht3xd_dev); + if (!status) + { + status = sensor_channel_get(sht3xd_dev, SENSOR_CHAN_AMBIENT_TEMP, &sensorTemp); + } + + if (!status) + { + status = sensor_channel_get(sht3xd_dev, SENSOR_CHAN_HUMIDITY, &sensorHum); + } + + if (status) + { + LOG_ERR("Device %s is not ready for temperature and humidity measurement (status: %d)", sht3xd_dev->name, status); + return System::MapErrorZephyr(status); + } + + temp = (float)sensor_value_to_double(&sensorTemp); + hum = (float)sensor_value_to_double(&sensorHum); +#else + /* Temperature simulation is used */ + static uint8_t nbOfRepetition = 0; + static uint8_t simulatedIndex = 0; + if (simulatedIndex >= ArraySize(mSimulatedTemp)) + { + simulatedIndex = 0; + } + temp = mSimulatedTemp[simulatedIndex]; + + nbOfRepetition++; + + if (nbOfRepetition >= kSimulatedReadingFrequency) + { + simulatedIndex++; + nbOfRepetition = 0; + } + + /* Humidity simulation is used */ + hum = kSimulatedHum; + +#endif // CONFIG_CHIP_USE_MARS_SENSOR + + if ((temp >= (lastTemp + kMinTempDelta)) || temp <= (lastTemp - kMinTempDelta)) + { + lastTemp = temp; + } + else + { + temp = lastTemp; + } + + // Per spec Application Clusters 2.3.4.1. : MeasuredValue = 100 x temperature [°C] + *pTempMeasured = (int16_t) 100 * temp; + *pHumMeasured = (uint16_t) hum; + + return CHIP_NO_ERROR; +} diff --git a/src/platform/telink/tlsr9518adk80d_mars.conf b/src/platform/telink/tlsr9518adk80d_mars.conf new file mode 100644 index 00000000000000..1c076febc2f4bd --- /dev/null +++ b/src/platform/telink/tlsr9518adk80d_mars.conf @@ -0,0 +1,4 @@ +CONFIG_CHIP_USE_MARS_SENSOR=y +CONFIG_CHIP_BUTTON_MANAGER_IRQ_MODE=y +CONFIG_I2C=y +CONFIG_SENSOR=y \ No newline at end of file diff --git a/src/platform/telink/tlsr9518adk80d_mars.overlay b/src/platform/telink/tlsr9518adk80d_mars.overlay new file mode 100644 index 00000000000000..87a2f67ae50d56 --- /dev/null +++ b/src/platform/telink/tlsr9518adk80d_mars.overlay @@ -0,0 +1,62 @@ +/ { + leds { + led_red: led_3 { + gpios = <&gpiob 6 GPIO_ACTIVE_HIGH>; + label = "LED Red"; + }; + }; + + keys { + /delete-node/ button_1; + /delete-node/ button_3; + compatible = "gpio-keys"; + key_1: button_1 { + gpios = <&gpiod 2 (GPIO_PULL_UP | GPIO_ACTIVE_LOW)>; + }; + key_2: button_2 { + gpios = <&gpiod 1 (GPIO_PULL_UP | GPIO_ACTIVE_LOW)>; + }; + key_3: button_3 { + gpios = <&gpioc 1 (GPIO_PULL_UP | GPIO_ACTIVE_LOW)>; + }; + key_4: button_4 { + gpios = <&gpiod 3 (GPIO_PULL_UP | GPIO_ACTIVE_LOW)>; + }; + }; +}; + +&gpioc { + interrupts = <38 1>; +}; + +&gpiod { + interrupts = <37 1>; + status = "okay"; +}; + +&pinctrl { + /* Define I2C pins: SCL(PE0), SDA(PC2) */ + i2c_scl_pe0_default: i2c_scl_pe0_default { + pinmux = ; + }; + i2c_sda_pc2_default: i2c_sda_pc2_default { + pinmux = ; + }; + + pwm_ch4_pe4_default: pwm_ch4_pe4_default { + pinmux = ; + }; +}; + +&i2c { + pinctrl-0 = <&i2c_scl_pe0_default &i2c_sda_pc2_default>; + + sht3xd@44 { + compatible = "sensirion,sht3xd"; + reg = <0x44>; + }; +}; + +&pwm0 { + pinctrl-0 = <&pwm_ch0_pb4_default &pwm_ch1_pb5_default &pwm_ch2_pe2_default &pwm_ch4_pe4_default>; +}; From 1be8d6a4d4927ef08666cd9e14a09569ec98acf0 Mon Sep 17 00:00:00 2001 From: Serhii Salamakha Date: Tue, 14 Nov 2023 17:01:17 +0200 Subject: [PATCH 2/7] [Telink] Add DFU via USB for Mars board --- config/telink/chip-module/CMakeLists.txt | 12 +++++++- src/platform/telink/tlsr9518adk80d_mars.conf | 5 +++- .../telink/tlsr9518adk80d_mars.overlay | 12 +++----- .../telink/tlsr9518adk80d_mars_boot.overlay | 28 +++++++++++++++++++ 4 files changed, 47 insertions(+), 10 deletions(-) create mode 100644 src/platform/telink/tlsr9518adk80d_mars_boot.overlay diff --git a/config/telink/chip-module/CMakeLists.txt b/config/telink/chip-module/CMakeLists.txt index 545074b0b928b3..8e798c058525c6 100644 --- a/config/telink/chip-module/CMakeLists.txt +++ b/config/telink/chip-module/CMakeLists.txt @@ -155,6 +155,16 @@ else() unset(USB_BOOT_DTC_OVERLAY_FILE) endif() +if(${TLNK_MARS_BOARD} MATCHES y) + if(EXISTS "${CHIP_ROOT}/src/platform/telink/${BASE_BOARD}_mars_boot.overlay") + set(MARS_BOOT_DTC_OVERLAY_FILE "${CHIP_ROOT}/src/platform/telink/${BASE_BOARD}_mars_boot.overlay") + else() + unset(MARS_BOOT_DTC_OVERLAY_FILE) + endif() +else() + unset(MARS_CONF_OVERLAY_FILE) +endif() + if(EXISTS "${CHIP_ROOT}/src/platform/telink/${BASE_BOARD}.overlay") set(GLOBAL_DTC_OVERLAY_FILE "${CHIP_ROOT}/src/platform/telink/${BASE_BOARD}.overlay") else() @@ -186,7 +196,7 @@ if (CONFIG_BOOTLOADER_MCUBOOT) add_custom_target(build_mcuboot ALL COMMAND west build -b ${BASE_BOARD} -d build_mcuboot ${ZEPHYR_BASE}/../bootloader/mcuboot/boot/zephyr - -- -DOVERLAY_CONFIG=${GLOBAL_BOOTLOADER_CONF_OVERLAY_FILE} -DDTC_OVERLAY_FILE="${GLOBAL_DTC_OVERLAY_FILE};${FLASH_DTC_OVERLAY_FILE};${USB_BOOT_DTC_OVERLAY_FILE}" + -- -DOVERLAY_CONFIG=${GLOBAL_BOOTLOADER_CONF_OVERLAY_FILE} -DDTC_OVERLAY_FILE="${GLOBAL_DTC_OVERLAY_FILE};${FLASH_DTC_OVERLAY_FILE};${USB_BOOT_DTC_OVERLAY_FILE};${MARS_BOOT_DTC_OVERLAY_FILE}" COMMAND cp ${PROJECT_BINARY_DIR}/../modules/chip-module/build_mcuboot/zephyr/zephyr.bin ${PROJECT_BINARY_DIR}/zephyr.mcuboot.bin ) diff --git a/src/platform/telink/tlsr9518adk80d_mars.conf b/src/platform/telink/tlsr9518adk80d_mars.conf index 1c076febc2f4bd..fe619104c86578 100644 --- a/src/platform/telink/tlsr9518adk80d_mars.conf +++ b/src/platform/telink/tlsr9518adk80d_mars.conf @@ -1,4 +1,7 @@ CONFIG_CHIP_USE_MARS_SENSOR=y CONFIG_CHIP_BUTTON_MANAGER_IRQ_MODE=y CONFIG_I2C=y -CONFIG_SENSOR=y \ No newline at end of file +CONFIG_SENSOR=y +CONFIG_USB_DEVICE_STACK=y +CONFIG_USB_DEVICE_INITIALIZE_AT_BOOT=n +CONFIG_USB_CDC_ACM_LOG_LEVEL_OFF=y \ No newline at end of file diff --git a/src/platform/telink/tlsr9518adk80d_mars.overlay b/src/platform/telink/tlsr9518adk80d_mars.overlay index 87a2f67ae50d56..2b93d82d507626 100644 --- a/src/platform/telink/tlsr9518adk80d_mars.overlay +++ b/src/platform/telink/tlsr9518adk80d_mars.overlay @@ -1,24 +1,20 @@ / { leds { + /delete-node/ led_3; + led_red: led_3 { - gpios = <&gpiob 6 GPIO_ACTIVE_HIGH>; label = "LED Red"; + gpios = <&gpiob 6 GPIO_ACTIVE_HIGH>; }; }; keys { /delete-node/ button_1; - /delete-node/ button_3; + /delete-node/ button_4; compatible = "gpio-keys"; key_1: button_1 { - gpios = <&gpiod 2 (GPIO_PULL_UP | GPIO_ACTIVE_LOW)>; - }; - key_2: button_2 { gpios = <&gpiod 1 (GPIO_PULL_UP | GPIO_ACTIVE_LOW)>; }; - key_3: button_3 { - gpios = <&gpioc 1 (GPIO_PULL_UP | GPIO_ACTIVE_LOW)>; - }; key_4: button_4 { gpios = <&gpiod 3 (GPIO_PULL_UP | GPIO_ACTIVE_LOW)>; }; diff --git a/src/platform/telink/tlsr9518adk80d_mars_boot.overlay b/src/platform/telink/tlsr9518adk80d_mars_boot.overlay new file mode 100644 index 00000000000000..64a998bb306178 --- /dev/null +++ b/src/platform/telink/tlsr9518adk80d_mars_boot.overlay @@ -0,0 +1,28 @@ +/ { + leds { + /delete-node/ led_0; + + led_blue: led_0 { + label = "LED Blue"; + gpios = <&gpiob 6 GPIO_ACTIVE_HIGH>; + }; + }; + + keys { + /delete-node/ button_dfu; + + key_dfu: button_dfu { + label = "USB DFU"; + gpios = <&gpiod 2 (GPIO_PULL_UP | GPIO_ACTIVE_LOW)>; + }; + }; +}; + +&gpioc { + interrupts = <38 1>; +}; + +&gpiod { + interrupts = <37 1>; + status = "okay"; +}; From 49bc6c138155d296eb0bc02700a0ca96c0612ff5 Mon Sep 17 00:00:00 2001 From: Serhii Salamakha Date: Tue, 14 Nov 2023 17:01:17 +0200 Subject: [PATCH 3/7] [Telink] Update the Mars board overlay --- .../telink/src/AppTask.cpp | 8 +-- src/platform/telink/tlsr9518adk80d_mars.conf | 10 ++++ .../telink/tlsr9518adk80d_mars.overlay | 53 ++++++++++++++++++- 3 files changed, 66 insertions(+), 5 deletions(-) diff --git a/examples/air-quality-sensor-app/telink/src/AppTask.cpp b/examples/air-quality-sensor-app/telink/src/AppTask.cpp index 5e841faa8d5623..16f738286926e6 100644 --- a/examples/air-quality-sensor-app/telink/src/AppTask.cpp +++ b/examples/air-quality-sensor-app/telink/src/AppTask.cpp @@ -38,18 +38,18 @@ AppTask AppTask::sAppTask; CHIP_ERROR AppTask::Init(void) { - CHIP_ERROR ret; + CHIP_ERROR err; #if APP_USE_EXAMPLE_START_BUTTON SetExampleButtonCallbacks(AirQualitySensorMeasurActionEventHandler); #endif InitCommonParts(); - ret = SensorMgr().Init(); - if (ret != CHIP_NO_ERROR) + err = SensorMgr().Init(); + if (err != CHIP_NO_ERROR) { LOG_ERR("Init of the Sensor Manager failed"); - return ret; + return err; } // Initialize sensor measurement timer diff --git a/src/platform/telink/tlsr9518adk80d_mars.conf b/src/platform/telink/tlsr9518adk80d_mars.conf index fe619104c86578..9917bb97cd2631 100644 --- a/src/platform/telink/tlsr9518adk80d_mars.conf +++ b/src/platform/telink/tlsr9518adk80d_mars.conf @@ -1,7 +1,17 @@ CONFIG_CHIP_USE_MARS_SENSOR=y CONFIG_CHIP_BUTTON_MANAGER_IRQ_MODE=y + +# SHT3XD CONFIG_I2C=y CONFIG_SENSOR=y + +# WS2812 +#CONFIG_LED_STRIP=y +#CONFIG_LED_STRIP_LOG_LEVEL_DBG=y +#CONFIG_WS2812_STRIP=y +#CONFIG_WS2812_STRIP_GPIO=y + +# DFU via USB CONFIG_USB_DEVICE_STACK=y CONFIG_USB_DEVICE_INITIALIZE_AT_BOOT=n CONFIG_USB_CDC_ACM_LOG_LEVEL_OFF=y \ No newline at end of file diff --git a/src/platform/telink/tlsr9518adk80d_mars.overlay b/src/platform/telink/tlsr9518adk80d_mars.overlay index 2b93d82d507626..303474ca04774d 100644 --- a/src/platform/telink/tlsr9518adk80d_mars.overlay +++ b/src/platform/telink/tlsr9518adk80d_mars.overlay @@ -1,4 +1,11 @@ +#include +#include + / { + aliases { + led-strip = &led_strip; + }; + leds { /delete-node/ led_3; @@ -8,6 +15,30 @@ }; }; + pwm_leds { + /delete-node/ pwm_led_0; + /delete-node/ pwm_led_1; + /delete-node/ pwm_led_2; + /delete-node/ pwm_led_3; + + pwm_led0: pwm_led_0 { + pwms = <&pwm0 0 PWM_MSEC(20) PWM_POLARITY_NORMAL>; + label = "PWM LED Blue"; + }; + pwm_led1: pwm_led_1 { + pwms = <&pwm0 3 PWM_MSEC(20) PWM_POLARITY_NORMAL>; + label = "PWM LED Green"; + }; + pwm_led2: pwm_led_2 { + pwms = <&pwm0 2 PWM_MSEC(20) PWM_POLARITY_NORMAL>; + label = "PWM LED Red"; + }; + pwm_led3: pwm_led_3 { + pwms = <&pwm0 1 PWM_MSEC(20) PWM_POLARITY_NORMAL>; + label = "PWM IDENTIFY LED Green"; + }; + }; + keys { /delete-node/ button_1; /delete-node/ button_4; @@ -19,6 +50,16 @@ gpios = <&gpiod 3 (GPIO_PULL_UP | GPIO_ACTIVE_LOW)>; }; }; + + led_strip: ws2812 { + compatible = "worldsemi,ws2812-gpio"; + + chain-length = <1>; /* arbitrary; change at will */ + color-mapping = ; + in-gpios = <&gpiob 4 0>; + }; }; &gpioc { @@ -39,6 +80,16 @@ pinmux = ; }; + /* PWMs */ + pwm_ch0_pe3_default: pwm_ch0_pe3_default { + pinmux = ; + }; + pwm_ch1_pb5_default: pwm_ch1_pb5_default { + pinmux = ; + }; + pwm_ch2_pe2_default: pwm_ch2_pe2_default { + pinmux = ; + }; pwm_ch4_pe4_default: pwm_ch4_pe4_default { pinmux = ; }; @@ -54,5 +105,5 @@ }; &pwm0 { - pinctrl-0 = <&pwm_ch0_pb4_default &pwm_ch1_pb5_default &pwm_ch2_pe2_default &pwm_ch4_pe4_default>; + pinctrl-0 = <&pwm_ch0_pe3_default &pwm_ch1_pb5_default &pwm_ch2_pe2_default &pwm_ch4_pe4_default>; }; From cc48adc8597aab9f29a97bdf26f0a56760a12b4c Mon Sep 17 00:00:00 2001 From: Serhii Salamakha Date: Tue, 14 Nov 2023 17:01:17 +0200 Subject: [PATCH 4/7] [Telink] Move Sensor Manager files to common part --- .../telink/CMakeLists.txt | 4 +- .../telink/include/AppTask.h | 5 +- .../telink/src/AppTask.cpp | 65 ++++--------- .../common/include/SensorManagerCommon.h} | 14 +++ .../common/src/SensorManagerCommon.cpp} | 89 ++++++++++++++---- .../telink/CMakeLists.txt | 23 ++++- .../telink/include/AppTask.h | 4 +- .../telink/include/SensorManager.h | 54 ----------- .../telink/src/AppTask.cpp | 51 ++++++---- .../telink/src/SensorManager.cpp | 92 ------------------- examples/thermostat/telink/CMakeLists.txt | 23 ++++- examples/thermostat/telink/include/AppTask.h | 6 +- .../thermostat/telink/include/SensorManager.h | 47 ---------- examples/thermostat/telink/src/AppTask.cpp | 59 +++++++++++- .../thermostat/telink/src/SensorManager.cpp | 90 ------------------ src/platform/telink/tlsr9518adk80d_mars.conf | 5 +- 16 files changed, 241 insertions(+), 390 deletions(-) rename examples/{air-quality-sensor-app/telink/include/SensorManager.h => platform/telink/common/include/SensorManagerCommon.h} (72%) rename examples/{air-quality-sensor-app/telink/src/SensorManager.cpp => platform/telink/common/src/SensorManagerCommon.cpp} (55%) delete mode 100644 examples/temperature-measurement-app/telink/include/SensorManager.h delete mode 100644 examples/temperature-measurement-app/telink/src/SensorManager.cpp delete mode 100644 examples/thermostat/telink/include/SensorManager.h delete mode 100644 examples/thermostat/telink/src/SensorManager.cpp diff --git a/examples/air-quality-sensor-app/telink/CMakeLists.txt b/examples/air-quality-sensor-app/telink/CMakeLists.txt index a6148fe2933e2d..a16d4299d35484 100644 --- a/examples/air-quality-sensor-app/telink/CMakeLists.txt +++ b/examples/air-quality-sensor-app/telink/CMakeLists.txt @@ -65,7 +65,7 @@ else() set(DTC_OVERLAY_FILE ${GLOBAL_DTC_OVERLAY_FILE} ${MARS_DTC_OVERLAY_FILE} ${FLASH_DTC_OVERLAY_FILE} ${LOCAL_DTC_OVERLAY_FILE}) endif() -set(CONF_FILE ${MARS_CONF_OVERLAY_FILE} prj.conf) +set(CONF_FILE prj.conf ${MARS_CONF_OVERLAY_FILE}) # Load NCS/Zephyr build system list(APPEND ZEPHYR_EXTRA_MODULES ${CHIP_ROOT}/config/telink/chip-module) @@ -92,10 +92,10 @@ add_definitions( target_sources(app PRIVATE src/AppTask.cpp - src/SensorManager.cpp src/ZclCallbacks.cpp ${TELINK_COMMON}/common/src/mainCommon.cpp ${TELINK_COMMON}/common/src/AppTaskCommon.cpp + ${TELINK_COMMON}/common/src/SensorManagerCommon.cpp ${TELINK_COMMON}/util/src/LEDWidget.cpp ${TELINK_COMMON}/util/src/ButtonManager.cpp ${TELINK_COMMON}/util/src/ThreadUtil.cpp diff --git a/examples/air-quality-sensor-app/telink/include/AppTask.h b/examples/air-quality-sensor-app/telink/include/AppTask.h index e76897cdd81d99..659de081dbf01f 100644 --- a/examples/air-quality-sensor-app/telink/include/AppTask.h +++ b/examples/air-quality-sensor-app/telink/include/AppTask.h @@ -31,9 +31,8 @@ class AppTask : public AppTaskCommon CHIP_ERROR Init(void); - static void AirQualitySensorMeasurTimerTimeoutCallback(k_timer * timer); - static void AirQualitySensorBanForNextMeasurTimerTimeoutCallback(k_timer * timer); - static void AirQualitySensorMeasurActionEventHandler(AppEvent * aEvent); + static void AirQualitySensorUpdateTimerTimeoutCallback(k_timer * timer); + static void AirQualitySensorUpdateTimerEventHandler(AppEvent * aEvent); static AppTask sAppTask; }; diff --git a/examples/air-quality-sensor-app/telink/src/AppTask.cpp b/examples/air-quality-sensor-app/telink/src/AppTask.cpp index 16f738286926e6..ac2f61b3c06f52 100644 --- a/examples/air-quality-sensor-app/telink/src/AppTask.cpp +++ b/examples/air-quality-sensor-app/telink/src/AppTask.cpp @@ -17,7 +17,7 @@ */ #include "AppTask.h" -#include "SensorManager.h" +#include "SensorManagerCommon.h" #include LOG_MODULE_DECLARE(app, CONFIG_CHIP_APP_LOG_LEVEL); @@ -27,11 +27,8 @@ using namespace ::chip::app::Clusters; using namespace ::chip::app::Clusters::AirQuality; namespace { -volatile bool mSensorBanForNextMeasur = false; -k_timer sSensorMeasurTimer; -k_timer sSensorBanForNextMeasurTimer; -constexpr uint16_t kSensorMeasurTimerPeriodMs = 10000; // 10s timer period -constexpr uint16_t kSensorBanForNextMeasurTimeoutMs = 1000; // 1s timeout +k_timer sAirQualitySensorUpdateTimer; +constexpr uint16_t kAirQualitySensorUpdateTimerPeriod = 10000; // 10s timer period } // namespace AppTask AppTask::sAppTask; @@ -41,7 +38,7 @@ CHIP_ERROR AppTask::Init(void) CHIP_ERROR err; #if APP_USE_EXAMPLE_START_BUTTON - SetExampleButtonCallbacks(AirQualitySensorMeasurActionEventHandler); + SetExampleButtonCallbacks(AirQualitySensorUpdateTimerEventHandler); #endif InitCommonParts(); @@ -52,14 +49,10 @@ CHIP_ERROR AppTask::Init(void) return err; } - // Initialize sensor measurement timer - k_timer_init(&sSensorMeasurTimer, &AppTask::AirQualitySensorMeasurTimerTimeoutCallback, nullptr); - k_timer_user_data_set(&sSensorMeasurTimer, this); - k_timer_start(&sSensorMeasurTimer, K_MSEC(kSensorMeasurTimerPeriodMs), K_NO_WAIT); - - // Initialise the timer to ban sensor measurement - k_timer_init(&sSensorBanForNextMeasurTimer, &AppTask::AirQualitySensorBanForNextMeasurTimerTimeoutCallback, nullptr); - k_timer_user_data_set(&sSensorBanForNextMeasurTimer, this); + // Initialize air quality sensor update timer + k_timer_init(&sAirQualitySensorUpdateTimer, &AppTask::AirQualitySensorUpdateTimerTimeoutCallback, nullptr); + k_timer_user_data_set(&sAirQualitySensorUpdateTimer, this); + k_timer_start(&sAirQualitySensorUpdateTimer, K_MSEC(kAirQualitySensorUpdateTimerPeriod), K_NO_WAIT); AirQualitySensorManager::InitInstance(kExampleEndpointId); @@ -70,24 +63,18 @@ void AppTask::UpdateClusterState(void) { CHIP_ERROR ret; AirQualitySensorManager * mInstance = AirQualitySensorManager::GetInstance(); - static int16_t temperature = 0; - static uint16_t humidity = 0; + int16_t temperature; + uint16_t humidity; - if (!mSensorBanForNextMeasur) + ret = SensorMgr().GetTempAndHumMeasurValue(&temperature, &humidity); + if (ret != CHIP_NO_ERROR) { - ret = SensorMgr().GetTempAndHumMeasurValue(&temperature, &humidity); - if (ret != CHIP_NO_ERROR) - { - LOG_ERR("Update of the Air Quality clusters failed"); - return; - } - - mSensorBanForNextMeasur = true; - - // Start next timer to measurement the air quality sensor - k_timer_start(&sSensorBanForNextMeasurTimer, K_MSEC(kSensorBanForNextMeasurTimeoutMs), K_NO_WAIT); + LOG_ERR("Update of the Air Quality clusters failed"); + return; } + LOG_INF("Update Air Quality: temperature is (%d*0.01)°C, humidity is %d", temperature, humidity); + // Update AirQuality value mInstance->OnAirQualityChangeHandler(AirQualityEnum::kUnknown); @@ -99,11 +86,9 @@ void AppTask::UpdateClusterState(void) // Update Humidity value mInstance->OnHumidityMeasurementChangeHandler(humidity); - - LOG_INF("Update Air Quality: temperature is %d*0.01°C, humidity is %d", temperature, humidity); } -void AppTask::AirQualitySensorMeasurTimerTimeoutCallback(k_timer * timer) +void AppTask::AirQualitySensorUpdateTimerTimeoutCallback(k_timer * timer) { if (!timer) { @@ -112,21 +97,11 @@ void AppTask::AirQualitySensorMeasurTimerTimeoutCallback(k_timer * timer) AppEvent event; event.Type = AppEvent::kEventType_Timer; - event.Handler = AirQualitySensorMeasurActionEventHandler; + event.Handler = AirQualitySensorUpdateTimerEventHandler; sAppTask.PostEvent(&event); } -void AppTask::AirQualitySensorBanForNextMeasurTimerTimeoutCallback(k_timer * timer) -{ - if (!timer) - { - return; - } - - mSensorBanForNextMeasur = false; -} - -void AppTask::AirQualitySensorMeasurActionEventHandler(AppEvent * aEvent) +void AppTask::AirQualitySensorUpdateTimerEventHandler(AppEvent * aEvent) { if ((aEvent->Type == AppEvent::kEventType_Button) || (aEvent->Type == AppEvent::kEventType_Timer)) { @@ -136,6 +111,6 @@ void AppTask::AirQualitySensorMeasurActionEventHandler(AppEvent * aEvent) if (aEvent->Type == AppEvent::kEventType_Timer) { // Start next timer to measurement the air quality sensor - k_timer_start(&sSensorMeasurTimer, K_MSEC(kSensorMeasurTimerPeriodMs), K_NO_WAIT); + k_timer_start(&sAirQualitySensorUpdateTimer, K_MSEC(kAirQualitySensorUpdateTimerPeriod), K_NO_WAIT); } } diff --git a/examples/air-quality-sensor-app/telink/include/SensorManager.h b/examples/platform/telink/common/include/SensorManagerCommon.h similarity index 72% rename from examples/air-quality-sensor-app/telink/include/SensorManager.h rename to examples/platform/telink/common/include/SensorManagerCommon.h index f545120df238ab..c26ebdd565f0e8 100644 --- a/examples/air-quality-sensor-app/telink/include/SensorManager.h +++ b/examples/platform/telink/common/include/SensorManagerCommon.h @@ -32,9 +32,23 @@ class SensorManager CHIP_ERROR Init(); CHIP_ERROR GetTempAndHumMeasurValue(int16_t *pTempMeasured, uint16_t *pHumMeasured); + int16_t GetMinMeasuredTempValue(); + int16_t GetMaxMeasuredTempValue(); + private: friend SensorManager & SensorMgr(); +#ifdef CONFIG_CHIP_USE_MARS_SENSOR + static void SensorBanForNextMeasurTimerTimeoutCallback(k_timer * timer); + + // SHT30 operating range −40…125°C + int16_t mMinMeasuredTempCelsius = -40; + int16_t mMaxMeasuredTempCelsius = 125; +#else + int16_t mMinMeasuredTempCelsius = -40; + int16_t mMaxMeasuredTempCelsius = 120; +#endif // CONFIG_CHIP_USE_MARS_SENSOR + static SensorManager sSensorManager; }; diff --git a/examples/air-quality-sensor-app/telink/src/SensorManager.cpp b/examples/platform/telink/common/src/SensorManagerCommon.cpp similarity index 55% rename from examples/air-quality-sensor-app/telink/src/SensorManager.cpp rename to examples/platform/telink/common/src/SensorManagerCommon.cpp index de580ed9ba00fb..04d02c9cc2f1bd 100644 --- a/examples/air-quality-sensor-app/telink/src/SensorManager.cpp +++ b/examples/platform/telink/common/src/SensorManagerCommon.cpp @@ -16,10 +16,10 @@ * limitations under the License. */ -#include "SensorManager.h" -#include "AppConfig.h" -#include "AppTask.h" +#include "SensorManagerCommon.h" +#ifdef CONFIG_CHIP_USE_MARS_SENSOR #include +#endif // CONFIG_CHIP_USE_MARS_SENSOR LOG_MODULE_DECLARE(app, CONFIG_CHIP_APP_LOG_LEVEL); @@ -29,10 +29,14 @@ using namespace ::chip::DeviceLayer; constexpr float kMinTempDelta = 0.5; // 0.5 degree Celsius #ifdef CONFIG_CHIP_USE_MARS_SENSOR +k_timer sSensorBanForNextMeasurTimer; +volatile bool mSensorBanForNextMeasurFlag = false; +constexpr uint16_t kSensorBanForNextMeasurTimeout = 1000; // 1s timeout + const struct device *const sht3xd_dev = DEVICE_DT_GET_ONE(sensirion_sht3xd); -#elif +#else constexpr float kSimulatedHum = 55.5; // percents -constexpr uint16_t kSimulatedReadingFrequency = 5; // change Simulated number +constexpr uint16_t kSimulatedReadingFrequency = 4; // change simulated number static float mSimulatedTemp[] = { 23.01, 24.02, 28.03, 25.50, 22.05, 21.25, 21.07, 26.08, 18.09, 27.11 }; #endif // CONFIG_CHIP_USE_MARS_SENSOR @@ -46,6 +50,10 @@ CHIP_ERROR SensorManager::Init() LOG_ERR("Device %s is not ready", sht3xd_dev->name); return CHIP_ERROR_INCORRECT_STATE; } + + // Initialise the timer to ban sensor measurement + k_timer_init(&sSensorBanForNextMeasurTimer, &SensorManager::SensorBanForNextMeasurTimerTimeoutCallback, nullptr); + k_timer_user_data_set(&sSensorBanForNextMeasurTimer, this); #endif // CONFIG_CHIP_USE_MARS_SENSOR return CHIP_NO_ERROR; @@ -58,24 +66,36 @@ CHIP_ERROR SensorManager::GetTempAndHumMeasurValue(int16_t *pTempMeasured, uint1 float hum = 0.0; #ifdef CONFIG_CHIP_USE_MARS_SENSOR - struct sensor_value sensorTemp = {0}; - struct sensor_value sensorHum = {0}; + static struct sensor_value sensorTemp = {0}; + static struct sensor_value sensorHum = {0}; - int status = sensor_sample_fetch(sht3xd_dev); - if (!status) + if (!mSensorBanForNextMeasurFlag) { + 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); + return System::MapErrorZephyr(status); + } + status = sensor_channel_get(sht3xd_dev, SENSOR_CHAN_AMBIENT_TEMP, &sensorTemp); - } + if (status) + { + LOG_ERR("Device %s is not ready to temperature measurement (status: %d)", sht3xd_dev->name, status); + return System::MapErrorZephyr(status); + } - if (!status) - { status = sensor_channel_get(sht3xd_dev, SENSOR_CHAN_HUMIDITY, &sensorHum); - } + if (status) + { + LOG_ERR("Device %s is not ready to humidity measurement (status: %d)", sht3xd_dev->name, status); + return System::MapErrorZephyr(status); + } - if (status) - { - LOG_ERR("Device %s is not ready for temperature and humidity measurement (status: %d)", sht3xd_dev->name, status); - return System::MapErrorZephyr(status); + mSensorBanForNextMeasurFlag = true; + + // Start next timer to measurement the air quality sensor + k_timer_start(&sSensorBanForNextMeasurTimer, K_MSEC(kSensorBanForNextMeasurTimeout), K_NO_WAIT); } temp = (float)sensor_value_to_double(&sensorTemp); @@ -112,9 +132,38 @@ CHIP_ERROR SensorManager::GetTempAndHumMeasurValue(int16_t *pTempMeasured, uint1 temp = lastTemp; } - // Per spec Application Clusters 2.3.4.1. : MeasuredValue = 100 x temperature [°C] - *pTempMeasured = (int16_t) 100 * temp; - *pHumMeasured = (uint16_t) hum; + if (pTempMeasured != NULL) + { + // Per spec Application Clusters 2.3.4.1. : MeasuredValue = 100 x temperature [°C] + *pTempMeasured = (int16_t) 100 * temp; + } + + if (pHumMeasured != NULL) + { + *pHumMeasured = (uint16_t) hum; + } return CHIP_NO_ERROR; } + +int16_t SensorManager::GetMinMeasuredTempValue() +{ + return mMinMeasuredTempCelsius; +} + +int16_t SensorManager::GetMaxMeasuredTempValue() +{ + return mMaxMeasuredTempCelsius; +} + +#ifdef CONFIG_CHIP_USE_MARS_SENSOR +void SensorManager::SensorBanForNextMeasurTimerTimeoutCallback(k_timer * timer) +{ + if (!timer) + { + return; + } + + mSensorBanForNextMeasurFlag = false; +} +#endif // CONFIG_CHIP_USE_MARS_SENSOR diff --git a/examples/temperature-measurement-app/telink/CMakeLists.txt b/examples/temperature-measurement-app/telink/CMakeLists.txt index fd8a6e33132970..00c77fd368dcf8 100644 --- a/examples/temperature-measurement-app/telink/CMakeLists.txt +++ b/examples/temperature-measurement-app/telink/CMakeLists.txt @@ -19,6 +19,21 @@ get_filename_component(CHIP_ROOT ${CMAKE_CURRENT_SOURCE_DIR}/third_party/connect get_filename_component(TELINK_COMMON ${CHIP_ROOT}/examples/platform/telink REALPATH) get_filename_component(GEN_DIR ${CHIP_ROOT}/zzz_generated/ REALPATH) +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(EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/boards/${BOARD}.overlay") set(LOCAL_DTC_OVERLAY_FILE "${CMAKE_CURRENT_SOURCE_DIR}/boards/${BOARD}.overlay") else() @@ -41,14 +56,14 @@ endif() if(DTC_OVERLAY_FILE) set(DTC_OVERLAY_FILE - "${DTC_OVERLAY_FILE} ${GLOBAL_DTC_OVERLAY_FILE} ${FLASH_DTC_OVERLAY_FILE} ${LOCAL_DTC_OVERLAY_FILE}" + "${DTC_OVERLAY_FILE} ${GLOBAL_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} ${FLASH_DTC_OVERLAY_FILE} ${LOCAL_DTC_OVERLAY_FILE}) + set(DTC_OVERLAY_FILE ${GLOBAL_DTC_OVERLAY_FILE} ${MARS_DTC_OVERLAY_FILE} ${FLASH_DTC_OVERLAY_FILE} ${LOCAL_DTC_OVERLAY_FILE}) endif() -set(CONF_FILE prj.conf) +set(CONF_FILE prj.conf ${MARS_CONF_OVERLAY_FILE}) # Load NCS/Zephyr build system list(APPEND ZEPHYR_EXTRA_MODULES ${CHIP_ROOT}/config/telink/chip-module) @@ -76,9 +91,9 @@ add_definitions( target_sources(app PRIVATE src/AppTask.cpp - src/SensorManager.cpp ${TELINK_COMMON}/common/src/mainCommon.cpp ${TELINK_COMMON}/common/src/AppTaskCommon.cpp + ${TELINK_COMMON}/common/src/SensorManagerCommon.cpp ${TELINK_COMMON}/util/src/LEDWidget.cpp ${TELINK_COMMON}/util/src/ButtonManager.cpp ${TELINK_COMMON}/util/src/ThreadUtil.cpp diff --git a/examples/temperature-measurement-app/telink/include/AppTask.h b/examples/temperature-measurement-app/telink/include/AppTask.h index f681fa37fa2378..1aac92dc45aa7d 100644 --- a/examples/temperature-measurement-app/telink/include/AppTask.h +++ b/examples/temperature-measurement-app/telink/include/AppTask.h @@ -28,8 +28,8 @@ class AppTask : public AppTaskCommon CHIP_ERROR Init(void); - static void TemperatureMeasurementTimerTimeoutCallback(k_timer * timer); - static void TemperatureMeasurementTimerEventHandler(AppEvent * aEvent); + static void TemperatureMeasurementUpdateTimerTimeoutCallback(k_timer * timer); + static void TemperatureMeasurementUpdateTimerEventHandler(AppEvent * aEvent); static AppTask sAppTask; }; diff --git a/examples/temperature-measurement-app/telink/include/SensorManager.h b/examples/temperature-measurement-app/telink/include/SensorManager.h deleted file mode 100644 index df1ffcf9378657..00000000000000 --- a/examples/temperature-measurement-app/telink/include/SensorManager.h +++ /dev/null @@ -1,54 +0,0 @@ -/* - * - * 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 "AppEventCommon.h" - -#include -#include - -class SensorManager -{ -public: - CHIP_ERROR Init(); - - int16_t GetMeasuredValue(); - int16_t GetMinMeasuredValue(); - int16_t GetMaxMeasuredValue(); - -private: - friend SensorManager & SensorMgr(); - - // Reads new generated sensor value, stores it, and updates local temperature attribute - static int16_t SensorEventHandler(); - - int16_t mMeasuredTempCelsius; - int16_t mMinMeasuredTempCelsius = -40; - int16_t mMaxMeasuredTempCelsius = 120; - - static SensorManager sSensorManager; -}; - -inline SensorManager & SensorMgr() -{ - return SensorManager::sSensorManager; -} diff --git a/examples/temperature-measurement-app/telink/src/AppTask.cpp b/examples/temperature-measurement-app/telink/src/AppTask.cpp index 023381e5225db2..f2fecad01c8981 100644 --- a/examples/temperature-measurement-app/telink/src/AppTask.cpp +++ b/examples/temperature-measurement-app/telink/src/AppTask.cpp @@ -17,43 +17,44 @@ */ #include "AppTask.h" -#include "SensorManager.h" +#include "SensorManagerCommon.h" LOG_MODULE_DECLARE(app, CONFIG_CHIP_APP_LOG_LEVEL); namespace { -k_timer sTemperatureMeasurementTimer; -constexpr uint16_t kSensorTimerPeriodMs = 5000; // 5s timer period +k_timer sTemperatureMeasurementUpdateTimer; +constexpr uint16_t kTemperatureMeasurementUpdateTimerPeriodMs = 5000; // 5s timer period } // namespace AppTask AppTask::sAppTask; CHIP_ERROR AppTask::Init(void) { - InitCommonParts(); + CHIP_ERROR err; - // Initialize temperature measurement timer - k_timer_init(&sTemperatureMeasurementTimer, &AppTask::TemperatureMeasurementTimerTimeoutCallback, nullptr); - k_timer_user_data_set(&sTemperatureMeasurementTimer, this); - k_timer_start(&sTemperatureMeasurementTimer, K_MSEC(kSensorTimerPeriodMs), K_NO_WAIT); + InitCommonParts(); - // Init Temperature Sensor - CHIP_ERROR err = SensorMgr().Init(); + err = SensorMgr().Init(); if (err != CHIP_NO_ERROR) { - LOG_ERR("SensorMgr Init fail"); + LOG_ERR("Init of the Sensor Manager failed"); return err; } + // Initialize temperature measurement update timer + k_timer_init(&sTemperatureMeasurementUpdateTimer, &AppTask::TemperatureMeasurementUpdateTimerTimeoutCallback, nullptr); + k_timer_user_data_set(&sTemperatureMeasurementUpdateTimer, this); + k_timer_start(&sTemperatureMeasurementUpdateTimer, K_MSEC(kTemperatureMeasurementUpdateTimerPeriodMs), K_NO_WAIT); + PlatformMgr().LockChipStack(); - app::Clusters::TemperatureMeasurement::Attributes::MinMeasuredValue::Set(kExampleEndpointId, SensorMgr().GetMinMeasuredValue()); - app::Clusters::TemperatureMeasurement::Attributes::MaxMeasuredValue::Set(kExampleEndpointId, SensorMgr().GetMaxMeasuredValue()); + app::Clusters::TemperatureMeasurement::Attributes::MinMeasuredValue::Set(kExampleEndpointId, SensorMgr().GetMinMeasuredTempValue()); + app::Clusters::TemperatureMeasurement::Attributes::MaxMeasuredValue::Set(kExampleEndpointId, SensorMgr().GetMaxMeasuredTempValue()); PlatformMgr().UnlockChipStack(); return CHIP_NO_ERROR; } -void AppTask::TemperatureMeasurementTimerTimeoutCallback(k_timer * timer) +void AppTask::TemperatureMeasurementUpdateTimerTimeoutCallback(k_timer * timer) { if (!timer) { @@ -62,23 +63,33 @@ void AppTask::TemperatureMeasurementTimerTimeoutCallback(k_timer * timer) AppEvent event; event.Type = AppEvent::kEventType_Timer; - event.Handler = TemperatureMeasurementTimerEventHandler; + event.Handler = TemperatureMeasurementUpdateTimerEventHandler; sAppTask.PostEvent(&event); } -void AppTask::TemperatureMeasurementTimerEventHandler(AppEvent * aEvent) +void AppTask::TemperatureMeasurementUpdateTimerEventHandler(AppEvent * aEvent) { + CHIP_ERROR ret; + int16_t temperature; + if (aEvent->Type != AppEvent::kEventType_Timer) { return; } + ret = SensorMgr().GetTempAndHumMeasurValue(&temperature, NULL); + if (ret != CHIP_NO_ERROR) + { + LOG_ERR("Update of the Temperature clusters failed"); + return; + } + + LOG_INF("Current temperature is (%d*0.01)°C", temperature); + PlatformMgr().LockChipStack(); - app::Clusters::TemperatureMeasurement::Attributes::MeasuredValue::Set(kExampleEndpointId, SensorMgr().GetMeasuredValue()); + app::Clusters::TemperatureMeasurement::Attributes::MeasuredValue::Set(kExampleEndpointId, temperature); PlatformMgr().UnlockChipStack(); - LOG_INF("Current temperature is (%d*0.01)°C", SensorMgr().GetMeasuredValue()); - // Start next timer to handle temp sensor. - k_timer_start(&sTemperatureMeasurementTimer, K_MSEC(kSensorTimerPeriodMs), K_NO_WAIT); + k_timer_start(&sTemperatureMeasurementUpdateTimer, K_MSEC(kTemperatureMeasurementUpdateTimerPeriodMs), K_NO_WAIT); } diff --git a/examples/temperature-measurement-app/telink/src/SensorManager.cpp b/examples/temperature-measurement-app/telink/src/SensorManager.cpp deleted file mode 100644 index 22d9bb1c57ef19..00000000000000 --- a/examples/temperature-measurement-app/telink/src/SensorManager.cpp +++ /dev/null @@ -1,92 +0,0 @@ -/* - * - * 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 "SensorManager.h" -#include "AppConfig.h" -#include "AppTask.h" - -#define TEMPERATURE_SIMULATION_IS_USED - -LOG_MODULE_DECLARE(app, CONFIG_CHIP_APP_LOG_LEVEL); - -using namespace chip; -using namespace ::chip::DeviceLayer; - -constexpr float kMinTemperatureDelta = 0.5; // 0.5 degree Celsius -constexpr uint16_t kSimulatedReadingFrequency = 5; // Change Simulated number -static float mSimulatedTemp[] = { 23.01, 24.02, 28.03, 25.50, 22.05, 21.25, 21.07, 26.08, 18.09, 27.11 }; - -k_timer sSensorTimer; - -SensorManager SensorManager::sSensorManager; - -CHIP_ERROR SensorManager::Init() -{ - // TODO: Initialize temp sensor - return CHIP_NO_ERROR; -} - -int16_t SensorManager::SensorEventHandler() -{ - float temperature = 0.0; - static float lastTemperature = 0.0; - -#ifdef TEMPERATURE_SIMULATION_IS_USED - static uint8_t nbOfRepetition = 0; - static uint8_t simulatedIndex = 0; - if (simulatedIndex >= ArraySize(mSimulatedTemp)) - { - simulatedIndex = 0; - } - temperature = mSimulatedTemp[simulatedIndex]; - - nbOfRepetition++; - - if (nbOfRepetition >= kSimulatedReadingFrequency) - { - simulatedIndex++; - nbOfRepetition = 0; - } - - if ((temperature >= (lastTemperature + kMinTemperatureDelta)) || temperature <= (lastTemperature - kMinTemperatureDelta)) - { - lastTemperature = temperature; - // Per spec Application Clusters 2.3.4.1. : MeasuredValue = 100 x temperature [°C] - sSensorManager.mMeasuredTempCelsius = (int16_t) 100 * temperature; - } -#else - // TODO: provide REAL sensor implementation - sSensorManager.mMeasuredTempCelsius = (int16_t) 100 * GetRealSensorTemperature(); -#endif // TEMPERATURE_SIMULATION_IS_USED - return sSensorManager.mMeasuredTempCelsius; -} - -int16_t SensorManager::GetMeasuredValue() -{ - return SensorEventHandler(); -} - -int16_t SensorManager::GetMinMeasuredValue() -{ - return mMinMeasuredTempCelsius; -} - -int16_t SensorManager::GetMaxMeasuredValue() -{ - return mMaxMeasuredTempCelsius; -} diff --git a/examples/thermostat/telink/CMakeLists.txt b/examples/thermostat/telink/CMakeLists.txt index f1588efa2c48e7..c3938949508c33 100755 --- a/examples/thermostat/telink/CMakeLists.txt +++ b/examples/thermostat/telink/CMakeLists.txt @@ -19,6 +19,21 @@ get_filename_component(CHIP_ROOT ${CMAKE_CURRENT_SOURCE_DIR}/third_party/connect get_filename_component(TELINK_COMMON ${CHIP_ROOT}/examples/platform/telink REALPATH) get_filename_component(GEN_DIR ${CHIP_ROOT}/zzz_generated/ REALPATH) +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(EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/boards/${BOARD}.overlay") set(LOCAL_DTC_OVERLAY_FILE "${CMAKE_CURRENT_SOURCE_DIR}/boards/${BOARD}.overlay") else() @@ -41,14 +56,14 @@ endif() if(DTC_OVERLAY_FILE) set(DTC_OVERLAY_FILE - "${DTC_OVERLAY_FILE} ${GLOBAL_DTC_OVERLAY_FILE} ${FLASH_DTC_OVERLAY_FILE} ${LOCAL_DTC_OVERLAY_FILE}" + "${DTC_OVERLAY_FILE} ${GLOBAL_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} ${FLASH_DTC_OVERLAY_FILE} ${LOCAL_DTC_OVERLAY_FILE}) + set(DTC_OVERLAY_FILE ${GLOBAL_DTC_OVERLAY_FILE} ${MARS_DTC_OVERLAY_FILE} ${FLASH_DTC_OVERLAY_FILE} ${LOCAL_DTC_OVERLAY_FILE}) endif() -set(CONF_FILE prj.conf) +set(CONF_FILE prj.conf ${MARS_CONF_OVERLAY_FILE}) # Load NCS/Zephyr build system list(APPEND ZEPHYR_EXTRA_MODULES ${CHIP_ROOT}/config/telink/chip-module) @@ -75,11 +90,11 @@ add_definitions( target_sources(app PRIVATE src/AppTask.cpp - src/SensorManager.cpp src/TemperatureManager.cpp src/ZclCallbacks.cpp ${TELINK_COMMON}/common/src/mainCommon.cpp ${TELINK_COMMON}/common/src/AppTaskCommon.cpp + ${TELINK_COMMON}/common/src/SensorManagerCommon.cpp ${TELINK_COMMON}/util/src/LEDWidget.cpp ${TELINK_COMMON}/util/src/ButtonManager.cpp ${TELINK_COMMON}/util/src/ThreadUtil.cpp diff --git a/examples/thermostat/telink/include/AppTask.h b/examples/thermostat/telink/include/AppTask.h index 8babf015ba4817..40cd86f2f551ea 100755 --- a/examples/thermostat/telink/include/AppTask.h +++ b/examples/thermostat/telink/include/AppTask.h @@ -19,13 +19,12 @@ #pragma once #include "AppTaskCommon.h" -#include "SensorManager.h" +#include "SensorManagerCommon.h" #include "TemperatureManager.h" class AppTask : public AppTaskCommon { public: - void UpdateClusterState(void); void UpdateThermoStatUI(void); private: @@ -34,6 +33,9 @@ class AppTask : public AppTaskCommon CHIP_ERROR Init(void); + static void ThermostatUpdateTimerTimeoutCallback(k_timer * timer); + static void ThermostatUpdateTimerEventHandler(AppEvent * aEvent); + static AppTask sAppTask; }; diff --git a/examples/thermostat/telink/include/SensorManager.h b/examples/thermostat/telink/include/SensorManager.h deleted file mode 100644 index 2d2cdb9dd44fc6..00000000000000 --- a/examples/thermostat/telink/include/SensorManager.h +++ /dev/null @@ -1,47 +0,0 @@ -/* - * - * Copyright (c) 2022 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 "AppEventCommon.h" - -#include -#include - -class SensorManager -{ -public: - CHIP_ERROR Init(); - -private: - friend SensorManager & SensorMgr(); - - // Reads new generated sensor value, stores it, and updates local temperature attribute - static void TimerEventHandler(k_timer * timer); - static void SensorTimerEventHandler(AppEvent * aEvent); - - static SensorManager sSensorManager; -}; - -inline SensorManager & SensorMgr() -{ - return SensorManager::sSensorManager; -} diff --git a/examples/thermostat/telink/src/AppTask.cpp b/examples/thermostat/telink/src/AppTask.cpp index d9b7a2c2883844..101d1a9f0fda4d 100644 --- a/examples/thermostat/telink/src/AppTask.cpp +++ b/examples/thermostat/telink/src/AppTask.cpp @@ -20,18 +20,26 @@ LOG_MODULE_DECLARE(app, CONFIG_CHIP_APP_LOG_LEVEL); +namespace { +k_timer sThermostatUpdateTimer; +constexpr uint16_t kThermostatUpdateTimerPeriodMs = 30000; // 30s timer period +} // namespace + AppTask AppTask::sAppTask; CHIP_ERROR AppTask::Init(void) { + CHIP_ERROR err; + InitCommonParts(); - CHIP_ERROR err = SensorMgr().Init(); + err = SensorMgr().Init(); if (err != CHIP_NO_ERROR) { - LOG_ERR("SensorMgr Init fail"); + LOG_ERR("Init of the Sensor Manager failed"); return err; } + err = TempMgr().Init(); if (err != CHIP_NO_ERROR) { @@ -39,13 +47,56 @@ CHIP_ERROR AppTask::Init(void) return err; } + // Initialize temperature measurement timer + k_timer_init(&sThermostatUpdateTimer, &AppTask::ThermostatUpdateTimerTimeoutCallback, nullptr); + k_timer_user_data_set(&sThermostatUpdateTimer, this); + k_timer_start(&sThermostatUpdateTimer, K_MSEC(kThermostatUpdateTimerPeriodMs), K_NO_WAIT); + return CHIP_NO_ERROR; } +void AppTask::ThermostatUpdateTimerTimeoutCallback(k_timer * timer) +{ + if (!timer) + { + return; + } + + AppEvent event; + event.Type = AppEvent::kEventType_Timer; + event.Handler = ThermostatUpdateTimerEventHandler; + sAppTask.PostEvent(&event); +} + +void AppTask::ThermostatUpdateTimerEventHandler(AppEvent * aEvent) +{ + CHIP_ERROR ret; + int16_t temperature; + + if (aEvent->Type != AppEvent::kEventType_Timer) + { + return; + } + + ret = SensorMgr().GetTempAndHumMeasurValue(&temperature, NULL); + if (ret != CHIP_NO_ERROR) + { + LOG_ERR("Update of the Temperature clusters failed"); + return; + } + + LOG_INF("Current temperature is (%d*0.01)°C", temperature); + + PlatformMgr().LockChipStack(); + app::Clusters::Thermostat::Attributes::LocalTemperature::Set(kExampleEndpointId, temperature); + PlatformMgr().UnlockChipStack(); + + // Start next timer to handle temp sensor. + k_timer_start(&sThermostatUpdateTimer, K_MSEC(kThermostatUpdateTimerPeriodMs), K_NO_WAIT); +} + void AppTask::UpdateThermoStatUI(void) { LOG_INF("Thermostat Status - M:%d T:%d'C H:%d'C C:%d'C", TempMgr().GetMode(), TempMgr().GetCurrentTemp(), TempMgr().GetHeatingSetPoint(), TempMgr().GetCoolingSetPoint()); } - -void AppTask::UpdateClusterState() {} diff --git a/examples/thermostat/telink/src/SensorManager.cpp b/examples/thermostat/telink/src/SensorManager.cpp deleted file mode 100644 index 981447c8a12bd6..00000000000000 --- a/examples/thermostat/telink/src/SensorManager.cpp +++ /dev/null @@ -1,90 +0,0 @@ -/* - * - * Copyright (c) 2022 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 "SensorManager.h" -#include "AppConfig.h" -#include "AppTask.h" - -LOG_MODULE_DECLARE(app, CONFIG_CHIP_APP_LOG_LEVEL); - -using namespace chip; -using namespace ::chip::DeviceLayer; - -constexpr EndpointId kThermostatEndpoint = 1; -constexpr uint16_t kSensorTimerPeriodMs = 30000; // 30s timer period -constexpr uint16_t kMinTemperatureDelta = 50; // 0.5 degree Celsius - -k_timer sSensorTimer; - -SensorManager SensorManager::sSensorManager; - -constexpr uint16_t kSimulatedReadingFrequency = (60000 / kSensorTimerPeriodMs); // Change Simulated number at each minutes -static int16_t mSimulatedTemp[] = { 2300, 2400, 2800, 2550, 2200, 2125, 2100, 2600, 1800, 2700 }; - -CHIP_ERROR SensorManager::Init() -{ - // Initialize temp sensor timer - k_timer_init(&sSensorTimer, &TimerEventHandler, nullptr); - k_timer_user_data_set(&sSensorTimer, this); - k_timer_start(&sSensorTimer, K_MSEC(kSensorTimerPeriodMs), K_NO_WAIT); - - return CHIP_NO_ERROR; -} - -void SensorManager::TimerEventHandler(k_timer * timer) -{ - AppEvent event; - event.Type = AppEvent::kEventType_Thermostat; - event.TimerEvent.Context = k_timer_user_data_get(timer); - event.Handler = SensorTimerEventHandler; - GetAppTask().PostEvent(&event); -} - -void SensorManager::SensorTimerEventHandler(AppEvent * aEvent) -{ - int16_t temperature = 0; - static int16_t lastTemperature = 0; - - static uint8_t nbOfRepetition = 0; - static uint8_t simulatedIndex = 0; - if (simulatedIndex >= ArraySize(mSimulatedTemp)) - { - simulatedIndex = 0; - } - temperature = mSimulatedTemp[simulatedIndex]; - - nbOfRepetition++; - if (nbOfRepetition >= kSimulatedReadingFrequency) - { - simulatedIndex++; - nbOfRepetition = 0; - } - - LOG_INF("Sensor Temp is : %d", temperature); - - if ((temperature >= (lastTemperature + kMinTemperatureDelta)) || temperature <= (lastTemperature - kMinTemperatureDelta)) - { - lastTemperature = temperature; - PlatformMgr().LockChipStack(); - app::Clusters::Thermostat::Attributes::LocalTemperature::Set(kThermostatEndpoint, temperature); - PlatformMgr().UnlockChipStack(); - } - - // Start next timer to handle temp sensor. - k_timer_start(&sSensorTimer, K_MSEC(kSensorTimerPeriodMs), K_NO_WAIT); -} diff --git a/src/platform/telink/tlsr9518adk80d_mars.conf b/src/platform/telink/tlsr9518adk80d_mars.conf index 9917bb97cd2631..fda163eb6ad985 100644 --- a/src/platform/telink/tlsr9518adk80d_mars.conf +++ b/src/platform/telink/tlsr9518adk80d_mars.conf @@ -14,4 +14,7 @@ CONFIG_SENSOR=y # DFU via USB CONFIG_USB_DEVICE_STACK=y CONFIG_USB_DEVICE_INITIALIZE_AT_BOOT=n -CONFIG_USB_CDC_ACM_LOG_LEVEL_OFF=y \ No newline at end of file +CONFIG_USB_CDC_ACM_LOG_LEVEL_OFF=y + +# Mars sensors don't work in PM (through gpio_shutdown(GPIO_ALL) in Zephyr telink_b9x/soc.c) +CONFIG_PM=n \ No newline at end of file From 12ed51250a4638ee98ca0b0d86c35f8a5f98b0b2 Mon Sep 17 00:00:00 2001 From: Serhii Salamakha Date: Tue, 14 Nov 2023 17:01:17 +0200 Subject: [PATCH 5/7] [Telink] Add Set Color Temperature Light to Sensor Manager --- .../common/include/SensorManagerCommon.h | 8 ++ .../telink/common/src/SensorManagerCommon.cpp | 99 ++++++++++++++++++- src/platform/telink/tlsr9518adk80d_mars.conf | 8 +- 3 files changed, 110 insertions(+), 5 deletions(-) diff --git a/examples/platform/telink/common/include/SensorManagerCommon.h b/examples/platform/telink/common/include/SensorManagerCommon.h index c26ebdd565f0e8..4b804b5ecf0723 100644 --- a/examples/platform/telink/common/include/SensorManagerCommon.h +++ b/examples/platform/telink/common/include/SensorManagerCommon.h @@ -26,6 +26,10 @@ #include #include +#if defined(CONFIG_CHIP_USE_MARS_SENSOR) && !defined(CONFIG_PM) +#define USE_COLOR_TEMPERATURE_LIGHT +#endif + class SensorManager { public: @@ -41,6 +45,10 @@ class SensorManager #ifdef CONFIG_CHIP_USE_MARS_SENSOR static void SensorBanForNextMeasurTimerTimeoutCallback(k_timer * timer); +#ifdef USE_COLOR_TEMPERATURE_LIGHT + int SetColorTemperatureLight(int8_t temp); +#endif // USE_COLOR_TEMPERATURE_LIGHT + // SHT30 operating range −40…125°C int16_t mMinMeasuredTempCelsius = -40; int16_t mMaxMeasuredTempCelsius = 125; diff --git a/examples/platform/telink/common/src/SensorManagerCommon.cpp b/examples/platform/telink/common/src/SensorManagerCommon.cpp index 04d02c9cc2f1bd..e3510340630d0d 100644 --- a/examples/platform/telink/common/src/SensorManagerCommon.cpp +++ b/examples/platform/telink/common/src/SensorManagerCommon.cpp @@ -19,6 +19,10 @@ #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); @@ -34,6 +38,17 @@ volatile bool mSensorBanForNextMeasurFlag = false; constexpr uint16_t kSensorBanForNextMeasurTimeout = 1000; // 1s timeout 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 #else constexpr float kSimulatedHum = 55.5; // percents constexpr uint16_t kSimulatedReadingFrequency = 4; // change simulated number @@ -51,6 +66,20 @@ CHIP_ERROR SensorManager::Init() return CHIP_ERROR_INCORRECT_STATE; } +#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; + } + + int status = SetColorTemperatureLight(mMinMeasuredTempCelsius); + if (status) { + LOG_ERR("Couldn't update strip: %d", status); + return System::MapErrorZephyr(status); + } +#endif // USE_COLOR_TEMPERATURE_LIGHT + // Initialise the timer to ban sensor measurement k_timer_init(&sSensorBanForNextMeasurTimer, &SensorManager::SensorBanForNextMeasurTimerTimeoutCallback, nullptr); k_timer_user_data_set(&sSensorBanForNextMeasurTimer, this); @@ -68,10 +97,11 @@ 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) { - int status = sensor_sample_fetch(sht3xd_dev); + 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); @@ -100,6 +130,14 @@ CHIP_ERROR SensorManager::GetTempAndHumMeasurValue(int16_t *pTempMeasured, uint1 temp = (float)sensor_value_to_double(&sensorTemp); 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); + } +#endif // USE_COLOR_TEMPERATURE_LIGHT #else /* Temperature simulation is used */ static uint8_t nbOfRepetition = 0; @@ -166,4 +204,63 @@ void SensorManager::SensorBanForNextMeasurTimerTimeoutCallback(k_timer * timer) mSensorBanForNextMeasurFlag = false; } + +#ifdef USE_COLOR_TEMPERATURE_LIGHT +int SensorManager::SetColorTemperatureLight(int8_t temp) +{ + int status; + struct led_rgb rgb = {0}; + + if (temp >= mMinMeasuredTempCelsius && temp <= TEMP_LOW_LIM) + { + /* Set Color Temperature Light in range -40...0°C */ + rgb.b = RGB_MAX_VALUE * (1 - ((float)temp - TEMP_LOW_LIM)/(mMinMeasuredTempCelsius - TEMP_LOW_LIM)); + } + else if (temp >= TEMP_HIGH_LIM && temp <= mMaxMeasuredTempCelsius) + { + /* Set Color Temperature Light in range 40...125°C */ + rgb.r = RGB_MAX_VALUE * (1 - ((float)temp - TEMP_HIGH_LIM)/(mMaxMeasuredTempCelsius - TEMP_HIGH_LIM)); + } + else if (temp > TEMP_LOW_LIM && temp < TEMP_HIGH_LIM) + { + uint8_t steps_in_part = (TEMP_HIGH_LIM - TEMP_LOW_LIM) / 4; + uint8_t step_num = temp % steps_in_part; + float step_val = (float)RGB_MAX_VALUE / steps_in_part; + + if (temp < steps_in_part) + { + /* Set Color Temperature Light in range 1...9°C */ + rgb.b = RGB_MAX_VALUE; + rgb.g = step_num * step_val; + } + else if (temp < 2 * steps_in_part) + { + /* Set Color Temperature Light in range 10...19°C */ + rgb.b = RGB_MAX_VALUE; + rgb.g = RGB_MAX_VALUE; + rgb.r = step_num * step_val; + } + else if (temp < 3 * steps_in_part) + { + /* Set Color Temperature Light in range 20...29°C */ + rgb.r = RGB_MAX_VALUE; + rgb.g = RGB_MAX_VALUE; + rgb.b = RGB_MAX_VALUE - (step_num * step_val) ; + } + else + { + /* Set Color Temperature Light in range 30...39°C */ + rgb.r = RGB_MAX_VALUE; + rgb.g = RGB_MAX_VALUE - (step_num * step_val) ; + } + } + else + { + LOG_ERR("Couldn't set the Color Temperature Light"); + } + + status = led_strip_update_rgb(ws2812_dev, &rgb, STRIP_NUM_PIXELS); + return status; +} +#endif // USE_COLOR_TEMPERATURE_LIGHT #endif // CONFIG_CHIP_USE_MARS_SENSOR diff --git a/src/platform/telink/tlsr9518adk80d_mars.conf b/src/platform/telink/tlsr9518adk80d_mars.conf index fda163eb6ad985..b4e1322e1fbdd2 100644 --- a/src/platform/telink/tlsr9518adk80d_mars.conf +++ b/src/platform/telink/tlsr9518adk80d_mars.conf @@ -6,10 +6,10 @@ CONFIG_I2C=y CONFIG_SENSOR=y # WS2812 -#CONFIG_LED_STRIP=y -#CONFIG_LED_STRIP_LOG_LEVEL_DBG=y -#CONFIG_WS2812_STRIP=y -#CONFIG_WS2812_STRIP_GPIO=y +CONFIG_LED_STRIP=y +CONFIG_LED_STRIP_LOG_LEVEL_DBG=y +CONFIG_WS2812_STRIP=y +CONFIG_WS2812_STRIP_GPIO_TELINK=y # DFU via USB CONFIG_USB_DEVICE_STACK=y From 6ef1ae8742c5004458e2e0ad711ce127c4603909 Mon Sep 17 00:00:00 2001 From: Serhii Salamakha Date: Tue, 14 Nov 2023 17:01:17 +0200 Subject: [PATCH 6/7] [Telink] Add WS2812Device util and update Lighting app for Mars board --- .../telink/CMakeLists.txt | 3 +- .../air-quality-sensor-app/telink/prj.conf | 2 +- examples/lighting-app/telink/CMakeLists.txt | 22 ++- .../lighting-app/telink/include/AppTask.h | 12 +- examples/lighting-app/telink/src/AppTask.cpp | 126 +++++++++++++----- .../lighting-app/telink/src/ZclCallbacks.cpp | 4 +- .../telink/common/include/AppTaskCommon.h | 4 + .../common/include/SensorManagerCommon.h | 7 +- .../telink/common/src/SensorManagerCommon.cpp | 43 ++---- .../telink/util/include/WS2812Device.h | 57 ++++++++ .../platform/telink/util/src/PWMDevice.cpp | 2 +- .../platform/telink/util/src/WS2812Device.cpp | 85 ++++++++++++ .../telink/CMakeLists.txt | 4 +- examples/thermostat/telink/CMakeLists.txt | 5 +- src/platform/telink/tlsr9518adk80d_mars.conf | 3 - 15 files changed, 301 insertions(+), 78 deletions(-) create mode 100644 examples/platform/telink/util/include/WS2812Device.h create mode 100644 examples/platform/telink/util/src/WS2812Device.cpp diff --git a/examples/air-quality-sensor-app/telink/CMakeLists.txt b/examples/air-quality-sensor-app/telink/CMakeLists.txt index a16d4299d35484..908bea9eb73a98 100644 --- a/examples/air-quality-sensor-app/telink/CMakeLists.txt +++ b/examples/air-quality-sensor-app/telink/CMakeLists.txt @@ -65,7 +65,7 @@ else() set(DTC_OVERLAY_FILE ${GLOBAL_DTC_OVERLAY_FILE} ${MARS_DTC_OVERLAY_FILE} ${FLASH_DTC_OVERLAY_FILE} ${LOCAL_DTC_OVERLAY_FILE}) endif() -set(CONF_FILE prj.conf ${MARS_CONF_OVERLAY_FILE}) +set(CONF_FILE ${MARS_CONF_OVERLAY_FILE} prj.conf) # Load NCS/Zephyr build system list(APPEND ZEPHYR_EXTRA_MODULES ${CHIP_ROOT}/config/telink/chip-module) @@ -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/air-quality-sensor-app/telink/prj.conf b/examples/air-quality-sensor-app/telink/prj.conf index f55000798a16cb..b43fbe9636d9aa 100644 --- a/examples/air-quality-sensor-app/telink/prj.conf +++ b/examples/air-quality-sensor-app/telink/prj.conf @@ -48,4 +48,4 @@ CONFIG_CHIP_FACTORY_DATA_BUILD=n CONFIG_CHIP_FACTORY_DATA_MERGE_WITH_FIRMWARE=n # Enable Power Management -CONFIG_PM=n +CONFIG_PM=y diff --git a/examples/lighting-app/telink/CMakeLists.txt b/examples/lighting-app/telink/CMakeLists.txt index e4dd1a4adc4094..2a7e46fc3d8076 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} ${MARS_CONF_OVERLAY_FILE} prj.conf) # 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..e409b2e2bc59de 100644 --- a/examples/temperature-measurement-app/telink/CMakeLists.txt +++ b/examples/temperature-measurement-app/telink/CMakeLists.txt @@ -63,7 +63,7 @@ else() set(DTC_OVERLAY_FILE ${GLOBAL_DTC_OVERLAY_FILE} ${MARS_DTC_OVERLAY_FILE} ${FLASH_DTC_OVERLAY_FILE} ${LOCAL_DTC_OVERLAY_FILE}) endif() -set(CONF_FILE prj.conf ${MARS_CONF_OVERLAY_FILE}) +set(CONF_FILE ${MARS_CONF_OVERLAY_FILE} prj.conf) # Load NCS/Zephyr build system list(APPEND ZEPHYR_EXTRA_MODULES ${CHIP_ROOT}/config/telink/chip-module) @@ -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..71f14895b28a55 100755 --- a/examples/thermostat/telink/CMakeLists.txt +++ b/examples/thermostat/telink/CMakeLists.txt @@ -63,7 +63,7 @@ else() set(DTC_OVERLAY_FILE ${GLOBAL_DTC_OVERLAY_FILE} ${MARS_DTC_OVERLAY_FILE} ${FLASH_DTC_OVERLAY_FILE} ${LOCAL_DTC_OVERLAY_FILE}) endif() -set(CONF_FILE prj.conf ${MARS_CONF_OVERLAY_FILE}) +set(CONF_FILE ${MARS_CONF_OVERLAY_FILE} prj.conf) # Load NCS/Zephyr build system list(APPEND ZEPHYR_EXTRA_MODULES ${CHIP_ROOT}/config/telink/chip-module) @@ -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 diff --git a/src/platform/telink/tlsr9518adk80d_mars.conf b/src/platform/telink/tlsr9518adk80d_mars.conf index b4e1322e1fbdd2..1b7b4a47378f6c 100644 --- a/src/platform/telink/tlsr9518adk80d_mars.conf +++ b/src/platform/telink/tlsr9518adk80d_mars.conf @@ -15,6 +15,3 @@ CONFIG_WS2812_STRIP_GPIO_TELINK=y CONFIG_USB_DEVICE_STACK=y CONFIG_USB_DEVICE_INITIALIZE_AT_BOOT=n CONFIG_USB_CDC_ACM_LOG_LEVEL_OFF=y - -# Mars sensors don't work in PM (through gpio_shutdown(GPIO_ALL) in Zephyr telink_b9x/soc.c) -CONFIG_PM=n \ No newline at end of file From a7ac9715ce3fb2c70d1dea82b9903d9f87d30007 Mon Sep 17 00:00:00 2001 From: "Restyled.io" Date: Tue, 14 Nov 2023 15:15:31 +0000 Subject: [PATCH 7/7] Restyled by clang-format --- examples/lighting-app/telink/src/AppTask.cpp | 14 +++--- .../common/include/SensorManagerCommon.h | 2 +- .../telink/common/src/SensorManagerCommon.cpp | 44 +++++++++---------- .../telink/util/include/WS2812Device.h | 10 ++--- .../platform/telink/util/src/WS2812Device.cpp | 8 ++-- .../telink/src/AppTask.cpp | 6 ++- 6 files changed, 43 insertions(+), 41 deletions(-) diff --git a/examples/lighting-app/telink/src/AppTask.cpp b/examples/lighting-app/telink/src/AppTask.cpp index f9c7f989628554..7dbb9e1aac2102 100644 --- a/examples/lighting-app/telink/src/AppTask.cpp +++ b/examples/lighting-app/telink/src/AppTask.cpp @@ -28,9 +28,9 @@ 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)); +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)); +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)); @@ -152,7 +152,8 @@ 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) || @@ -211,9 +212,8 @@ void AppTask::UpdateClusterState(void) #ifdef CONFIG_WS2812_STRIP isTurnedOn = sAppTask.mWS2812Device.IsTurnedOn(); #else - isTurnedOn = sAppTask.mPwmRgbRedLed.IsTurnedOn() - || sAppTask.mPwmRgbGreenLed.IsTurnedOn() - || sAppTask.mPwmRgbBlueLed.IsTurnedOn(); + isTurnedOn = + sAppTask.mPwmRgbRedLed.IsTurnedOn() || sAppTask.mPwmRgbGreenLed.IsTurnedOn() || sAppTask.mPwmRgbBlueLed.IsTurnedOn(); #endif // CONFIG_WS2812_STRIP if (sColorAction == PWMDevice::COLOR_ACTION_XY || sColorAction == PWMDevice::COLOR_ACTION_HSV || @@ -235,7 +235,7 @@ void AppTask::UpdateClusterState(void) } #else isTurnedOn = sAppTask.mPwmRgbBlueLed.IsTurnedOn(); - setLevel = sAppTask.mPwmRgbBlueLed.GetLevel(); + setLevel = sAppTask.mPwmRgbBlueLed.GetLevel(); #endif // CONFIG_WS2812_STRIP || USE_RGB_PWM // write the new on/off value diff --git a/examples/platform/telink/common/include/SensorManagerCommon.h b/examples/platform/telink/common/include/SensorManagerCommon.h index 8420391000472d..7181f8212a944c 100644 --- a/examples/platform/telink/common/include/SensorManagerCommon.h +++ b/examples/platform/telink/common/include/SensorManagerCommon.h @@ -34,7 +34,7 @@ class SensorManager { public: CHIP_ERROR Init(); - CHIP_ERROR GetTempAndHumMeasurValue(int16_t *pTempMeasured, uint16_t *pHumMeasured); + CHIP_ERROR GetTempAndHumMeasurValue(int16_t * pTempMeasured, uint16_t * pHumMeasured); int16_t GetMinMeasuredTempValue(); int16_t GetMaxMeasuredTempValue(); diff --git a/examples/platform/telink/common/src/SensorManagerCommon.cpp b/examples/platform/telink/common/src/SensorManagerCommon.cpp index 1c3b8a221919d8..1c4d5f9990a0cb 100644 --- a/examples/platform/telink/common/src/SensorManagerCommon.cpp +++ b/examples/platform/telink/common/src/SensorManagerCommon.cpp @@ -26,21 +26,21 @@ LOG_MODULE_DECLARE(app, CONFIG_CHIP_APP_LOG_LEVEL); using namespace chip; using namespace ::chip::DeviceLayer; -constexpr float kMinTempDelta = 0.5; // 0.5 degree Celsius +constexpr float kMinTempDelta = 0.5; // 0.5 degree Celsius #ifdef CONFIG_CHIP_USE_MARS_SENSOR k_timer sSensorBanForNextMeasurTimer; -volatile bool mSensorBanForNextMeasurFlag = false; +volatile bool mSensorBanForNextMeasurFlag = false; constexpr uint16_t kSensorBanForNextMeasurTimeout = 1000; // 1s timeout -const struct device *const sht3xd_dev = DEVICE_DT_GET_ONE(sensirion_sht3xd); +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)); +const struct device * const ws2812_dev = DEVICE_DT_GET(DT_ALIAS(led_strip)); -#define TEMP_LOW_LIM 0 // °C -#define TEMP_HIGH_LIM 40 // °C -#endif // USE_COLOR_TEMPERATURE_LIGHT +#define TEMP_LOW_LIM 0 // °C +#define TEMP_HIGH_LIM 40 // °C +#endif // USE_COLOR_TEMPERATURE_LIGHT #else constexpr float kSimulatedHum = 55.5; // percents constexpr uint16_t kSimulatedReadingFrequency = 4; // change simulated number @@ -56,10 +56,10 @@ CHIP_ERROR SensorManager::Init() { LOG_ERR("Device %s is not ready", sht3xd_dev->name); return CHIP_ERROR_INCORRECT_STATE; - } + } #ifdef USE_COLOR_TEMPERATURE_LIGHT - RgbColor_t rgb = {0}; + RgbColor_t rgb = { 0 }; CHIP_ERROR err = sSensorManager.mWS2812Device.Init(ws2812_dev, STRIP_NUM_PIXELS(led_strip)); if (err != CHIP_NO_ERROR) @@ -80,15 +80,15 @@ CHIP_ERROR SensorManager::Init() return CHIP_NO_ERROR; } -CHIP_ERROR SensorManager::GetTempAndHumMeasurValue(int16_t *pTempMeasured, uint16_t *pHumMeasured) +CHIP_ERROR SensorManager::GetTempAndHumMeasurValue(int16_t * pTempMeasured, uint16_t * pHumMeasured) { static float lastTemp = 0.0; float temp = 0.0; float hum = 0.0; #ifdef CONFIG_CHIP_USE_MARS_SENSOR - static struct sensor_value sensorTemp = {0}; - static struct sensor_value sensorHum = {0}; + static struct sensor_value sensorTemp = { 0 }; + static struct sensor_value sensorHum = { 0 }; if (!mSensorBanForNextMeasurFlag) { @@ -119,8 +119,8 @@ CHIP_ERROR SensorManager::GetTempAndHumMeasurValue(int16_t *pTempMeasured, uint1 k_timer_start(&sSensorBanForNextMeasurTimer, K_MSEC(kSensorBanForNextMeasurTimeout), K_NO_WAIT); } - temp = (float)sensor_value_to_double(&sensorTemp); - hum = (float)sensor_value_to_double(&sensorHum); + temp = (float) sensor_value_to_double(&sensorTemp); + hum = (float) sensor_value_to_double(&sensorHum); #ifdef USE_COLOR_TEMPERATURE_LIGHT SetColorTemperatureLight(temp); @@ -165,7 +165,7 @@ CHIP_ERROR SensorManager::GetTempAndHumMeasurValue(int16_t *pTempMeasured, uint1 if (pHumMeasured != NULL) { - *pHumMeasured = (uint16_t) hum; + *pHumMeasured = (uint16_t) hum; } return CHIP_NO_ERROR; @@ -195,23 +195,23 @@ void SensorManager::SensorBanForNextMeasurTimerTimeoutCallback(k_timer * timer) #ifdef USE_COLOR_TEMPERATURE_LIGHT void SensorManager::SetColorTemperatureLight(int8_t temp) { - RgbColor_t rgb = {0}; + RgbColor_t rgb = { 0 }; if (temp >= mMinMeasuredTempCelsius && temp <= TEMP_LOW_LIM) { /* Set Color Temperature Light in range -40...0°C */ - rgb.b = RGB_MAX_VALUE * (1 - ((float)temp - TEMP_LOW_LIM)/(mMinMeasuredTempCelsius - TEMP_LOW_LIM)); + rgb.b = RGB_MAX_VALUE * (1 - ((float) temp - TEMP_LOW_LIM) / (mMinMeasuredTempCelsius - TEMP_LOW_LIM)); } else if (temp >= TEMP_HIGH_LIM && temp <= mMaxMeasuredTempCelsius) { /* Set Color Temperature Light in range 40...125°C */ - rgb.r = RGB_MAX_VALUE * (1 - ((float)temp - TEMP_HIGH_LIM)/(mMaxMeasuredTempCelsius - TEMP_HIGH_LIM)); + rgb.r = RGB_MAX_VALUE * (1 - ((float) temp - TEMP_HIGH_LIM) / (mMaxMeasuredTempCelsius - TEMP_HIGH_LIM)); } else if (temp > TEMP_LOW_LIM && temp < TEMP_HIGH_LIM) { uint8_t steps_in_part = (TEMP_HIGH_LIM - TEMP_LOW_LIM) / 4; - uint8_t step_num = temp % steps_in_part; - float step_val = (float)RGB_MAX_VALUE / steps_in_part; + uint8_t step_num = temp % steps_in_part; + float step_val = (float) RGB_MAX_VALUE / steps_in_part; if (temp < steps_in_part) { @@ -231,13 +231,13 @@ void SensorManager::SetColorTemperatureLight(int8_t temp) /* Set Color Temperature Light in range 20...29°C */ rgb.r = RGB_MAX_VALUE; rgb.g = RGB_MAX_VALUE; - rgb.b = RGB_MAX_VALUE - (step_num * step_val) ; + rgb.b = RGB_MAX_VALUE - (step_num * step_val); } else { /* Set Color Temperature Light in range 30...39°C */ rgb.r = RGB_MAX_VALUE; - rgb.g = RGB_MAX_VALUE - (step_num * step_val) ; + rgb.g = RGB_MAX_VALUE - (step_num * step_val); } } else diff --git a/examples/platform/telink/util/include/WS2812Device.h b/examples/platform/telink/util/include/WS2812Device.h index 24351db7836115..261802bde5f5be 100644 --- a/examples/platform/telink/util/include/WS2812Device.h +++ b/examples/platform/telink/util/include/WS2812Device.h @@ -23,12 +23,12 @@ #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 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 +#define SET_RGB_TURN_OFF 0 +#define SET_RGB_TURN_ON 1 class WS2812Device { diff --git a/examples/platform/telink/util/src/WS2812Device.cpp b/examples/platform/telink/util/src/WS2812Device.cpp index 3ff00a6ba85ab7..06b70373f8ff29 100644 --- a/examples/platform/telink/util/src/WS2812Device.cpp +++ b/examples/platform/telink/util/src/WS2812Device.cpp @@ -29,15 +29,15 @@ using namespace chip; CHIP_ERROR WS2812Device::Init(const struct device * ws2812Device, uint32_t aChainLength) { mWs2812Device = ws2812Device; - mChainLength = aChainLength; - mState = kRgbState_Off; + 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(); @@ -47,7 +47,7 @@ CHIP_ERROR WS2812Device::Init(const struct device * ws2812Device, uint32_t aChai void WS2812Device::UpdateRgbLight() { int status; - led_rgb setRgb = {0}; + led_rgb setRgb = { 0 }; if (mState == kRgbState_On) { diff --git a/examples/temperature-measurement-app/telink/src/AppTask.cpp b/examples/temperature-measurement-app/telink/src/AppTask.cpp index f2fecad01c8981..df1c5f6dc20e2d 100644 --- a/examples/temperature-measurement-app/telink/src/AppTask.cpp +++ b/examples/temperature-measurement-app/telink/src/AppTask.cpp @@ -47,8 +47,10 @@ CHIP_ERROR AppTask::Init(void) k_timer_start(&sTemperatureMeasurementUpdateTimer, K_MSEC(kTemperatureMeasurementUpdateTimerPeriodMs), K_NO_WAIT); PlatformMgr().LockChipStack(); - app::Clusters::TemperatureMeasurement::Attributes::MinMeasuredValue::Set(kExampleEndpointId, SensorMgr().GetMinMeasuredTempValue()); - app::Clusters::TemperatureMeasurement::Attributes::MaxMeasuredValue::Set(kExampleEndpointId, SensorMgr().GetMaxMeasuredTempValue()); + app::Clusters::TemperatureMeasurement::Attributes::MinMeasuredValue::Set(kExampleEndpointId, + SensorMgr().GetMinMeasuredTempValue()); + app::Clusters::TemperatureMeasurement::Attributes::MaxMeasuredValue::Set(kExampleEndpointId, + SensorMgr().GetMaxMeasuredTempValue()); PlatformMgr().UnlockChipStack(); return CHIP_NO_ERROR;