Skip to content
This repository was archived by the owner on Sep 10, 2024. It is now read-only.

Commit 3faed57

Browse files
author
Adrian McEwen
committed
Add lux calculation to LTR329ALS sensor
1 parent f5c7451 commit 3faed57

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

pysense/lib/LTR329ALS01.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,14 @@ class LTR329ALS01:
1818
ALS_GAIN_8X = const(0x03)
1919
ALS_GAIN_48X = const(0x06)
2020
ALS_GAIN_96X = const(0x07)
21+
ALS_GAIN_VALUES = {
22+
ALS_GAIN_1X: 1,
23+
ALS_GAIN_2X: 2,
24+
ALS_GAIN_4X: 4,
25+
ALS_GAIN_8X: 8,
26+
ALS_GAIN_48X: 48,
27+
ALS_GAIN_96X: 96
28+
}
2129

2230
ALS_INT_50 = const(0x01)
2331
ALS_INT_100 = const(0x00)
@@ -27,6 +35,16 @@ class LTR329ALS01:
2735
ALS_INT_300 = const(0x06)
2836
ALS_INT_350 = const(0x07)
2937
ALS_INT_400 = const(0x03)
38+
ALS_INT_VALUES = {
39+
ALS_INT_50: 0.5,
40+
ALS_INT_100: 1,
41+
ALS_INT_150: 1.5,
42+
ALS_INT_200: 2,
43+
ALS_INT_250: 2.5,
44+
ALS_INT_300: 3,
45+
ALS_INT_350: 3.5,
46+
ALS_INT_400: 4
47+
}
3048

3149
ALS_RATE_50 = const(0x00)
3250
ALS_RATE_100 = const(0x01)
@@ -41,6 +59,9 @@ def __init__(self, pysense = None, sda = 'P22', scl = 'P21', gain = ALS_GAIN_1X,
4159
else:
4260
self.i2c = I2C(0, mode=I2C.MASTER, pins=(sda, scl))
4361

62+
self.gain = gain
63+
self.integration = integration
64+
4465
contr = self._getContr(gain)
4566
self.i2c.writeto_mem(ALS_I2CADDR, ALS_CONTR_REG, bytearray([contr]))
4667

@@ -68,3 +89,16 @@ def light(self):
6889
data0 = int(self._getWord(ch0high[0], ch0low[0]))
6990

7091
return (data0, data1)
92+
93+
def lux(self):
94+
# Calculate Lux value from formula in Appendix A of the datasheet
95+
light_level = self.light()
96+
ratio = light_level[1]/(light_level[0]+light_level[1])
97+
if ratio < 0.45:
98+
return (1.7743 * light_level[0] + 1.1059 * light_level[1]) / self.ALS_GAIN_VALUES[self.gain] / self.ALS_INT_VALUES[self.integration]
99+
elif ratio < 0.64 and ratio >= 0.45:
100+
return (4.2785 * light_level[0] - 1.9548 * light_level[1]) / self.ALS_GAIN_VALUES[self.gain] / self.ALS_INT_VALUES[self.integration]
101+
elif ratio < 0.85 and ratio >= 0.64:
102+
return (0.5926 * light_level[0] + 0.1185 * light_level[1]) / self.ALS_GAIN_VALUES[self.gain] / self.ALS_INT_VALUES[self.integration]
103+
else:
104+
return 0

0 commit comments

Comments
 (0)