From 8ca654c330aa448b562496801d5cb5c34e49646d Mon Sep 17 00:00:00 2001 From: dataprolet Date: Fri, 14 Jun 2024 23:37:54 +0200 Subject: [PATCH] Add brightness-changer.sh --- Scripts/brightness-changer.sh | 60 +++++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 Scripts/brightness-changer.sh diff --git a/Scripts/brightness-changer.sh b/Scripts/brightness-changer.sh new file mode 100644 index 0000000..6564b2e --- /dev/null +++ b/Scripts/brightness-changer.sh @@ -0,0 +1,60 @@ +#!/bin/usr/env bash + +# Simple script to slowly lower your displays brightness after sunset using ddcutil +# Assuming brightness is 75 and lower is to 50 + +# Get the current brightness +ddcutil getvcp 10 | awk '{print $9}' | sed 's/,//g' > /tmp/brightness +current_brightness=$(cat /tmp/brightness) + +# Function to slowly lower the brightness +lower_brightness() { + while (( $current_brightness > 50 )); do + current_brightness=$(($current_brightness - 1)) + ddcutil setvcp 10 $current_brightness + sleep 1 + done +} + +# Function to reset brightness +reset_brightness() { + if (( $current_brightness < 75 )); then + ddcutil setvcp 10 75 + fi +} + +# Get your coordinates here: https://www.latlong.net/ +lat= 12.345678 +long=98.765432 + +# Get the times for sunrise and sunset +curl "https://api.sunrisesunset.io/json?lat=$lat&lng=$long" |jq -r '.results | .sunrise' > /tmp/sunrise +curl "https://api.sunrisesunset.io/json?lat=$lat&lng=$long" |jq -r '.results | .sunset' > /tmp/sunset + +sunrise=$(cat /tmp/sunrise) +sunset=$(cat /tmp/sunset) + +# Convert them into hours and minutes +hour_sunrise=$(date -d "$sunrise" +"%H") +min_sunrise=$(date -d "$sunrise" +"%M") +hour_sunset=$(date -d "$sunset" +"%H") +min_sunset=$(date -d "$sunset" +"%M") + +# Get the current hour and minute +current_hour=$(date +%H) +current_minute=$(date +%M) + +# Convert current time to minutes since midnight +current_time=$((10#$current_hour * 60 + 10#$current_minute)) + +# Define the start times in minutes since midnight +start_command1=$((10#$hour_sunset * 60 + 10#$min_sunset)) +end_command1=$((10#$hour_sunrise * 60 + 10#$min_sunrise)) + +if (( current_time >= start_command1 || current_time < end_command1 )); then + # Execute command1 if the time is between sunset and sunrise + lower_brightness +else + # Execute command2 if the time is between sunrise and sunset + reset_brightness +fi