From 4e6c8ee256d0b7e626d21ea491bfdf06eca61639 Mon Sep 17 00:00:00 2001 From: Theluga <96307393+Theluga@users.noreply.github.com> Date: Sun, 17 Dec 2023 14:17:55 +0000 Subject: [PATCH 1/9] fixing typo adding new script and service example I figure out how to proper pull haha sorry for the confusion The darker mode, when the illuminance is less than 1 i.e. 0, it will go to the target brightness that is 1. The target can be easily changed. If the file AB.offset have 0 as value. Darker mode is activated and setting brightness will reset to target value 1 when illuminance is less than 1. if the file has value of 1, it will be the normal mode and the screen brightness can be adjusted until the illuminance sensor changes. easier to change variables and places and chmod 666 /tmp/AB.offset for the user to change modes when script is running as root (systemd service) darker mode as default with normal mode done by typing "echo 1 > /tmp/AB.offset" they are in autobrightness.sh --- AutomaticBrightness.sh | 2 +- autobright.service | 25 ++++++ autobrightness.sh | 171 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 197 insertions(+), 1 deletion(-) create mode 100644 autobright.service create mode 100644 autobrightness.sh diff --git a/AutomaticBrightness.sh b/AutomaticBrightness.sh index a407b2c..a716c8f 100644 --- a/AutomaticBrightness.sh +++ b/AutomaticBrightness.sh @@ -66,7 +66,7 @@ fi priority=19 # Priority level , 0 = regular app , 19 = very much background app # Set the priority of the current script, Thank you Theluga. -renice "$cpu_limit" "$$" +renice "$priority" "$$" diff --git a/autobright.service b/autobright.service new file mode 100644 index 0000000..735662d --- /dev/null +++ b/autobright.service @@ -0,0 +1,25 @@ +[Unit] + +Description=roda autobrightness com 1% de processamento +After=multi-user.target +After=graphical.target +After=basic.target +After=sysinit.target +After=system.slice +After=systemd-journald.socket +After=initrd-switch-root.service +After=iio-sensor-proxy.service + + + +[Service] +ExecStartPre=/bin/bash -c "sleep 5" +ExecStart=/bin/bash -c "exec /etc/luztecla/autobrightness.sh" + + +Restart=always + +[Install] + +WantedBy=graphical.target +WantedBy=multi-user.target diff --git a/autobrightness.sh b/autobrightness.sh new file mode 100644 index 0000000..15d5a5e --- /dev/null +++ b/autobrightness.sh @@ -0,0 +1,171 @@ +#!/bin/bash + +#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=24 + +# 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 + +# 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 + +AnbientSensorIlluminance=/sys/bus/iio/devices/iio:device5/in_illuminance_raw + +#Normal Mode, if the file offset is set to 1. Darker Mode if set to 0 +# the offset file in /tmp/AB.offset will start with 0 so darker mode is default +# setting to 1 will enable normal mode. (darker mode ignores brightness set if iluminance is 0 and will set display to actual_brightness=1 + +# Function to smoothly decrease brightness +#darker mode +smoothly_decrease_brightness() { + current_brightness=$(cat $MonitorBrightness) + target_brightness=1 # can be set to 0 or more when illuminance is 0. + + steps=60 # You can adjust the number of steps for smoother transition + animation_delay=0.016 + + diff_count=$((($target_brightness - $current_brightness) / $steps)) + + for ((i = 1; i <= $steps; i++)); do + new_brightness=$((current_brightness + i * diff_count)) + brightnessctl -q s $new_brightness + sleep $animation_delay + done + + # Set the final brightness value + brightnessctl -q s $target_brightness +} + +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) + Light=$((Light + 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 + 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 + + brightnessctl -q s $NewLight + sleep $AnimationDelay + done + + OldLight=$Light + fi + +#darker mode activated + if [[ $Light -lt 1 ]]; then + smoothly_decrease_brightness + fi + + sleep $SensorDelay + fi +done + +rm '/tmp/AB.running' +rm '/tmp/AB.kill' From 39ff5f9d8b189769d294ac37d4c49a4fb1c9b5c2 Mon Sep 17 00:00:00 2001 From: Theluga <96307393+Theluga@users.noreply.github.com> Date: Tue, 19 Dec 2023 12:51:50 -0300 Subject: [PATCH 2/9] Improved sensor detection and more Removed nonsensical function, improved sensor detection with * : very important when you have multiple sensors that are changing device order when booting improved user configurable variable on the top of scrip detecting minimum illuminance to set minimum brightness inside the loop (now brightness key works) --- autobrightness.sh | 63 ++++++++++++++++++++++------------------------- 1 file changed, 30 insertions(+), 33 deletions(-) diff --git a/autobrightness.sh b/autobrightness.sh index 15d5a5e..7ae7e36 100644 --- a/autobrightness.sh +++ b/autobrightness.sh @@ -6,13 +6,13 @@ Priority=19 # CPU limit, adjust as needed # How much light change must be seen by the sensor before it will act -LightChange=10 +LightChange=5 # How often it checks the sensor SensorDelay=1 # Scale sensor to display brightness range -SensorToDisplayScale=24 +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 @@ -24,38 +24,20 @@ AnimationDelay=0.016 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 -AnbientSensorIlluminance=/sys/bus/iio/devices/iio:device5/in_illuminance_raw +#my sensor keep changing places, this way it will always be found with * if you have lots of sensors -#Normal Mode, if the file offset is set to 1. Darker Mode if set to 0 -# the offset file in /tmp/AB.offset will start with 0 so darker mode is default -# setting to 1 will enable normal mode. (darker mode ignores brightness set if iluminance is 0 and will set display to actual_brightness=1 +AnbientSensorIlluminance=/sys/bus/iio/devices/iio:*/in_illuminance_raw -# Function to smoothly decrease brightness -#darker mode -smoothly_decrease_brightness() { - current_brightness=$(cat $MonitorBrightness) - target_brightness=1 # can be set to 0 or more when illuminance is 0. - - steps=60 # You can adjust the number of steps for smoother transition - animation_delay=0.016 - - diff_count=$((($target_brightness - $current_brightness) / $steps)) - - for ((i = 1; i <= $steps; i++)); do - new_brightness=$((current_brightness + i * diff_count)) - brightnessctl -q s $new_brightness - sleep $animation_delay - done - - # Set the final brightness value - brightnessctl -q s $target_brightness -} while getopts i:d: flag; do case "${flag}" in @@ -118,7 +100,9 @@ until [ -f /tmp/AB.kill ]; do fi Light=$(cat $AnbientSensorIlluminance) + RealLight=$(cat $AnbientSensorIlluminance) Light=$((Light + OffSet)) + RealLight=$((RealLight + OffSet)) if [[ $Light -lt $LightChange ]]; then MaxOld=$((OldLight + LightChange)) @@ -135,12 +119,16 @@ until [ -f /tmp/AB.kill ]; do 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 )) @@ -150,7 +138,21 @@ until [ -f /tmp/AB.kill ]; do 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 @@ -158,11 +160,6 @@ until [ -f /tmp/AB.kill ]; do OldLight=$Light fi -#darker mode activated - if [[ $Light -lt 1 ]]; then - smoothly_decrease_brightness - fi - sleep $SensorDelay fi done From 0d1e5e5c148f1b402e84d94b283d949c043263ad Mon Sep 17 00:00:00 2001 From: Theluga <96307393+Theluga@users.noreply.github.com> Date: Tue, 19 Dec 2023 16:02:00 +0000 Subject: [PATCH 3/9] Update autobright.service Changed Systemd service example from: https://github.com/wagensveld/Mac-like-automatic-brightness without my system specif paths --- autobright.service | 23 +++-------------------- 1 file changed, 3 insertions(+), 20 deletions(-) diff --git a/autobright.service b/autobright.service index 735662d..cbdde28 100644 --- a/autobright.service +++ b/autobright.service @@ -1,25 +1,8 @@ [Unit] - -Description=roda autobrightness com 1% de processamento -After=multi-user.target -After=graphical.target -After=basic.target -After=sysinit.target -After=system.slice -After=systemd-journald.socket -After=initrd-switch-root.service -After=iio-sensor-proxy.service - - +Description=Mac like automatic brightness service [Service] -ExecStartPre=/bin/bash -c "sleep 5" -ExecStart=/bin/bash -c "exec /etc/luztecla/autobrightness.sh" - - -Restart=always +ExecStart=/path/to/AutomaticBrightness.sh [Install] - -WantedBy=graphical.target -WantedBy=multi-user.target +WantedBy=default.target From d04075c5e5bc726ed285e3d87dc12c9839f3b01e Mon Sep 17 00:00:00 2001 From: Theluga <96307393+Theluga@users.noreply.github.com> Date: Tue, 19 Dec 2023 16:03:27 +0000 Subject: [PATCH 4/9] Rename autobright.service to AutomaticBrightness.service changing name to generic --- autobright.service => AutomaticBrightness.service | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename autobright.service => AutomaticBrightness.service (100%) diff --git a/autobright.service b/AutomaticBrightness.service similarity index 100% rename from autobright.service rename to AutomaticBrightness.service From 78f3c14ed9b39542b06f2cf27bd27d2b38e86b08 Mon Sep 17 00:00:00 2001 From: Theluga <96307393+Theluga@users.noreply.github.com> Date: Tue, 19 Dec 2023 16:06:28 +0000 Subject: [PATCH 5/9] Update README.md Dell Inspiron 7359 updated --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 11ccf1c..1a63342 100644 --- a/README.md +++ b/README.md @@ -17,7 +17,7 @@ Your sensor has a diffrent range thant the 12th Gen Intel Framework laptop senso Type | Sensor Rnge | STDScale 11th Gen Intel | 0 - 3207633 | 1 12th Gen Intel | 0 - 3984 | 24 - Dell Insp 7359 | 0 - 15000 | ? + Dell Insp 7359 | 0 - 15000 | 20 ## Controls ```./AutomaticBrightness.sh | Defualt running mode of script``` From 346577c270ea375a7f36e1625ce2d83bb1fdeb7b Mon Sep 17 00:00:00 2001 From: Theluga <96307393+Theluga@users.noreply.github.com> Date: Tue, 19 Dec 2023 17:04:06 +0000 Subject: [PATCH 6/9] Update autobrightness.sh default sensitivity to use. --- autobrightness.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/autobrightness.sh b/autobrightness.sh index 7ae7e36..2a521a1 100644 --- a/autobrightness.sh +++ b/autobrightness.sh @@ -6,7 +6,7 @@ Priority=19 # CPU limit, adjust as needed # How much light change must be seen by the sensor before it will act -LightChange=5 +LightChange=10 # How often it checks the sensor SensorDelay=1 From 07e9284ec45582782eac2575d0902e293494d7a9 Mon Sep 17 00:00:00 2001 From: steel99xl Date: Thu, 21 Dec 2023 12:50:47 -0500 Subject: [PATCH 7/9] minor update autobrightness.sh The only possible issue I could see with the * in the iio device is if you have 2 sensors, also I'm not 100% sure if it has to check each iio device every time the value is called --- autobrightness.sh | 1 + 1 file changed, 1 insertion(+) diff --git a/autobrightness.sh b/autobrightness.sh index 2a521a1..c3d2921 100644 --- a/autobrightness.sh +++ b/autobrightness.sh @@ -1,4 +1,5 @@ #!/bin/bash +# Slight Alteration of AutomaticBrighness.sh (could be a dell specific fork) #variables to run From 4a4db6e2beae37bbdbc0228254726d664ae26d96 Mon Sep 17 00:00:00 2001 From: Theluga <96307393+Theluga@users.noreply.github.com> Date: Thu, 21 Dec 2023 18:11:08 +0000 Subject: [PATCH 8/9] Update autobrightness.sh Now it saves the path to the variable after detecting the correct path --- autobrightness.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/autobrightness.sh b/autobrightness.sh index c3d2921..6248382 100644 --- a/autobrightness.sh +++ b/autobrightness.sh @@ -37,7 +37,7 @@ 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=/sys/bus/iio/devices/iio:*/in_illuminance_raw +AnbientSensorIlluminance=`echo realpath /sys/bus/iio/devices/iio:*/in_illuminance_raw` while getopts i:d: flag; do From 0fb675b825c887a6b30eda6b6d3f846316dda5b2 Mon Sep 17 00:00:00 2001 From: Theluga <96307393+Theluga@users.noreply.github.com> Date: Thu, 21 Dec 2023 18:39:10 +0000 Subject: [PATCH 9/9] als autodetection working Now it will always use the first als found on the system. Auto detection if only 1 such device exists --- autobrightness.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/autobrightness.sh b/autobrightness.sh index 6248382..4c64893 100644 --- a/autobrightness.sh +++ b/autobrightness.sh @@ -37,7 +37,7 @@ 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 realpath /sys/bus/iio/devices/iio:*/in_illuminance_raw` +AnbientSensorIlluminance=`echo "$(set -- $(realpath /sys/bus/iio/devices/iio:*/in_illuminance_raw); echo "$1")"` while getopts i:d: flag; do