Accessing buttons and switch of wio terminal with ardupy

For those interested, here is a solution using Circuitpython. The only required library not included in the distribution is the debounce library. Works reasonably well. To get this to work with Ardupy, I had to put in the exact pin numbers which was a real pain–it was for this reason that after coding up the 2nd button I bailed on that approach. Until regular updates occur with Ardupy, I’m gonna work with Circuitpython. However, I’d love to have true interrupts with the micropython support.

Finally, I know the code could be cleaner…

Cheers

Using Circuitpython 6.0

import time
import board
from digitalio import DigitalInOut, Direction, Pull
from adafruit_debouncer import Debouncer


led = DigitalInOut(board.D13)
led.direction = Direction.OUTPUT


def setupWioButton(pinRef):
    pin = DigitalInOut(pinRef)
    pin.direction = Direction.INPUT
    pin.pull = Pull.UP
    button = Debouncer(pin)
    return button


buttonNames = ['A', 'B', 'C']
buttonList = [board.BUTTON_3, board.BUTTON_2, board.BUTTON_1]
buttonDict = {}

for i,b in enumerate(buttonNames):
    buttonDict[b] = setupWioButton(buttonList[i])



joyNames = ['DOWN', 'LEFT', 'PRESS', 'RIGHT', 'UP']
joyList = [board.SWITCH_DOWN,
               board.SWITCH_LEFT,
               board.SWITCH_PRESS,
               board.SWITCH_RIGHT,
               board.SWITCH_UP]

joyDict = {}

for i,j in enumerate(joyNames):
    joyDict[j] = setupWioButton(joyList[i])


while True:
    for i,b in enumerate(buttonNames):
        curSwitch = buttonDict[b]
        curSwitch.update()
        if curSwitch.fell:
            print("Button %s Pressed"%b)
            led.value = True
            # print("LED ON")
            time.sleep(1)
        if curSwitch.rose:
            print("Button %s Released"%b)
            led.value = False
            # print("LED OFF")
            time.sleep(1)

    for i,j in enumerate(joyNames):
        curSwitch = joyDict[j]
        curSwitch.update()
        if curSwitch.fell:
            print("Joystick %s"%j)