Would this work (Python code below, but you get the idea).
Start with a known bit state, 00000000 (all bits off). I'm using decimal values in the example below but imagine their binary equivalents.
Then use a bitwise XOR to toggle a bit value at a known placeholder. You would reset the state variable with this new state and then send to the MCP21037. If this were to be done we could then toggle the relay outputs whilst easily being able to send a new 8-bit value to the MCP21037 leaving all other bits untouched.
The first operation toggles bit 0 on and off, the second operation toggles bit 7 on and off:
Code: Select all
>>> current_state = 0
>>> current_state ^= 1 << 0
>>> print current_state
1
>>> current_state ^= 1 << 0
>>> print current_state
0
>>> current_state ^= 1 << 7
>>> print current_state
128
>>> current_state ^= 1 << 7
>>> print current_state
0
>>>