Commit a5fd92b0 authored by Yishai Jaffe's avatar Yishai Jaffe Committed by Benjamin Cabé
Browse files

samples: fade_led: use all available LEDs

Refactored the sample code to use all available PWM LEDs

Signed-off-by: default avatarYishai Jaffe <yishai1999@gmail.com>
parent 02b2d556
Loading
Loading
Loading
Loading
+29 −18
Changes for samples/basic/fade_led/src/main.c: 29 added lines, 18 removed lines.
Original line number Diff line number Diff line
@@ -14,46 +14,57 @@
#include <zephyr/device.h>
#include <zephyr/drivers/pwm.h>

static const struct pwm_dt_spec pwm_led0 = PWM_DT_SPEC_GET(DT_ALIAS(pwm_led0));
#define PWM_LED_ALIAS(i) DT_ALIAS(_CONCAT(pwm_led, i))
#define PWM_LED(i, _)                                                                              \
	IF_ENABLED(DT_NODE_EXISTS(PWM_LED_ALIAS(i)), (PWM_DT_SPEC_GET(PWM_LED_ALIAS(i)),))

#define MAX_LEDS 10
static const struct pwm_dt_spec pwm_leds[] = {LISTIFY(MAX_LEDS, PWM_LED, ())};

#define NUM_STEPS	50U
#define SLEEP_MSEC	25U

int main(void)
{
	uint32_t pulse_width = 0U;
	uint32_t step = pwm_led0.period / NUM_STEPS;
	uint32_t pulse_widths[ARRAY_SIZE(pwm_leds)];
	uint32_t steps[ARRAY_SIZE(pwm_leds)];
	uint8_t dir = 1U;
	int ret;

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

	if (!pwm_is_ready_dt(&pwm_led0)) {
		printk("Error: PWM device %s is not ready\n",
		       pwm_led0.dev->name);
	for (size_t i = 0; i < ARRAY_SIZE(pwm_leds); i++) {
		pulse_widths[i] = 0;
		steps[i] = pwm_leds[i].period / NUM_STEPS;
		if (!pwm_is_ready_dt(&pwm_leds[i])) {
			printk("Error: PWM device %s is not ready\n", pwm_leds[i].dev->name);
			return 0;
		}
	}

	while (1) {
		ret = pwm_set_pulse_dt(&pwm_led0, pulse_width);
		for (size_t i = 0; i < ARRAY_SIZE(pwm_leds); i++) {
			ret = pwm_set_pulse_dt(&pwm_leds[i], pulse_widths[i]);
			if (ret) {
			printk("Error %d: failed to set pulse width\n", ret);
			return 0;
				printk("Error %d: failed to set pulse width for LED %d\n", ret, i);
			}
		printk("Using pulse width %d%%\n", 100 * pulse_width / pwm_led0.period);
			printk("LED %d: Using pulse width %d%%\n", i,
			       100 * pulse_widths[i] / pwm_leds[i].period);

			if (dir) {
			pulse_width += step;
			if (pulse_width >= pwm_led0.period) {
				pulse_width = pwm_led0.period - step;
				if (pulse_widths[i] + steps[i] >= pwm_leds[i].period) {
					pulse_widths[i] = pwm_leds[i].period;
					dir = 0U;
			}
				} else {
			if (pulse_width >= step) {
				pulse_width -= step;
					pulse_widths[i] += steps[i];
				}
			} else {
				pulse_width = step;
				if (pulse_widths[i] <= steps[i]) {
					pulse_widths[i] = 0;
					dir = 1U;
				} else {
					pulse_widths[i] -= steps[i];
				}
			}
		}