2018-05-30 02:59:17 +02:00
|
|
|
# MCP4451 digipot code
|
|
|
|
#
|
|
|
|
# Copyright (C) 2018 Kevin O'Connor <kevin@koconnor.net>
|
|
|
|
#
|
|
|
|
# This file may be distributed under the terms of the GNU GPLv3 license.
|
2018-11-24 06:36:49 +01:00
|
|
|
import bus
|
2018-05-30 02:59:17 +02:00
|
|
|
|
|
|
|
WiperRegisters = [0x00, 0x01, 0x06, 0x07]
|
|
|
|
|
|
|
|
class mcp4451:
|
|
|
|
def __init__(self, config):
|
2018-11-24 06:28:54 +01:00
|
|
|
self.i2c = bus.MCU_I2C_from_config(config)
|
2018-05-30 02:59:17 +02:00
|
|
|
scale = config.getfloat('scale', 1., above=0.)
|
2018-11-24 06:28:54 +01:00
|
|
|
# Configure registers
|
|
|
|
self.set_register(0x04, 0xff)
|
|
|
|
self.set_register(0x0a, 0xff)
|
|
|
|
for i in range(4):
|
2018-05-30 02:59:17 +02:00
|
|
|
val = config.getfloat('wiper_%d' % (i,), None,
|
|
|
|
minval=0., maxval=scale)
|
|
|
|
if val is not None:
|
2018-11-24 06:28:54 +01:00
|
|
|
val = int(val * 255. / scale + .5)
|
|
|
|
self.set_register(WiperRegisters[i], val)
|
|
|
|
def set_register(self, reg, value):
|
|
|
|
self.i2c.i2c_write([(reg << 4) | ((value >> 8) & 0x03), value])
|
2018-05-30 02:59:17 +02:00
|
|
|
|
|
|
|
def load_config_prefix(config):
|
|
|
|
return mcp4451(config)
|