Created
January 16, 2022 23:05
-
-
Save makermelissa/932d902c093425c41836fbec2a7e2aa4 to your computer and use it in GitHub Desktop.
Code to Control Servos with Bluepad32
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Copyright 2020 - 2021, Ricardo Quesada, http://retro.moe | |
| # SPDX-License-Identifier: Apache-2.0 | |
| import time | |
| import board | |
| import busio | |
| from digitalio import DigitalInOut | |
| from simpleio import map_range | |
| from adafruit_servokit import ServoKit | |
| from bluepad32.bluepad32 import Bluepad32 | |
| DEADZONE = 50 | |
| # Connected gamepad | |
| gamepad = None | |
| # Set channels to the number of servo channels on your kit. | |
| # 8 for FeatherWing, 16 for Shield/HAT/Bonnet. | |
| left_kit = ServoKit(channels=16) | |
| # Callback that will be called once a gamepad is connected | |
| def on_connect(gp): | |
| global gamepad # pylint: disable=global-statement | |
| gamepad = gp | |
| print("on_connect: ", gp) | |
| # Change ligthbar to Green: Red, Green, Blue | |
| gp.set_lightbar_color((0x00, 0xFF, 0x00)) | |
| # Callback that will be called when a gamepad is disconnected | |
| def on_disconnect(gp): | |
| global gamepad # pylint: disable=global-statement | |
| gamepad = None | |
| print("on_disconnect: ", gp) | |
| def handleServo(input, servo): | |
| if input > (DEADZONE // 2) or input < (DEADZONE // -2): | |
| servo.angle = map_range(input, -512, 512, 0, 180) | |
| else: | |
| servo.angle = 90 | |
| esp32_cs = DigitalInOut(board.D10) | |
| esp32_ready = DigitalInOut(board.D9) | |
| esp32_reset = DigitalInOut(board.D6) | |
| spi = busio.SPI(board.SCK, board.MOSI, board.MISO) | |
| bp32 = Bluepad32(spi, esp32_cs, esp32_ready, esp32_reset, debug=0) | |
| bp32.setup_callbacks(on_connect, on_disconnect) | |
| # Should display "Bluepad32 for Airlift vXXX" | |
| print("Firmware version:", bp32.firmware_version) | |
| color = [0xFF, 0x00, 0x00] | |
| players_led = 0x01 | |
| while True: | |
| # Fetches data from Bluepad32 firmware, triggers callbaks, and more. | |
| # Must be called once per frame. | |
| try: | |
| bp32.update() | |
| except RuntimeError: | |
| pass | |
| if gamepad is None: | |
| continue | |
| if gamepad.button_a: | |
| print("X Button") | |
| if gamepad.button_b: | |
| print("Circle Button") | |
| if gamepad.button_x: | |
| print("Square Button") | |
| if gamepad.button_y: | |
| print("Triangle Button") | |
| if gamepad.button_l1: | |
| print("L1 Button") | |
| if gamepad.button_l2: | |
| print("L2 Button") | |
| if gamepad.button_thumb_l: | |
| print("L3 Button") | |
| if gamepad.button_r1: | |
| print("R1 Button") | |
| if gamepad.button_r2: | |
| print("R2 Button") | |
| if gamepad.button_thumb_r: | |
| print("R3 Button") | |
| if gamepad.dpad & 1: | |
| print("DPAD Up") | |
| if gamepad.dpad & 2: | |
| print("DPAD Down") | |
| if gamepad.dpad & 4: | |
| print("DPAD Right") | |
| if gamepad.dpad & 8: | |
| print("DPAD Left") | |
| if gamepad.misc_buttons & 1: | |
| print("PlayStation Button") | |
| if gamepad.misc_buttons & 2: | |
| print("Share Button") | |
| if gamepad.misc_buttons & 4: | |
| print("Option Button") | |
| handleServo(gamepad.axis_x, left_kit.servo[0]) | |
| handleServo(gamepad.axis_y, left_kit.servo[1]) | |
| handleServo(gamepad.axis_rx, left_kit.servo[2]) | |
| handleServo(gamepad.axis_ry, left_kit.servo[3]) | |
| #print(f"{gamepad.axis_x}, {gamepad.axis_y}, {gamepad.axis_rx}, {gamepad.axis_ry}") | |
| time.sleep(0.032) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment