Last active
December 29, 2023 23:38
-
-
Save finsterdexter/0bd1582a8a896e6bf807361b552cc62c to your computer and use it in GitHub Desktop.
A shell script that updates the RGB on my razer mouse dock based on the mouse's charge level
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
# put in /etc/systemd/system, then do $ sudo systemctl enable dock-charge.service | |
[Unit] | |
Description=Charge LED Monitor | |
After=syslog.target | |
[Service] | |
ExecStart=/usr/local/bin/dock-charge.sh | |
Restart=always | |
[Install] | |
WantedBy=multi-user.target |
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
#!/bin/bash | |
# This script depends on openrazer. I use this script as a systemctl service. | |
# Function to find the device path with the charge_level handle | |
find_charge_handle() { | |
for device_path in /sys/bus/hid/drivers/razermouse/*; do | |
if [[ -f "$device_path/charge_level" ]]; then | |
echo "$device_path" | |
return # Exit the function after finding the handle | |
fi | |
done | |
echo "charge_level handle not found" # Handle not found | |
exit 1 # Exit the script with an error | |
} | |
# Function to find the device path with the matrix_effect_static handle | |
find_led_handle() { | |
for device_path in /sys/bus/hid/drivers/razeraccessory/*; do | |
if [[ -f "$device_path/matrix_effect_static" ]]; then | |
echo "$device_path" # Return the full path | |
return | |
fi | |
done | |
echo "matrix_effect_static handle not found" | |
exit 1 | |
} | |
# Constants for device handles and color ranges | |
charge_handle="$(find_charge_handle)/charge_level" | |
# Get the device path with the LED handles | |
led_handle="$(find_led_handle)/matrix_effect_static" | |
breath_handle="$(find_led_handle)/matrix_effect_breath" | |
green_min=200 | |
yellow_min=100 | |
orange_min=50 | |
critical_level=15 | |
# Function to set LED colors based on charge level | |
set_led_colors() { | |
charge_level=$(<"$charge_handle") | |
if [ "$charge_level" -ge "$green_min" ]; then | |
color_hex="\x00\xFF\x00" # Green | |
elif [ "$charge_level" -ge "$yellow_min" ]; then | |
color_hex="\xFF\xFF\x00" # Yellow | |
elif [ "$charge_level" -ge "$orange_min" ]; then | |
color_hex="\xFF\xA5\x00" # Orange | |
else | |
color_hex="\xFF\x00\x00" # Red | |
fi | |
echo -n -e "$color_hex" >"$led_handle" | |
if [ "$charge_level" -lt "$critical_level" ]; then | |
echo -n -e "$color_hex" >"$breath_handle" # Send red to breath_handle too | |
fi | |
} | |
# Main loop to continuously update LEDs and output | |
while true; do | |
set_led_colors | |
echo "Charge level: $charge_level, Color: $color_hex" | |
sleep 5 | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment