Source code for herosdevices.hardware.guestgood
"""Drivers for guestgood ICs."""
from herosdevices.core.templates.i2c import I2CDeviceTemplate
from herosdevices.helper import mark_driver
from herosdevices.interfaces import atomiq
CMD_SET_MODE = 1
CMD_SET_VOUT0 = 2
CMD_SET_VOUT1 = 4
[docs]
@mark_driver(
state="beta",
info="Guestgood GP8403 0-10V 2ch DAC",
product_page="https://www.guestgood.com/en/solutions/gp8403/",
)
class GP8403(I2CDeviceTemplate, atomiq.DAC):
"""Driver for the GP8403 2ch 0-10V DAC."""
def __init__(self, bus_id: int = 1, device_address: int = 0x5f, mode_5v: bool = False) -> None:
"""Create a new GP8403 representation.
Args:
bus_id: I2C bus id (default 1).
device_address: I2C address set (by resistors or switches) on the GP8403 (0x58 - 0x5f)
mode_5v: whether to use 0-5V range (True) or 0-10V range (False, default)
"""
super().__init__(bus_id, keep_alive=True)
self.device_address = device_address
self.mode_5v = mode_5v
self.vmax = 5 if self.mode_5v else 10
self._set_mode(self.mode_5v)
@property
def min_voltage(self) -> float:
"""Specify the minimum voltage read-only."""
return 0.0
@property
def max_voltage(self) -> float:
"""Specify the maximum voltage read-only."""
return float(self.vmax)
def _send_cmd(self, cmd: int, data: list) -> None:
self.connection.connection.write_i2c_block_data(self.device_address, cmd, data)
def _set_mode(self, mode_5v: bool) -> None:
with self.connection.operation():
self._send_cmd(CMD_SET_MODE, [0x00 if mode_5v else 0x11])
[docs]
def set_channel_voltage(self, channel: int, voltage: float) -> None:
"""Set the DAC output voltage.
Args:
channel: number of the channel for which to set the voltage (0 or 1)
voltage: voltage to set in the range 0-10V (or 0-5V if mode_5v=True)
"""
if channel > 1:
raise ValueError("Only channels 0 and 1 are supported")
if (voltage > self.vmax) or voltage < 0:
raise ValueError("Voltage out of bounds")
cmd = CMD_SET_VOUT0 if channel == 0 else CMD_SET_VOUT1
bitvalue = int(voltage/self.vmax*0xFFF)
data = [(bitvalue & 0xF) << 4,
(bitvalue & 0xFF0) >> 4
]
with self.connection.operation():
self.connection.connection.write_i2c_block_data(self.device_address, cmd, data)