pwm_cycle_time: New module for output pins with dynamic cycle times

Remove support for changing the cycle time of pwm pins from the
output_pin module.  Use a new pwm_cycle_time module that supports
setting dynamic cycle times.  This simplifies the output_pin code and
low-level pin update code.

Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
This commit is contained in:
Kevin O'Connor 2023-01-13 11:20:19 -05:00
parent 1baa45913f
commit fd2feff67d
9 changed files with 204 additions and 38 deletions

View file

@ -1,6 +1,6 @@
# Code to configure miscellaneous chips
# PWM and digital output pin handling
#
# Copyright (C) 2017-2021 Kevin O'Connor <kevin@koconnor.net>
# Copyright (C) 2017-2024 Kevin O'Connor <kevin@koconnor.net>
#
# This file may be distributed under the terms of the GNU GPLv3 license.
@ -21,11 +21,9 @@ class PrinterOutputPin:
hardware_pwm = config.getboolean('hardware_pwm', False)
self.mcu_pin.setup_cycle_time(cycle_time, hardware_pwm)
self.scale = config.getfloat('scale', 1., above=0.)
self.last_cycle_time = self.default_cycle_time = cycle_time
else:
self.mcu_pin = ppins.setup_pin('digital_out', config.get('pin'))
self.scale = 1.
self.last_cycle_time = self.default_cycle_time = 0.
self.last_print_time = 0.
# Support mcu checking for maximum duration
self.reactor = self.printer.get_reactor()
@ -58,32 +56,30 @@ class PrinterOutputPin:
desc=self.cmd_SET_PIN_help)
def get_status(self, eventtime):
return {'value': self.last_value}
def _set_pin(self, print_time, value, cycle_time, is_resend=False):
if value == self.last_value and cycle_time == self.last_cycle_time:
if not is_resend:
return
def _set_pin(self, print_time, value, is_resend=False):
if value == self.last_value and not is_resend:
return
print_time = max(print_time, self.last_print_time + PIN_MIN_TIME)
if self.is_pwm:
self.mcu_pin.set_pwm(print_time, value, cycle_time)
self.mcu_pin.set_pwm(print_time, value)
else:
self.mcu_pin.set_digital(print_time, value)
self.last_value = value
self.last_cycle_time = cycle_time
self.last_print_time = print_time
if self.resend_interval and self.resend_timer is None:
self.resend_timer = self.reactor.register_timer(
self._resend_current_val, self.reactor.NOW)
cmd_SET_PIN_help = "Set the value of an output pin"
def cmd_SET_PIN(self, gcmd):
# Read requested value
value = gcmd.get_float('VALUE', minval=0., maxval=self.scale)
value /= self.scale
cycle_time = gcmd.get_float('CYCLE_TIME', self.default_cycle_time,
above=0., maxval=MAX_SCHEDULE_TIME)
if not self.is_pwm and value not in [0., 1.]:
raise gcmd.error("Invalid pin value")
# Obtain print_time and apply requested settings
toolhead = self.printer.lookup_object('toolhead')
toolhead.register_lookahead_callback(
lambda print_time: self._set_pin(print_time, value, cycle_time))
lambda print_time: self._set_pin(print_time, value))
def _resend_current_val(self, eventtime):
if self.last_value == self.shutdown_value:
@ -97,8 +93,7 @@ class PrinterOutputPin:
if time_diff > 0.:
# Reschedule for resend time
return systime + time_diff
self._set_pin(print_time + PIN_MIN_TIME,
self.last_value, self.last_cycle_time, True)
self._set_pin(print_time + PIN_MIN_TIME, self.last_value, True)
return systime + self.resend_interval
def load_config_prefix(config):