Skip to content

Commit 3b45fee

Browse files
yishai1999danieldegrasse
authored andcommitted
samples: fade_led: add direction per LED
Fixed a bug where that was caused because the different LEDs use the same 'dir' variable and that caused them to eventually lose synchronization. Signed-off-by: Yishai Jaffe <yishai1999@gmail.com>
1 parent 8d88d02 commit 3b45fee

File tree

1 file changed

+5
-4
lines changed
  • samples/basic/fade_led/src

1 file changed

+5
-4
lines changed

samples/basic/fade_led/src/main.c

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,15 @@ int main(void)
2828
{
2929
uint32_t pulse_widths[ARRAY_SIZE(pwm_leds)];
3030
uint32_t steps[ARRAY_SIZE(pwm_leds)];
31-
uint8_t dir = 1U;
31+
uint8_t dirs[ARRAY_SIZE(pwm_leds)];
3232
int ret;
3333

3434
printk("PWM-based LED fade. Found %d LEDs\n", ARRAY_SIZE(pwm_leds));
3535

3636
for (size_t i = 0; i < ARRAY_SIZE(pwm_leds); i++) {
3737
pulse_widths[i] = 0;
3838
steps[i] = pwm_leds[i].period / NUM_STEPS;
39+
dirs[i] = 1U;
3940
if (!pwm_is_ready_dt(&pwm_leds[i])) {
4041
printk("Error: PWM device %s is not ready\n", pwm_leds[i].dev->name);
4142
return 0;
@@ -51,17 +52,17 @@ int main(void)
5152
printk("LED %d: Using pulse width %d%%\n", i,
5253
100 * pulse_widths[i] / pwm_leds[i].period);
5354

54-
if (dir) {
55+
if (dirs[i] == 1) {
5556
if (pulse_widths[i] + steps[i] >= pwm_leds[i].period) {
5657
pulse_widths[i] = pwm_leds[i].period;
57-
dir = 0U;
58+
dirs[i] = 0U;
5859
} else {
5960
pulse_widths[i] += steps[i];
6061
}
6162
} else {
6263
if (pulse_widths[i] <= steps[i]) {
6364
pulse_widths[i] = 0;
64-
dir = 1U;
65+
dirs[i] = 1U;
6566
} else {
6667
pulse_widths[i] -= steps[i];
6768
}

0 commit comments

Comments
 (0)