|
4 | 4 | import os |
5 | 5 | import subprocess |
6 | 6 | from time import sleep |
| 7 | +from python_tsl2591 import tsl2591 |
| 8 | + |
7 | 9 |
|
8 | 10 | def get_var(varname): |
9 | 11 | try: |
10 | 12 | 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() |
13 | 16 | except: |
14 | 17 | 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() |
17 | 21 |
|
18 | 22 | # --------------------------------- |
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) |
25 | 28 | # --------------------------------- |
26 | 29 |
|
27 | | -i2cBus = smbus.SMBus(BUS) |
28 | 30 |
|
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 | + |
32 | 97 |
|
33 | 98 | lastvalue = 0 |
34 | 99 |
|
35 | 100 | 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) |
76 | 102 | if lastvalue != Luxrounded: |
77 | | - #print ("Lux = {}\n".format(Luxrounded)) |
| 103 | + # print ("Lux = {}\n".format(Luxrounded)) |
78 | 104 | os.system("echo {} > /tmp/tsl2561".format(Luxrounded)) |
79 | 105 | 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')) + " &") |
83 | 110 | 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')) + " &") |
86 | 114 | 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')) + " &") |
89 | 118 | 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')) + " &") |
92 | 122 | 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')) + " &") |
95 | 126 | 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 |
96 | 131 |
|
97 | 132 | 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") |
100 | 136 | os.system("touch /tmp/night_mode_enabled >/dev/null 2>&1") |
101 | 137 | 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") |
104 | 141 | 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