Finished keyboard color change implementation and solved bugs of shell script

This commit is contained in:
Kevin Veen-Birkenbach 2023-12-15 18:17:46 +01:00
parent 351be2e5b1
commit 11b4fe5029
6 changed files with 41 additions and 148 deletions

View File

@ -38,5 +38,8 @@ This role was created by [Kevin Veen-Birkenbach](https://github.com/kevinveenbir
## Chat Conversation
To see how this role was developed, you can refer to the [Chat Conversation](https://chat.openai.com/share/41c47fdb-a92d-466d-9e92-5a894fe6bec3) that produced this software.
To see how this role was developed, you can refer to the following ChatGPT Conversation that produced this software:
- https://chat.openai.com/share/1f1dde28-3fb2-4df7-be89-4f7ffe67e5c7
- https://chat.openai.com/share/41c47fdb-a92d-466d-9e92-5a894fe6bec3
- https://chat.openai.com/share/e45d186a-f2e1-4c96-9390-36269e274193

View File

@ -69,6 +69,10 @@ def calculate_transition_ratio(current_time, start_time, end_time):
start_timestamp = datetime.datetime.combine(today, start_time).timestamp()
end_timestamp = datetime.datetime.combine(today, end_time).timestamp()
# If start and end times are the same, return 0 to indicate no transition
if start_timestamp == end_timestamp:
return 0
# Calculate the current timestamp
current_timestamp = datetime.datetime.combine(today, current_time).timestamp()
@ -107,11 +111,7 @@ def main(vendor_and_product_id):
"05:00": ("5bc0eb", "ff7f00"), # Morning Blue to Dawn
"04:00": ("990000", "5bc0eb"), # Night to Blue
}
# Ensure 24-hour coverage
if "00:00" not in color_times:
raise ValueError("Time periods must cover the full 24-hour cycle.")
current_time = datetime.datetime.now().time()
start_color, end_color = get_current_period(current_time, color_times)

View File

@ -1,139 +0,0 @@
#!/bin/bash
set -euo pipefail
# Check if the vendor and product ID is provided
if [ -z "${1-}" ]; then
echo "Error: Vendor and product ID is missing."
echo "Usage: /opt/keyboard_color.sh <vendor_and_product_id>"
exit 1
fi
# Extract the vendor and product ID from the command-line argument
vendor_and_product_id=$1
# Define the color transition parameters
morning_blue_color="5bc0eb" # Morning Blue Color
dawn_color="ff7f00" # Dawn Color
morning_golden_hour_color="f9c80e" # Morning Golden Hour Color
sunrise_color="ffd700" # Sunrise Color
noon_color="ffffff" # Noon Color
twilight_color="ff6f61" # Twilight Color
evening_golden_hour_color="fdbe51" # Evening Golden Hour Color
evening_blue_color="0000ff" # Evening Blue Color
sunset_color="ff4500" # Sunset Color
night_color="990000" # Night Color
# Function to calculate the color based on the transition ratio
calculate_color() {
local color_start=$1
local color_end=$2
local transition_ratio=$3
local start_r=$((16#${color_start:0:2}))
local start_g=$((16#${color_start:2:2}))
local start_b=$((16#${color_start:4:2}))
local end_r=$((16#${color_end:0:2}))
local end_g=$((16#${color_end:2:2}))
local end_b=$((16#${color_end:4:2}))
local current_r=$(awk "BEGIN { r = ${start_r} + (${end_r} - ${start_r}) * ${transition_ratio}; printf(\"%.0f\", r) }")
local current_g=$(awk "BEGIN { g = ${start_g} + (${end_g} - ${start_g}) * ${transition_ratio}; printf(\"%.0f\", g) }")
local current_b=$(awk "BEGIN { b = ${start_b} + (${end_b} - ${start_b}) * ${transition_ratio}; printf(\"%.0f\", b) }")
local calculated_color=$(printf "%02x%02x%02x" "$current_r" "$current_g" "$current_b")
if [ "$calculated_color" = "ffff100" ]; then
calculated_color="ffffff"
fi
echo "$calculated_color"
}
# Get the current time in HH:MM format
current_time=$(date +%H:%M)
# Calculate the transition ratio based on the current time
if [[ "$current_time" > "22:00" || "$current_time" < "04:00" ]]; then
# Night (22:00 to 04:00)
color_start="$night_color"
color_end="$night_color"
color_start_time="22:00"
color_end_time="04:00"
elif [[ "$current_time" > "21:00" ]]; then
# Evening Blue Hour to Night (21:00 to 22:00)
color_start="$evening_blue_color"
color_end="$night_color"
color_start_time="21:00"
color_end_time="22:00"
elif [[ "$current_time" > "20:00" ]]; then
# Evening Golden Hour to Evening Blue Hour (20:00 to 21:00)
color_start="$evening_golden_hour_color"
color_end="$evening_blue_color"
color_start_time="20:00"
color_end_time="21:00"
elif [[ "$current_time" > "19:30" ]]; then
# Sunset to Evening Golden Hour (19:30 to 20:00)
color_start="$sunset_color"
color_end="$evening_golden_hour_color"
color_start_time="19:30"
color_end_time="20:00"
elif [[ "$current_time" > "12:00" ]]; then
# Noon to Sunset (12:00 to 19:30)
color_start="$noon_color"
color_end="$sunset_color"
color_start_time="12:00"
color_end_time="19:30"
elif [[ "$current_time" > "07:30" ]]; then
# Sunrise to Noon (07:30 to 12:00)
color_start="$sunrise_color"
color_end="$noon_color"
color_start_time="07:30"
color_end_time="12:00"
elif [[ "$current_time" > "07:00" ]]; then
# Morning Golden Hour to Sunrise (07:00 to 07:30)
color_start="$morning_golden_hour_color"
color_end="$sunrise_color"
color_start_time="07:00"
color_end_time="07:30"
elif [[ "$current_time" > "06:00" ]]; then
# Dawn to Morning Golden Hour (06:00 to 07:00)
color_start="$dawn_color"
color_end="$morning_golden_hour_color"
color_start_time="06:00"
color_end_time="07:00"
elif [[ "$current_time" > "05:00" ]]; then
# Morning Blue Hour to Dawn (05:00 to 06:00)
color_start="$morning_blue_color"
color_end="$dawn_color"
color_start_time="05:00"
color_end_time="06:00"
elif [[ "$current_time" > "04:00" ]]; then
# Night to Morning Blue Hour (22:00 to 04:00)
color_start="$night_color"
color_end="$morning_blue_color"
color_start_time="04:00"
color_end_time="05:00"
fi
# Get the current date in YYYY-MM-DD format
current_date=$(date +%Y-%m-%d)
# Calculate the start and end timestamps based on the current date and time
start_timestamp=$(date -d "${current_date} ${color_start_time}" +%s)
end_timestamp=$(date -d "${current_date} ${color_end_time}" +%s)
# Get the current timestamp
current_timestamp=$(date +%s)
# Calculate the transition ratio
transition_ratio=$(awk "BEGIN { ratio = ($current_timestamp - $start_timestamp) / ($end_timestamp - $start_timestamp); print ratio }")
# Calculate the current color based on the transition ratio
current_color=$(calculate_color "$color_start" "$color_end" "$transition_ratio")
echo "Changing keyboard color to #$current_color."
# Set the color using msi-perkeyrgb
msi-perkeyrgb --model GS65 -s "$current_color" --id "$vendor_and_product_id"

View File

@ -1,8 +1,8 @@
---
- name: Copy keyboard_color.sh script
copy:
src: keyboard_color.sh
dest: /opt/keyboard_color.sh
src: keyboard_color.py
dest: /opt/keyboard_color.py
mode: 0755
tags:
- keyboard-color

View File

@ -4,4 +4,4 @@ OnFailure=systemd-notifier@%n.service
[Service]
Type=oneshot
ExecStart=/opt/keyboard_color.sh {{ vendor_and_product_id }}
ExecStart=/bin/python /opt/keyboard_color.py {{ vendor_and_product_id }}

View File

@ -0,0 +1,29 @@
#!/bin/bash
# Pfad zum Keyboard-Color-Skript
KEYBOARD_COLOR_SCRIPT="$1"
DEVICE_ID="$2"
# NTP deaktivieren
timedatectl set-ntp false
# Startzeit speichern
START_TIME=$(date +%s)
# 2 Minuten (120 Sekunden) für den gesamten Tag simulieren
for i in {0..60}; do
# Berechnung der aktuellen simulierten Zeit
CURRENT_TIME=$((START_TIME + (( 86400 / 60 ) * i )))
# Systemzeit auf die simulierte Zeit setzen
date +%s -s "@$CURRENT_TIME"
# Keyboard-Color-Skript ausführen
python $KEYBOARD_COLOR_SCRIPT $DEVICE_ID
# 2 Sekunden Pause
sleep 2
done
# NTP wieder aktivieren
timedatectl set-ntp true