Skip to content

Commit

Permalink
i2c_gpio: Extend output_values to only update part of the register
Browse files Browse the repository at this point in the history
With this change we are able to update only part of the output pins
of a I2C port expander.

This will be needed when we want to preserve some state in the USB-SD-Mux.
For example: When updating the multiplexer we want to keep the state of the
outputs.

Signed-off-by: Chris Fiege <[email protected]>
  • Loading branch information
SmithChart committed Nov 24, 2023
1 parent a9d5d7f commit 0bb9612
Showing 1 changed file with 12 additions and 2 deletions.
14 changes: 12 additions & 2 deletions usbsdmux/i2c_gpio.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,15 +77,25 @@ def set_pin_to_input(self, pins):
self._directionMask = self._directionMask | pins
self._write_register(self._register_configuration, self._directionMask)

def output_values(self, values):
def output_values(self, values: int, bitmask: int = 0xFF):
"""
Writes the given values to the GPIO-expander.
Pins configured as Inputs are not affected by this.
Arguments:
values -- Combination of Pca9536.gpio_*
bitmask -- Only update bits in the register that are '1' in the bitmask
"""
self._write_register(self._register_outputPort, values)

if bitmask == 0xFF:
# trivial case: Let's just write the value
self._write_register(self._register_outputPort, values)
else:
# complex case: Let's do a read-modify-write
val = self.read_register(self.register_outputPort, 1)[0]
val = (val & ~bitmask) & 0xFF # reset masked bits
val = val | (values & bitmask) # set bits set in values and bitmask
self._write_register(self.register_outputPort, val)


class Pca9536(I2cGpio):
Expand Down

0 comments on commit 0bb9612

Please sign in to comment.