-
Notifications
You must be signed in to change notification settings - Fork 4
/
AutomaticBrightness.sh
executable file
·183 lines (130 loc) · 4.65 KB
/
AutomaticBrightness.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
#!/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
# NOW WITH FLOAT SUPPORT
SensorToDisplayScale=24.09
#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
MinimumBrightness=001
# 2 : Default | 1 : Add Offset | 0 : Subtract Offset, Recomended not to change
op=2
# Only look for flags -i or -d with an aditional value
# AutomaticBrightness.sh -i 100
while getopts i:d: flag
do
case "${flag}" in
i) op=1
num=${OPTARG};;
d) op=0
num=${OPTARG};;
esac
done
# Verigy offset file exsits and if so read it
if [[ -f /dev/shm/AB.offset ]]
then
OffSet=$(cat /dev/shm/AB.offset)
else
OffSet=0
$(echo $OffSet > /dev/shm/AB.offset)
$(chmod 666 /dev/shm/AB.offset)
fi
#if no offset or its less than 0 make 0
OffSet=$((OffSet < 0 ? 0 : OffSet))
# relativly change number in Offset file and write it
if [[ $op -lt 2 ]]
then
if [[ $op -eq 1 ]]
then
OffSet=$((OffSet + num))
else
OffSet=$((OffSet - num))
fi
# verify offset is not less than 0
OffSet=$((OffSet < 0 ? 0 : OffSet))
$(echo $OffSet > /dev/shm/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" "$$"
sleep 5
# Get screen max brightness value
MaxScreenBrightness=$(find -L /sys/class/backlight -maxdepth 2 -name "max_brightness" 2>/dev/null | grep "max_brightness" | xargs cat)
# Set path to current screen brightness value
BLightPath=$(find -L /sys/class/backlight -maxdepth 2 -name "brightness" 2>/dev/null | grep "brightness")
# Set path to current luminance sensor
LSensorPath=$(find -L /sys/bus/iio/devices -maxdepth 2 -name "in_illuminance_raw" 2>/dev/null | grep "in_illuminance_raw")
#Set the current light value so we have something to compare to
OldLight=$(cat $LSensorPath)
while true
do
if [[ -f /dev/shm/AB.offset ]]
then
OffSet=$(cat /dev/shm/AB.offset)
else
OffSet=0
$(echo $OffSet > /dev/shm/AB.offset)
$(chmod 666 /dev/shm/AB.offset)
fi
Light=$(cat $LSensorPath)
## apply offset to current light value
Light=$((Light + OffSet))
# Set allowed range for light
MaxOld=$((OldLight + OldLight/LightChange))
MinOld=$((OldLight - OldLight/LightChange))
if [[ $Light -gt $MaxOld ]] || [[ $Light -lt $MinOld ]]
then
CurrentBrightness=$(cat $BLightPath)
# Add MinimumBrighness here to not effect comparison but the outcome
Light=$(LC_NUMERIC=C printf "%.0f" $(echo "scale=2; $Light + ( ($MaxScreenBrightness * ( $MinimumBrightness / 100 )) / $SensorToDisplayScale ) " | bc ))
# Gernate a TempLight value for the screen to be set to
# Float math thanks Matthias_Wachter
TempLight=$(LC_NUMERIC=C printf "%.0f" $(echo "scale=2; $Light * $SensorToDisplayScale" | bc))
# Check we dont ask the screen to go brighter than it can
if [[ $TempLight -gt $MaxScreenBrightness ]]
then
NewLight=$MaxScreenBrightness
else
NewLight=$TempLight
fi
# How diffrent should each stop be
DiffCount=$(LC_NUMERIC=C printf "%.0f" $(echo "scale=2; ( $NewLight - $CurrentBrightness ) / $LevelSteps" | bc ))
# Step once per Screen Hz to make animation
for i in $(eval echo {1..$LevelSteps} )
do
# Set new relative light value
NewLight=$(( $DiffCount ))
CurrentBrightness=$(cat $BLightPath)
FakeLight=$(( $NewLight + $CurrentBrightness))
if [[ $FakeLight -gt $MaxScreenBrightness ]]
then
NewLight=$MaxScreenBrightness
echo "ERROR"
else
echo $FakeLight > $BLightPath
fi
# Format values apropriatly for brightnessctl
#if [[ $NewLight -lt 0 ]]
#then
#NewLight=$( echo "$NewLight" | awk -F "-" {'print$2'})
#NewLight=$(echo $NewLight-)
#else
#NewLight=$(echo +$NewLight)
#fi
# Adjust brightness relativly
#brightnessctl -q s $NewLight
# Sleep for the screen Hz time so he effect is visible
sleep $AnimationDelay
done
# Store new light as old light for next comparison
OldLight=$Light
fi
sleep $SensorDelay
done