From 05bb5484b43cd7e5b1f9ee235eacfec7c1f1d928 Mon Sep 17 00:00:00 2001 From: Kevin O'Connor Date: Sat, 17 Aug 2019 21:46:45 -0400 Subject: [PATCH] bus: Add MCU_bus_digital_out helper class Add a helper class for tracking gpio outputs that are synchronized to bus updates on a particular command queue. Signed-off-by: Kevin O'Connor --- klippy/extras/bus.py | 41 +++++++++++++++++++++++++++++++++ klippy/extras/display/uc1701.py | 30 +++++------------------- 2 files changed, 47 insertions(+), 24 deletions(-) diff --git a/klippy/extras/bus.py b/klippy/extras/bus.py index d7e2be51..04795a88 100644 --- a/klippy/extras/bus.py +++ b/klippy/extras/bus.py @@ -197,3 +197,44 @@ def MCU_I2C_from_config(config, default_addr=None, default_speed=100000): addr = config.getint('i2c_address', default_addr, minval=0, maxval=127) # Create MCU_I2C object return MCU_I2C(i2c_mcu, bus, addr, speed) + + +###################################################################### +# Bus synchronized digital outputs +###################################################################### + +# Helper code for a gpio that updates on a cmd_queue +class MCU_bus_digital_out: + def __init__(self, mcu, pin_desc, cmd_queue=None, value=0): + self.mcu = mcu + self.oid = mcu.create_oid() + ppins = mcu.get_printer().lookup_object('pins') + pin_params = ppins.lookup_pin(pin_desc) + if pin_params['chip'] is not mcu: + raise ppins.error("Pin %s must be on mcu %s" % ( + pin_desc, mcu.get_name())) + mcu.add_config_cmd("config_digital_out oid=%d pin=%s value=%d" + " default_value=%d max_duration=%d" + % (self.oid, pin_params['pin'], value, value, 0)) + mcu.register_config_callback(self.build_config) + if cmd_queue is None: + cmd_queue = mcu.alloc_command_queue() + self.cmd_queue = cmd_queue + self.update_pin_cmd = None + def get_oid(self): + return self.oid + def get_mcu(self): + return self.mcu + def get_command_queue(self): + return self.cmd_queue + def build_config(self): + self.update_pin_cmd = self.mcu.lookup_command( + "update_digital_out oid=%c value=%c", cq=self.cmd_queue) + def update_digital_out(self, value, minclock=0, reqclock=0): + if self.update_pin_cmd is None: + # Send setup message via mcu initialization + self.mcu.add_config_cmd("update_digital_out oid=%c value=%c" + % (self.oid, not not value)) + return + self.update_pin_cmd.send([self.oid, not not value], + minclock=minclock, reqclock=reqclock) diff --git a/klippy/extras/display/uc1701.py b/klippy/extras/display/uc1701.py index ff7d595e..3766fa13 100644 --- a/klippy/extras/display/uc1701.py +++ b/klippy/extras/display/uc1701.py @@ -1,6 +1,6 @@ # Support for UC1701 (and similar) 128x64 graphics LCD displays # -# Copyright (C) 2018 Kevin O'Connor +# Copyright (C) 2018-2019 Kevin O'Connor # Copyright (C) 2018 Eric Callahan # Copyright (C) 2019 Dmitry Budaev # @@ -110,30 +110,12 @@ class SPI4wire: def __init__(self, config, data_pin_name): self.spi = extras.bus.MCU_SPI_from_config(config, 0, default_speed=10000000) - mcu = self.spi.get_mcu() - # Create data/control pin - ppins = config.get_printer().lookup_object('pins') - pin_params = ppins.lookup_pin(config.get(data_pin_name)) - if pin_params['chip'] != mcu: - raise ppins.error("%s: all pins must be on same mcu" % ( - config.get_name())) - self.dc_oid = mcu.create_oid() - mcu.add_config_cmd("config_digital_out oid=%d pin=%s" - " value=%d default_value=%d max_duration=%d" % ( - self.dc_oid, pin_params['pin'], 0, 0, 0)) - mcu.register_config_callback(self.build_config) - self.update_pin_cmd = None - def build_config(self): - self.update_pin_cmd = self.spi.get_mcu().lookup_command( - "update_digital_out oid=%c value=%c", - cq=self.spi.get_command_queue()) + dc_pin = config.get(data_pin_name) + self.mcu_dc = extras.bus.MCU_bus_digital_out( + self.spi.get_mcu(), dc_pin, self.spi.get_command_queue()) def send(self, cmds, is_data=False): - if is_data: - self.update_pin_cmd.send([self.dc_oid, 1], - reqclock=BACKGROUND_PRIORITY_CLOCK) - else: - self.update_pin_cmd.send([self.dc_oid, 0], - reqclock=BACKGROUND_PRIORITY_CLOCK) + self.mcu_dc.update_digital_out(is_data, + reqclock=BACKGROUND_PRIORITY_CLOCK) self.spi.spi_send(cmds, reqclock=BACKGROUND_PRIORITY_CLOCK) # IO wrapper for i2c bus