blob: c6a49a13a12221594d122e2415938003ceba4b84 [file] [log] [blame]
/*
* Copyright (c) 2016 Intel Corporation
* Copyright (c) 2020 Nordic Semiconductor ASA
*
* SPDX-License-Identifier: Apache-2.0
*/
/**
* @file Sample app to demonstrate PWM-based LED fade
*/
#include <zephyr/kernel.h>
#include <zephyr/sys/printk.h>
#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 NUM_STEPS 50U
#define SLEEP_MSEC 25U
int main(void)
{
uint32_t pulse_width = 0U;
uint32_t step = pwm_led0.period / NUM_STEPS;
uint8_t dir = 1U;
int ret;
printk("PWM-based LED fade\n");
if (!pwm_is_ready_dt(&pwm_led0)) {
printk("Error: PWM device %s is not ready\n",
pwm_led0.dev->name);
return 0;
}
while (1) {
ret = pwm_set_pulse_dt(&pwm_led0, pulse_width);
if (ret) {
printk("Error %d: failed to set pulse width\n", ret);
return 0;
}
printk("Using pulse width %d%%\n", 100 * pulse_width / pwm_led0.period);
if (dir) {
pulse_width += step;
if (pulse_width >= pwm_led0.period) {
pulse_width = pwm_led0.period - step;
dir = 0U;
}
} else {
if (pulse_width >= step) {
pulse_width -= step;
} else {
pulse_width = step;
dir = 1U;
}
}
k_sleep(K_MSEC(SLEEP_MSEC));
}
return 0;
}