# IX_libr_pico.py

# This example works with Maker Pi Pico
# buttonAction()
# returns True when button Action is down i.e. pressed within 10 seconds
#    else returns False.

#imports for buttonAction
import machine
import utime

def buttonAction(gPnn,withinTime):

    if gPnn <0 or gPnn>25:
        print("Error in buttonAction: pin # is out of range")
        return
    #end if
    if withinTime <0 or withinTime>180:
        print("Error in buttonAction: withinTime is out of range")
        return
    #end if
    button = machine.Pin(gPnn, machine.Pin.IN)
    t0=utime.time()
    #print("t0:",t0)
    tDelta=0
    isDown=False
    while tDelta<withinTime:
        state=button.value()
        #print("button (active low) state:",state)
        tNow=utime.time()
        tDelta=tNow-t0
        #print("at:",tDelta,"button",gPnn, "is ", state)
        utime.sleep(0.5)
        if state==0:
            isDown=True
            break                 # out of loop
        #end if
    #end while
    return isDown #returns a Boolean result
#end def #buttonAction_pico.py
# by David at ColeCanada.com
# on 2021EMay25
#source: na
#end program IX_libr_pico.py

GPnn=22
tSecs=10
print("Press button GP"+str(GPnn),"within",tSecs,"secs")
isDown=buttonAction(GPnn,tSecs)
if isDown==None:
    print("Error: Aborted Execution")
else:
    print("buttonAction is",isDown)
#end if
#testButton_pico.py