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

Commit 63fb420

Browse files
author
Alex Bucknall
committed
Updated LTR329ALS01.py for value corrections
1 parent 3d18093 commit 63fb420

File tree

1 file changed

+41
-30
lines changed

1 file changed

+41
-30
lines changed

pysense/lib/LTR329ALS01.py

Lines changed: 41 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,24 @@
11
import time
2+
from machine import I2C
23

34
class LTR329ALS01:
5+
ALS_I2CADDR = const(0x29) # The device's I2C address
46

5-
ALS_I2CADDR = const(0x29)
67
ALS_CONTR_REG = const(0x80)
7-
ALS_MEAS_REG = const(0x85)
8-
ALS_DATA_CH0_LOW = const(0x8A)
9-
ALS_DATA_CH0_HIGH = const(0x8B)
8+
ALS_MEAS_RATE_REG = const(0x85)
9+
1010
ALS_DATA_CH1_LOW = const(0x88)
1111
ALS_DATA_CH1_HIGH = const(0x89)
12-
ALS_GAIN_1X = const(0x01)
13-
ALS_GAIN_2X = const(0x05)
14-
ALS_GAIN_4X = const(0x09)
15-
ALS_GAIN_8X = const(0x0D)
16-
ALS_GAIN_48X = const(0x19)
17-
ALS_GAIN_96X = const(0x1D)
12+
ALS_DATA_CH0_LOW = const(0x8A)
13+
ALS_DATA_CH0_HIGH = const(0x8B)
14+
15+
ALS_GAIN_1X = const(0x00)
16+
ALS_GAIN_2X = const(0x01)
17+
ALS_GAIN_4X = const(0x02)
18+
ALS_GAIN_8X = const(0x03)
19+
ALS_GAIN_48X = const(0x06)
20+
ALS_GAIN_96X = const(0x07)
21+
1822
ALS_INT_50 = const(0x01)
1923
ALS_INT_100 = const(0x00)
2024
ALS_INT_150 = const(0x04)
@@ -23,37 +27,44 @@ class LTR329ALS01:
2327
ALS_INT_300 = const(0x06)
2428
ALS_INT_350 = const(0x07)
2529
ALS_INT_400 = const(0x03)
30+
2631
ALS_RATE_50 = const(0x00)
2732
ALS_RATE_100 = const(0x01)
2833
ALS_RATE_200 = const(0x02)
2934
ALS_RATE_500 = const(0x03)
3035
ALS_RATE_1000 = const(0x04)
3136
ALS_RATE_2000 = const(0x05)
3237

33-
def __init__(self, pysense=None, sda='P22', scl='P21', gain=ALS_GAIN_1X, integration=ALS_INT_100, rate=ALS_RATE_500):
38+
def __init__(self, pysense = None, sda = 'P22', scl = 'P21', gain = ALS_GAIN_1X, integration = ALS_INT_100, rate = ALS_RATE_500):
3439
if pysense is not None:
3540
self.i2c = pysense.i2c
3641
else:
37-
from machine import I2C
3842
self.i2c = I2C(0, mode=I2C.MASTER, pins=(sda, scl))
3943

40-
self.i2c.writeto_mem(ALS_I2CADDR, ALS_CONTR_REG, bytearray([gain]))
41-
meas = self._concat_hex(integration, rate)
42-
self.i2c.writeto_mem(ALS_I2CADDR, ALS_MEAS_REG, bytearray([meas]))
44+
contr = self._getContr(gain)
45+
self.i2c.writeto_mem(ALS_I2CADDR, ALS_CONTR_REG, bytearray([contr]))
46+
47+
measrate = self._getMeasRate(integration, rate)
48+
self.i2c.writeto_mem(ALS_I2CADDR, ALS_MEAS_RATE_REG, bytearray([measrate]))
49+
4350
time.sleep(0.01)
4451

45-
def _concat_hex(self, a, b):
46-
sizeof_b = 0
47-
while((b >> sizeof_b) > 0):
48-
sizeof_b += 1
49-
sizeof_b += sizeof_b % 4
50-
return (a << sizeof_b) | b
51-
52-
def lux(self):
53-
data0 = self.i2c.readfrom_mem(ALS_I2CADDR , ALS_DATA_CH1_LOW, 1)
54-
data1 = self.i2c.readfrom_mem(ALS_I2CADDR , ALS_DATA_CH1_HIGH, 1)
55-
data2 = self.i2c.readfrom_mem(ALS_I2CADDR , ALS_DATA_CH0_LOW, 1)
56-
data3 = self.i2c.readfrom_mem(ALS_I2CADDR , ALS_DATA_CH0_HIGH, 1)
57-
data_reg_CH1 = self._concat_hex(data1[0], data0[0])
58-
data_reg_CH0 = self._concat_hex(data2[0], data3[0])
59-
return(data_reg_CH0, data_reg_CH1)
52+
def _getContr(self, gain):
53+
return ((gain & 0x07) << 2) + 0x01
54+
55+
def _getMeasRate(self, integration, rate):
56+
return ((integration & 0x07) << 3) + (rate & 0x07)
57+
58+
def _getWord(self, high, low):
59+
return ((high & 0xFF) << 8) + (low & 0xFF)
60+
61+
def light(self):
62+
ch1low = self.i2c.readfrom_mem(ALS_I2CADDR , ALS_DATA_CH1_LOW, 1)
63+
ch1high = self.i2c.readfrom_mem(ALS_I2CADDR , ALS_DATA_CH1_HIGH, 1)
64+
data1 = int(self._getWord(ch1high[0], ch1low[0]))
65+
66+
ch0low = self.i2c.readfrom_mem(ALS_I2CADDR , ALS_DATA_CH0_LOW, 1)
67+
ch0high = self.i2c.readfrom_mem(ALS_I2CADDR , ALS_DATA_CH0_HIGH, 1)
68+
data0 = int(self._getWord(ch0high[0], ch0low[0]))
69+
70+
return (data0, data1)

0 commit comments

Comments
 (0)