# mPy - Monitor running on kit FSK0001
#    incoming USB strings are displayed on LCD (connect SDA-SDA and SCL-SCL)
#    button pressed are sent as JSON {"btn": "red"}
# Copyright by Anthony Vogelaar, Perth Scotland
# rev 0.10  2026-03-26  initial release

import	time, json
from	machine		import	Pin
from	drivers		import	nbInput
from	driver_lcd	import	Lcd

# ======================== Button ==========================

class Button:
    
    def __init__(self, pin_nr):
        self.btn = Pin(pin_nr, Pin.IN, Pin.PULL_UP)
        self.sts = 0
      
      
    # call at 50 mS, return True on release edge
    def exe50(self):
        if self.btn(): 			# if released
            if self.sts:
                self.sts -= 1	# debounce
                if self.sts: return
                return True
        else: self.sts = 4		# if pressed
        
# ======================= Hardware =======================

buttons = [Button(pin_nr) for pin_nr in (14, 27, 26, 25)]

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

def monitor():
    lcd      = Lcd()
    colours  = ('red', 'yellow', 'green', 'blue')
    while True:
        if s := nbInput():				# s is UTF-8 or None
            lcd.print(s[:16])
        for btn, col in zip(buttons, colours):
            if btn.exe50(): print(json.dumps({'btn':col}))
        time.sleep_ms(50)


monitor()

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