Skip to content

Commit

Permalink
Merge pull request #445 from opencardev/lightsensor_fixes
Browse files Browse the repository at this point in the history
Lightsensor fixes and debug fix
  • Loading branch information
matt2005 authored Feb 6, 2021
2 parents f8d7ba3 + aa7a9ee commit eff4779
Show file tree
Hide file tree
Showing 8 changed files with 134 additions and 77 deletions.
2 changes: 1 addition & 1 deletion prebuilts
2 changes: 1 addition & 1 deletion stage2/03-crankshaft-base-packages/00-packages-nr
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ libboost-chrono1.67.0 libboost-atomic1.67.0 libpulse-mainloop-glib0 libfontconfi
librtaudio6 fbi libts0 wiringpi insserv watchdog pulseaudio evtest mpg321 gstreamer1.0-plugins-base
gstreamer1.0-plugins-good gstreamer1.0-plugins-ugly gstreamer1.0-plugins-bad gstreamer1.0-pulseaudio gstreamer1.0-tools gstreamer1.0-plugins-base-apps
libjpeg9 libtag1v5 libgps23 dos2unix triggerhappy locate eyed3 plymouth cpufrequtils libraspberrypi0 libgles2 libdouble-conversion1
gpsd ntp hostapd dnsmasq i2c-tools
gpsd ntp hostapd dnsmasq i2c-tools python3-pip
7 changes: 7 additions & 0 deletions stage3/02-pip-installs/00-run.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#!/bin/bash -e

on_chroot << EOF
pip3 install --upgrade pip
pip3 install smbus
pip3 install python-tsl2591
EOF
1 change: 1 addition & 0 deletions stage3/03-crankshaft-base/00-packages
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
python-smbus zip
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,13 @@ EXTERNAL_BLUETOOTH=0
# System updates
ALLOW_USB_FLASH=1

# LightSensor
LIGHTSENSOR_TYPE='TSL2561' # Allowed Values TSL2561 TSL2591
# the address of TSL2561/TSL2591 can be
# 0x29, 0x39 or 0x49
TSL_I2C_BUS=1
TSL_ADDR=0x29

# Auto brightness control based on tsl2561 light sensor
# Check interval sensor 5,10,15,20,25,30
TSL2561_CHECK_INTERVAL=10
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,17 +71,21 @@ if [ "$(i2cdetect -l | grep i2c-1)" != "" ]; then

# check for lightsensor
if [ -f /etc/cs_lightsensor ]; then
checkdevice 39
checkdevice $(echo $TSL_ADDR| cut -c 3-)
if [ $? -eq 1 ]; then
echo "[${CYAN}${BOLD} INFO ${RESET}] *******************************************************" > /dev/tty3
echo "[${CYAN}${BOLD} INFO ${RESET}] Check for tsl2561 ok. Device at 0x39 is present." > /dev/tty3
echo "[${CYAN}${BOLD} INFO ${RESET}] Check for light sensor ok. " > /dev/tty3
echo "[${CYAN}${BOLD} INFO ${RESET}] LightSensor ${LIGHTSENSOR_TYPE} found." > /dev/tty3
echo "[${CYAN}${BOLD} INFO ${RESET}] Device at ${TSL_ADDR} is present." > /dev/tty3
echo "[${CYAN}${BOLD} INFO ${RESET}] *******************************************************" > /dev/tty3
else
show_screen
echo "[${RED}${BOLD} WARN ${RESET}] *******************************************************" > /dev/tty3
echo "[${RED}${BOLD} WARN ${RESET}] Check for tsl2561 failed. Device at 0x39 is missing!" > /dev/tty3
echo "[${RED}${BOLD} WARN ${RESET}] Check for light sensor failed. " > /dev/tty3
echo "[${RED}${BOLD} WARN ${RESET}] LightSensor ${LIGHTSENSOR_TYPE} not found." > /dev/tty3
echo "[${RED}${BOLD} WARN ${RESET}] Device at ${TSL_ADDR} is missing!" > /dev/tty3
echo "[${RED}${BOLD} WARN ${RESET}] *******************************************************" > /dev/tty3
log_echo "Check for tsl2561 failed. Seems device at 0x39 is missing!"
log_echo "Check for LightSensor ${LIGHTSENSOR_TYPE} failed. Seems device at ${TSL_ADDR} is missing!"
fi
fi
else
Expand Down
180 changes: 109 additions & 71 deletions stage3/03-crankshaft-base/files/opt/crankshaft/service_lightsensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,102 +4,140 @@
import os
import subprocess
from time import sleep
from python_tsl2591 import tsl2591


def get_var(varname):
try:
CMD = 'echo $(source /boot/crankshaft/crankshaft_env.sh; echo $%s)' % varname
p = subprocess.Popen(CMD, stdout=subprocess.PIPE, shell=True, executable='/bin/bash')
return int(p.stdout.readlines()[0].strip())
p = subprocess.Popen(CMD, stdout=subprocess.PIPE,
shell=True, executable='/bin/bash')
return p.stdout.readlines()[0].strip()
except:
CMD = 'echo $(source /opt/crankshaft/crankshaft_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())
p = subprocess.Popen(CMD, stdout=subprocess.PIPE,
shell=True, executable='/bin/bash')
return 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')
# Get Variables from Config
daynight_gpio = int(get_var('DAYNIGHT_PIN'))
LIGHTSENSOR = str(get_var('LIGHTSENSOR_TYPE').decode())
TSL_I2C_BUS = int(get_var('TSL_I2C_BUS'))
TSL_ADDR = int(get_var('TSL_ADDR').decode(),16)
# ---------------------------------

