2017-08-24 16:34:31 +03:00
|
|
|
# Code to configure miscellaneous chips
|
|
|
|
#
|
2021-03-26 18:37:40 +03:00
|
|
|
# Copyright (C) 2017-2021 Kevin O'Connor <kevin@koconnor.net>
|
2017-08-24 16:34:31 +03:00
|
|
|
#
|
|
|
|
# This file may be distributed under the terms of the GNU GPLv3 license.
|
|
|
|
|
2017-12-19 03:13:23 +03:00
|
|
|
PIN_MIN_TIME = 0.100
|
2021-03-26 18:31:30 +03:00
|
|
|
RESEND_HOST_TIME = 0.300 + PIN_MIN_TIME
|
2021-03-26 18:37:40 +03:00
|
|
|
MAX_SCHEDULE_TIME = 5.0
|
2017-12-19 03:13:23 +03:00
|
|
|
|
2018-03-17 20:29:03 +03:00
|
|
|
class PrinterOutputPin:
|
|
|
|
def __init__(self, config):
|
|
|
|
self.printer = config.get_printer()
|
|
|
|
ppins = self.printer.lookup_object('pins')
|
|
|
|
self.is_pwm = config.getboolean('pwm', False)
|
2017-12-19 03:13:23 +03:00
|
|
|
if self.is_pwm:
|
2018-03-17 20:29:03 +03:00
|
|
|
self.mcu_pin = ppins.setup_pin('pwm', config.get('pin'))
|
2021-03-26 18:37:40 +03:00
|
|
|
cycle_time = config.getfloat('cycle_time', 0.100, above=0.,
|
|
|
|
maxval=MAX_SCHEDULE_TIME)
|
2018-01-29 20:54:06 +03:00
|
|
|
hardware_pwm = config.getboolean('hardware_pwm', False)
|
|
|
|
self.mcu_pin.setup_cycle_time(cycle_time, hardware_pwm)
|
2017-12-19 03:13:23 +03:00
|
|
|
self.scale = config.getfloat('scale', 1., above=0.)
|
2020-09-27 03:52:42 +03:00
|
|
|
self.last_cycle_time = self.default_cycle_time = cycle_time
|
2017-08-24 16:34:31 +03:00
|
|
|
else:
|
2018-03-17 20:29:03 +03:00
|
|
|
self.mcu_pin = ppins.setup_pin('digital_out', config.get('pin'))
|
2017-12-19 03:13:23 +03:00
|
|
|
self.scale = 1.
|
2020-11-13 20:41:31 +03:00
|
|
|
self.last_cycle_time = self.default_cycle_time = 0.
|
2020-09-27 03:52:42 +03:00
|
|
|
self.last_print_time = 0.
|
2018-03-17 20:29:03 +03:00
|
|
|
static_value = config.getfloat('static_value', None,
|
|
|
|
minval=0., maxval=self.scale)
|
2021-03-26 18:25:09 +03:00
|
|
|
self.reactor = self.printer.get_reactor()
|
|
|
|
self.resend_timer = None
|
2021-03-26 18:31:30 +03:00
|
|
|
self.resend_interval = 0.
|
2018-03-17 20:29:03 +03:00
|
|
|
if static_value is not None:
|
2021-03-26 18:25:09 +03:00
|
|
|
self.mcu_pin.setup_max_duration(0.)
|
2018-03-17 20:29:03 +03:00
|
|
|
self.last_value = static_value / self.scale
|
2017-12-19 03:13:23 +03:00
|
|
|
self.mcu_pin.setup_start_value(
|
2018-01-09 03:51:35 +03:00
|
|
|
self.last_value, self.last_value, True)
|
|
|
|
else:
|
2021-03-26 18:37:40 +03:00
|
|
|
max_mcu_duration = config.getfloat('maximum_mcu_duration', 0.,
|
|
|
|
minval=0.500,
|
|
|
|
maxval=MAX_SCHEDULE_TIME)
|
2021-03-26 18:31:30 +03:00
|
|
|
self.mcu_pin.setup_max_duration(max_mcu_duration)
|
2021-03-27 21:50:32 +03:00
|
|
|
if max_mcu_duration:
|
|
|
|
self.resend_interval = max_mcu_duration - RESEND_HOST_TIME
|
2021-03-26 18:25:09 +03:00
|
|
|
|
2018-03-17 20:29:03 +03:00
|
|
|
self.last_value = config.getfloat(
|
|
|
|
'value', 0., minval=0., maxval=self.scale) / self.scale
|
2021-03-26 18:25:09 +03:00
|
|
|
self.shutdown_value = config.getfloat(
|
2018-01-09 03:51:35 +03:00
|
|
|
'shutdown_value', 0., minval=0., maxval=self.scale) / self.scale
|
2021-03-26 18:25:09 +03:00
|
|
|
self.mcu_pin.setup_start_value(self.last_value, self.shutdown_value)
|
2018-05-20 19:44:02 +03:00
|
|
|
pin_name = config.get_name().split()[1]
|
2020-04-25 07:12:01 +03:00
|
|
|
gcode = self.printer.lookup_object('gcode')
|
|
|
|
gcode.register_mux_command("SET_PIN", "PIN", pin_name,
|
|
|
|
self.cmd_SET_PIN,
|
|
|
|
desc=self.cmd_SET_PIN_help)
|
2019-06-02 16:33:05 +03:00
|
|
|
def get_status(self, eventtime):
|
|
|
|
return {'value': self.last_value}
|
2021-03-26 18:25:09 +03:00
|
|
|
def _set_pin(self, print_time, value, cycle_time, is_resend=False):
|
2020-11-13 20:41:31 +03:00
|
|
|
if value == self.last_value and cycle_time == self.last_cycle_time:
|
2021-03-26 18:25:09 +03:00
|
|
|
if not is_resend:
|
|
|
|
return
|
2020-09-27 03:52:42 +03:00
|
|
|
print_time = max(print_time, self.last_print_time + PIN_MIN_TIME)
|
2017-12-19 03:13:23 +03:00
|
|
|
if self.is_pwm:
|
2020-09-27 03:52:42 +03:00
|
|
|
self.mcu_pin.set_pwm(print_time, value, cycle_time)
|
2017-12-19 03:13:23 +03:00
|
|
|
else:
|
|
|
|
self.mcu_pin.set_digital(print_time, value)
|
|
|
|
self.last_value = value
|
2020-11-13 20:41:31 +03:00
|
|
|
self.last_cycle_time = cycle_time
|
2020-09-27 03:52:42 +03:00
|
|
|
self.last_print_time = print_time
|
2021-03-26 18:31:30 +03:00
|
|
|
if self.resend_interval and self.resend_timer is None:
|
2021-03-26 18:25:09 +03:00
|
|
|
self.resend_timer = self.reactor.register_timer(
|
|
|
|
self._resend_current_val, self.reactor.NOW)
|
2020-11-13 20:41:31 +03:00
|
|
|
cmd_SET_PIN_help = "Set the value of an output pin"
|
|
|
|
def cmd_SET_PIN(self, gcmd):
|
|
|
|
value = gcmd.get_float('VALUE', minval=0., maxval=self.scale)
|
|
|
|
value /= self.scale
|
|
|
|
cycle_time = gcmd.get_float('CYCLE_TIME', self.default_cycle_time,
|
2021-03-26 18:37:40 +03:00
|
|
|
above=0., maxval=MAX_SCHEDULE_TIME)
|
2020-11-13 20:41:31 +03:00
|
|
|
if not self.is_pwm and value not in [0., 1.]:
|
|
|
|
raise gcmd.error("Invalid pin value")
|
|
|
|
toolhead = self.printer.lookup_object('toolhead')
|
|
|
|
toolhead.register_lookahead_callback(
|
|
|
|
lambda print_time: self._set_pin(print_time, value, cycle_time))
|
2017-08-24 16:34:31 +03:00
|
|
|
|
2021-03-26 18:25:09 +03:00
|
|
|
def _resend_current_val(self, eventtime):
|
|
|
|
if self.last_value == self.shutdown_value:
|
|
|
|
self.reactor.unregister_timer(self.resend_timer)
|
|
|
|
self.resend_timer = None
|
|
|
|
return self.reactor.NEVER
|
|
|
|
|
|
|
|
systime = self.reactor.monotonic()
|
|
|
|
print_time = self.mcu_pin.get_mcu().estimated_print_time(systime)
|
|
|
|
time_diff = print_time - (self.last_print_time + self.resend_interval)
|
|
|
|
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)
|
|
|
|
return systime + self.resend_interval
|
|
|
|
|
2018-03-17 20:29:03 +03:00
|
|
|
def load_config_prefix(config):
|
|
|
|
return PrinterOutputPin(config)
|