# mPy - Application controlled by external USB commands for toggling 4 leds
#       use cpy_gui.py as external controller
# Copyright by Anthony Vogelaar, Perth Scotland
# rev 0.10  2026-03-18  initial release

import	json
from	machine		import	Pin
from	drivers		import	nbInput

# ======================== Hardware ======================

class H:
    Red    = Pin(25, Pin.OUT, value=0)		# led Red   on pin 25
    Yellow = Pin(26, Pin.OUT, value=0)
    Green  = Pin(27, Pin.OUT, value=0)
    Blue   = Pin(14, Pin.OUT, value=0)

# ======================== Main ==========================

class Main:
    
    # setup and main loop
    def __init__(self):
        
        try:
            while True:
                if s_rx := nbInput():
                    try:
                        for k, v in json.loads(s_rx).items():
                            led = getattr(H, k)
                            print(led, v)
                            led(v)
                    except: pass
        finally:
            H.Red(0); H.Yellow(0); H.Green(0); H.Blue(0)
            
        
    # monitor and interpret incoming USB commands
    def taskMonitor(self):
        if s_rx := nbInput():
            try:
                for k, v in json.loads(s_rx).items():
                    getattr(H, k)(v)
            except: pass
        
        
Main()

# ======================== end ==========================
