-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadaptive_brightness.py
112 lines (104 loc) · 4.18 KB
/
adaptive_brightness.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
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
#!/usr/bin/python3 -u
import smbus
import os
import subprocess
from time import sleep
def get_var(varname):
try:
CMD = 'echo $(source /opt/lightsensor/lightsensor_env.sh; echo $%s)' % varname
p = subprocess.Popen(CMD, stdout=subprocess.PIPE, shell=True, executable='/bin/bash')
return int(p.stdout.readlines()[0].strip())
except:
CMD = 'echo $(source /opt/lightsensor/lightsensor_default_env.sh; echo $%s)' % varname
p = subprocess.Popen(CMD, stdout=subprocess.PIPE, shell=True, executable='/bin/bash')
return int(p.stdout.readlines()[0].strip())
# ---------------------------------
# the addresss of TSL2561 can be
# 0x29, 0x39 or 0x49
BUS = 1
TSL2561_ADDR = 0x39
daynight_gpio = get_var('DAYNIGHT_PIN')
# ---------------------------------
i2cBus = smbus.SMBus(BUS)
# Start messure with 402 ms
# (scale factor 1)
i2cBus.write_byte_data(TSL2561_ADDR, 0x80, 0x03)
lastvalue = 0
while True:
# read global brightness read low byte
LSB = i2cBus.read_byte_data(TSL2561_ADDR, 0x8C)
# read high byte
MSB = i2cBus.read_byte_data(TSL2561_ADDR, 0x8D)
Ambient = (MSB << 8) + LSB
#print ("Ambient: {}".format(Ambient))
# read infra red read low byte
LSB = i2cBus.read_byte_data(TSL2561_ADDR, 0x8E)
# read high byte
MSB = i2cBus.read_byte_data(TSL2561_ADDR, 0x8F)
Infrared = (MSB << 8) + LSB
#print ("Infrared: {}".format(Infrared))
# Calc visible spectrum
Visible = Ambient - Infrared
#print ("Visible: {}".format(Visible))
# Calc factor Infrared/Ambient
Ratio = 0
Lux = 0
if Ambient != 0:
Ratio = float(Infrared)/float(Ambient)
#print ("Ratio: {}".format(Ratio))
# Calc lux based on data sheet TSL2561T T, FN, and CL Package
if 0 < Ratio <= 0.50:
Lux = 0.0304*float(Ambient) - 0.062*float(Ambient)*(Ratio**1.4)
elif 0.50 < Ratio <= 0.61:
Lux = 0.0224*float(Ambient) - 0.031*float(Infrared)
elif 0.61 < Ratio <= 0.80:
Lux = 0.0128*float(Ambient) - 0.0153*float(Infrared)
elif 0.80 < Ratio <= 1.3:
Lux = 0.00146*float(Ambient) - 0.00112*float(Infrared)
else:
Lux = 0
Luxrounded = round(Lux,1)
if lastvalue != Luxrounded:
print ("Lux = {}\n".format(Luxrounded))
os.system("echo {} > /tmp/tsl2561".format(Luxrounded))
lastvalue = Luxrounded
#Set display brigthness
if Luxrounded <= get_var('LUX_LEVEL_1'):
#os.system("xbacklight -set " + str(get_var('DISP_BRIGHTNESS_1')) + " &")
file = open("/sys/class/backlight/rpi_backlight/brightness", "w")
file.write(str(get_var('DISP_BRIGHTNESS_1')))
file.close()
step = 1
elif Luxrounded > get_var('LUX_LEVEL_1') and Luxrounded < get_var('LUX_LEVEL_2'):
#os.system("xbacklight -set " + str(get_var('DISP_BRIGHTNESS_2')) + " &")
file = open("/sys/class/backlight/rpi_backlight/brightness", "w")
file.write(str(get_var('DISP_BRIGHTNESS_2')))
file.close()
step = 2
elif Luxrounded >= get_var('LUX_LEVEL_2') and Luxrounded < get_var('LUX_LEVEL_3'):
#os.system("xbacklight -set " + str(get_var('DISP_BRIGHTNESS_3')) + " &")
file = open("/sys/class/backlight/rpi_backlight/brightness", "w")
file.write(str(get_var('DISP_BRIGHTNESS_3')))
file.close()
step = 3
elif Luxrounded >= get_var('LUX_LEVEL_3') and Luxrounded < get_var('LUX_LEVEL_4'):
#os.system("xbacklight -set " + str(get_var('DISP_BRIGHTNESS_4')) + " &")
file = open("/sys/class/backlight/rpi_backlight/brightness", "w")
file.write( str(get_var('DISP_BRIGHTNESS_4')))
file.close()
step = 4
elif Luxrounded >= get_var('LUX_LEVEL_5'):
#os.system("xbacklight -set " + str(get_var('DISP_BRIGHTNESS_5')) + " &")
file = open("/sys/class/backlight/rpi_backlight/brightness", "w")
file.write(str(get_var('DISP_BRIGHTNESS_5')))
file.close()
step = 5
if daynight_gpio == 0:
if step <= get_var('TSL2561_DAYNIGHT_ON_STEP'):
print("Lux = {} | ".format(Luxrounded) + "Level " + str(step) + " -> trigger night")
os.system("touch /tmp/night_mode_enabled >/dev/null 2>&1")
else:
if step > get_var('TSL2561_DAYNIGHT_ON_STEP'):
print("Lux = {} | ".format(Luxrounded) + "Level " + str(step) + " -> trigger day")
os.system("sudo rm /tmp/night_mode_enabled >/dev/null 2>&1")
sleep (get_var('TSL2561_CHECK_INTERVAL'))