forked from dandydanny/PiAutoDim
-
Notifications
You must be signed in to change notification settings - Fork 0
/
autobrightness.py
40 lines (32 loc) · 1.07 KB
/
autobrightness.py
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
# PiAutoDim
# Auto-Dimming for the Raspberry-Pi 7" Touchscreen LCD Display
# Based on https://gpiozero.readthedocs.io/en/stable/recipes.html#light-sensor
from gpiozero import LightSensor
import time
# Desired maximum & minimum backlight values
max = 255
min = 30
prev = 0
offset = 10
delta = 0
# Open backlight control 'file' for writing
f = open('/sys/class/backlight/rpi_backlight/brightness', 'w')
# Instantiate sensor object, set charge time suitable for 10uF capacitor
sensor = LightSensor(18, charge_time_limit=0.2, threshold=0.1)
while True:
# Get light sensor value, map to 0-255
lightValue = round(255 * sensor.value + offset)
# Keep values within defined maximum-minimum values
if lightValue > 220:
lightValue = max
if lightValue < min:
lightValue = min
# Calculate change in brightness value
delta = abs(lightValue - prev)
# Only write (change) backlight value if there's sensor level change
if delta > 5:
prev = lightValue
f.seek(0)
f.write(str(lightValue))
f.truncate()
time.sleep(1)