Skip to content

Commit 3542062

Browse files
committed
Integrate green LED with sample of BACnet Lighting Device profile. Added tracking value command, and tracking value and override and out-of-service flags to bacnet light shell commands.
1 parent 7fd3463 commit 3542062

File tree

4 files changed

+185
-101
lines changed

4 files changed

+185
-101
lines changed

zephyr/samples/profiles/b-ld/boards/nucleo_f429zi.overlay

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,33 @@
66

77
/delete-node/ &storage_partition;
88

9+
/ {
10+
leds {
11+
status = "disabled";
12+
};
13+
14+
pwmleds {
15+
compatible = "pwm-leds";
16+
green_pwm_led: green_pwm_led {
17+
pwms = <&pwm3 3 PWM_MSEC(20) PWM_POLARITY_NORMAL>;
18+
};
19+
};
20+
aliases {
21+
pwm-led0 = &green_pwm_led;
22+
};
23+
};
24+
25+
&timers3 {
26+
status = "okay";
27+
st,prescaler = <64>;
28+
/* User LD1 GREEN */
29+
pwm3: pwm {
30+
status = "okay";
31+
pinctrl-0 = <&tim3_ch3_pb0>;
32+
pinctrl-names = "default";
33+
};
34+
};
35+
936
&flash0 {
1037
partitions {
1138
/* Set 48KB of storage at the beginning of bank2 in order to have 3 sectors smaller than 32K

zephyr/samples/profiles/b-ld/prj.conf

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ CONFIG_INIT_STACKS=y
2222

2323
# BACnet Library
2424
CONFIG_BACNETSTACK=y
25-
CONFIG_BACNETSTACK_LOG_LEVEL=3
2625
# BACnet Library - shell
2726
CONFIG_BACNETSTACK_BACNET_SHELL=y
2827
CONFIG_BACNETSTACK_BACNET_BASIC_SHELL=y
@@ -139,3 +138,7 @@ CONFIG_SETTINGS_RUNTIME=y
139138

140139
CONFIG_TEST_RANDOM_GENERATOR=y
141140
CONFIG_TIMER_RANDOM_GENERATOR=y
141+
142+
# Lighting output controlling PWM LEDs
143+
CONFIG_PWM=y
144+
CONFIG_LED=y

zephyr/samples/profiles/b-ld/src/main.c

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,11 @@
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 */
3943
static 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,41 @@ static const uint32_t Lighting_Instance = 1;
4655
void 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);
6793
}
6894

6995
/**

0 commit comments

Comments
 (0)