Skip to content

Commit

Permalink
Merge pull request #3 from ivanbratovic/kernel-driver
Browse files Browse the repository at this point in the history
Add support for latest kernel driver and dynamic MAC address
  • Loading branch information
cow-killer authored Jan 27, 2022
2 parents c13dabd + f3da456 commit 15a5820
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 34 deletions.
7 changes: 2 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ This program allows the user to adjust color and brightness to the various LEDs

## Prerequisites (besides your DS, obviously)
* Linux-based operating system
* Sony's official driver for the DualSense, available in the [AUR](https://aur.archlinux.org/packages/hid-playstation-dkms). As it's not in the kernel yet, you'll need to otherwise download the [patches](https://patchwork.kernel.org/project/linux-input/list/?series=404369) and compile the kernel
* Access to root privileges (the program modifies the values found in `sys/class/leds/playstation::[mac_address]::[name_of_LED]/`)
* Access to root privileges (the program modifies the values found in `/sys/devices`)
* Python 3
* GTK 3
* Tkinter
Expand All @@ -24,9 +23,7 @@ Other tasks going on on your computer will still run regardless.

## How To Use
1. Clone the repository, i.e. `git clone https://github.com/cow-killer/dualsense-led-configurator.git`
2. Plug your DS in or connect it via Bluetooth, then take note of its MAC address (can be found in `sys/class/leds/`)
3. Open `ds_led.py` with a text editor, and fill in the MAC address at or somewhere near line 16
4. Save the file, then run the script as root: `sudo python ds_led.py`
2. Run the script as root: `sudo python ds_led.py`

## To-Do
* ~~Code is a mess, more than likely a way to reduce the amount of code needed~~ Thanks thats-the-joke!
Expand Down
64 changes: 35 additions & 29 deletions ds_led.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,23 @@
import os # detects if program is being run as root
import select
import tkinter.colorchooser # need tkiner for now for color picker and RGB brightness
import subprocess # for running Linux commands

# Define the MAC address for your DS here
mac_address = "00:00:00:00:00:00"
# find the path to the ps-controller-battery directory
device_path = subprocess.check_output(["find", "/sys/devices/","-name","ps-controller-battery*"])
# extract the MAC address from the filepath
mac_address = device_path.decode().split("/")[-1].split("-")[-1].strip()
# extract the main controller directory
device_path = "/".join(device_path.decode().split("/")[0:-2])

# get the random number assigned to the contoller
input_num = subprocess.check_output(["find", "/sys/devices/", "-name", "input*:white:player-1"])
input_num = input_num.decode().split("/")[-1].split(":")[0][5:]

# create useful variables for other controller subsystems
battery_path = f"{device_path}/power_supply/ps-controller-battery-{mac_address}"
player_leds_path = f"{device_path}/leds/input{input_num}:white:player"
rgb_leds_path = f"{device_path}/leds/input{input_num}:rgb:indicator"

icon = "assets/icon.png"
controller = "assets/pad.png"
Expand Down Expand Up @@ -82,18 +96,14 @@ def init_ui(self):
# Get battery percentage. Display error if file is not available and exit
try:
f = open(
"/sys/class/power_supply/ps-controller-battery-"
+ mac_address
+ "/capacity"
f"{battery_path}/capacity"
)
status_file = open(
"/sys/class/power_supply/ps-controller-battery-"
+ mac_address
+ "/status"
f"{battery_path}/status"
)
except FileNotFoundError:
print(
"ERROR: You either entered the wrong MAC address or your device is not connected."
"ERROR: Your controller can't be detected."
)
sys.exit()
else:
Expand Down Expand Up @@ -221,7 +231,7 @@ def init_ui(self):

def toggle_individual_led(self, led_num):
wr = open(
f"/sys/class/leds/playstation::{mac_address}::led{led_num}/brightness", "r+"
f"{player_leds_path}-{led_num}/brightness", "r+"
)
value = wr.readline().strip()
new_value = "1" if value == "0" else "0"
Expand All @@ -230,14 +240,14 @@ def toggle_individual_led(self, led_num):

def enable_individual_led(self, led_num):
wr = open(
f"/sys/class/leds/playstation::{mac_address}::led{led_num}/brightness", "w"
f"{player_leds_path}-{led_num}/brightness", "w"
)
wr.write("1")
wr.close()

