Source code for CB3.pca9557

"""Driver for PCA9557 I2C I/O Expander"""

REG_INP = 0
REG_OUT = 1   # Default: 0x00
REG_INV = 2   # Invert input pins. Defaults to 0xF0
REG_DIR = 3   # Direction: 1=In, 0=Out. Defaults to 0xFF


[docs]class PCA9557(): """I2C I/O Expander Driver""" def __init__(self, i2c, address): self.i2c = i2c self.addr = address self.resultbuf = bytearray(1) self.cached_out = 0
[docs] def read(self, reg): """Read 8-bit register""" self.i2c.try_lock() self.i2c.writeto(self.addr, reg.to_bytes(1, 'big')) self.i2c.readfrom_into(self.addr, self.resultbuf) val = self.resultbuf[0] self.i2c.unlock() return val
[docs] def write(self, reg, val): """Write 8-bit register""" cmd = bytes((reg, val)) self.i2c.try_lock() self.i2c.writeto(self.addr, cmd) self.i2c.unlock()
[docs] def output(self, val): """Cached write to output port""" if val != self.cached_out: self.write(REG_OUT, val) self.cached_out = val
[docs] def setbits(self, mask, do_set): """Bitmask write to ouput port""" val = self.cached_out if do_set: val |= mask else: val &= ~mask self.output(val)