#!/usr/bin/env python from smbus import SMBus import time from struct import * import array import numpy as np # instead of having some varisable associated to the class that we should # update continuously with the the chip, we only access to the variables # to the chip via set_/get_ functions class ADUCv2p1: # Initialize the connection to the board # addr: i2c addres, can be obtained by running "i2detect -y 1" from terminal # if with_about = True, will print information from the board def __init__(self,addr,with_about=False): self.bus_pi = SMBus(1) self._addr = addr; self.get_wf_len(); if with_about==True: print( self.get_text()) print( "-----------------\n") # Read last valid waveform from the microcontroller, up to 256 points def read_data(self): dat = [] if self._wf_len>=256: N=16 self._wf_len = 256 else: N = int(self._wf_len/16+1) ## add an error message in case wf_len/16 is not an integer? self.set_transf(1)# to remove for i in range(0,N): block = self.bus_pi.read_i2c_block_data(self._addr,i,32) buff = pack("32B",*block) dat = np.append(dat,unpack("<16H",buff)) self.set_transf(0) #to remove dat=np.uint16(dat) return dat[0:self._wf_len] # def get_text(self): dat = [] for i in range(0,16): block = self.bus_pi.read_i2c_block_data(self._addr,i+50,32) buff = pack("32B",*block) dat = np.append(dat,unpack("32c",buff)) return ''.join(dat.astype(str))#''.join(str((dat[0:3]).tostring())) # sets DACX where X is the channel ch to the voltage V (in the range 0 to 4095) # Make sure you are in a mode where the voltage is not being inmediately overwritten def set_Vout(self,ch,V): self.bus_pi.write_word_data(self._addr,100+ch,V) #execute the setting commande on the chip self.bus_pi.read_i2c_block_data(self._addr,105,1) return def get_Vout(self,ch): #execute the reading commande on the chip self.bus_pi.read_i2c_block_data(self._addr,104,1) block = self.bus_pi.read_i2c_block_data(self._addr,100+ch,2) buff = pack("2B",*block) dat = unpack(">1 def set_ID(self,ID): self._addr = ID return self.bus_pi.write_byte_data(self._addr,120,ID<<1) # def get_channel(self): block = self.bus_pi.read_i2c_block_data(self._addr,121,2) buff = pack("2B",*block) dat = unpack("256: print( "sorry, it is not possible to have more than 256 point per waveform.\n") self._wf_len = 256 return self.bus_pi.write_word_data(self._addr,123,self._wf_len) def get_wf_len(self): block = self.bus_pi.read_i2c_block_data(self._addr,123,2) buff = pack("2B",*block) dat = unpack("