aboutsummaryrefslogtreecommitdiffstats
path: root/klippy/extras/dac084S085.py
blob: ad67c40e1ae5b2f24e2df8fdd108c20e19aab042 (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
29
30
# SPI DAC DAC084S085 implementation
#
# Copyright (C) 2021  Lorenzo Franco <lorenzo.franco@lorenzing.com>
#
# This file may be distributed under the terms of the GNU GPLv3 license.

from . import bus


class dac084S085:
    def __init__(self, config):
        self.spi = bus.MCU_SPI_from_config(
            config, 1, pin_option="enable_pin", default_speed=10000000
        )
        scale = config.getfloat("scale", 1.0, above=0.0)
        for chan, name in enumerate("ABCD"):
            val = config.getfloat(
                "channel_%s" % (name,), None, minval=0.0, maxval=scale
            )
            if val is not None:
                self.set_register(chan, int(val * 255.0 / scale))

    def set_register(self, chan, value):
        b1 = (chan << 6) | (1 << 4) | ((value >> 4) & 0x0F)
        b2 = (value << 4) & 0xF0
        self.spi.spi_send([b1, b2])


def load_config_prefix(config):
    return dac084S085(config)