-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathchecklight.sh
executable file
·58 lines (44 loc) · 1.22 KB
/
checklight.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
#!/bin/sh
#----
# Captures from the camera and changes the brightness of the
# screen backlight according to the brightness level of the
# image captured by the cam.
#
# Uses ffmpeg, imagemagick, xbacklight and acpi
#----
# Time for the camera to capture
TIMECK=0.05
# Minimal % for a change to be made
TOLERANCE=5
# Maximal % to change (smaller means smoother)
TOP=10
# Limits in brightness level detected
UPPER=35
LOWER=10
acpi -a | grep "off-line" && { # change limits if on battery
UPPER=60
LOWER=30
}
# This makes it easier for the script to be run by cron
[ -z $DISPLAY ] && export DISPLAY=:0
IMG=$(mktemp --suffix=.jpg)
ffmpeg -loglevel error -f video4linux2 -i /dev/video0 -r $TIMECK -t $TIMECK "$IMG" || exit $?
BRIGHT=$(convert "$IMG" -format "%[fx:100*mean]" info:)
echo "Brightness detected: ${BRIGHT}%"
[ ${BRIGHT%.*} -lt $LOWER ] && {
LIGHT=0
} || {
[ ${BRIGHT%.*} -gt $UPPER ] && {
LIGHT=100
} || {
LIGHT=$(( ${BRIGHT%.*}*(100/UPPER) ))
}
}
LIGHT=${LIGHT%%.*}
DIFF=$(($(xbacklight -get | cut -f1 -d.) - $LIGHT))
[ $DIFF -gt $TOP ] && DIFF=$TOP
[ $DIFF -gt $TOLERANCE -o $DIFF -lt -$TOLERANCE ] && {
echo Setting backlight to $LIGHT
xbacklight -set ${LIGHT%%.*} -time 3000
}
rm "$IMG"