2024-09-03 16:20:54 -07:00
|
|
|
#!/bin/bash
|
2024-09-05 13:04:18 -07:00
|
|
|
# Customize the four variables below for your setup.
|
|
|
|
###############################
|
|
|
|
PATH="$PATH:$HOME/.local/bin" #
|
|
|
|
WIRELESS_DEVICE_NAME='' #
|
|
|
|
WIRED_DEVICE_NAME='' #
|
|
|
|
SYNC='true' #
|
|
|
|
###############################
|
2024-09-03 16:20:54 -07:00
|
|
|
|
|
|
|
# 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
|
|
|
|
|
2024-09-05 13:04:18 -07:00
|
|
|
if [[ $SYNC == 'true' ]]; then
|
|
|
|
SYNC="--sync"
|
|
|
|
else
|
|
|
|
SYNC=""
|
|
|
|
fi
|
2024-09-03 16:20:54 -07:00
|
|
|
|
2024-09-05 13:04:18 -07:00
|
|
|
echo "WIRELESS_DEVICE_NAME: $WIRELESS_DEVICE_NAME"
|
|
|
|
echo "WIRED_DEVICE_NAME: $WIRED_DEVICE_NAME"
|
2024-09-03 16:20:54 -07:00
|
|
|
|
|
|
|
# Function to calculate color between two colors by percentage
|
|
|
|
# Takes
|
2024-09-05 13:04:18 -07:00
|
|
|
# COLOR_PERCENTAGE=$1 # required
|
2024-09-03 16:20:54 -07:00
|
|
|
# Returns
|
2024-09-05 13:04:18 -07:00
|
|
|
# Space-separated RGB value like: 123 134 0
|
2024-09-03 16:20:54 -07:00
|
|
|
function get_intermediate_color() {
|
|
|
|
local COLOR_PERCENTAGE=$1
|
2024-09-05 13:04:18 -07:00
|
|
|
|
|
|
|
IFS=',' read -r -a start_color <<< "255,0,0"
|
|
|
|
IFS=',' read -r -a end_color <<< "0,255,0"
|
|
|
|
local steps=4
|
2024-09-03 16:20:54 -07:00
|
|
|
|
|
|
|
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 ))
|
2024-09-05 13:04:18 -07:00
|
|
|
local intermediate_colors+=("$r $g $b")
|
2024-09-03 16:20:54 -07:00
|
|
|
done
|
2024-09-05 13:04:18 -07:00
|
|
|
local index=$(( ( $COLOR_PERCENTAGE / (100 / $steps ) ) ))
|
|
|
|
if [[ $index -gt $(( ${#intermediate_colors[@]} - 1 )) ]]; then
|
|
|
|
echo "COLOR: ${end_color[@]}" >&2
|
|
|
|
echo "${end_color[@]}"
|
|
|
|
elif [[ $index -eq 0 ]]; then
|
|
|
|
echo "COLOR: ${start_color[@]}" >&2
|
|
|
|
echo "${start_color[@]}"
|
|
|
|
else
|
|
|
|
echo "COLOR: ${intermediate_colors[$index]}" >&2
|
|
|
|
echo ${intermediate_colors[$index]}
|
2024-09-03 16:20:54 -07:00
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
# Function to read mouse charge level and return it as percentage
|
|
|
|
# Takes
|
2024-09-05 13:04:18 -07:00
|
|
|
# WIRELESS_DEVICE_NAME=$1
|
2024-09-03 16:20:54 -07:00
|
|
|
# Returns
|
2024-09-05 13:04:18 -07:00
|
|
|
# Percentage battery charge value between 0 and 100
|
2024-09-03 16:20:54 -07:00
|
|
|
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
|
|
|
|
)
|
2024-09-05 13:04:18 -07:00
|
|
|
echo "CHARGE: $CHARGE" >&2
|
2024-09-03 16:20:54 -07:00
|
|
|
echo "$CHARGE"
|
|
|
|
}
|
|
|
|
|
|
|
|
# Main
|
2024-09-05 13:04:18 -07:00
|
|
|
razer-cli -d "$WIRED_DEVICE_NAME" -c $(get_intermediate_color "$(get_charge_percentage "$WIRELESS_DEVICE_NAME")") $SYNC
|