gpiocmds: Change MAX_SOFT_PWM from 255 to 256

Change the range of values used for software PWM to avoid doing an
integer division in the main code.

Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
This commit is contained in:
Kevin O'Connor 2017-05-07 17:48:23 -04:00
parent 5c4cc0d646
commit c292006421
1 changed files with 5 additions and 6 deletions

View File

@ -90,14 +90,14 @@ DECL_COMMAND(command_set_digital_out, "set_digital_out pin=%u value=%c");
* Soft PWM output pins
****************************************************************/
#define MAX_SOFT_PWM 255
#define MAX_SOFT_PWM 256
DECL_CONSTANT(SOFT_PWM_MAX, MAX_SOFT_PWM);
struct soft_pwm_s {
struct timer timer;
uint32_t on_duration, off_duration, end_time;
uint32_t next_on_duration, next_off_duration;
uint32_t max_duration, cycle_time, pulse_time;
uint32_t max_duration, cycle_time;
struct gpio_out pin;
uint8_t default_value, flags;
};
@ -162,7 +162,6 @@ command_config_soft_pwm_out(uint32_t *args)
struct soft_pwm_s *s = oid_alloc(args[0], command_config_soft_pwm_out
, sizeof(*s));
s->cycle_time = args[2];
s->pulse_time = s->cycle_time / MAX_SOFT_PWM;
s->default_value = !!args[3];
s->max_duration = args[4];
s->flags = s->default_value ? SPF_ON : 0;
@ -177,16 +176,16 @@ command_schedule_soft_pwm_out(uint32_t *args)
{
struct soft_pwm_s *s = oid_lookup(args[0], command_config_soft_pwm_out);
uint32_t time = args[1];
uint8_t value = args[2];
uint16_t value = args[2];
uint8_t next_flags = SPF_CHECK_END | SPF_HAVE_NEXT;
uint32_t next_on_duration, next_off_duration;
if (value == 0 || value == MAX_SOFT_PWM) {
if (value == 0 || value >= MAX_SOFT_PWM) {
next_on_duration = next_off_duration = 0;
next_flags |= value ? SPF_NEXT_ON : 0;
if (!!value != s->default_value && s->max_duration)
next_flags |= SPF_NEXT_CHECK_END;
} else {
next_on_duration = s->pulse_time * value;
next_on_duration = (s->cycle_time / MAX_SOFT_PWM) * value;
next_off_duration = s->cycle_time - next_on_duration;
next_flags |= SPF_NEXT_ON | SPF_NEXT_TOGGLING;
if (s->max_duration)