########################################
# TEST FOR BUTTONS
# This script will test the buttons wired into the ESP8266
# These are wired with different coloured buttons as follows
#    BTN_BLUE     input pin on GPIO5  = d1
#    BTN_YELLOW   input pin on GPIO4  = d2
#    BTN_RED      input pin on GPIO14 = d5
#    BTN_BLACK    input pin on GPIO12 = d6
#    BTN_GREEN    input pin on GPIO13 = d7
########################################
from machine import Pin
from time import sleep  
from Button import Button

BTN_BLUE="BLUE"
BTN_YELLOW="YELLOW"
BTN_RED="RED"
BTN_BLACK="BLACK"
BTN_GREEN="GREEN"

# List of bttons
buttons = {
    BTN_BLUE : Button(BTN_BLUE, 5),     # create input pin on GPIO5 = d1
    BTN_YELLOW: Button(BTN_YELLOW, 4),  # create input pin on GPIO4 = d2
    BTN_RED : Button(BTN_RED, 14),      # create input pin on GPIO14 = d5
    BTN_BLACK : Button(BTN_BLACK, 12),  # create input pin on GPIO12 = d6
    BTN_GREEN : Button(BTN_GREEN, 13)   # create input pin on GPIO13 = d7
}
LED = Pin(2, Pin.OUT)   # onboard LED is PIN 2
    
########################################
# Setup
########################################
def initialiseAll():
    
    LED.value(0)
    print("initialiseAll")
   
    LED.value(1)

########################################
# test an individual button and return boolean
########################################
def testButton(buttonKey):
    buttonToTest = buttons[buttonKey]
    if buttonToTest is None:
        print("Cannot find button "+buttonKey)
    else:
        if buttonToTest.isPressed():
            return True 
    return False 

########################################
# test pin and update button flags
########################################
def testButtonSetFlags(buttonFlags, buttonKey):
    if testButton(buttonKey):
        buttonFlags = buttonFlags +" | " + buttonKey
    return buttonFlags

########################################
# Test all of the buttons updating the flags
########################################
def testAllButtons():
    buttonFlags = ""
    buttonFlags =  testButtonSetFlags(buttonFlags, BTN_BLUE)
    buttonFlags =  testButtonSetFlags(buttonFlags, BTN_YELLOW)
    buttonFlags =  testButtonSetFlags(buttonFlags, BTN_RED)
    buttonFlags =  testButtonSetFlags(buttonFlags, BTN_BLACK)
    buttonFlags =  testButtonSetFlags(buttonFlags, BTN_GREEN)

    return buttonFlags

########################################
# Print state of all buttons
########################################
def printButtonState(buttonFlags, previousFlags):

    if buttonFlags != previousFlags:
        LED.value(0)
        send_data = "/act/" + buttonFlags
        
        print("*** CHANGED ***" + str(send_data))
        LED.value(1)
        


########################################
# main program loop
########################################
def mainLoop():
    initialiseAll()
    # take a copy to hold the previous values
    buttonFlags = ""
    while True:
        previousFlags = buttonFlags
        buttonFlags = testAllButtons()
        printButtonState(buttonFlags, previousFlags)

        sleep(0.1)

########################################
# start it
########################################

mainLoop()
    
