apix-pwm-example: make a more visual PWM change on LED

When running this example on an LED-connected GPIO, the delay
of 1 second is too large, and the selected values not very good
to notice the difference in brightness.

Use a ramp array of values with a more visual difference when
running on an LED and shorten the wait between changes.

Signed-off-by: Hector Palacios <hector.palacios@digi.com>
This commit is contained in:
Hector Palacios 2021-06-08 16:23:51 +02:00
parent 8d41c0b7ee
commit c8257ea56e
1 changed files with 8 additions and 11 deletions

View File

@ -139,8 +139,11 @@ static int parse_argument(char *argv, int arg_type)
int main(int argc, char **argv) int main(int argc, char **argv)
{ {
int ret, duty_cycle = 10, ascending = 1, pwm_chip, pwm_channel, pwm_freq = 0; int ret, ascending = 1, pwm_chip, pwm_channel, pwm_freq = 0;
char *name = basename(argv[0]); char *name = basename(argv[0]);
int duty_cycle[] = {0, 1, 3, 5, 10, 15, 20, 30, 50, 100};
int d = 0;
int levels = sizeof(duty_cycle) / sizeof(duty_cycle[0]);
/* Check if the PWM values are passed in the command line. */ /* Check if the PWM values are passed in the command line. */
if (argc == 1) { if (argc == 1) {
@ -211,25 +214,19 @@ int main(int argc, char **argv)
/* Main application loop.*/ /* Main application loop.*/
while (running) { while (running) {
/* Set the duty cycle. */ /* Set the duty cycle. */
ret = ldx_pwm_set_duty_cycle_percentage(pwm_line, duty_cycle); ret = ldx_pwm_set_duty_cycle_percentage(pwm_line, duty_cycle[d]);
if (ret != PWM_CONFIG_ERROR_NONE) { if (ret != PWM_CONFIG_ERROR_NONE) {
printf("Failed to set the duty cycle\n"); printf("Failed to set the duty cycle\n");
return EXIT_FAILURE; return EXIT_FAILURE;
} }
if (ascending) { if (ascending) {
duty_cycle += 10; if (++d == levels - 1)
if (duty_cycle == 100) {
duty_cycle = 80;
ascending = 0; ascending = 0;
}
} else { } else {
duty_cycle -= 10; if (--d == 0)
if (duty_cycle == 0) {
duty_cycle = 20;
ascending = 1; ascending = 1;
} }
} usleep(150000);
sleep(1);
} }
return EXIT_SUCCESS; return EXIT_SUCCESS;