gamepad

Button handling in the background

class gamepad.GamePad(b1: digitalio.DigitalInOut, b2: digitalio.DigitalInOut, b3: digitalio.DigitalInOut, b4: digitalio.DigitalInOut, b5: digitalio.DigitalInOut, b6: digitalio.DigitalInOut, b7: digitalio.DigitalInOut, b8: digitalio.DigitalInOut)

Scan buttons for presses

Usage:

import board
import digitalio
import gamepad
import time

B_UP = 1 << 0
B_DOWN = 1 << 1


pad = gamepad.GamePad(
    digitalio.DigitalInOut(board.D10),
    digitalio.DigitalInOut(board.D11),
)

y = 0
while True:
    buttons = pad.get_pressed()
    if buttons & B_UP:
        y -= 1
        print(y)
    elif buttons & B_DOWN:
        y += 1
        print(y)
    time.sleep(0.1)
    while buttons:
        # Wait for all buttons to be released.
        buttons = pad.get_pressed()
        time.sleep(0.1)
get_pressed(self)

Get the status of buttons pressed since the last call and clear it.

Returns an 8-bit number, with bits that correspond to buttons, which have been pressed (or held down) since the last call to this function set to 1, and the remaining bits set to 0. Then it clears the button state, so that new button presses (or buttons that are held down) can be recorded for the next call.

deinit(self)

Disable button scanning.