diff --git a/klippy/mcu.py b/klippy/mcu.py index 53d1ff74..8e819191 100644 --- a/klippy/mcu.py +++ b/klippy/mcu.py @@ -250,21 +250,23 @@ class MCU_pwm: "schedule_soft_pwm_out oid=%c clock=%u on_ticks=%u off_ticks=%u", cq=cmd_queue) def set_pwm(self, print_time, value, cycle_time=None): + clock = self._mcu.print_time_to_clock(print_time) + minclock = self._last_clock + self._last_clock = clock + if self._invert: + value = 1. - value + if self._hardware_pwm: + v = int(max(0., min(1., value)) * self._pwm_max + 0.5) + self._set_cmd.send([self._oid, clock, v], + minclock=minclock, reqclock=clock) + return + # Soft pwm update if cycle_time is None: cycle_time = self._cycle_time cycle_ticks = self._mcu.seconds_to_clock(cycle_time) - clock = self._mcu.print_time_to_clock(print_time) - if self._invert: - value = 1. - value on_ticks = int(max(0., min(1., value)) * float(cycle_ticks) + 0.5) - if self._hardware_pwm: - self._set_cmd.send([self._oid, clock, on_ticks], - minclock=self._last_clock, reqclock=clock) - else: - off_ticks = cycle_ticks - on_ticks - self._set_cmd.send([self._oid, clock, on_ticks, off_ticks], - minclock=self._last_clock, reqclock=clock) - self._last_clock = clock + self._set_cmd.send([self._oid, clock, on_ticks, cycle_ticks - on_ticks], + minclock=minclock, reqclock=clock) class MCU_adc: def __init__(self, mcu, pin_params):