Skip to content

Commit eff4779

Browse files
authored
Merge pull request #445 from opencardev/lightsensor_fixes
Lightsensor fixes and debug fix
2 parents f8d7ba3 + aa7a9ee commit eff4779

File tree

8 files changed

+134
-77
lines changed

8 files changed

+134
-77
lines changed

prebuilts

stage2/03-crankshaft-base-packages/00-packages-nr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@ libboost-chrono1.67.0 libboost-atomic1.67.0 libpulse-mainloop-glib0 libfontconfi
33
librtaudio6 fbi libts0 wiringpi insserv watchdog pulseaudio evtest mpg321 gstreamer1.0-plugins-base
44
gstreamer1.0-plugins-good gstreamer1.0-plugins-ugly gstreamer1.0-plugins-bad gstreamer1.0-pulseaudio gstreamer1.0-tools gstreamer1.0-plugins-base-apps
55
libjpeg9 libtag1v5 libgps23 dos2unix triggerhappy locate eyed3 plymouth cpufrequtils libraspberrypi0 libgles2 libdouble-conversion1
6-
gpsd ntp hostapd dnsmasq i2c-tools
6+
gpsd ntp hostapd dnsmasq i2c-tools python3-pip

stage3/02-pip-installs/00-run.sh

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#!/bin/bash -e
2+
3+
on_chroot << EOF
4+
pip3 install --upgrade pip
5+
pip3 install smbus
6+
pip3 install python-tsl2591
7+
EOF
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
python-smbus zip
38.5 KB
Loading

stage3/03-crankshaft-base/files/opt/crankshaft/crankshaft_default_env.sh

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,13 @@ EXTERNAL_BLUETOOTH=0
186186
# System updates
187187
ALLOW_USB_FLASH=1
188188

189+
# LightSensor
190+
LIGHTSENSOR_TYPE='TSL2561' # Allowed Values TSL2561 TSL2591
191+
# the address of TSL2561/TSL2591 can be
192+
# 0x29, 0x39 or 0x49
193+
TSL_I2C_BUS=1
194+
TSL_ADDR=0x29
195+
189196
# Auto brightness control based on tsl2561 light sensor
190197
# Check interval sensor 5,10,15,20,25,30
191198
TSL2561_CHECK_INTERVAL=10

stage3/03-crankshaft-base/files/opt/crankshaft/service_i2ccheck.sh

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -71,17 +71,21 @@ if [ "$(i2cdetect -l | grep i2c-1)" != "" ]; then
7171

7272
# check for lightsensor
7373
if [ -f /etc/cs_lightsensor ]; then
74-
checkdevice 39
74+
checkdevice $(echo $TSL_ADDR| cut -c 3-)
7575
if [ $? -eq 1 ]; then
7676
echo "[${CYAN}${BOLD} INFO ${RESET}] *******************************************************" > /dev/tty3
77-
echo "[${CYAN}${BOLD} INFO ${RESET}] Check for tsl2561 ok. Device at 0x39 is present." > /dev/tty3
77+
echo "[${CYAN}${BOLD} INFO ${RESET}] Check for light sensor ok. " > /dev/tty3
78+
echo "[${CYAN}${BOLD} INFO ${RESET}] LightSensor ${LIGHTSENSOR_TYPE} found." > /dev/tty3
79+
echo "[${CYAN}${BOLD} INFO ${RESET}] Device at ${TSL_ADDR} is present." > /dev/tty3
7880
echo "[${CYAN}${BOLD} INFO ${RESET}] *******************************************************" > /dev/tty3
7981
else
8082
show_screen
8183
echo "[${RED}${BOLD} WARN ${RESET}] *******************************************************" > /dev/tty3
82-
echo "[${RED}${BOLD} WARN ${RESET}] Check for tsl2561 failed. Device at 0x39 is missing!" > /dev/tty3
84+
echo "[${RED}${BOLD} WARN ${RESET}] Check for light sensor failed. " > /dev/tty3
85+
echo "[${RED}${BOLD} WARN ${RESET}] LightSensor ${LIGHTSENSOR_TYPE} not found." > /dev/tty3
86+
echo "[${RED}${BOLD} WARN ${RESET}] Device at ${TSL_ADDR} is missing!" > /dev/tty3
8387
echo "[${RED}${BOLD} WARN ${RESET}] *******************************************************" > /dev/tty3
84-
log_echo "Check for tsl2561 failed. Seems device at 0x39 is missing!"
88+
log_echo "Check for LightSensor ${LIGHTSENSOR_TYPE} failed. Seems device at ${TSL_ADDR} is missing!"
8589
fi
8690
fi
8791
else

