zephyr/samples/basic/fade_led/src/main.c
Keith Packard 0b90fd5adf samples, tests, boards: Switch main return type from void to int
As both C and C++ standards require applications running under an OS to
return 'int', adapt that for Zephyr to align with those standard. This also
eliminates errors when building with clang when not using -ffreestanding,
and reduces the need for compiler flags to silence warnings for both clang
and gcc.

Most of these changes were automated using coccinelle with the following
script:

@@
@@
- void
+ int
main(...) {
	...
-	return;
+	return 0;
	...
}

Approximately 40 files had to be edited by hand as coccinelle was unable to
fix them.

Signed-off-by: Keith Packard <keithp@keithp.com>
2023-04-14 07:49:41 +09:00

63 lines
1.2 KiB
C

/*
* 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 (!device_is_ready(pwm_led0.dev)) {
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;
}
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;
}