From 5bb0c72b5da6df101978e5f9d724a5b13edfe189 Mon Sep 17 00:00:00 2001 From: steel99xl Date: Sun, 24 Dec 2023 13:19:24 -0500 Subject: [PATCH 1/7] Update README.md specify initial difference --- README.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 1a63342..80c8c16 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,6 @@ # Mac-like-automatic-brightness -A simple script to provide a "Mac" like automatic brightness adjustemnt/ animation +A simple script to provide a "Mac" like automatic brightness adjustemnt/ animation. +## Now as a system service made for the FrameWork laptop From d05bf0244f7413cae214b884590abef0499deed8 Mon Sep 17 00:00:00 2001 From: steel99xl Date: Sun, 24 Dec 2023 14:28:01 -0500 Subject: [PATCH 2/7] Inital automatic setup of the AB service --- AB.service | 12 ++++++++++++ setup.sh | 15 +++++++++++++++ 2 files changed, 27 insertions(+) create mode 100644 AB.service create mode 100755 setup.sh diff --git a/AB.service b/AB.service new file mode 100644 index 0000000..d6aa319 --- /dev/null +++ b/AB.service @@ -0,0 +1,12 @@ +[Unit] +Description=Mac like automatic brightness as service + +[Service] +Type=simple +Restart=no +RestartSec=1 + +ExecStart=/usr/local/bin/AutomaticBrightness.sh + +[Install] +WantedBy=default.target diff --git a/setup.sh b/setup.sh new file mode 100755 index 0000000..ab8ab0c --- /dev/null +++ b/setup.sh @@ -0,0 +1,15 @@ +#!/bin/bash +echo "Setting up AutomaticBrightness.sh as a service..." + +echo "Cloning AutomaticBrighness.sh..." +sudo cp AutomaticBrightness.sh /usr/local/bin/ + +echo "Cloning AB.service for systemD" +sudo cp AB.service /etc/systemd/system/ + +echo "Startin Service..." +sudo systemctl enable AB +sudo systemctl start AB + + + From a9d2623851da79337976697b57835998be6f9d93 Mon Sep 17 00:00:00 2001 From: steel99xl Date: Sun, 24 Dec 2023 14:39:29 -0500 Subject: [PATCH 3/7] Minor cleanup --- AutomaticBrightness.service | 8 -- README.md | 1 + autobrightness.sh | 169 ------------------------------------ 3 files changed, 1 insertion(+), 177 deletions(-) delete mode 100644 AutomaticBrightness.service delete mode 100644 autobrightness.sh diff --git a/AutomaticBrightness.service b/AutomaticBrightness.service deleted file mode 100644 index cbdde28..0000000 --- a/AutomaticBrightness.service +++ /dev/null @@ -1,8 +0,0 @@ -[Unit] -Description=Mac like automatic brightness service - -[Service] -ExecStart=/path/to/AutomaticBrightness.sh - -[Install] -WantedBy=default.target diff --git a/README.md b/README.md index 80c8c16..7d81699 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,7 @@ # Mac-like-automatic-brightness A simple script to provide a "Mac" like automatic brightness adjustemnt/ animation. ## Now as a system service +Run ```setup.sh``` to make it a service made for the FrameWork laptop diff --git a/autobrightness.sh b/autobrightness.sh deleted file mode 100644 index 4c64893..0000000 --- a/autobrightness.sh +++ /dev/null @@ -1,169 +0,0 @@ -#!/bin/bash -# Slight Alteration of AutomaticBrighness.sh (could be a dell specific fork) - -#variables to run - -#decrease cpu usage -Priority=19 # CPU limit, adjust as needed - -# How much light change must be seen by the sensor before it will act -LightChange=10 - -# How often it checks the sensor -SensorDelay=1 - -# Scale sensor to display brightness range -SensorToDisplayScale=20 - -# This should match your refresh rate otherwise it will either change the backlight more times than needed or too few for a smooth animation -LevelSteps=60 - -# The is should match the LevelSteps but in the actual time each event should take to see -AnimationDelay=0.016 - -# Read the variable names -MaxScreenBrightness=937 -MinimumBrightness=1 - -#minimum illuminance to get minimum brightness - -MinimimumIlluminance=0 - -# 2 : Default | 1 : Add Offset | 0 : Subtract Offset, Recommended not to change -op=2 - -#place where you get the brightness of the monitor being controled and the sensor of brightness -MonitorBrightness=/sys/class/backlight/intel_backlight/brightness - -#my sensor keep changing places, this way it will always be found with * if you have lots of sensors - -AnbientSensorIlluminance=`echo "$(set -- $(realpath /sys/bus/iio/devices/iio:*/in_illuminance_raw); echo "$1")"` - - -while getopts i:d: flag; do - case "${flag}" in - i) op=1 - num=${OPTARG};; - d) op=0 - num=${OPTARG};; - esac -done - -if [[ -f /tmp/AB.offset ]]; then - OffSet=$(cat /tmp/AB.offset) -else - OffSet=0 - echo $OffSet > /tmp/AB.offset - chmod 666 /tmp/AB.offset -fi - -OffSet=$((OffSet < 0 ? 0 : OffSet)) - -if [[ $op -lt 2 ]]; then - if [[ $op -eq 1 ]]; then - OffSet=$((OffSet + num)) - else - OffSet=$((OffSet - num)) - fi - - OffSet=$((OffSet < 0 ? 0 : OffSet)) - echo $OffSet > /tmp/AB.offset - chmod 666 /tmp/AB.offset - exit -fi - - -# Set the priority of the current script and says it is running -renice "$Priority" "$$" -touch '/tmp/AB.running' - - -OldLight=$(cat $AnbientSensorIlluminance) - -until [ -f /tmp/AB.kill ]; do - if [[ -f /tmp/AB.stop ]]; then - rm '/tmp/AB.stop' - rm '/tmp/AB.running' - - until [[ -f /tmp/AB.start ]]; do - sleep 10 - done - - rm '/tmp/AB.start' - touch '/tmp/AB.running' - else - if [[ -f /tmp/AB.offset ]]; then - OffSet=$(cat /tmp/AB.offset) - else - OffSet=0 - echo $OffSet > /tmp/AB.offset - chmod 666 /tmp/AB.offset - fi - - Light=$(cat $AnbientSensorIlluminance) - RealLight=$(cat $AnbientSensorIlluminance) - Light=$((Light + OffSet)) - RealLight=$((RealLight + OffSet)) - - if [[ $Light -lt $LightChange ]]; then - MaxOld=$((OldLight + LightChange)) - MinOld=$((OldLight - LightChange)) - else - MaxOld=$((OldLight + OldLight/LightChange)) - MinOld=$((OldLight - OldLight/LightChange)) - fi - - if [[ $Light -gt $MaxOld ]] || [[ $Light -lt $MinOld ]]; then - CurrentBrightness=$(cat $MonitorBrightness) - Light=$(( $Light + $MinimumBrightness )) - TempLight=$(($Light * $SensorToDisplayScale)) - - if [[ $TempLight -gt $MaxScreenBrightness ]]; then - NewLight=$MaxScreenBrightness - LimitMaxIlluminanceReached=1 - elif [[ $RealLight -le $MinimimumIlluminance ]]; then - NewLight=$MinimumBrightness - LimitMinIlluminanceReached=1 - else - NewLight=$TempLight - fi - - DiffCount=$(( ($NewLight - $CurrentBrightness)/$LevelSteps )) - - for i in $(eval echo {1..$LevelSteps}); do - NewLight=$(( $DiffCount )) - - if [[ $NewLight -lt 0 ]]; then - NewLight=$( echo "$NewLight" | awk -F "-" {'print$2'}) - NewLight=$(echo $NewLight-) - else - NewLight=$(echo +$NewLight) - fi - - if [[ $i -eq $LevelSteps ]]; then - brightnessctl -q s $NewLight - - if [[ $LimitMaxIlluminanceReached -eq 1 ]]; then - NewLight=$MaxScreenBrightness - LimitMaxIlluminanceReached=0 - fi - - if [[ $LimitMinIlluminanceReached -eq 1 ]]; then - NewLight=$MinimumBrightness - LimitMinIlluminanceReached=0 - fi - fi - - brightnessctl -q s $NewLight - sleep $AnimationDelay - done - - OldLight=$Light - fi - - sleep $SensorDelay - fi -done - -rm '/tmp/AB.running' -rm '/tmp/AB.kill' From e6807f6dce25fc5741789de30444a9301ca55318 Mon Sep 17 00:00:00 2001 From: steel99xl Date: Sun, 24 Dec 2023 16:45:42 -0500 Subject: [PATCH 4/7] Minor performance improvments --- AutomaticBrightness.sh | 40 ++++++++++++---------------------------- setup.sh | 1 + 2 files changed, 13 insertions(+), 28 deletions(-) mode change 100644 => 100755 AutomaticBrightness.sh diff --git a/AutomaticBrightness.sh b/AutomaticBrightness.sh old mode 100644 new mode 100755 index a716c8f..4462c2c --- a/AutomaticBrightness.sh +++ b/AutomaticBrightness.sh @@ -34,14 +34,17 @@ do num=${OPTARG};; esac done -if [[ -f /tmp/AB.offset ]] + +if [[ -f /dev/shm/AB.offset ]] then - OffSet=$(cat /tmp/AB.offset) + OffSet=$(cat /dev/shm/AB.offset) else OffSet=0 - $(echo $OffSet > /tmp/AB.offset) + $(echo $OffSet > /dev/shm/AB.offset) + $(chmod 666 /dev/shm/AB.offset) fi + OffSet=$((OffSet < 0 ? 0 : OffSet)) @@ -56,7 +59,7 @@ then OffSet=$((OffSet < 0 ? 0 : OffSet)) - $(echo $OffSet > /tmp/AB.offset) + $(echo $OffSet > /dev/shm/AB.offset) exit @@ -70,31 +73,18 @@ renice "$priority" "$$" -touch '/tmp/AB.running' OldLight=$(cat /sys/bus/iio/devices/iio\:device0/in_illuminance_raw) -until [ -f /tmp/AB.kill ] +while true do - if [[ -f /tmp/AB.stop ]] - then - rm '/tmp/AB.stop' - rm '/tmp/AB.running' - - until [[ -f /tmp/AB.start ]] - do - sleep 10 - done - rm '/tmp/AB.start' - touch '/tmp/AB.running' - else - - if [[ -f /tmp/AB.offset ]] + if [[ -f /dev/shm/AB.offset ]] then - OffSet=$(cat /tmp/AB.offset) + OffSet=$(cat /dev/shm/AB.offset) else OffSet=0 - $(echo $OffSet > /tmp/AB.offset) + $(echo $OffSet > /dev/shm/AB.offset) + $(chmod 666 /dev/shm/AB.offset) fi Light=$(cat /sys/bus/iio/devices/iio\:device0/in_illuminance_raw) @@ -152,13 +142,7 @@ do fi sleep $SensorDelay - fi - done -rm '/tmp/AB.running' -rm '/tmp/AB.kill' - - diff --git a/setup.sh b/setup.sh index ab8ab0c..8689d3c 100755 --- a/setup.sh +++ b/setup.sh @@ -7,6 +7,7 @@ sudo cp AutomaticBrightness.sh /usr/local/bin/ echo "Cloning AB.service for systemD" sudo cp AB.service /etc/systemd/system/ + echo "Startin Service..." sudo systemctl enable AB sudo systemctl start AB From 5eeae81ebe4044c54616965f56c4d2e4328143c0 Mon Sep 17 00:00:00 2001 From: steel99xl Date: Sun, 24 Dec 2023 17:27:05 -0500 Subject: [PATCH 5/7] Update README.md --- README.md | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/README.md b/README.md index 7d81699..7484d60 100644 --- a/README.md +++ b/README.md @@ -28,13 +28,8 @@ Your sensor has a diffrent range thant the 12th Gen Intel Framework laptop senso ```./AutomaticBrightness.sh -d [NUMBER] | Decrease the offset your brightness sensors raw reading ``` -```/tmp/AB.offset | Stores current offset for the sensor``` +```/dev/shm/AB.offset | Stores current offset for the sensor``` -```/tmp/AB.stop | Stops AutomaticBrightness.sh``` - -```/tmp/AB.start | Starts stopped AutomaticBrightness.sh``` - -```/tmp/AB.kill | Kills AutomaticBrightness.sh``` when running you will see a ```AB.running``` file and ```AB.offset``` in ```/tmp``` From 1e0b3a01ca8fa23a6f3362bc065a2cb12fe1ec28 Mon Sep 17 00:00:00 2001 From: steel99xl Date: Sun, 24 Dec 2023 17:27:26 -0500 Subject: [PATCH 6/7] Update README.md --- README.md | 2 -- 1 file changed, 2 deletions(-) diff --git a/README.md b/README.md index 7484d60..b9faf48 100644 --- a/README.md +++ b/README.md @@ -31,8 +31,6 @@ Your sensor has a diffrent range thant the 12th Gen Intel Framework laptop senso ```/dev/shm/AB.offset | Stores current offset for the sensor``` -when running you will see a ```AB.running``` file and ```AB.offset``` in ```/tmp``` - ## Configuring ```Light Change``` The percent of light change needed to be seen by the sensor for it to change the screen brightness From 21173c5eeec4f68f8634789e1cdecffdcf1a6c8e Mon Sep 17 00:00:00 2001 From: steel99xl Date: Wed, 3 Jan 2024 18:45:44 -0500 Subject: [PATCH 7/7] Auto Backlight and Sensor detection, Scale needs to be set manualy --- AutomaticBrightness.sh | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/AutomaticBrightness.sh b/AutomaticBrightness.sh index 4462c2c..4e8ee3f 100755 --- a/AutomaticBrightness.sh +++ b/AutomaticBrightness.sh @@ -16,8 +16,6 @@ AnimationDelay=0.016 # Read the variable names -MaxScreenBrightness=96000 - MinimumBrightness=001 @@ -71,10 +69,15 @@ priority=19 # Priority level , 0 = regular app , 19 = very much background app # Set the priority of the current script, Thank you Theluga. renice "$priority" "$$" +MaxScreenBrightness=$(find -L /sys/class/backlight -maxdepth 2 -name "max_brightness" 2>/dev/null | grep "max_brightness" | xargs cat) + +BLightPath=$(find -L /sys/class/backlight -maxdepth 2 -name "brightness" 2>/dev/null | grep "brightness") + +LSensorPath=$(find -L /sys/bus/iio/devices -maxdepth 2 -name "in_illuminance_raw" 2>/dev/null | grep "in_illuminance_raw") -OldLight=$(cat /sys/bus/iio/devices/iio\:device0/in_illuminance_raw) +OldLight=$(cat $LSensorPath) while true do @@ -87,7 +90,7 @@ do $(chmod 666 /dev/shm/AB.offset) fi - Light=$(cat /sys/bus/iio/devices/iio\:device0/in_illuminance_raw) + Light=$(cat $LSensorPath) Light=$((Light + OffSet)) if [[ $Light -lt $LightChange ]] @@ -103,7 +106,7 @@ do then - CurrentBrightness=$(cat /sys/class/backlight/intel_backlight/brightness) + CurrentBrightness=$(cat $BLightPath) Light=$(( $Light + $MinimumBrightness ))