def read(self, length, config=I2CConfig.AA_I2C_NO_FLAGS): '''read 1 byte from slave address ''' # read 1 byte each time for easy # length = 2 ata_in = array_u08(length) (ret, num_read) = api.py_aa_i2c_read_ext(self.handle, self.slave_addr, config, length, ata_in) if (ret != 0): api.py_aa_i2c_free_bus(self.handle) raise_i2c_ex(ret) if (num_read != length): raise_aa_ex(-102) val = ata_in return val
def read(self, length, config=I2CConfig.AA_I2C_NO_FLAGS): '''read 1 byte from slave address ''' # read 1 byte each time for easy # length = 2 ata_in = array_u08(length) (ret, num_read) = api.py_aa_i2c_read_ext(self.handle, self.slave_addr, config, length, ata_in) if(ret != 0): api.py_aa_i2c_free_bus(self.handle) raise_i2c_ex(ret) if(num_read != length): raise_aa_ex(-102) val = ata_in return val
def write(self, wata, config=I2CConfig.AA_I2C_NO_FLAGS): '''write ata to slave address ata can be byte or array of byte ''' if (type(wata) == int): ata_out = array('B', [wata]) length = 1 elif (type(wata) == list): ata_out = array('B', wata) length = len(wata) else: raise TypeError("i2c ata to be written is not valid") (ret, num_written) = api.py_aa_i2c_write_ext(self.handle, self.slave_addr, config, length, ata_out) if (ret != 0): api.py_aa_i2c_free_bus(self.handle) raise_i2c_ex(ret) if (num_written != length): raise_aa_ex(-103)
def write(self, wata, config=I2CConfig.AA_I2C_NO_FLAGS): '''write ata to slave address ata can be byte or array of byte ''' if(type(wata) == int): ata_out = array('B', [wata]) length = 1 elif(type(wata) == list): ata_out = array('B', wata) length = len(wata) else: raise TypeError("i2c ata to be written is not valid") (ret, num_written) = api.py_aa_i2c_write_ext(self.handle, self.slave_addr, config, length, ata_out) if(ret != 0): api.py_aa_i2c_free_bus(self.handle) raise_i2c_ex(ret) if(num_written != length): raise_aa_ex(-103)
def open(self, portnum=None, serialnumber=None): ''' find ports, and open the port with portnum or sn, config the aardvark tool params like bitrate, slave address etc, ''' ports = self.find_devices() logging.debug("find ports: " + str(ports)) self.port = None if(serialnumber): for port in ports: handle = api.py_aa_open(port) if(api.py_aa_unique_id(handle) == serialnumber): logging.debug("SN: " + str(api.py_aa_unique_id(handle))) self.port = port api.py_aa_close(handle) break api.py_aa_close(handle) if(self.port is None): raise_aa_ex(-601) elif(portnum): self.port = portnum else: self.port = 0 logging.debug("open: " + str(self.port)) self.handle = api.py_aa_open(self.port) if(self.handle <= 0): raise_aa_ex(self.handle) # Ensure that the I2C subsystem is enabled api.py_aa_configure(self.handle, I2CConfig.AA_CONFIG_SPI_I2C) api.py_aa_i2c_pullup(self.handle, I2CConfig.AA_I2C_PULLUP_BOTH) api.py_aa_target_power(self.handle, I2CConfig.AA_TARGET_POWER_NONE) # Set the bitrate, in khz self.bitrate = api.py_aa_i2c_bitrate(self.handle, self.bitrate) # Set the bus lock timeout, in ms api.py_aa_i2c_bus_timeout(self.handle, self.bus_timeout) # Free bus api.py_aa_i2c_free_bus(self.handle)
def open(self, portnum=None, serialnumber=None): ''' find ports, and open the port with portnum or sn, config the aardvark tool params like bitrate, slave address etc, ''' ports = self.find_devices() logging.debug("find ports: " + str(ports)) self.port = None if (serialnumber): for port in ports: handle = api.py_aa_open(port) if (api.py_aa_unique_id(handle) == serialnumber): logging.debug("SN: " + str(api.py_aa_unique_id(handle))) self.port = port api.py_aa_close(handle) break api.py_aa_close(handle) if (self.port is None): raise_aa_ex(-601) elif (portnum): self.port = portnum else: self.port = 0 logging.debug("open: " + str(self.port)) self.handle = api.py_aa_open(self.port) if (self.handle <= 0): raise_aa_ex(self.handle) # Ensure that the I2C subsystem is enabled api.py_aa_configure(self.handle, I2CConfig.AA_CONFIG_SPI_I2C) api.py_aa_i2c_pullup(self.handle, I2CConfig.AA_I2C_PULLUP_BOTH) api.py_aa_target_power(self.handle, I2CConfig.AA_TARGET_POWER_NONE) # Set the bitrate, in khz self.bitrate = api.py_aa_i2c_bitrate(self.handle, self.bitrate) # Set the bus lock timeout, in ms api.py_aa_i2c_bus_timeout(self.handle, self.bus_timeout) # Free bus api.py_aa_i2c_free_bus(self.handle)
def aa_i2c_free_bus (aardvark): """usage: int return = aa_i2c_free_bus(Aardvark aardvark)""" if not AA_LIBRARY_LOADED: return AA_INCOMPATIBLE_LIBRARY # Call API function return api.py_aa_i2c_free_bus(aardvark)