stage3/03-crankshaft-base/files/opt/crankshaft/service_lightsensor.py

Lines changed: 109 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -4,102 +4,140 @@
44
import os
55
import subprocess
66
from time import sleep
7+
from python_tsl2591 import tsl2591
8+
79

810
def get_var(varname):
911
try:
1012
CMD = 'echo $(source /boot/crankshaft/crankshaft_env.sh; echo $%s)' % varname
11-
p = subprocess.Popen(CMD, stdout=subprocess.PIPE, shell=True, executable='/bin/bash')
12-
return int(p.stdout.readlines()[0].strip())
13+
p = subprocess.Popen(CMD, stdout=subprocess.PIPE,
14+
shell=True, executable='/bin/bash')
15+
return p.stdout.readlines()[0].strip()
1316
except:
1417
CMD = 'echo $(source /opt/crankshaft/crankshaft_default_env.sh; echo $%s)' % varname
15-
p = subprocess.Popen(CMD, stdout=subprocess.PIPE, shell=True, executable='/bin/bash')
16-
return int(p.stdout.readlines()[0].strip())
18+
p = subprocess.Popen(CMD, stdout=subprocess.PIPE,
19+
shell=True, executable='/bin/bash')
20+
return p.stdout.readlines()[0].strip()
1721

1822
# ---------------------------------
19-
# the addresss of TSL2561 can be
20-
# 0x29, 0x39 or 0x49
21-
BUS = 1
22-
TSL2561_ADDR = 0x39
23-
24-
daynight_gpio = get_var('DAYNIGHT_PIN')
23+
# Get Variables from Config
24+
daynight_gpio = int(get_var('DAYNIGHT_PIN'))
25+
LIGHTSENSOR = str(get_var('LIGHTSENSOR_TYPE').decode())
26+
TSL_I2C_BUS = int(get_var('TSL_I2C_BUS'))
27+
TSL_ADDR = int(get_var('TSL_ADDR').decode(),16)
2528
# ---------------------------------
2629

27-
i2cBus = smbus.SMBus(BUS)
2830

