Mac-like-automatic-brightness/AutomaticBrightness.sh
Theluga 4e6c8ee256
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
2023-12-17 14:17:55 +00:00

164 lines
3 KiB
Bash

#!/bin/bash
#How much light change must be seen by the sensor befor it will act
LightChange=10
#How often it check the sensor
SensorDelay=1
# Scale sesor to displas brightness range
SensorToDisplayScale=24
#This should match your refesh rate other wise it will either change the back light more times than needed or too few for a smooth animation
LevelSteps=60
# The is should match the LevelSteps but in the acual time each event should take to see
AnimationDelay=0.016
# Read the variable names
MaxScreenBrightness=96000
MinimumBrightness=001
# 2 : Default | 1 : Add Offset | 0 : Subtract Offset, Recomended not to change
op=2
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)
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)
exit
fi
# This was moved down here to not affect performance of setting AB.offset
priority=19 # Priority level , 0 = regular app , 19 = very much background app
# Set the priority of the current script, Thank you Theluga.
renice "$priority" "$$"
touch '/tmp/AB.running'
OldLight=$(cat /sys/bus/iio/devices/iio\:device0/in_illuminance_raw)
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)
fi
Light=$(cat /sys/bus/iio/devices/iio\:device0/in_illuminance_raw)
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 /sys/class/backlight/intel_backlight/brightness)
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
sleep $SensorDelay
fi
done
rm '/tmp/AB.running'
rm '/tmp/AB.kill'