Update razer-bat; now working!
- Update README.md to document installation and usage - Implement razer-bat as simply as possible. - Most parameters can be adjusted, but only mouse and dock names are required. Further implementation of customizability should be pretty easy.
This commit is contained in:
parent
7a15a272bd
commit
fd20f1a9c2
@ -1,17 +1,16 @@
|
||||
# Razer Battery Level RGB
|
||||
See your mouse or keyboard's battery level on your wireless dock.
|
||||
# Razer-bat - Update dock status color based on battery level
|
||||
This script uses [razer-cli](https://github.com/lolei/razer-cli) and standard bash utilities to set the RGB LEDs of your Razer wireless dock to reflect the battery level of your wireless mouse (or keyboard).
|
||||
|
||||
# Installation
|
||||
# Installation & Usage
|
||||
Before installing, we need to install some dependencies:
|
||||
1. Install razer-cli: [instructions](https://github.com/lolei/razer-cli?tab=readme-ov-file#installation).
|
||||
2. Download this script and make it executable: `curl -s https://raw.githubusercontent.com/Jafner/Razer-BatteryLevelRGB/master/Razer-BatteryLevelRGB.sh > ./ && chmod +x ./Razer-BatteryLevelRGB.sh`.
|
||||
3. Edit the script
|
||||
1. Replace the device name with your device's name (you can find it by running `razer-cli -ls`).
|
||||
2. Set the low-charge and full-charge color codes to your liking. Defaults to red and green respectively.
|
||||
4. Run the script: `./Razer-BatteryLevelRGB.sh`.
|
||||
|
||||
- If you want to run the script in the background, refer to [this StackOverflow post](https://stackoverflow.com/questions/3683910/executing-shell-command-in-background-from-script).
|
||||
- If you want to run the script with Systemd (as a background process), refer to [this StackEx post](https://unix.stackexchange.com/questions/47695/how-to-write-startup-script-for-systemd).
|
||||
2. Download this script and make it executable: `curl -s https://gitea.jafner.tools/Jafner/Jafner.net/raw/branch/main/projects/razer-bat/razer-bat.sh > ./ && chmod +x ./razer-bat.sh`.
|
||||
3. Set the `WIRELESS_DEVICE_NAME` and `WIRED_DEVICE_NAME` variables at the top of the script.
|
||||
1. Set `WIRELESS_DEVICE_NAME` with the name of your wireless mouse, keyboard, or whatever you want to monitor the battery level of. (Tip: You can find it by running `razer-cli -ls`).
|
||||
2. Set `WIRED_DEVICE_NAME` with the name of your wired charging dock, or whatever device whose RGB you want to reflect the battery level of your wireless device.
|
||||
4. Run the script once: `./razer-bat.sh`. Verify everything is working properly.
|
||||
5. Move the script to somewhere safe. For example: `mv ./razer-bat.sh $HOME/.local/razer-bat`
|
||||
6. Create a cronjob for the script: `echo "*/5 * * * * $HOME/.local/razer-bat" | crontab -`
|
||||
|
||||
# Tested With
|
||||
```
|
||||
|
@ -1,91 +0,0 @@
|
||||
#!/bin/bash
|
||||
|
||||
debug=false # set to true if you want to see debug messages
|
||||
# Find your values from the output of `razer-cli -ls`
|
||||
|
||||
# These devices are queried for the charge value used to set the RGB color
|
||||
device_name="Razer Viper Ultimate (Wireless)"
|
||||
device_name_alternate="Razer Viper Ultimate (Wired)"
|
||||
|
||||
# This device gets its RGB set based on the charge value.
|
||||
# This *can* be the same as one of the above, but I like to see the RGB on my dock.
|
||||
# You could also use a non-Razer device by changing the command at the end of
|
||||
# the set_color_from_charge function to one which works for your device.
|
||||
dock_name="Razer Mouse Dock"
|
||||
|
||||
# Set how often to check the new battery level.
|
||||
polling_interval=600
|
||||
|
||||
# Set the colors to use for full charge and low charge below.
|
||||
# These are used to create a gradient to represent partial charge levels.
|
||||
# E.g. if full charge is green and low charge is red, 50% charge will be yellow
|
||||
# Or e.g. if full charge is purple (102, 0, 255) and low charge is cyan (0, 255, 255)
|
||||
# then 50% charge will be a sky blue (51, 127, 255).
|
||||
# You can use the rgb output of a tool like this:
|
||||
# https://www.w3schools.com/colors/colors_picker.asp
|
||||
full_charge_color_code="85 255 0"
|
||||
low_charge_color_code="255 0 0"
|
||||
|
||||
|
||||
# Razer devices often have different names when they're plugged in.
|
||||
# But we still want to know the charge level while we're charging, so we query both names.
|
||||
low_charge_percent=$(razer-cli -d "$device_name" --battery low | grep "low threshold" | tr -s ' ' | cut -d' ' -f 4)
|
||||
if [[ -z $low_charge_percent ]]; then
|
||||
low_charge_percent=$(razer-cli -d "$device_name_alternate" --battery low | grep "low threshold" | tr -s ' ' | cut -d' ' -f 4)
|
||||
fi
|
||||
|
||||
# This is just for the math to get the color gradients.
|
||||
charge_range=$((100 - $low_charge_percent))
|
||||
full_r=$(echo "$full_charge_color_code" | cut -d' ' -f 1)
|
||||
low_r=$(echo "$low_charge_color_code" | cut -d' ' -f 1)
|
||||
color_range_r=$(($full_r - $low_r))
|
||||
|
||||
full_g=$(echo "$full_charge_color_code" | cut -d' ' -f 2)
|
||||
low_g=$(echo "$low_charge_color_code" | cut -d' ' -f 2)
|
||||
color_range_g=$(($full_g - $low_g))
|
||||
|
||||
full_b=$(echo "$full_charge_color_code" | cut -d' ' -f 3)
|
||||
low_b=$(echo "$low_charge_color_code" | cut -d' ' -f 3)
|
||||
color_range_b=$(($full_b - $low_b))
|
||||
|
||||
# Some debug echoes.
|
||||
if [[ $debug -eq "true" ]]; then
|
||||
echo "full_r: $full_r, low_r: $low_r, color_range_r: $color_range_r"
|
||||
echo "full_g: $full_g, low_g: $low_g, color_range_g: $color_range_g"
|
||||
echo "full_b: $full_b, low_b: $low_b, color_range_b: $color_range_b"
|
||||
fi
|
||||
|
||||
# Our function for setting the new RGB color
|
||||
# Uses a gradient between the "full" and "low" charge colors.
|
||||
function set_color_from_charge () {
|
||||
charge_scaled=$(($(($(($1 - $low_charge_percent)) * 100)) / $charge_range))
|
||||
r=$(($low_r + $(($(($charge_scaled * $color_range_r)) / 100))))
|
||||
g=$(($low_g + $(($(($charge_scaled * $color_range_g)) / 100))))
|
||||
b=$(($low_b + $(($(($charge_scaled * $color_range_b)) / 100))))
|
||||
|
||||
if [[ $debug -eq "true" ]]; then
|
||||
echo "charge scaled: $charge_scaled"
|
||||
echo "new color: $r, $g, $b"
|
||||
fi
|
||||
razer-cli -d $dock_name -c $r $g $b
|
||||
}
|
||||
|
||||
# Our main loop. Get current charge level.
|
||||
# If it's reporting zero, we assume it's asleep.
|
||||
# If it's reporting at or below the low battery threshold, we use the low battery color
|
||||
# If it's reporting above the low battery threshold, we set the RGB color based on our gradient
|
||||
# Consider adding logic to handle distinction between discharging (normal) and charging.
|
||||
while true; do
|
||||
charge=$(razer-cli -d "$device_name" --battery print | grep charge | tr -s ' ' | cut -d' ' -f 3)
|
||||
if [[ $debug -eq "true" ]]; then
|
||||
echo "charge: $charge"
|
||||
fi
|
||||
if [[ $charge -eq 0 ]]; then
|
||||
echo "Device charge 0. Assuming the device is sleeping. Do nothing."
|
||||
elif [[ $charge -lt $low_charge_percent ]]; then
|
||||
razer-cli -d $dock_name -c $full_charge_color_code
|
||||
else
|
||||
set_color_from_charge $charge
|
||||
fi
|
||||
sleep $polling_interval
|
||||
done
|
96
projects/razer-bat/razer-bat.sh
Executable file
96
projects/razer-bat/razer-bat.sh
Executable file
@ -0,0 +1,96 @@
|
||||
#!/bin/bash
|
||||
|
||||
WIRELESS_DEVICE_NAME=''
|
||||
WIRED_DEVICE_NAME=''
|
||||
# Init
|
||||
WIRELESS_DEVICE_NAME=${WIRELESS_DEVICE_NAME:-'Razer Viper Ultimate'} # required, non-sane default
|
||||
WIRED_DEVICE_NAME=${WIRED_DEVICE_NAME:-'Razer Mouse Dock'} # required, sane default
|
||||
|
||||
SYNC_ALL_DEVICES=${SYNC_ALL_DEVICES:-'false'} # optional; default false; applies color to all found devices
|
||||
START_COLOR=${START_COLOR:-'255 0 0'} # optional; TODO
|
||||
END_COLOR=${END_COLOR:-'0 255 0'} # optional; TODO
|
||||
STEPS=${STEPS:-'20'} # optional; can be any factor of 100
|
||||
|
||||
# echo "WIRELESS_DEVICE_NAME: $WIRELESS_DEVICE_NAME"
|
||||
# echo "WIRED_DEVICE_NAME: $WIRED_DEVICE_NAME"
|
||||
# echo "SYNC_ALL_DEVICES: $SYNC_ALL_DEVICES"
|
||||
# echo "START_COLOR: $START_COLOR"
|
||||
# echo "END_COLOR: $END_COLOR"
|
||||
# echo "STEPS: $STEPS"
|
||||
|
||||
# Function to calculate color between two colors by percentage
|
||||
# Takes
|
||||
# COLOR_PERCENTAGE=$1 # required
|
||||
# START_COLOR_RANGE=$2 # default '255 0 0' (pure red)
|
||||
# END_COLOR_RANGE=$3 # default '0 255 0' (pure green)
|
||||
# STEPS=$4 # default 20
|
||||
# Returns
|
||||
# Space-separated RGB value like: 123 134 0
|
||||
function get_intermediate_color() {
|
||||
local COLOR_PERCENTAGE=$1
|
||||
local START_COLOR_RANGE=${2:-255,0,0}
|
||||
local END_COLOR_RANGE=${3:-0,255,0}
|
||||
local STEPS=${4:-20}
|
||||
|
||||
IFS=',' read -r -a start_color <<< "$START_COLOR_RANGE"
|
||||
IFS=',' read -r -a end_color <<< "$END_COLOR_RANGE"
|
||||
local steps=$STEPS
|
||||
local charge=$COLOR_PERCENTAGE
|
||||
|
||||
local step_r=$(( (end_color[0] - start_color[0]) / (steps + 1) ))
|
||||
local step_g=$(( (end_color[1] - start_color[1]) / (steps + 1) ))
|
||||
local step_b=$(( (end_color[2] - start_color[2]) / (steps + 1) ))
|
||||
|
||||
local intermediate_colors=()
|
||||
for ((i = 1; i <= steps; i++)); do
|
||||
local r=$(( start_color[0] + step_r * i ))
|
||||
local g=$(( start_color[1] + step_g * i ))
|
||||
local b=$(( start_color[2] + step_b * i ))
|
||||
intermediate_colors+=("$r $g $b")
|
||||
done
|
||||
|
||||
local index=$(( ( $charge / (100 / $steps ) ) ))
|
||||
if [[ $index -gt ${#intermediate_colors[@]} ]]; then
|
||||
index=${#intermediate_colors[@]}
|
||||
fi
|
||||
|
||||
echo ${intermediate_colors[$index]}
|
||||
}
|
||||
|
||||
# Function to read mouse charge level and return it as percentage
|
||||
# Takes
|
||||
# WIRELESS_DEVICE_NAME=$1
|
||||
# Returns
|
||||
# Percentage value between 0 and 100
|
||||
function get_charge_percentage() {
|
||||
local WIRELESS_DEVICE_NAME=$1
|
||||
local CHARGE=$(\
|
||||
razer-cli -d "$(\
|
||||
echo "$(\
|
||||
razer-cli -ls
|
||||
)" |\
|
||||
grep "$WIRELESS_DEVICE_NAME" |\
|
||||
sed 's/://'
|
||||
)" --battery print |\
|
||||
grep charge |\
|
||||
tr -s ' ' |\
|
||||
cut -d' ' -f3
|
||||
)
|
||||
echo "$CHARGE"
|
||||
}
|
||||
|
||||
# Our function for setting the new RGB color
|
||||
# Takes
|
||||
# WIRED_DEVICE_NAME=$1
|
||||
# COLOR=$2 # space-separated
|
||||
function set_dock_color () {
|
||||
local WIRED_DEVICE_NAME=\"$1\"
|
||||
local COLOR="$2"
|
||||
echo razer-cli -d $WIRED_DEVICE_NAME -c $COLOR
|
||||
razer-cli -d $WIRED_DEVICE_NAME -c $COLOR
|
||||
|
||||
}
|
||||
|
||||
# Main
|
||||
set_dock_color "$WIRED_DEVICE_NAME" "$(get_intermediate_color "$(get_charge_percentage "$WIRELESS_DEVICE_NAME")")" &&\
|
||||
echo "Updated \"$WIRED_DEVICE_NAME\" \($(get_charge_percentage "$WIRELESS_DEVICE_NAME")% battery)"
|
Loading…
Reference in New Issue
Block a user