I have a Raspberry Pi 5 project where I'm trying to use two Bluetooth devices simultaneously in a python script. The devices are a PS4 game controller and DW-CT14++ Bluetooth audio amplifier. The python script uses "pygame" to read the inputs from one of the joysticks on the PS4 controller and based on that input its supposed to output specific sayings through speakers connected to the Audio Amp. See below;
When I run the script from Thonny, the "speech" works but the inputs from the PS4 are erratic. If I run this script from the "Terminal-command line" it acknowledges the controller and then I get the following:
Error: Device or resource busy.
Checking to see if it was a conflict between the PS4 and DW-CT14, I switched the controller with a PS3 unit that communicates through a USB dongle. Running the script from Thonny, it worked as I had hoped. However, running the script from the command line I get the same error as the PS4.
1. Any advice on why I get different results when running the script from Thonny versus "Terminal-command-line".
2. Request advice on what needs to be changed to be able to use both the PS4 controller and "DW" audio amp simultaneously.
Thank you for your help
Brian
Code:
#!/usr/bin/env python3import pygameimport subprocessimport boardimport timeimport sysimport EYESfrom adafruit_ht16k33 import matrix# Create the I2C interface.i2c = board.I2C()matrix = matrix.Matrix8x8(i2c)matrix.brightness = 0.90# Clear the matrix initiallymatrix.fill(0)matrix.show()# --- Configuration ---# Threshold to ignore small stick movements (deadzone)DEADZONE = 0.05# --- End Configuration ---def speak(text, speed=120, pitch=20, voice="en-us+m7"): #command = ['espeak-ng', '-v', voice, '-s', str(speed), text] try: command = [ "espeak-ng", f"-s{speed}", f"-p{pitch}", f"-v{voice}", text ] subprocess.run(command, check=True) except subprocess.CalledProcessError as e: print(f"Error speaking: {e}") except FileNotFoundError: print("espeak-ng command not found") def draw_eye(pattern): for y in range(8): for x in range(8): matrix[x, y] = pattern[y][x] matrix.show() def mix_joystick_inputs(axis4, axis3): """ MIxes two joystick axes for tank-style steering (e.g., throttle/yaw) Returns: (left_motor, right_motor) """ # Aplly Deadzone if abs(axis4) < DEADZONE: axis4 = 0 if abs(axis3) < DEADZONE: axis3 = 0 # Mixing Algorithm (Tank Drive) # axis3: Forward/Backward (Throttle) # axis2: Left/Right (Steering) left = axis4 + axis3 right = axis4 - axis3 # Normalize values to be within -1.0 to 1.0 range left = max(-1.0, min(1.0, left)) right = max(-1.0, min(1.0, right)) return left, rightpygame.init()pygame.joystick.init() # Check for joysticksif pygame.joystick.get_count() == 0: print("No joystick detected.") #return # Initialize the first joystickjoystick = pygame.joystick.Joystick(0)joystick.init()print(f"Detected joystick: {joystick.get_name()}")time.sleep(1)if __name__ == "__main__": speak(' Ready For Testing.') time.sleep(2.0) try: while True: pygame.event.pump() # Process event queue # Axis 2 is Steering, Axis 3 is Throttle # Note: Pygame axes are usually -1.0 to 1.0 steering = joystick.get_axis(3) throttle = -joystick.get_axis(4) #Invert if forward is negative left_motor, right_motor = mix_joystick_inputs(throttle, steering) #Print mixed values (or send to motor controller) #print(f"L:{left_motor:.2f} R:{right_motor:.2f}") time.sleep(0.05) # Small delay for readable output leftM = left_motor * 30 rghtM = right_motor * 30 if leftM < 0 and rghtM < 0: speak(' Backing Up. ') draw_eye(EYES.BLINK) time.sleep(0.2) elif leftM < 0 and rghtM > 0: speak(' Turning Left.') draw_eye(EYES.LEFT2) time.sleep(0.2) elif leftM > 0 and rghtM < 0: speak(' Turning Right. ') draw_eye(EYES.RIGHT2) time.sleep(0.2) else: draw_eye(EYES.STRGHT) except KeyboardInterrupt: print("Close serial communication.") #ser.close() matrix.fill(0) matrix.show()Error: Device or resource busy.
Checking to see if it was a conflict between the PS4 and DW-CT14, I switched the controller with a PS3 unit that communicates through a USB dongle. Running the script from Thonny, it worked as I had hoped. However, running the script from the command line I get the same error as the PS4.
1. Any advice on why I get different results when running the script from Thonny versus "Terminal-command-line".
2. Request advice on what needs to be changed to be able to use both the PS4 controller and "DW" audio amp simultaneously.
Thank you for your help
Brian
Statistics: Posted by briank1179 — Thu Feb 26, 2026 11:22 pm — Replies 0 — Views 18