Skip to content

Commit a75ed26

Browse files
yishai1999kartben
authored andcommitted
samples: led: pwm: add decreasing brightness logic
Add a decreasing brightness phase to the sample's logic. Signed-off-by: Yishai Jaffe <[email protected]>
1 parent 7260f2d commit a75ed26

File tree

3 files changed

+15
-1
lines changed

3 files changed

+15
-1
lines changed

samples/drivers/led/pwm/README.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ For each PWM LEDs (one after the other):
2020
- Turning on
2121
- Turning off
2222
- Increasing brightness gradually
23+
- Decreasing brightness gradually
2324
- Blinking on: 0.1 sec, off: 0.1 sec
2425
- Blinking on: 1 sec, off: 1 sec
2526
- Turning off

samples/drivers/led/pwm/sample.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ tests:
1717
- "Turned on"
1818
- "Turned off"
1919
- "Increasing brightness gradually"
20+
- "Decreasing brightness gradually"
2021
- "Blinking on: ([0-9]+) msec, off: ([0-9]+) msec"
2122
- "(Blinking on: ([0-9]+) msec, off: ([0-9]+) msec|Cycle period not supported)"
2223
- "Turned off, loop end"

samples/drivers/led/pwm/src/main.c

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ const int num_leds = ARRAY_SIZE(led_label);
3333
static void run_led_test(const struct device *led_pwm, uint8_t led)
3434
{
3535
int err;
36-
uint16_t level;
36+
int16_t level;
3737

3838
LOG_INF("Testing LED %d - %s", led, led_label[led] ? : "no label");
3939

@@ -67,6 +67,18 @@ static void run_led_test(const struct device *led_pwm, uint8_t led)
6767
}
6868
k_sleep(K_MSEC(1000));
6969

70+
/* Decrease LED brightness gradually down to the minimum level. */
71+
LOG_INF(" Decreasing brightness gradually");
72+
for (level = MAX_BRIGHTNESS; level >= 0; level--) {
73+
err = led_set_brightness(led_pwm, led, level);
74+
if (err < 0) {
75+
LOG_ERR("err=%d brightness=%d\n", err, level);
76+
return;
77+
}
78+
k_sleep(K_MSEC(CONFIG_FADE_DELAY));
79+
}
80+
k_sleep(K_MSEC(1000));
81+
7082
#if CONFIG_BLINK_DELAY_SHORT > 0
7183
/* Start LED blinking (short cycle) */
7284
err = led_blink(led_pwm, led, CONFIG_BLINK_DELAY_SHORT, CONFIG_BLINK_DELAY_SHORT);

0 commit comments

Comments
 (0)