mcu: Support inverted PWM pins

Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
This commit is contained in:
Kevin O'Connor 2017-03-09 00:44:25 -05:00
parent 64407dc5d2
commit d5fc594317
1 changed files with 8 additions and 4 deletions

View File

@ -254,25 +254,29 @@ class MCU_pwm:
def __init__(self, mcu, pin, cycle_ticks, max_duration, hard_pwm=False): def __init__(self, mcu, pin, cycle_ticks, max_duration, hard_pwm=False):
self._mcu = mcu self._mcu = mcu
self._oid = mcu.create_oid() self._oid = mcu.create_oid()
pin, pullup, self._invert = parse_pin_extras(pin)
self._last_clock = 0 self._last_clock = 0
self._mcu_freq = mcu.get_mcu_freq() self._mcu_freq = mcu.get_mcu_freq()
self._cmd_queue = mcu.alloc_command_queue() self._cmd_queue = mcu.alloc_command_queue()
if hard_pwm: if hard_pwm:
mcu.add_config_cmd( mcu.add_config_cmd(
"config_pwm_out oid=%d pin=%s cycle_ticks=%d default_value=0" "config_pwm_out oid=%d pin=%s cycle_ticks=%d default_value=%d"
" max_duration=%d" % (self._oid, pin, cycle_ticks, max_duration)) " max_duration=%d" % (
self._oid, pin, cycle_ticks, self._invert, max_duration))
self._set_cmd = mcu.lookup_command( self._set_cmd = mcu.lookup_command(
"schedule_pwm_out oid=%c clock=%u value=%c") "schedule_pwm_out oid=%c clock=%u value=%c")
else: else:
mcu.add_config_cmd( mcu.add_config_cmd(
"config_soft_pwm_out oid=%d pin=%s cycle_ticks=%d" "config_soft_pwm_out oid=%d pin=%s cycle_ticks=%d"
" default_value=0 max_duration=%d" % ( " default_value=%d max_duration=%d" % (
self._oid, pin, cycle_ticks, max_duration)) self._oid, pin, cycle_ticks, self._invert, max_duration))
self._set_cmd = mcu.lookup_command( self._set_cmd = mcu.lookup_command(
"schedule_soft_pwm_out oid=%c clock=%u value=%c") "schedule_soft_pwm_out oid=%c clock=%u value=%c")
self.print_to_mcu_time = mcu.print_to_mcu_time self.print_to_mcu_time = mcu.print_to_mcu_time
def set_pwm(self, mcu_time, value): def set_pwm(self, mcu_time, value):
clock = int(mcu_time * self._mcu_freq) clock = int(mcu_time * self._mcu_freq)
if self._invert:
value = 1. - value
value = int(value * self.PWM_MAX + 0.5) value = int(value * self.PWM_MAX + 0.5)
msg = self._set_cmd.encode(self._oid, clock, value) msg = self._set_cmd.encode(self._oid, clock, value)
self._mcu.send(msg, minclock=self._last_clock, reqclock=clock self._mcu.send(msg, minclock=self._last_clock, reqclock=clock