aboutsummaryrefslogtreecommitdiffstats
path: root/klippy/extras/mcp4728.py
blob: 800c1082d9377b82ac941fdab5ac3b6e9a14a2cd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# MCP4728 dac code
#
# Copyright (C) 2018  Kevin O'Connor <kevin@koconnor.net>
#
# This file may be distributed under the terms of the GNU GPLv3 license.
from . import bus


class mcp4728:
    def __init__(self, config):
        self.i2c = bus.MCU_I2C_from_config(config, default_addr=0x60)
        scale = config.getfloat("scale", 1.0, above=0.0)
        # Configure registers
        for i, name in enumerate("abcd"):
            val = config.getfloat(
                "channel_%s" % (name,), None, minval=0.0, maxval=scale
            )
            if val is not None:
                self.set_dac(i, int(val * 4095.0 / scale + 0.5))

    def set_dac(self, dac, value):
        self.i2c.i2c_write(
            [0x40 | (dac << 1), ((value >> 8) & 0x0F) | 0x80, value & 0xFF]
        )


def load_config_prefix(config):
    return mcp4728(config)