Python: Controlling the Dualshock4's LED light bar in Windows

246 Views Asked by At

I want to control the Dualshock4 light bar in Python on Windows. My controller is connected via Bluetooth and i use the pygame library to communicate with it.

I can detect the controller input and I'd like them to be able to change the lightbar's color.

How can I change the color of the controller's light bar in Python under Windows via Bluetooth?

I searched for solutions through several forums but only found solutions that works on Linux. I am aware that some programs such as "DS4Windows" let you change the lightbar color, but I'd like to do it using a python script.

1

There are 1 best solutions below

0
On

There is, unfortunately, no way to affect this with the pygame interface. UNLESS, you use this really nasty regex. I don't recommend using it but it's better than nothing:


ledPattern="[0-9A-F]{4}:[0-9A-F]{4}:[0-9A-F]{4}\.[0-9A-F]{4}:"
deviceName=`ls -1 /sys/class/leds | grep -E $ledPattern | sed -r "s/:\w+$//" | head -n 1`

function setLED {
    echo $1 > /sys/class/leds/$deviceName:$2/brightness
}

spectrum
red=0
green=2
blue=4
function getColorVal {
    echo "(s($1 * 0.3 + $2) * 127 + 128)/1" | bc -l | sed -r 's/^([0-9]{1,3})(\..+)$/\1/'
}

for i in {0..256}; do
    r=$(getColorVal $i $red)
    g=$(getColorVal $i $green)
    b=$(getColorVal $i $blue)
    
    setLED $r "red"
    setLED $g "green"
    setLED $b "blue"

    sleep 0.1
done