#test_anyButton.py # #anyButton.py Function # by D@CC on 2023CMar23 # Minicom >>>minicom -o -D /dev/ttyACM0 import time import board from digitalio import DigitalInOut, Direction def anyButton(device,isInUse0): # this routine monitors 8 buttons on the Wio Terminal # it returns the name of the button that is pressed (or "none") # the list of 8 buttons are named: bList=['SWITCH_PRESS', 'SWITCH_DOWN', 'SWITCH_LEFT', 'SWITCH_RIGHT', 'SWITCH_UP','BUTTON_1', 'BUTTON_2', 'BUTTON_3'] # The names in the board function library can be seen using the statement dir(board) # isInUse0=False result="None" if not device == "Wio Terminal" : print("device must be 'Wio Terminal'") return result else : lenList=len(bList) lenList=1 #initially on the center uJoyStick button will be monitored for i in range(lenList) : if i==0: # set up the next button if isInUse0 == False : isInUse0= True button = DigitalInOut(board.SWITCH_PRESS) button.direction = Direction.INPUT #if end isPressed=False #is Pressed is False until a button is pressed while isPressed==False: isUp = button.value # isUp = True when button not pressed # isUp = False when button is pressed if isUp== False : #meaning the button was pressed. print("in anyButton, a button was pressed") buttonName=bList[0] # bList[0] is 'SWITCH_PRESS' print(buttonName," was pressed.") isPressed=True # takes us out of the while loop #break #out of the while loop #if end #while end #if end time.sleep(1) # don't stay in a tight loop print("in anyButton, after sleep") #for end #if end return buttonName #def end progname="test_anyButton.py" isInUse0=False print("isInUse0:",isInUse0) while True : print("while monitoring buttons at 21:42. . . ") buttonPressed = anyButton("Wio Terminal",isInUse0) print("in main,after anyButton, saw:",buttonPressed) time.sleep(1) #while end #/test_anyButton.py