Skip to content

Commit

Permalink
Fix GPIO expander writes being broken
Browse files Browse the repository at this point in the history
Previous code was only writing to one expander & may have suffered from
the multi-byte read bug
  • Loading branch information
hedgehog1029 committed Jun 10, 2024
1 parent e8b7477 commit bf5eeef
Showing 1 changed file with 11 additions and 9 deletions.
20 changes: 11 additions & 9 deletions modules/tildagonos.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]:
Expand Down

0 comments on commit bf5eeef

Please sign in to comment.