2018-01-20 08:14:52 +03:00
|
|
|
# AD5206 digipot code
|
|
|
|
#
|
|
|
|
# Copyright (C) 2017,2018 Kevin O'Connor <kevin@koconnor.net>
|
|
|
|
#
|
|
|
|
# This file may be distributed under the terms of the GNU GPLv3 license.
|
|
|
|
|
|
|
|
class ad5206:
|
|
|
|
def __init__(self, config):
|
2018-04-04 19:07:41 +03:00
|
|
|
ppins = config.get_printer().lookup_object('pins')
|
|
|
|
enable_pin = config.get('enable_pin')
|
|
|
|
enable_pin_params = ppins.lookup_pin('digital_out', enable_pin)
|
2018-02-18 07:18:10 +03:00
|
|
|
if enable_pin_params['invert']:
|
2018-04-04 19:07:41 +03:00
|
|
|
raise ppins.error("ad5206 can not invert pin")
|
2018-05-04 19:27:52 +03:00
|
|
|
mcu = enable_pin_params['chip']
|
|
|
|
pin = enable_pin_params['pin']
|
2018-01-20 08:14:52 +03:00
|
|
|
scale = config.getfloat('scale', 1., above=0.)
|
2018-05-04 19:27:52 +03:00
|
|
|
channels = [None]*6
|
|
|
|
for i in range(len(channels)):
|
2018-01-20 08:14:52 +03:00
|
|
|
val = config.getfloat('channel_%d' % (i+1,), None,
|
|
|
|
minval=0., maxval=scale)
|
|
|
|
if val is not None:
|
2018-05-04 19:27:52 +03:00
|
|
|
channels[i] = int(val * 256. / scale + .5)
|
|
|
|
oid = mcu.create_oid()
|
|
|
|
mcu.add_config_cmd(
|
|
|
|
"config_spi oid=%d bus=%d pin=%s mode=%u rate=%u shutdown_msg=" % (
|
|
|
|
oid, 0, pin, 0, 25000000))
|
|
|
|
for i, val in enumerate(channels):
|
2018-01-20 08:14:52 +03:00
|
|
|
if val is not None:
|
2018-05-04 19:27:52 +03:00
|
|
|
mcu.add_config_cmd(
|
|
|
|
"spi_send oid=%d data=%02x%02x" % (oid, i, val),
|
|
|
|
is_init=True)
|
2018-01-20 08:14:52 +03:00
|
|
|
|
2018-02-03 20:53:11 +03:00
|
|
|
def load_config_prefix(config):
|
2018-01-20 08:14:52 +03:00
|
|
|
return ad5206(config)
|