Skip to content

samples: fade_led: Refactor LED fade logic #93744

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 20 additions & 15 deletions samples/basic/fade_led/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,25 @@ static const struct pwm_dt_spec pwm_leds[] = {LISTIFY(MAX_LEDS, PWM_LED, ())};
#define NUM_STEPS 50U
#define SLEEP_MSEC 25U

static void update_fade_state(size_t i, uint32_t *pulse_widths, uint32_t *steps, uint8_t *dirs)
{
if (dirs[i] == 1) {
if (pulse_widths[i] + steps[i] >= pwm_leds[i].period) {
pulse_widths[i] = pwm_leds[i].period;
dirs[i] = 0U;
} else {
pulse_widths[i] += steps[i];
}
} else {
if (pulse_widths[i] <= steps[i]) {
pulse_widths[i] = 0;
dirs[i] = 1U;
} else {
pulse_widths[i] -= steps[i];
}
}
}

int main(void)
{
uint32_t pulse_widths[ARRAY_SIZE(pwm_leds)];
Expand Down Expand Up @@ -52,21 +71,7 @@ int main(void)
printk("LED %d: Using pulse width %d%%\n", i,
100 * pulse_widths[i] / pwm_leds[i].period);

if (dirs[i] == 1) {
if (pulse_widths[i] + steps[i] >= pwm_leds[i].period) {
pulse_widths[i] = pwm_leds[i].period;
dirs[i] = 0U;
} else {
pulse_widths[i] += steps[i];
}
} else {
if (pulse_widths[i] <= steps[i]) {
pulse_widths[i] = 0;
dirs[i] = 1U;
} else {
pulse_widths[i] -= steps[i];
}
}
update_fade_state(i, pulse_widths, steps, dirs);
}

k_sleep(K_MSEC(SLEEP_MSEC));
Expand Down