def disable_individual_led(self, led_num):
wr = open(
f"/sys/class/leds/playstation::{mac_address}::led{led_num}/brightness", "w"
f"{player_leds_path}-{led_num}/brightness", "w"
)
wr.write("0")
wr.close()
Expand All @@ -259,7 +269,7 @@ def open_color_picker(self, widget): # Color picker for side LEDs
else:
# Write the RGB value to 'multi-intensity'
wr = open(
"/sys/class/leds/playstation::" + mac_address + "::rgb/multi_intensity",
f"{rgb_leds_path}/multi_intensity",
"r+",
)
wr.write(str(red) + " " + str(green) + " " + str(blue))
Expand All @@ -269,13 +279,13 @@ def open_color_picker(self, widget): # Color picker for side LEDs

def rgb_random_clicked(self, widget):
wr = open(
"/sys/class/leds/playstation::" + mac_address + "::rgb/multi_intensity",
f"{rgb_leds_path}/multi_intensity",
"r+",
)
random_red = random.randint(1, 255)
random_green = random.randint(1, 255)
random_blue = random.randint(1, 255)
wr.write(str(random_red) + " " + str(random_green) + " " + str(random_blue))
wr.write(f"{random_red} {random_green} {random_blue}")
wr.close()
print(
"RGB set to "
Expand All @@ -291,7 +301,7 @@ def rgb_rainbow_clicked(self, widget):

# Set RGB to 0 0 0 before we begin
wr = open(
"/sys/class/leds/playstation::" + mac_address + "::rgb/multi_intensity",
f"{rgb_leds_path}/multi_intensity",
"r+",
)
wr.write("0 0 0")
Expand All @@ -310,11 +320,9 @@ def rgb_rainbow_clicked(self, widget):
random_blue_increment = random.randint(1, 10)
while (
red <= max_rgb and green <= max_rgb and blue <= max_rgb
): # slowly glow brighter
): # slowly glomonorepo% w brighter
wr = open(
"/sys/class/leds/playstation::"
+ mac_address
+ "::rgb/multi_intensity",
f"{rgb_leds_path}/multi_intensity",
"r+",
)
wr.write(str(red) + " " + str(green) + " " + str(blue))
Expand All @@ -324,9 +332,7 @@ def rgb_rainbow_clicked(self, widget):
blue += random_blue_increment
while red > min_rgb and green > min_rgb and blue > min_rgb: # die down
wr = open(
"/sys/class/leds/playstation::"
+ mac_address
+ "::rgb/multi_intensity",
f"{rgb_leds_path}/multi_intensity",
"r+",
)
time.sleep(0.05)
Expand All @@ -345,7 +351,7 @@ def rgb_rainbow_clicked(self, widget):
def no_brightness_clicked(self, widget):
brightness_off = 0
wr = open(
"/sys/class/leds/playstation::" + mac_address + "::rgb/brightness", "r+"
f"{rgb_leds_path}/brightness", "r+"
)
wr.write(str(brightness_off))
wr.close()
Expand All @@ -354,7 +360,7 @@ def no_brightness_clicked(self, widget):
def low_brightness_clicked(self, widget):
low_brightness_value = 85
wr = open(
"/sys/class/leds/playstation::" + mac_address + "::rgb/brightness", "r+"
f"{rgb_leds_path}/brightness", "r+"
)
wr.write(str(low_brightness_value))
wr.close()
Expand All @@ -363,7 +369,7 @@ def low_brightness_clicked(self, widget):
def medium_brightness_clicked(self, widget):
med_brightness_value = 170
wr = open(
"/sys/class/leds/playstation::" + mac_address + "::rgb/brightness", "r+"
f"{rgb_leds_path}/brightness", "r+"
)
wr.write(str(med_brightness_value))
wr.close()
Expand All @@ -372,7 +378,7 @@ def medium_brightness_clicked(self, widget):
def max_brightness_clicked(self, widget):
max_brightness_value = 255
wr = open(
"/sys/class/leds/playstation::" + mac_address + "::rgb/brightness", "r+"
f"{rgb_leds_path}/brightness", "r+"
)
wr.write(str(max_brightness_value))
wr.close()
Expand All @@ -381,7 +387,7 @@ def max_brightness_clicked(self, widget):
def choose_brightness_clicked(self, widget):
def set_value():
wr = open(
"/sys/class/leds/playstation::" + mac_address + "::rgb/brightness", "r+"
f"{rgb_leds_path}/brightness", "r+"
)
wr.write(str(brightness.get()))
wr.close()
Expand All @@ -398,7 +404,7 @@ def cancel():

# Retrieve brightness value from file and set this as the default in the slider
wr = open(
"/sys/class/leds/playstation::" + mac_address + "::rgb/brightness", "r+"
f"{rgb_leds_path}/brightness", "r+"
)
value = wr.readline().strip()

Expand Down

0 comments on commit 15a5820

Please sign in to comment.