i2cBus = smbus.SMBus(BUS)

# Start messure with 402 ms
# (scale factor 1)
i2cBus.write_byte_data(TSL2561_ADDR, 0x80, 0x03)
def get_LUX(LIGHTSENSOR):
print(LIGHTSENSOR)
if 'TSL2561' == LIGHTSENSOR:
Lux = get_LUX_TSL2561(TSL_I2C_BUS, TSL_ADDR)
elif 'TSL2591' == LIGHTSENSOR:
Lux = get_LUX_TSL2591(TSL_I2C_BUS, TSL_ADDR)
else:
Lux = 0
return round(Lux, 1)


def get_LUX_TSL2561(TSL_I2C_BUS, TSL_ADDR):
i2cBus = smbus.SMBus(TSL_I2C_BUS)
# Start messure with 402 ms
# (scale factor 1)
i2cBus.write_byte_data(TSL_ADDR, 0x80, 0x03)

# read global brightness
# read low byte
LSB = i2cBus.read_byte_data(TSL_ADDR, 0x8C)
# read high byte
MSB = i2cBus.read_byte_data(TSL_ADDR, 0x8D)
Ambient = (MSB << 8) + LSB
# print ("Ambient: {}".format(Ambient))

# read infra red
# read low byte
LSB = i2cBus.read_byte_data(TSL_ADDR, 0x8E)
# read high byte
MSB = i2cBus.read_byte_data(TSL_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
return round(Lux, 1)


def get_LUX_TSL2591(TSL_I2C_BUS, TSL_ADDR):
# Initialize the connector
tsl = tsl2591(i2c_bus=TSL_I2C_BUS, sensor_address=TSL_ADDR)
full, ir = tsl.get_full_luminosity()
Lux = tsl.calculate_lux(full, ir)
return round(Lux, 1)


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)
Luxrounded = get_LUX(LIGHTSENSOR)
if lastvalue != Luxrounded:
#print ("Lux = {}\n".format(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("crankshaft brightness set " + str(get_var('DISP_BRIGHTNESS_1')) + " &")
# Set display brightness
if Luxrounded <= int(get_var('LUX_LEVEL_1')):
os.system("crankshaft brightness set " +
str(get_var('DISP_BRIGHTNESS_1')) + " &")
step = 1
elif Luxrounded > get_var('LUX_LEVEL_1') and Luxrounded < get_var('LUX_LEVEL_2'):
os.system("crankshaft brightness set " + str(get_var('DISP_BRIGHTNESS_2')) + " &")
elif Luxrounded > int(get_var('LUX_LEVEL_1')) and Luxrounded < int(get_var('LUX_LEVEL_2')):
os.system("crankshaft brightness set " +
str(get_var('DISP_BRIGHTNESS_2')) + " &")
step = 2
elif Luxrounded >= get_var('LUX_LEVEL_2') and Luxrounded < get_var('LUX_LEVEL_3'):
os.system("crankshaft brightness set " + str(get_var('DISP_BRIGHTNESS_3')) + " &")
elif Luxrounded >= int(get_var('LUX_LEVEL_2')) and Luxrounded < int(get_var('LUX_LEVEL_3')):
os.system("crankshaft brightness set " +
str(get_var('DISP_BRIGHTNESS_3')) + " &")
step = 3
elif Luxrounded >= get_var('LUX_LEVEL_3') and Luxrounded < get_var('LUX_LEVEL_4'):
os.system("crankshaft brightness set " + str(get_var('DISP_BRIGHTNESS_4')) + " &")
elif Luxrounded >= int(get_var('LUX_LEVEL_3')) and Luxrounded < int(get_var('LUX_LEVEL_4')):
os.system("crankshaft brightness set " +
str(get_var('DISP_BRIGHTNESS_4')) + " &")
step = 4
elif Luxrounded >= get_var('LUX_LEVEL_5'):
os.system("crankshaft brightness set " + str(get_var('DISP_BRIGHTNESS_5')) + " &")
elif Luxrounded >= int(get_var('LUX_LEVEL_4')) and Luxrounded < int(get_var('LUX_LEVEL_5')):
os.system("crankshaft brightness set " +
str(get_var('DISP_BRIGHTNESS_5')) + " &")
step = 5
elif Luxrounded >= int(get_var('LUX_LEVEL_5')):
os.system("crankshaft brightness set " +
str(get_var('DISP_BRIGHTNESS_5')) + " &")
step = 6

if daynight_gpio == 0:
if step <= get_var('TSL2561_DAYNIGHT_ON_STEP'):
print("Lux = {} | ".format(Luxrounded) + "Level " + str(step) + " -> trigger night")
if step <= int(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")
if step > int(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'))

sleep(int(get_var('TSL2561_CHECK_INTERVAL')))

0 comments on commit eff4779

Please sign in to comment.