diff options
author | Raghuram Subramani <raghus2247@gmail.com> | 2022-06-19 19:47:51 +0530 |
---|---|---|
committer | Raghuram Subramani <raghus2247@gmail.com> | 2022-06-19 19:47:51 +0530 |
commit | 4fd287655a72b9aea14cdac715ad5b90ed082ed2 (patch) | |
tree | 65d393bc0e699dd12d05b29ba568e04cea666207 /circuitpython/frozen/Adafruit_CircuitPython_HID/examples/hid_simple_gamepad.py | |
parent | 0150f70ce9c39e9e6dd878766c0620c85e47bed0 (diff) |
add circuitpython code
Diffstat (limited to 'circuitpython/frozen/Adafruit_CircuitPython_HID/examples/hid_simple_gamepad.py')
-rw-r--r-- | circuitpython/frozen/Adafruit_CircuitPython_HID/examples/hid_simple_gamepad.py | 57 |
1 files changed, 57 insertions, 0 deletions
diff --git a/circuitpython/frozen/Adafruit_CircuitPython_HID/examples/hid_simple_gamepad.py b/circuitpython/frozen/Adafruit_CircuitPython_HID/examples/hid_simple_gamepad.py new file mode 100644 index 0000000..614cc84 --- /dev/null +++ b/circuitpython/frozen/Adafruit_CircuitPython_HID/examples/hid_simple_gamepad.py @@ -0,0 +1,57 @@ +# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries +# SPDX-License-Identifier: MIT + +# You must add a gamepad HID device inside your boot.py file +# in order to use this example. +# See this Learn Guide for details: +# https://learn.adafruit.com/customizing-usb-devices-in-circuitpython/hid-devices#custom-hid-devices-3096614-9 + +import board +import digitalio +import analogio +import usb_hid + +from hid_gamepad import Gamepad + +gp = Gamepad(usb_hid.devices) + +# Create some buttons. The physical buttons are connected +# to ground on one side and these and these pins on the other. +button_pins = (board.D2, board.D3, board.D4, board.D5) + +# Map the buttons to button numbers on the Gamepad. +# gamepad_buttons[i] will send that button number when buttons[i] +# is pushed. +gamepad_buttons = (1, 2, 8, 15) + +buttons = [digitalio.DigitalInOut(pin) for pin in button_pins] +for button in buttons: + button.direction = digitalio.Direction.INPUT + button.pull = digitalio.Pull.UP + +# Connect an analog two-axis joystick to A4 and A5. +ax = analogio.AnalogIn(board.A4) +ay = analogio.AnalogIn(board.A5) + +# Equivalent of Arduino's map() function. +def range_map(x, in_min, in_max, out_min, out_max): + return (x - in_min) * (out_max - out_min) // (in_max - in_min) + out_min + + +while True: + # Buttons are grounded when pressed (.value = False). + for i, button in enumerate(buttons): + gamepad_button_num = gamepad_buttons[i] + if button.value: + gp.release_buttons(gamepad_button_num) + print(" release", gamepad_button_num, end="") + else: + gp.press_buttons(gamepad_button_num) + print(" press", gamepad_button_num, end="") + + # Convert range[0, 65535] to -127 to 127 + gp.move_joysticks( + x=range_map(ax.value, 0, 65535, -127, 127), + y=range_map(ay.value, 0, 65535, -127, 127), + ) + print(" x", ax.value, "y", ay.value) |