def initialize(self): import adafruit_mcp4728 from adafruit_extended_bus import ExtendedI2C self.setup_output_variables(OUTPUT_INFORMATION) try: self.dac = adafruit_mcp4728.MCP4728( ExtendedI2C(self.output.i2c_bus), address=int(str(self.output.i2c_location), 16)) self.channel = { 0: self.dac.channel_a, 1: self.dac.channel_b, 2: self.dac.channel_c, 3: self.dac.channel_d } # Set up Channels for channel in channels_dict: if self.options_channels['vref'][channel] == "internal": self.channel[channel].vref = adafruit_mcp4728.Vref.INTERNAL else: self.channel[channel].vref = adafruit_mcp4728.Vref.VDD self.channel[channel].gain = self.options_channels['gain'][ channel] if (self.options_channels['state_start'][channel] == "value" and self.options_channels['state_start_value'][channel]): self.channel[channel].value = int( 65535 * (self.options_channels['state_start_value'][channel] / self.vref)) self.dac.save_settings() self.output_setup = True except: self.logger.exception("Error setting up Output")
def setup_output(self): import adafruit_mcp4728 from adafruit_extended_bus import ExtendedI2C self.setup_output_variables(OUTPUT_INFORMATION) try: self.dac = adafruit_mcp4728.MCP4728( ExtendedI2C(self.output.i2c_bus), address=int(str(self.output.i2c_location), 16)) self.channel = { 0: self.dac.channel_a, 1: self.dac.channel_b, 2: self.dac.channel_c, 3: self.dac.channel_d } # Channel A if self.options_channels['vref'][0] == "internal": self.channel[0].vref = adafruit_mcp4728.Vref.INTERNAL else: self.channel[0].vref = adafruit_mcp4728.Vref.VDD self.channel[0].gain = self.options_channels['gain'][0] if (self.options_channels['state_start'][0] == "value" and self.options_channels['state_start_value'][0]): self.channel[0].value = int( 65535 * (self.options_channels['state_start_value'][0] / self.vref)) # Channel B if self.options_channels['vref'][1] == "internal": self.channel[1].vref = adafruit_mcp4728.Vref.INTERNAL else: self.channel[1].vref = adafruit_mcp4728.Vref.VDD self.channel[1].gain = self.options_channels['gain'][1] if (self.options_channels['state_start'][1] == "value" and self.options_channels['state_start_value'][1]): self.channel[1].value = int( 65535 * (self.options_channels['state_start_value'][1] / self.vref)) # Channel C if self.options_channels['vref'][2] == "internal": self.channel[2].vref = adafruit_mcp4728.Vref.INTERNAL else: self.channel[2].vref = adafruit_mcp4728.Vref.VDD self.channel[2].gain = self.options_channels['gain'][2] if (self.options_channels['state_start'][2] == "value" and self.options_channels['state_start_value'][2]): self.channel[2].value = int( 65535 * (self.options_channels['state_start_value'][2] / self.vref)) # Channel D if self.options_channels['vref'][3] == "internal": self.channel[3].vref = adafruit_mcp4728.Vref.INTERNAL else: self.channel[3].vref = adafruit_mcp4728.Vref.VDD self.channel[3].gain = self.options_channels['gain'][3] if (self.options_channels['state_start'][3] == "value" and self.options_channels['state_start_value'][3]): self.channel[3].value = int( 65535 * (self.options_channels['state_start_value'][3] / self.vref)) self.dac.save_settings() self.output_setup = True except: self.output_setup = False
# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries # SPDX-License-Identifier: MIT import board import busio import adafruit_mcp4728 i2c = busio.I2C(board.SCL, board.SDA) mcp4728 = adafruit_mcp4728.MCP4728(i2c) mcp4728.channel_a.value = 65535 # Voltage = VDD mcp4728.channel_b.value = int(65535 / 2) # VDD/2 mcp4728.channel_c.value = int(65535 / 4) # VDD/4 mcp4728.channel_d.value = 0 # 0V