# mPy - Monitor running on kit FSK0001, simple apprach, lacks contact d#ebounce
#    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

# ========================= IRQ ==========================

def onBtnRed(sender):
    print(json.dumps({'btn': 'red'}))
    
    
def onBtnYellow(sender):
    print(json.dumps({'btn': 'yellow'}))
    
    
def onBtnGreen(sender):
    print(json.dumps({'btn': 'green'}))
    
    
def onBtnBlue(sender):
    print(json.dumps({'btn': 'blue'}))
    
# ======================= Hardware =======================

btn_red    = Pin(14, Pin.IN, Pin.PULL_UP)
btn_yellow = Pin(27, Pin.IN, Pin.PULL_UP)
btn_green  = Pin(26, Pin.IN, Pin.PULL_UP)
btn_blue   = Pin(25, Pin.IN, Pin.PULL_UP)

btn_red.irq   (onBtnRed,    Pin.IRQ_FALLING)
btn_yellow.irq(onBtnYellow, Pin.IRQ_FALLING)
btn_green.irq (onBtnGreen,  Pin.IRQ_FALLING)
btn_blue.irq  (onBtnBlue,   Pin.IRQ_FALLING)

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

def monitor():
    lcd = Lcd()
    while True:
        if s := nbInput():				# s is UTF-8
            lcd.print(s[:16])


monitor()

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