mcp4451: Use bus.py helper code for i2c

Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
This commit is contained in:
Kevin O'Connor 2018-11-24 00:28:54 -05:00
parent 729a2e8306
commit 147d805231
2 changed files with 11 additions and 23 deletions

View File

@ -581,7 +581,7 @@
# Statically configured MCP4451 digipot connected via I2C bus (one may # Statically configured MCP4451 digipot connected via I2C bus (one may
# define any number of sections with an "mcp4451" prefix). # define any number of sections with an "mcp4451" prefix).
#[mcp4451 my_digipot] #[mcp4451 my_digipot]
#mcu: mcu #i2c_mcu: mcu
# The name of the micro-controller that the MCP4451 chip is # The name of the micro-controller that the MCP4451 chip is
# connected to. The default is "mcu". # connected to. The default is "mcu".
#i2c_address: #i2c_address:

View File

@ -3,37 +3,25 @@
# Copyright (C) 2018 Kevin O'Connor <kevin@koconnor.net> # Copyright (C) 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 mcu import mcu, bus
WiperRegisters = [0x00, 0x01, 0x06, 0x07] WiperRegisters = [0x00, 0x01, 0x06, 0x07]
class mcp4451: class mcp4451:
def __init__(self, config): def __init__(self, config):
printer = config.get_printer() self.i2c = bus.MCU_I2C_from_config(config)
# Read config parameters
self.mcu = mcu.get_printer_mcu(printer, config.get('mcu', 'mcu'))
i2c_addr = config.getint('i2c_address')
scale = config.getfloat('scale', 1., above=0.) scale = config.getfloat('scale', 1., above=0.)
wipers = [None]*4 # Configure registers
for i in range(len(wipers)): self.set_register(0x04, 0xff)
self.set_register(0x0a, 0xff)
for i in range(4):
val = config.getfloat('wiper_%d' % (i,), None, val = config.getfloat('wiper_%d' % (i,), None,
minval=0., maxval=scale) minval=0., maxval=scale)
if val is not None: if val is not None:
wipers[i] = int(val * 255. / scale + .5) val = int(val * 255. / scale + .5)
# Setup i2c self.set_register(WiperRegisters[i], val)
self.oid = self.mcu.create_oid() def set_register(self, reg, value):
self.mcu.add_config_cmd( self.i2c.i2c_write([(reg << 4) | ((value >> 8) & 0x03), value])
"config_i2c oid=%d bus=0 rate=100000 addr=%d" % (
self.oid, i2c_addr))
# Configure registers
self.add_config_cmd(0x04, 0xff)
self.add_config_cmd(0x0a, 0xff)
for reg, val in zip(WiperRegisters, wipers):
if val is not None:
self.add_config_cmd(reg, val)
def add_config_cmd(self, reg, val):
self.mcu.add_config_cmd("i2c_write oid=%d data=%02x%02x" % (
self.oid, (reg << 4) | ((val >> 8) & 0x03), val), is_init=True)
def load_config_prefix(config): def load_config_prefix(config):
return mcp4451(config) return mcp4451(config)