def __write_config(self, config): try: I2C.start_tx(self.__addr) I2C.write(ADS1115.__REG_CONFIG, config >> 8, config & 0xff) finally: I2C.end_tx()
def convert(cls): try: I2C.start_tx(MPL115A2Reg.__I2C_ADDR) I2C.write(cls.__CMD_START_CONV, 0x00) finally: I2C.end_tx()
def write(self, byte): try: I2C.start_tx(self.__addr) I2C.write(byte) finally: I2C.end_tx()
def __write(self, config): try: I2C.start_tx(self.addr) I2C.write(config) finally: I2C.end_tx()
def __read_reg(cls, addr): try: I2C.start_tx(cls.__ADDR) value = I2C.read_cmd(addr, 1) finally: I2C.end_tx() return value
def __write_image(cls, memory_addr, values): # max 32 values try: I2C.start_tx(Host.DFE_EEPROM_ADDR) I2C.write_addr(memory_addr, *values) time.sleep(cls.__TWR) finally: I2C.end_tx()
def read(self): try: I2C.start_tx(self.__addr) byte = I2C.read(1) finally: I2C.end_tx() return byte
def __read_raw(self): try: I2C.start_tx(self.__I2C_ADDR) values = I2C.read_cmd(self.__reg_addr, 2) return values[0] << 8 | values[1] finally: I2C.end_tx()
def status(self): try: I2C.start_tx(self.__addr) status_msb, status_lsb, _ = I2C.read_cmd16(SHT31.__CMD_READ_STATUS, 3) return (status_msb << 8) | status_lsb finally: I2C.end_tx()
def __read_image(cls, memory_addr, count): try: I2C.start_tx(Host.DFE_EEPROM_ADDR) content = I2C.read_cmd(memory_addr, count) return EEPROMImage(content) finally: I2C.end_tx()
def __read_config(self): try: I2C.start_tx(self.__addr) msb, lsb = I2C.read_cmd(ADS1115.__REG_CONFIG, 2) finally: I2C.end_tx() config = (msb << 8) | lsb return config
def reset(self): try: I2C.start_tx(self.__addr) I2C.write16(SHT31.__CMD_RESET) time.sleep(0.001) I2C.write16(SHT31.__CMD_CLEAR) time.sleep(0.001) finally: I2C.end_tx()
def sample(self): try: I2C.start_tx(self.__addr) temp_msb, temp_lsb, _, humid_msb, humid_lsb, _ = I2C.read_cmd16( SHT31.__CMD_READ_SINGLE_HIGH, 6) raw_humid = (humid_msb << 8) | humid_lsb raw_temp = (temp_msb << 8) | temp_lsb return SHTDatum(SHT31.humid(raw_humid), SHT31.temp(raw_temp)) finally: I2C.end_tx()
def __read_temp(cls): try: I2C.start_tx(cls.__ADDR) msb, lsb = I2C.read_cmd(cls.__REG_TEMP, 2) finally: I2C.end_tx() # render voltage... unsigned_c = float(msb & 0x1f) * 16 + float(lsb) / 16 sign = msb & 0x10 temp = 256 - unsigned_c if sign else unsigned_c return temp
def __read_array(cls, device_addr, memory_addr, count): try: I2C.start_tx(Host.DFE_UID_ADDR) # I2C.read(1) finally: I2C.end_tx() try: I2C.start_tx(Host.DFE_UID_ADDR) # I2C.write(0x80) return I2C.read_cmd(0x80, count) # memory_addr, finally: I2C.end_tx()
def sample(self): if self.__addr == 0: return None try: I2C.start_tx(self.__addr) temp_msb, temp_lsb, _, humid_msb, humid_lsb, _ = I2C.read_cmd16(SHT31.__CMD_READ_SINGLE_HIGH, 6, wait=self.MEASUREMENT_DURATION) raw_humid = (humid_msb << 8) | humid_lsb raw_temp = (temp_msb << 8) | temp_lsb return SHTDatum(SHT31.humid(raw_humid), SHT31.temp(raw_temp)) finally: I2C.end_tx()
def __write(self, command, wait, *values): try: self.obtain_lock() try: I2C.start_tx(self.__i2c_addr) encoded = self.__encode(values) I2C.write_addr16(command, *encoded) finally: I2C.end_tx() time.sleep(wait) finally: self.release_lock()
def read_conversion(self): """ read most recent conversion returned value is voltage """ try: I2C.start_tx(self.addr) v, config = self.__read() finally: I2C.end_tx() self.release_lock() if config & MCP342X.__START: raise ValueError(self.__class__.__name__ + ":read_conversion: conversion not ready.") return v
def __read_conv(self): try: I2C.start_tx(self.__addr) msb, lsb = I2C.read_cmd(ADS1115.__REG_CONV, 2) finally: I2C.end_tx() # render voltage... unsigned = (msb << 8) | lsb # print("unsigned: 0x%04x" % unsigned) signed = struct.unpack('h', struct.pack('H', unsigned)) v = (signed[0] / 32767.5) * ADS1115.__FULL_SCALE[self.__gain] return v
def __read(self, command, wait, count=0): try: self.obtain_lock() try: I2C.start_tx(self.__i2c_addr) encoded = I2C.read_cmd16(command, count) values = self.__decode(encoded) finally: I2C.end_tx() time.sleep(wait) return values finally: self.release_lock()
def convert(self): """ start single-shot conversion, wait for ready, then read warning: creates high level of I2C traffic returned value is voltage """ self.start_conversion() try: I2C.start_tx(self.addr) while True: v, config = self.__read() if not (config & MCP342X.__START): break time.sleep(MCP342X.RATE_240) finally: I2C.end_tx() self.release_lock() return v
def __write_config(cls, config): try: I2C.start_tx(cls.__ADDR) I2C.write(cls.__REG_CONFIG, config >> 8, config & 0xff) finally: I2C.end_tx()
def __write_reg(cls, addr, value): try: I2C.start_tx(cls.__ADDR) I2C.write(addr, value) finally: I2C.end_tx()