66 * @copyright SPDX-License-Identifier: Apache-2.0
77 */
88#include <zephyr/kernel.h>
9+ #include <zephyr/device.h>
10+ #include <zephyr/devicetree.h>
11+ #include <zephyr/drivers/led.h>
912#include <zephyr/random/random.h>
13+ #include <zephyr/sys/util.h>
1014#include <stdint.h>
1115#include <stdlib.h>
1216/* BACnet Stack defines - first */
@@ -38,6 +42,11 @@ static const uint32_t Device_Instance = 260125;
3842/* object instances */
3943static const uint32_t Lighting_Instance = 1 ;
4044
45+ #define LED_PWM_NODE_ID DT_COMPAT_GET_ANY_STATUS_OKAY(pwm_leds)
46+ const char * led_label [] = { DT_FOREACH_CHILD_SEP_VARGS (
47+ LED_PWM_NODE_ID , DT_PROP_OR , (, ), label , NULL ) };
48+ const int num_leds = ARRAY_SIZE (led_label );
49+
4150/**
4251 * @brief BACnet Lighting Output tracking value handler
4352 * @param object-instance [in] The object-instance number of the object
@@ -46,24 +55,46 @@ static const uint32_t Lighting_Instance = 1;
4655void BACnet_Lighting_Output_Tracking_Value_Handler (
4756 uint32_t object_instance , float old_value , float value )
4857{
49- uint16_t steps = 0 ;
58+ uint8_t steps = 0 ;
59+ int err ;
60+ uint8_t led ;
61+ const struct device * led_pwm ;
5062
5163 (void )old_value ;
5264 if (object_instance != Lighting_Instance ) {
5365 return ;
5466 }
5567 /* Tracking value are 0.0 and 1.0-100.0 normalized */
5668 if (isgreaterequal (value , 1.0 ) && islessequal (value , 100.0 )) {
57- steps = linear_interpolate (1.0 , value , 100.0 , 1 , UINT16_MAX );
69+ steps = ( uint8_t ) linear_interpolate (1.0 , value , 100.0 , 1 , 100.0 );
5870 } else if (isgreater (value , 100.0 )) {
59- steps = UINT16_MAX ;
71+ steps = 100 ;
6072 } else {
6173 steps = 0 ;
6274 }
6375 LOG_INF (
6476 "Lighting Output[%lu]: value=%f step=%u/%u" ,
6577 (unsigned long )object_instance , (double )value , (unsigned )steps ,
66- (unsigned )UINT16_MAX );
78+ (unsigned )UINT8_MAX );
79+ /* hardware control */
80+ led_pwm = DEVICE_DT_GET (LED_PWM_NODE_ID );
81+ if (!device_is_ready (led_pwm )) {
82+ LOG_ERR ("Device %s is not ready" , led_pwm -> name );
83+ return ;
84+ }
85+ if (!num_leds ) {
86+ LOG_ERR ("No LEDs found for %s" , led_pwm -> name );
87+ return ;
88+ }
89+ if (object_instance > 0U ) {
90+ led = object_instance - 1U ;
91+ }
92+ err = led_set_brightness (led_pwm , led , steps );
93+ if (err < 0 ) {
94+ LOG_ERR (
95+ "Failed to set brightness of LED %u to %u: %d" , (unsigned )led ,
96+ (unsigned )steps , err );
97+ }
6798}
6899
69100/**
0 commit comments