import sys, time
from machine import Pin, UART
led = Pin("LED", Pin.OUT)
gp = [Pin(i, Pin.OUT) for i in range(8)]
time.sleep(0.1) # Wait for USB to become ready
print("GPIO expander")
state = 0
while True:
buf = sys.stdin.readline()
cmd, _, num = buf.partition(' ')
if buf.startswith("set"):
state = int(num)
else:
continue
for i in range(8):
# Invert the logic (open-drain means 0s are "on"), but
# it's easier if 1s are "on"
gp[i].value(((state>>i) & 1) ^ 1)
sys.stdout.write('State: {:x}\n'.format(state))