29-
# Start messure with 402 ms
30-
# (scale factor 1)
31-
i2cBus.write_byte_data(TSL2561_ADDR, 0x80, 0x03)
31+
def get_LUX(LIGHTSENSOR):
32+
print(LIGHTSENSOR)
33+
if 'TSL2561' == LIGHTSENSOR:
34+
Lux = get_LUX_TSL2561(TSL_I2C_BUS, TSL_ADDR)
35+
elif 'TSL2591' == LIGHTSENSOR:
36+
Lux = get_LUX_TSL2591(TSL_I2C_BUS, TSL_ADDR)
37+
else:
38+
Lux = 0
39+
return round(Lux, 1)
40+
41+
42+
def get_LUX_TSL2561(TSL_I2C_BUS, TSL_ADDR):
43+
i2cBus = smbus.SMBus(TSL_I2C_BUS)
44+
# Start messure with 402 ms
45+
# (scale factor 1)
46+
i2cBus.write_byte_data(TSL_ADDR, 0x80, 0x03)
47+
48+
# read global brightness
49+
# read low byte
50+
LSB = i2cBus.read_byte_data(TSL_ADDR, 0x8C)
51+
# read high byte
52+
MSB = i2cBus.read_byte_data(TSL_ADDR, 0x8D)
53+
Ambient = (MSB << 8) + LSB
54+
# print ("Ambient: {}".format(Ambient))
55+
56+
# read infra red
57+
# read low byte
58+
LSB = i2cBus.read_byte_data(TSL_ADDR, 0x8E)
59+
# read high byte
60+
MSB = i2cBus.read_byte_data(TSL_ADDR, 0x8F)
61+
Infrared = (MSB << 8) + LSB
62+
# print ("Infrared: {}".format(Infrared))
63+
64+
# Calc visible spectrum
65+
Visible = Ambient - Infrared
66+
# print ("Visible: {}".format(Visible))
67+
68+
# Calc factor Infrared/Ambient
69+
Ratio = 0
70+
Lux = 0
71+
if Ambient != 0:
72+
Ratio = float(Infrared)/float(Ambient)
73+
# print ("Ratio: {}".format(Ratio))
74+
75+
# Calc lux based on data sheet TSL2561T
76+
# T, FN, and CL Package
77+
if 0 < Ratio <= 0.50:
78+
Lux = 0.0304*float(Ambient) - 0.062*float(Ambient)*(Ratio**1.4)
79+
elif 0.50 < Ratio <= 0.61:
80+
Lux = 0.0224*float(Ambient) - 0.031*float(Infrared)
81+
elif 0.61 < Ratio <= 0.80:
82+
Lux = 0.0128*float(Ambient) - 0.0153*float(Infrared)
83+
elif 0.80 < Ratio <= 1.3:
84+
Lux = 0.00146*float(Ambient) - 0.00112*float(Infrared)
85+
else:
86+
Lux = 0
87+
return round(Lux, 1)
88+
89+
90+
def get_LUX_TSL2591(TSL_I2C_BUS, TSL_ADDR):
91+
# Initialize the connector
92+
tsl = tsl2591(i2c_bus=TSL_I2C_BUS, sensor_address=TSL_ADDR)
93+
full, ir = tsl.get_full_luminosity()
94+
Lux = tsl.calculate_lux(full, ir)
95+
return round(Lux, 1)
96+
3297

3398
lastvalue = 0
3499

