def _write24(self, command, value): i2c_start(self.busno) try: if not i2c_write(self.busno, self.address): raise I2CError("TCA6424A failed to ack address") if not i2c_write(self.busno, command): raise I2CError("TCA6424A failed to ack command") for i in range(3): if not i2c_write(self.busno, value >> 16): raise I2CError("TCA6424A failed to ack data") value <<= 8 finally: i2c_stop(self.busno)
def set(self, data): """Drive data on the quasi-bidirectional pins. :param data: Pin data. High bits are weakly driven high (and thus inputs), low bits are strongly driven low. """ i2c_start(self.busno) try: if not i2c_write(self.busno, self.address): raise I2CError("PCF8574A failed to ack address") if not i2c_write(self.busno, data): raise I2CError("PCF8574A failed to ack data") finally: i2c_stop(self.busno)
def i2c_write_byte(busno, busaddr, data, ack=True): """Write one byte to a device. :param busno: I2C bus number :param busaddr: 8 bit I2C device address (LSB=0) :param data: Data byte to be written :param nack: Allow NACK """ i2c_start(busno) try: if not i2c_write(busno, busaddr): raise I2CError("failed to ack bus address") if not i2c_write(busno, data) and ack: raise I2CError("failed to ack write data") finally: i2c_stop(busno)
def set(self, channel): """Select one channel. Selecting multiple channels at the same time is not supported by this driver. :param channel: channel number (0-7) """ i2c_start(self.busno) try: if not i2c_write(self.busno, self.address): raise I2CError("PCA9548 failed to ack address") if not i2c_write(self.busno, 1 << channel): raise I2CError("PCA9548 failed to ack control word") finally: i2c_stop(self.busno)
def readback(self): i2c_start(self.busno) r = 0 try: if not i2c_write(self.busno, self.address | 1): raise I2CError("PCA9548 failed to ack address") r = i2c_read(self.busno, False) finally: i2c_stop(self.busno) return r
def i2c_write_many(busno, busaddr, addr, data, ack_last=True): """Transfer multiple bytes to a device. :param busno: I2c bus number :param busaddr: 8 bit I2C device address (LSB=0) :param addr: 8 bit data address :param data: Data bytes to be written :param ack_last: Expect I2C ACK of the last byte written. If `False`, the last byte may be NACKed (e.g. EEPROM full page writes). """ n = len(data) i2c_start(busno) try: if not i2c_write(busno, busaddr): raise I2CError("failed to ack bus address") if not i2c_write(busno, addr): raise I2CError("failed to ack data address") for i in range(n): if not i2c_write(busno, data[i]) and (i < n - 1 or ack_last): raise I2CError("failed to ack write data") finally: i2c_stop(busno)
def i2c_read_many(busno, busaddr, addr, data): """Transfer multiple bytes from a device. :param busno: I2c bus number :param busaddr: 8 bit I2C device address (LSB=0) :param addr: 8 bit data address :param data: List of integers to be filled with the data read. One entry ber byte. """ m = len(data) i2c_start(busno) try: if not i2c_write(busno, busaddr): raise I2CError("failed to ack bus address") if not i2c_write(busno, addr): raise I2CError("failed to ack data address") i2c_restart(busno) if not i2c_write(busno, busaddr | 1): raise I2CError("failed to ack bus read address") for i in range(m): data[i] = i2c_read(busno, ack=i < m - 1) finally: i2c_stop(busno)
def get(self): """Retrieve quasi-bidirectional pin input data. :return: Pin data """ i2c_start(self.busno) ret = 0 try: if not i2c_write(self.busno, self.address | 1): raise I2CError("PCF8574A failed to ack address") ret = i2c_read(self.busno, False) finally: i2c_stop(self.busno) return ret
def i2c_read_byte(busno, busaddr): """Read one byte from a device. :param busno: I2C bus number :param busaddr: 8 bit I2C device address (LSB=0) :returns: Byte read """ i2c_start(busno) data = 0 try: if not i2c_write(busno, busaddr | 1): raise I2CError("failed to ack bus read address") data = i2c_read(busno, ack=False) finally: i2c_stop(busno) return data