Optimized msi-keyboard-color and caffeine

This commit is contained in:
2023-05-28 08:28:21 +02:00
parent b8a23f95db
commit df6e2c7fc5
20 changed files with 245 additions and 44 deletions

View File

@@ -0,0 +1,42 @@
# Ansible Role: keyboard-color
Ansible role to set up dynamic keyboard color change on MSI laptops.
## Requirements
- An MSI laptop
- The `msi-perkeyrgb` tool installed on the system
- Ansible 2.9 or later
## Role Variables
Available variables are listed below, along with their default values:
```yaml
vendor_and_product_id: ""
```
The `vendor_and_product_id` variable is required and should be set to the vendor and product ID of the MSI laptop.
## Dependencies
- `pc_system-aur-helper`
## Example Playbook
```yaml
- hosts: all
roles:
- keyboard-color
vars:
vendor_and_product_id: "your_vendor_and_product_id"
```
## Author
This role was created by [Kevin Veen-Birkenbach](https://github.com/kevinveenbirkenbach).
## 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.

View File

@@ -0,0 +1,65 @@
#!/bin/bash
# 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
fade_duration=1800 # 30 minutes in seconds
transition_start="06:00"
transition_end="06:30"
red_start="18:00"
red_end="06:00"
red_color="ff0000"
# Get the current time in HH:MM format
current_time=$(date +%H:%M)
# Check if it's within the red period
if [[ "$current_time" > "$red_start" || "$current_time" < "$red_end" ]]; then
# Set the color to red directly
sudo msi-perkeyrgb --model GS65 -s "$red_color" --id "$vendor_and_product_id"
else
# Check if it's within the fading period
if [[ "$current_time" > "$transition_start" && "$current_time" < "$transition_end" ]]; then
# Calculate the transition ratio within the fading period
start_seconds=$(date -d "$transition_start" +%s)
end_seconds=$(date -d "$transition_end" +%s)
current_seconds=$(date -d "$current_time" +%s)
transition_ratio=$(awk "BEGIN { ratio = ($current_seconds - $start_seconds) / ($end_seconds - $start_seconds); print ratio }")
# Calculate the current color based on the transition ratio
r=$(awk "BEGIN { value = 255 - 255 * $transition_ratio; printf(\"%.0f\", value) }")
g=0
b=0
# Convert the RGB values to hexadecimal format
current_color=$(printf '%02x%02x%02x' $r $g $b)
# Set the color using msi-perkeyrgb
sudo msi-perkeyrgb --model GS65 -s "$current_color" --id "$vendor_and_product_id"
else
# Calculate the transition ratio based on the time of day
start_seconds=$(date -d "$transition_end" +%s)
end_seconds=$(date -d "$red_start" +%s)
current_seconds=$(date -d "$current_time" +%s)
transition_ratio=$(awk "BEGIN { ratio = ($current_seconds - $start_seconds) / ($end_seconds - $start_seconds); print ratio }")
# Calculate the current color based on the transition ratio
r=$(awk "BEGIN { value = 255 * $transition_ratio; printf(\"%.0f\", value) }")
g=$(awk "BEGIN { value = 0 + (255 - 0) * $transition_ratio; printf(\"%.0f\", value) }")
b=$(awk "BEGIN { value = 0 + (255 - 0) * $transition_ratio; printf(\"%.0f\", value) }")
# Convert the RGB values to hexadecimal format
current_color=$(printf '%02x%02x%02x' $r $g $b)
# Set the color using msi-perkeyrgb
sudo msi-perkeyrgb --model GS65 -s "$current_color" --id "$vendor_and_product_id"
fi
fi

View File

@@ -0,0 +1,11 @@
galaxy_info:
role_name: keyboard-color
author: Kevin Veen-Birkenbach
description: "Ansible role to set up dynamic keyboard color change on MSI laptops"
min_ansible_version: 2.9
platforms:
- name: Linux
versions:
- all
dependencies:
- pc_system-aur-helper

View File

@@ -0,0 +1,9 @@
---
- name: Install MSI packages
kewlfft.aur.aur:
use: yay
name:
- msi-perkeyrgb
- include_tasks: setup_script.yml
- include_tasks: setup_timers.yml

View File

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

View File

@@ -0,0 +1,30 @@
---
- name: Copy keyboard-color.timer file
template:
src: keyboard-color.timer
dest: /etc/systemd/system/keyboard-color.j2
mode: 0644
tags:
- keyboard-color
- name: Copy keyboard-color.service file
template:
src: keyboard-color.service.j2
dest: /etc/systemd/system/keyboard-color.service
mode: 0644
tags:
- keyboard-color
- name: Reload systemd daemon
systemd:
daemon_reload: yes
tags:
- keyboard-color
- name: Enable and start keyboard-color.timer
systemd:
name: keyboard-color.timer
state: started
enabled: yes
tags:
- keyboard-color

View File

@@ -0,0 +1,9 @@
[Unit]
Description=Keyboard Color Service
[Service]
Type=oneshot
ExecStart=/opt/keyboard_color.sh {{ vendor_and_product_id }}
[Install]
WantedBy=default.target

View File

@@ -0,0 +1,9 @@
[Unit]
Description=Keyboard Color Timer
[Timer]
OnCalendar={{on_calendar_msi_keyboard_color}}
Persistent=true
[Install]
WantedBy=timers.target