forked from VergilYotov/oled-linux
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patholed-linux.sh
executable file
·218 lines (188 loc) · 6.95 KB
/
oled-linux.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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
#!/bin/bash
cd $(dirname ${BASH_SOURCE[0]})
# Load configuration file
source oled-linux.conf
function color(){ echo -e "\e[$1m${*:2}\e[0m"; }
function red(){ color 31 $@; }
function yellow(){ color 33 $@; }
function blue(){ color 34 $@; }
function abort(){ red $@; exit 1; }
# If no oled_screen is set - attempt to guess it
if [[ -z $oled_screen ]]; then
oled_screen=$(xrandr --current | grep -m 1 " connected" | awk '{print $1}')
blue "Guessed OLED display as: $oled_screen"
fi
# Verify the backlight directory is set correctly
if ! test -d "$backlight_dir"; then
abort "ERROR: wrong configuration. Backlight directory does not exist."
fi
# Verify dependencies are installed
if ! command -v inotifywait; then
abort "ERROR: dependency 'inotifywait' is not installed."
fi
if $use_redshift; then
if ! command -v redshift; then
yellow "WARNING: optional dependency 'redshift' is not installed." \
"Redshift functionality disabled."
use_redshift=false
fi
if ! command -v sunwait; then
yellow "WARNING: optional dependency 'sunwait' is not installed." \
"Redshift functionality disabled."
fi
# Attempt to get the location of where-am-i from geoclue2 demos
if [[ -z $location ]]; then
where_am_i=NULL
for where_am_i_location in "/usr/lib/geoclue-2.0/demos/where-am-i" \
"/usr/libexec/geoclue-2.0/demos/where-am-i"; do
if test -f $where_am_i_location; then
where_am_i=$where_am_i_location
break
fi
done
if ! test -f $where_am_i; then
yellow "WARNING: Optional dependency 'where-am-i' from geoclue2 demo files could not found." \
"Please install geoclue2 and or geoclue2-demo." \
"Redshift functionality disabled."
use_redshift=false
fi
fi
fi
# Create the file pipes directory if it does not exist
if ! test -d .file-pipes; then
mkdir .file-pipes
fi
##
# Redshift background services
#
if $use_redshift; then
if [[ -z $location ]]; then
blue "Enabled location service."
##
# Location service
#
{
sleep 1s # Just to make sure the other services are running
while true
do
$where_am_i > .file-pipes/where-am-i-result.txt
latitude=$(cat .file-pipes/where-am-i-result.txt | grep -m 1 Latitude | awk '{FS=":";print $2}' | sed 's/?//g')
longitude=$(cat .file-pipes/where-am-i-result.txt | grep -m 1 Longitude | awk '{FS=":";print $2}'| sed 's/?//g')
latitude=${latitude::-1} # removes trailing °
longitude=${longitude::-1} # removes trailing °
if (( $(echo "$latitude < 0" | bc -l) ))
then
latitude_suffix='S'
else
latitude_suffix='N'
fi
if (( $(echo "$longitude < 0" | bc -l) ))
then
longitude_suffix='W'
else
longitude_suffix='E'
fi
echo "${latitude}${latitude_suffix} ${longitude}${longitude_suffix}" > .file-pipes/current-location.txt
sleep 30m
done
} &
##
# Watch location service
#
{
if ! test -f .file-pipes/current_location.txt; then
touch .file-pipes/current-location.txt
fi
while true; do
inotifywait -e close_write .file-pipes/current-location.txt
if ! diff .file-pipes/location.txt .file-pipes/current-location.txt
then
cp .file-pipes/current-location.txt .file-pipes/location.txt
fi
done
} &
else
echo $location > .file-pipes/location.txt
fi
##
# Set day night service
#
{
# Make sure the location.txt exists before calling sunwait
if ! test -f .file-pipes/location.txt; then
touch .file-pipes/location.txt
inotifywait -e close_write .file-pipes/location.txt
fi
while true
do
sunwait poll `cat .file-pipes/location.txt` > .file-pipes/day-night.txt
sleep 1m
done
} &
fi
max_brightness=$(cat "$backlight_dir/max_brightness")
current_brightness=$max_brightness
target_shift=$daylight_temperature
current_shift=$daylight_temperature
while true; do
target_brightness=$(cat "$backlight_dir/brightness")
if test -f .file-pipes/day-night.txt; then
day_night=$(cat .file-pipes/day-night.txt)
else
touch .file-pipes/day-night.txt
day_night="DAY"
fi
if $use_redshift && [ "$day_night" = "NIGHT" ]
then
target_shift=$night_temperature
else
target_shift=$daylight_temperature
fi
if [ $current_brightness -eq $target_brightness ] && [ $current_shift -eq $target_shift ]
then
inotifywait -e close_write $backlight_dir/brightness -e close_write "./.file-pipes/day-night.txt" -e close_write oled-linux.conf > /dev/null
# Auto-reloads the following list of variables
eval $(
source oled-linux.conf;
echo backlight_dir="$backlight_dir";
echo brightness_step_size_factor="$brightness_step_size_factor";
echo daylight_temperature="$daylight_temperature";
echo night_temperature="$night_temperature";
echo redshift_step_size="$redshift_step_size";
)
continue
fi
step=$((current_brightness - target_brightness))
if [ $step -lt 0 ]; then step=$((-step)); fi
brightness_step_size=$((step / brightness_step_size_factor))
if [ $brightness_step_size -lt $((max_brightness / 500)) ]; then brightness_step_size=$((max_brightness / 500)); fi
if [ $step -gt $brightness_step_size ]; then step=$brightness_step_size; fi
if [ $current_brightness -gt $target_brightness ]
then
current_brightness=$((current_brightness - step))
else
current_brightness=$((current_brightness + step))
fi
percent=`echo "$current_brightness / $max_brightness * 0.9 + 0.1" | bc -l`
if $use_redshift; then
step=$((current_shift - target_shift))
if [ $step -lt 0 ]; then step=$((-step)); fi
if [ $step -gt $redshift_step_size ]; then step=$redshift_step_size; fi
if [ $current_shift -gt $target_shift ]
then
current_shift=$((current_shift - step))
else
current_shift=$((current_shift + step))
fi
redshift -m randr:screen=$oled_screen -P -O $current_shift -b $percent
xrandr | grep -m 1 " connected " | awk '{print $1}' | while read -r line
do
if ! [ "$line" == "$oled_screen" ]
then
redshift -m randr:screen=$line -P -O $current_shift
fi
done
else
xrandr --output $oled_screen --brightness $percent
fi
done