pca9685: Support default values

Allow the pwm pin to have a non-zero default value.

Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
This commit is contained in:
Kevin O'Connor 2017-10-11 23:18:33 -04:00
parent db97f36631
commit 3c4d14bfa9
2 changed files with 46 additions and 25 deletions

View File

@ -123,6 +123,7 @@ class pca9685_pwm:
self._max_duration = 2. self._max_duration = 2.
self._oid = None self._oid = None
self._invert = pin_params['invert'] self._invert = pin_params['invert']
self._shutdown_value = float(self._invert)
self._last_clock = 0 self._last_clock = 0
self._pwm_max = 0. self._pwm_max = 0.
self._cmd_queue = self._mcu.alloc_command_queue() self._cmd_queue = self._mcu.alloc_command_queue()
@ -141,6 +142,12 @@ class pca9685_pwm:
if self._invert: if self._invert:
value = 1. - value value = 1. - value
self._static_value = max(0., min(1., value)) self._static_value = max(0., min(1., value))
def setup_shutdown_value(self, value):
if self._invert:
value = 1. - value
self._shutdown_value = max(0., min(1., value))
if self._shutdown_value:
self._replicape.note_enable_on_shutdown()
def build_config(self): def build_config(self):
self._pwm_max = self._mcu.get_constant_float("PCA9685_MAX") self._pwm_max = self._mcu.get_constant_float("PCA9685_MAX")
cycle_ticks = self._mcu.seconds_to_clock(self._cycle_time) cycle_ticks = self._mcu.seconds_to_clock(self._cycle_time)
@ -154,10 +161,12 @@ class pca9685_pwm:
return return
self._oid = self._mcu.create_oid() self._oid = self._mcu.create_oid()
self._mcu.add_config_cmd( self._mcu.add_config_cmd(
"config_pca9685 oid=%d bus=%d addr=%d channel=%d" "config_pca9685 oid=%d bus=%d addr=%d channel=%d cycle_ticks=%d"
" cycle_ticks=%d max_duration=%d" % ( " value=%d default_value=%d max_duration=%d" % (
self._oid, self._bus, self._address, self._channel, self._oid, self._bus, self._address, self._channel, cycle_ticks,
cycle_ticks, self._mcu.seconds_to_clock(self._max_duration))) self._invert * self._pwm_max,
self._shutdown_value * self._pwm_max,
self._mcu.seconds_to_clock(self._max_duration)))
self._set_cmd = self._mcu.lookup_command( self._set_cmd = self._mcu.lookup_command(
"schedule_pca9685_out oid=%c clock=%u value=%hu") "schedule_pca9685_out oid=%c clock=%u value=%hu")
def set_pwm(self, print_time, value): def set_pwm(self, print_time, value):
@ -249,6 +258,8 @@ class Replicape:
self.host_mcu.add_config_cmd("send_spi bus=%d dev=%d msg=%s" % ( self.host_mcu.add_config_cmd("send_spi bus=%d dev=%d msg=%s" % (
REPLICAPE_SHIFT_REGISTER_BUS, REPLICAPE_SHIFT_REGISTER_DEVICE, REPLICAPE_SHIFT_REGISTER_BUS, REPLICAPE_SHIFT_REGISTER_DEVICE,
"".join(["%02x" % (x,) for x in shift_registers]))) "".join(["%02x" % (x,) for x in shift_registers])))
def note_enable_on_shutdown(self):
self.mcu_enable.setup_shutdown_value(1)
def note_enable(self, print_time, channel, is_enable): def note_enable(self, print_time, channel, is_enable):
if is_enable: if is_enable:
is_off = not self.enabled_channels is_off = not self.enabled_channels

View File

@ -118,15 +118,6 @@ open_i2c(uint8_t bus, uint8_t addr, uint32_t cycle_ticks)
return fd; return fd;
} }
void
pca9685_shutdown(void)
{
int i;
for (i=0; i<devices_count; i++)
pca9685_write(devices[i].fd, CHANNEL_ALL, 0);
}
DECL_SHUTDOWN(pca9685_shutdown);
/**************************************************************** /****************************************************************
* Command interface * Command interface
@ -136,7 +127,7 @@ struct i2cpwm_s {
struct timer timer; struct timer timer;
int fd; int fd;
uint8_t channel; uint8_t channel;
uint16_t value; uint16_t value, default_value;
uint32_t max_duration; uint32_t max_duration;
}; };
@ -153,7 +144,7 @@ pca9685_event(struct timer *timer)
{ {
struct i2cpwm_s *p = container_of(timer, struct i2cpwm_s, timer); struct i2cpwm_s *p = container_of(timer, struct i2cpwm_s, timer);
pca9685_write(p->fd, p->channel, p->value); pca9685_write(p->fd, p->channel, p->value);
if (!p->value || !p->max_duration) if (p->value == p->default_value || !p->max_duration)
return SF_DONE; return SF_DONE;
p->timer.waketime += p->max_duration; p->timer.waketime += p->max_duration;
p->timer.func = pca9685_end_event; p->timer.func = pca9685_end_event;
@ -163,18 +154,22 @@ pca9685_event(struct timer *timer)
void void
command_config_pca9685(uint32_t *args) command_config_pca9685(uint32_t *args)
{ {
struct i2cpwm_s *p = oid_alloc(args[0], command_config_pca9685 uint8_t bus = args[1], addr = args[2], channel = args[3];
, sizeof(*p)); uint16_t value = args[5], default_value = args[6];
uint8_t bus = args[1]; if (channel > CHANNEL_MAX || value > VALUE_MAX || default_value > VALUE_MAX)
uint8_t addr = args[2]; shutdown("Invalid pca9685 channel or value");
p->channel = args[3]; int fd = open_i2c(bus, addr, args[4]);
if (p->channel > CHANNEL_MAX) if (value)
shutdown("Invalid pca9685 channel"); pca9685_write(fd, channel, value);
p->max_duration = args[5]; struct i2cpwm_s *p = oid_alloc(args[0], command_config_pca9685, sizeof(*p));
p->fd = open_i2c(bus, addr, args[4]); p->fd = fd;
p->channel = channel;
p->default_value = default_value;
p->max_duration = args[7];
} }
DECL_COMMAND(command_config_pca9685, "config_pca9685 oid=%c bus=%c addr=%c" DECL_COMMAND(command_config_pca9685, "config_pca9685 oid=%c bus=%c addr=%c"
" channel=%c cycle_ticks=%u max_duration=%u"); " channel=%c cycle_ticks=%u value=%hu"
" default_value=%hu max_duration=%u");
void void
command_schedule_pca9685_out(uint32_t *args) command_schedule_pca9685_out(uint32_t *args)
@ -203,3 +198,18 @@ command_set_pca9685_out(uint32_t *args)
} }
DECL_COMMAND(command_set_pca9685_out, "set_pca9685_out bus=%c addr=%c" DECL_COMMAND(command_set_pca9685_out, "set_pca9685_out bus=%c addr=%c"
" channel=%c cycle_ticks=%u value=%hu"); " channel=%c cycle_ticks=%u value=%hu");
void
pca9685_shutdown(void)
{
int i;
for (i=0; i<devices_count; i++)
pca9685_write(devices[i].fd, CHANNEL_ALL, 0);
uint8_t j;
struct i2cpwm_s *p;
foreach_oid(j, p, command_config_pca9685) {
if (p->default_value)
pca9685_write(p->fd, p->channel, p->default_value);
}
}
DECL_SHUTDOWN(pca9685_shutdown);