output_pin: Move pwm and digital output pins to new module in extras/

Rename the digital_output, pwm_output, and static_pwm_output config
sections to output_pin and move to a new module in the extras/
directory.

Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
This commit is contained in:
Kevin O'Connor 2018-03-17 13:29:03 -04:00
parent 66eefa1da8
commit ef8c464d97
4 changed files with 59 additions and 87 deletions

View File

@ -244,21 +244,6 @@
# seconds. # seconds.
# Run-time configurable digital output pins (one may define any number
# of sections with a "digital_output" prefix). Pins configured here
# will be setup as digital outputs and one may modify them at run-time
# using the "SET_PIN PIN=my_pin VALUE=0" extended g-code command.
#[digital_output my_pin]
#pin:
# The pin to configure as a digital output. This parameter must be
# provided.
#value:
# The value to initially set the pin to during MCU configuration
# (either 0 or 1). The default is 0 (for low voltage).
#shutdown_value:
# The value to set the pin to on an MCU shutdown event. The default
# is 0 (for low voltage).
# Statically configured digital output pins (one may define any number # Statically configured digital output pins (one may define any number
# of sections with a "static_digital_output" prefix). Pins configured # of sections with a "static_digital_output" prefix). Pins configured
# here will be setup as a GPIO output during MCU configuration. They # here will be setup as a GPIO output during MCU configuration. They
@ -269,52 +254,48 @@
# pin will be set to a high level unless the pin name is prefaced # pin will be set to a high level unless the pin name is prefaced
# with "!". This parameter must be provided. # with "!". This parameter must be provided.
# Run-time configurable PWM (pulse width modulator) output pins (one
# may define any number of sections with a "pwm_output" prefix). Pins # Run-time configurable output pins (one may define any number of
# configured here will be setup as PWM outputs and one may modify them # sections with an "output_pin" prefix). Pins configured here will be
# at run-time using the "SET_PIN PIN=my_pin VALUE=.1" extended g-code # setup as output pins and one may modify them at run-time using the
# command. # "SET_PIN PIN=my_pin VALUE=.1" extended g-code command.
#[pwm_output my_pin] #[output_pin my_pin]
#pin: #pin:
# The pin to configure as a PWM output. This parameter must be # The pin to configure as an output. This parameter must be
# provided. # provided.
#pwm: False
# Set if the output pin should be capable of
# pulse-width-modulation. If this is true, the value fields should
# be between 0 and 1; if it is false the value fields should be
# either 0 or 1. The default is False.
#static_value:
# If this is set, then the pin is assigned to this value at startup
# and the pin can not be changed during runtime. A static pin uses
# slightly less ram in the micro-controller. The default is to use
# runtime configuration of pins.
#value: #value:
# The value to initially set the PWM output to during MCU # The value to initially set the pin to during MCU
# configuration. This is typically set to a number between 0.0 and # configuration. The default is 0 (for low voltage).
# 1.0 with 1.0 being full on and 0.0 being full off. However, the
# range may be changed with the 'scale' parameter (see below). The
# default is 0.
#shutdown_value: #shutdown_value:
# The value to set the pin to on an MCU shutdown event. The default # The value to set the pin to on an MCU shutdown event. The default
# is 0. # is 0 (for low voltage).
#cycle_time: 0.100 #cycle_time: 0.100
# The amount of time (in seconds) per PWM cycle. It is recommended # The amount of time (in seconds) per PWM cycle. It is recommended
# this be 10 milliseconds or greater when using software based # this be 10 milliseconds or greater when using software based
# PWM. The default is 0.100 seconds. # PWM. The default is 0.100 seconds for pwm pins.
#hardware_pwm: False #hardware_pwm: False
# Enable this to use hardware PWM instead of software PWM. The # Enable this to use hardware PWM instead of software PWM. The
# default is False. # default is False.
#scale: #scale:
# This parameter can be used to alter how the 'value' and # This parameter can be used to alter how the 'value' and
# 'shutdown_value' parameters are interpreted. If provided, then the # 'shutdown_value' parameters are interpreted for pwm pins. If
# 'value' parameter should be between 0.0 and 'scale'. This may be # provided, then the 'value' parameter should be between 0.0 and
# useful when configuring a PWM pin that controls a stepper voltage # 'scale'. This may be useful when configuring a PWM pin that
# reference. The 'scale' can be set to the equivalent stepper # controls a stepper voltage reference. The 'scale' can be set to
# amperage if the PWM were fully enabled, and then the 'value' # the equivalent stepper amperage if the PWM were fully enabled, and
# parameter can be specified using the desired amperage for the # then the 'value' parameter can be specified using the desired
# stepper. The default is to not scale the 'value' parameter. # amperage for the stepper. The default is to not scale the 'value'
# parameter.
# Statically configured PWM output pins (one may define any number of
# sections with a "static_pwm_output" prefix). Pins configured here
# will be setup as PWM outputs during MCU configuration. They can not
# be changed at run-time.
#[static_pwm_output my_output_pwm]
#pin:
#value:
#cycle_time:
#hardware_pwm:
#scale:
# See the 'pwm_output' section for details on these parameters.
# Multiple pin outputs (one may define any number of sections with a # Multiple pin outputs (one may define any number of sections with a