35100
while True:
36-
# read global brightness
37-
# read low byte
38-
LSB = i2cBus.read_byte_data(TSL2561_ADDR, 0x8C)
39-
# read high byte
40-
MSB = i2cBus.read_byte_data(TSL2561_ADDR, 0x8D)
41-
Ambient = (MSB << 8) + LSB
42-
#print ("Ambient: {}".format(Ambient))
43-
44-
# read infra red
45-
# read low byte
46-
LSB = i2cBus.read_byte_data(TSL2561_ADDR, 0x8E)
47-
# read high byte
48-
MSB = i2cBus.read_byte_data(TSL2561_ADDR, 0x8F)
49-
Infrared = (MSB << 8) + LSB
50-
#print ("Infrared: {}".format(Infrared))
51-
52-
# Calc visible spectrum
53-
Visible = Ambient - Infrared
54-
#print ("Visible: {}".format(Visible))
55-
56-
# Calc factor Infrared/Ambient
57-
Ratio = 0
58-
Lux = 0
59-
if Ambient != 0:
60-
Ratio = float(Infrared)/float(Ambient)
61-
#print ("Ratio: {}".format(Ratio))
62-
63-
# Calc lux based on data sheet TSL2561T
64-
# T, FN, and CL Package
65-
if 0 < Ratio <= 0.50:
66-
Lux = 0.0304*float(Ambient) - 0.062*float(Ambient)*(Ratio**1.4)
67-
elif 0.50 < Ratio <= 0.61:
68-
Lux = 0.0224*float(Ambient) - 0.031*float(Infrared)
69-
elif 0.61 < Ratio <= 0.80:
70-
Lux = 0.0128*float(Ambient) - 0.0153*float(Infrared)
71-
elif 0.80 < Ratio <= 1.3:
72-
Lux = 0.00146*float(Ambient) - 0.00112*float(Infrared)
73-
else:
74-
Lux = 0
75-
Luxrounded=round(Lux,1)
101+
Luxrounded = get_LUX(LIGHTSENSOR)
76102
if lastvalue != Luxrounded:
77-
#print ("Lux = {}\n".format(Luxrounded))
103+
# print ("Lux = {}\n".format(Luxrounded))
78104
os.system("echo {} > /tmp/tsl2561".format(Luxrounded))
79105
lastvalue = Luxrounded
80-
#Set display brigthness
81-
if Luxrounded <= get_var('LUX_LEVEL_1'):
82-
os.system("crankshaft brightness set " + str(get_var('DISP_BRIGHTNESS_1')) + " &")
106+
# Set display brightness
107+
if Luxrounded <= int(get_var('LUX_LEVEL_1')):
108+
os.system("crankshaft brightness set " +
109+
str(get_var('DISP_BRIGHTNESS_1')) + " &")
83110
step = 1
84-
elif Luxrounded > get_var('LUX_LEVEL_1') and Luxrounded < get_var('LUX_LEVEL_2'):
85-
os.system("crankshaft brightness set " + str(get_var('DISP_BRIGHTNESS_2')) + " &")
111+
elif Luxrounded > int(get_var('LUX_LEVEL_1')) and Luxrounded < int(get_var('LUX_LEVEL_2')):
112+
os.system("crankshaft brightness set " +
113+
str(get_var('DISP_BRIGHTNESS_2')) + " &")
86114
step = 2
87-
elif Luxrounded >= get_var('LUX_LEVEL_2') and Luxrounded < get_var('LUX_LEVEL_3'):
88-
os.system("crankshaft brightness set " + str(get_var('DISP_BRIGHTNESS_3')) + " &")
115+
elif Luxrounded >= int(get_var('LUX_LEVEL_2')) and Luxrounded < int(get_var('LUX_LEVEL_3')):
116+
os.system("crankshaft brightness set " +
117+
str(get_var('DISP_BRIGHTNESS_3')) + " &")
89118
step = 3
90-
elif Luxrounded >= get_var('LUX_LEVEL_3') and Luxrounded < get_var('LUX_LEVEL_4'):
91-
os.system("crankshaft brightness set " + str(get_var('DISP_BRIGHTNESS_4')) + " &")
119+
elif Luxrounded >= int(get_var('LUX_LEVEL_3')) and Luxrounded < int(get_var('LUX_LEVEL_4')):
120+
os.system("crankshaft brightness set " +
121+
str(get_var('DISP_BRIGHTNESS_4')) + " &")
92122
step = 4
93-
elif Luxrounded >= get_var('LUX_LEVEL_5'):
94-
os.system("crankshaft brightness set " + str(get_var('DISP_BRIGHTNESS_5')) + " &")
123+
elif Luxrounded >= int(get_var('LUX_LEVEL_4')) and Luxrounded < int(get_var('LUX_LEVEL_5')):
124+
os.system("crankshaft brightness set " +
125+
str(get_var('DISP_BRIGHTNESS_5')) + " &")
95126
step = 5
127+
elif Luxrounded >= int(get_var('LUX_LEVEL_5')):
128+
os.system("crankshaft brightness set " +
129+
str(get_var('DISP_BRIGHTNESS_5')) + " &")
130+
step = 6
96131

97132
if daynight_gpio == 0:
98-
if step <= get_var('TSL2561_DAYNIGHT_ON_STEP'):
99-
print("Lux = {} | ".format(Luxrounded) + "Level " + str(step) + " -> trigger night")
133+
if step <= int(get_var('TSL2561_DAYNIGHT_ON_STEP')):
134+
print("Lux = {} | ".format(Luxrounded) +
135+
"Level " + str(step) + " -> trigger night")
100136
os.system("touch /tmp/night_mode_enabled >/dev/null 2>&1")
101137
else:
102-
if step > get_var('TSL2561_DAYNIGHT_ON_STEP'):
103-
print("Lux = {} | ".format(Luxrounded) + "Level " + str(step) + " -> trigger day")
138+
if step > int(get_var('TSL2561_DAYNIGHT_ON_STEP')):
139+
print("Lux = {} | ".format(Luxrounded) +
140+
"Level " + str(step) + " -> trigger day")
104141
os.system("sudo rm /tmp/night_mode_enabled >/dev/null 2>&1")
105-
sleep (get_var('TSL2561_CHECK_INTERVAL'))
142+
143+
sleep(int(get_var('TSL2561_CHECK_INTERVAL')))

0 commit comments

Comments
 (0)