import time
from machine import UART, Pin
time.sleep(0.1) # Wait for USB to become ready
WAIT_UART_MS = 100
# , tx=Pin(4), rx=Pin(5),
uart0 = UART(0, baudrate=115200, timeout=WAIT_UART_MS)
led = Pin('LED', Pin.OUT, value=0)
buf = b''
def led_on():
led.on()
return b'on'
def led_off():
led.off()
return b'off'
COMMANDS = {
b'led_on': led_on,
b'led_off': led_off,
}
def run_cmd(cmd):
if cmd in COMMANDS:
print(f"Running {cmd:s}")
uart0.write(COMMANDS[cmd]() + b'\r\n')
else:
print(f"Invalid Command: {cmd:s}")
while (True):
char = uart0.read()
if not char:
continue
buf += char
while True:
idx = buf.find(b'\r')
if idx < 0:
break
token = buf[:idx].strip()
buf = buf[idx+1:]
run_cmd(token)
break