View File

@ -74,26 +74,29 @@ max_accel: 3000
max_z_velocity: 5 max_z_velocity: 5
max_z_accel: 100 max_z_accel: 100
[static_pwm_output stepper_xy_current] [output_pin stepper_xy_current]
pin: PL3 pin: PL3
pwm: True
scale: 2.0 scale: 2.0
value: 1.3
cycle_time: .002 cycle_time: .002
hardware_pwm: True hardware_pwm: True
static_value: 1.3
[static_pwm_output stepper_z_current] [output_pin stepper_z_current]
pin: PL4 pin: PL4
pwm: True
scale: 2.0 scale: 2.0
value: 1.3
cycle_time: .002 cycle_time: .002
hardware_pwm: True hardware_pwm: True
static_value: 1.3
[static_pwm_output stepper_e_current] [output_pin stepper_e_current]
pin: PL5 pin: PL5
pwm: True
scale: 2.0 scale: 2.0
value: 1.25
cycle_time: .002 cycle_time: .002
hardware_pwm: True hardware_pwm: True
static_value: 1.25
[static_digital_output stepper_config] [static_digital_output stepper_config]
pins: pins:

View File

@ -3,48 +3,46 @@
# Copyright (C) 2017,2018 Kevin O'Connor <kevin@koconnor.net> # Copyright (C) 2017,2018 Kevin O'Connor <kevin@koconnor.net>
# #
# This file may be distributed under the terms of the GNU GPLv3 license. # This file may be distributed under the terms of the GNU GPLv3 license.
import pins
######################################################################
# Output pins
######################################################################
PIN_MIN_TIME = 0.100 PIN_MIN_TIME = 0.100
class PrinterPin: class PrinterOutputPin:
def __init__(self, printer, config): def __init__(self, config):
self.printer = printer self.printer = config.get_printer()
self.is_pwm = 'pwm' in config.get_name().split()[0] ppins = self.printer.lookup_object('pins')
self.is_pwm = config.getboolean('pwm', False)
if self.is_pwm: if self.is_pwm:
self.mcu_pin = pins.setup_pin(printer, 'pwm', config.get('pin')) self.mcu_pin = ppins.setup_pin('pwm', config.get('pin'))
cycle_time = config.getfloat('cycle_time', 0.100, above=0.) cycle_time = config.getfloat('cycle_time', 0.100, above=0.)
hardware_pwm = config.getboolean('hardware_pwm', False) hardware_pwm = config.getboolean('hardware_pwm', False)
self.mcu_pin.setup_cycle_time(cycle_time, hardware_pwm) self.mcu_pin.setup_cycle_time(cycle_time, hardware_pwm)
self.scale = config.getfloat('scale', 1., above=0.) self.scale = config.getfloat('scale', 1., above=0.)
else: else:
self.mcu_pin = pins.setup_pin( self.mcu_pin = ppins.setup_pin('digital_out', config.get('pin'))
printer, 'digital_out', config.get('pin'))
self.scale = 1. self.scale = 1.
self.mcu_pin.setup_max_duration(0.) self.mcu_pin.setup_max_duration(0.)
self.last_value_time = 0. self.last_value_time = 0.
self.last_value = config.getfloat( static_value = config.getfloat('static_value', None,
'value', 0., minval=0., maxval=self.scale) / self.scale minval=0., maxval=self.scale)
self.is_static = config.get_name().startswith('static_') if static_value is not None:
if self.is_static: self.is_static = True
self.last_value = static_value / self.scale
self.mcu_pin.setup_start_value( self.mcu_pin.setup_start_value(
self.last_value, self.last_value, True) self.last_value, self.last_value, True)
else: else:
self.is_static = False
self.last_value = config.getfloat(
'value', 0., minval=0., maxval=self.scale) / self.scale
shutdown_value = config.getfloat( shutdown_value = config.getfloat(
'shutdown_value', 0., minval=0., maxval=self.scale) / self.scale 'shutdown_value', 0., minval=0., maxval=self.scale) / self.scale
self.mcu_pin.setup_start_value(self.last_value, shutdown_value) self.mcu_pin.setup_start_value(self.last_value, shutdown_value)
self.gcode = printer.lookup_object('gcode') self.gcode = self.printer.lookup_object('gcode')
self.gcode.register_command("SET_PIN", self.cmd_SET_PIN, self.gcode.register_command("SET_PIN", self.cmd_SET_PIN,
desc=self.cmd_SET_PIN_help) desc=self.cmd_SET_PIN_help)
cmd_SET_PIN_help = "Set the value of an output pin" cmd_SET_PIN_help = "Set the value of an output pin"
def cmd_SET_PIN(self, params): def cmd_SET_PIN(self, params):
pin_name = self.gcode.get_str('PIN', params) pin_name = self.gcode.get_str('PIN', params)
pin = self.printer.lookup_object('pin ' + pin_name, None) pin = self.printer.lookup_object('output_pin ' + pin_name, None)
if pin is not self: if pin is not self:
if pin is None: if pin is None:
raise self.gcode.error("Pin not configured") raise self.gcode.error("Pin not configured")
@ -67,15 +65,5 @@ class PrinterPin:
self.last_value = value self.last_value = value
self.last_value_time = print_time self.last_value_time = print_time
def load_config_prefix(config):
###################################################################### return PrinterOutputPin(config)
# Setup
######################################################################
def add_printer_objects(printer, config):
for s in config.get_prefix_sections('digital_output '):
printer.add_object('pin' + s.section[14:], PrinterPin(printer, s))
for s in config.get_prefix_sections('static_pwm_output '):
printer.add_object('pin' + s.section[17:], PrinterPin(printer, s))
for s in config.get_prefix_sections('pwm_output '):
printer.add_object('pin' + s.section[10:], PrinterPin(printer, s))

View File

@ -7,7 +7,7 @@
import sys, os, optparse, logging, time, threading import sys, os, optparse, logging, time, threading
import collections, ConfigParser, importlib import collections, ConfigParser, importlib
import util, reactor, queuelogger, msgproto import util, reactor, queuelogger, msgproto
import gcode, pins, mcu, chipmisc, toolhead, extruder, heater import gcode, pins, mcu, toolhead, extruder, heater
message_ready = "Printer is ready" message_ready = "Printer is ready"
@ -209,7 +209,7 @@ class Printer:
m.add_printer_objects(self, config) m.add_printer_objects(self, config)
for section in fileconfig.sections(): for section in fileconfig.sections():
self.try_load_module(config, section) self.try_load_module(config, section)
for m in [chipmisc, toolhead, extruder, heater]: for m in [toolhead, extruder, heater]:
m.add_printer_objects(self, config) m.add_printer_objects(self, config)
# Validate that there are no undefined parameters in the config file # Validate that there are no undefined parameters in the config file
valid_sections = { s: 1 for s, o in self.all_config_options } valid_sections = { s: 1 for s, o in self.all_config_options }