diff --git a/modules/tildagonos.py b/modules/tildagonos.py index 93ef995..b8e9a4b 100644 --- a/modules/tildagonos.py +++ b/modules/tildagonos.py @@ -65,15 +65,17 @@ def init_gpio(self): self.system_i2c.writeto_mem(0x5A, 0x11, bytes([0x10])) def set_egpio_pin(self, pin, state): - portstates = list(map(int, self.system_i2c.readfrom_mem(pin[0], 0x02, 2))) - if state: - self.system_i2c.writeto_mem( - 0x5A, 0x02 + pin[1], bytes([portstates[pin[1]] | pin[2]]) - ) - else: - self.system_i2c.writeto_mem( - 0x5A, 0x02 + pin[1], bytes([portstates[pin[1]] & (pin[2] ^ 0xFF)]) - ) + """ + Write an output state to a specific pin on a GPIO expander + + @param pin: tuple of (i2c addr, port number 0/1, bitmask) selecting the pin to modify + @param state: True to set the pin high, False to set the pin low + """ + addr, port, bit = pin + old_state = self.system_i2c.readfrom_mem(addr, 0x02 + port, 1)[0] + new_state = (old_state | bit) if state else (old_state & (0xFF ^ bit)) + + self.system_i2c.writeto_mem(addr, 0x02 + port, bytes([new_state])) def read_egpios(self): for i in [0x58, 0x59, 0x5A]: