2017-08-24 15:34:31 +02:00
|
|
|
# Code to configure miscellaneous chips
|
|
|
|
#
|
2018-01-20 04:22:17 +01:00
|
|
|
# Copyright (C) 2017,2018 Kevin O'Connor <kevin@koconnor.net>
|
2017-08-24 15:34:31 +02:00
|
|
|
#
|
|
|
|
# This file may be distributed under the terms of the GNU GPLv3 license.
|
|
|
|
|
2017-12-19 01:13:23 +01:00
|
|
|
PIN_MIN_TIME = 0.100
|
|
|
|
|
2018-03-17 18:29:03 +01: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 01:13:23 +01:00
|
|
|
if self.is_pwm:
|
2018-03-17 18:29:03 +01:00
|
|
|
self.mcu_pin = ppins.setup_pin('pwm', config.get('pin'))
|
2018-01-29 18:54:06 +01:00
|
|
|
cycle_time = config.getfloat('cycle_time', 0.100, above=0.)
|
|
|
|
hardware_pwm = config.getboolean('hardware_pwm', False)
|
|
|
|
self.mcu_pin.setup_cycle_time(cycle_time, hardware_pwm)
|
2017-12-19 01:13:23 +01:00
|
|
|
self.scale = config.getfloat('scale', 1., above=0.)
|
2017-08-24 15:34:31 +02:00
|
|
|
else:
|
2018-03-17 18:29:03 +01:00
|
|
|
self.mcu_pin = ppins.setup_pin('digital_out', config.get('pin'))
|
2017-12-19 01:13:23 +01:00
|
|
|
self.scale = 1.
|
|
|
|
self.mcu_pin.setup_max_duration(0.)
|
|
|
|
self.last_value_time = 0.
|
2018-03-17 18:29:03 +01:00
|
|
|
static_value = config.getfloat('static_value', None,
|
|
|
|
minval=0., maxval=self.scale)
|
|
|
|
if static_value is not None:
|
|
|
|
self.last_value = static_value / self.scale
|
2017-12-19 01:13:23 +01:00
|
|
|
self.mcu_pin.setup_start_value(
|
2018-01-09 01:51:35 +01:00
|
|
|
self.last_value, self.last_value, True)
|
|
|
|
else:
|
2018-03-17 18:29:03 +01:00
|
|
|
self.last_value = config.getfloat(
|
|
|
|
'value', 0., minval=0., maxval=self.scale) / self.scale
|
2018-01-09 01:51:35 +01:00
|
|
|
shutdown_value = config.getfloat(
|
|
|
|
'shutdown_value', 0., minval=0., maxval=self.scale) / self.scale
|
|
|
|
self.mcu_pin.setup_start_value(self.last_value, shutdown_value)
|
2018-05-20 18:44:02 +02:00
|
|
|
pin_name = config.get_name().split()[1]
|
2020-04-25 06:12:01 +02: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 15:33:05 +02:00
|
|
|
def get_status(self, eventtime):
|
|
|
|
return {'value': self.last_value}
|
2017-12-19 01:13:23 +01:00
|
|
|
cmd_SET_PIN_help = "Set the value of an output pin"
|
2020-04-25 06:12:01 +02:00
|
|
|
def cmd_SET_PIN(self, gcmd):
|
|
|
|
value = gcmd.get_float('VALUE', minval=0., maxval=self.scale)
|
2018-04-21 03:41:13 +02:00
|
|
|
value /= self.scale
|
2017-12-19 01:13:23 +01:00
|
|
|
if value == self.last_value:
|
|
|
|
return
|
2018-01-20 04:22:17 +01:00
|
|
|
print_time = self.printer.lookup_object('toolhead').get_last_move_time()
|
2017-12-19 01:13:23 +01:00
|
|
|
print_time = max(print_time, self.last_value_time + PIN_MIN_TIME)
|
|
|
|
if self.is_pwm:
|
|
|
|
self.mcu_pin.set_pwm(print_time, value)
|
|
|
|
else:
|
|
|
|
if value not in [0., 1.]:
|
2020-04-25 06:12:01 +02:00
|
|
|
raise gcmd.error("Invalid pin value")
|
2017-12-19 01:13:23 +01:00
|
|
|
self.mcu_pin.set_digital(print_time, value)
|
|
|
|
self.last_value = value
|
|
|
|
self.last_value_time = print_time
|
2017-08-24 15:34:31 +02:00
|
|
|
|
2018-03-17 18:29:03 +01:00
|
|
|
def load_config_prefix(config):
|
|
|
|
return PrinterOutputPin(config)
|