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

Commit 01ced27

Browse files
gijsioAdrian McEwen
andauthored
Merge pull request #146 from pycom/mcqn_pressure_hang_update
* Spot when pressure sensor has stalled and reset it. * Add lux calculation to LTR329ALS sensor * Fix division-by-zero bug in the lux() calculation * Add methods to control the heater in the SI7006 temp/humidity sensor. * Add lux reading in Pysense 2 example Co-authored-by: Adrian McEwen <adrianm@mcqn.com>
2 parents f24b3fb + f912301 commit 01ced27

File tree

4 files changed

+72
-4
lines changed

4 files changed

+72
-4
lines changed

shields/lib/LTR329ALS01.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,14 @@ class LTR329ALS01:
2828
ALS_GAIN_8X = const(0x03)
2929
ALS_GAIN_48X = const(0x06)
3030
ALS_GAIN_96X = const(0x07)
31+
ALS_GAIN_VALUES = {
32+
ALS_GAIN_1X: 1,
33+
ALS_GAIN_2X: 2,
34+
ALS_GAIN_4X: 4,
35+
ALS_GAIN_8X: 8,
36+
ALS_GAIN_48X: 48,
37+
ALS_GAIN_96X: 96
38+
}
3139

3240
ALS_INT_50 = const(0x01)
3341
ALS_INT_100 = const(0x00)
@@ -37,6 +45,16 @@ class LTR329ALS01:
3745
ALS_INT_300 = const(0x06)
3846
ALS_INT_350 = const(0x07)
3947
ALS_INT_400 = const(0x03)
48+
ALS_INT_VALUES = {
49+
ALS_INT_50: 0.5,
50+
ALS_INT_100: 1,
51+
ALS_INT_150: 1.5,
52+
ALS_INT_200: 2,
53+
ALS_INT_250: 2.5,
54+
ALS_INT_300: 3,
55+
ALS_INT_350: 3.5,
56+
ALS_INT_400: 4
57+
}
4058

4159
ALS_RATE_50 = const(0x00)
4260
ALS_RATE_100 = const(0x01)
@@ -51,6 +69,9 @@ def __init__(self, pysense = None, sda = 'P22', scl = 'P21', gain = ALS_GAIN_1X,
5169
else:
5270
self.i2c = I2C(0, mode=I2C.MASTER, pins=(sda, scl))
5371

72+
self.gain = gain
73+
self.integration = integration
74+
5475
contr = self._getContr(gain)
5576
self.i2c.writeto_mem(ALS_I2CADDR, ALS_CONTR_REG, bytearray([contr]))
5677

@@ -78,3 +99,19 @@ def light(self):
7899
data0 = int(self._getWord(ch0high[0], ch0low[0]))
79100

80101
return (data0, data1)
102+
103+
def lux(self):
104+
# Calculate Lux value from formular in Appendix A of the datasheet
105+
light_level = self.light()
106+
if light_level[0]+light_level[1] > 0:
107+
ratio = light_level[1]/(light_level[0]+light_level[1])
108+
if ratio < 0.45:
109+
return (1.7743 * light_level[0] + 1.1059 * light_level[1]) / self.ALS_GAIN_VALUES[self.gain] / self.ALS_INT_VALUES[self.integration]
110+
elif ratio < 0.64 and ratio >= 0.45:
111+
return (4.2785 * light_level[0] - 1.9548 * light_level[1]) / self.ALS_GAIN_VALUES[self.gain] / self.ALS_INT_VALUES[self.integration]
112+
elif ratio < 0.85 and ratio >= 0.64:
113+
return (0.5926 * light_level[0] + 0.1185 * light_level[1]) / self.ALS_GAIN_VALUES[self.gain] / self.ALS_INT_VALUES[self.integration]
114+
else:
115+
return 0
116+
else:
117+
return 0

shields/lib/MPL3115A2.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,10 @@ def __init__(self, pysense = None, sda = 'P22', scl = 'P21', mode = PRESSURE):
7979
raise MPL3115A2exception("Error with MPL3115A2")
8080

8181
def _read_status(self):
82-
while True:
82+
read_attempts = 0
83+
while read_attempts < 500:
8384
self.i2c.readfrom_mem_into(MPL3115_I2CADDR, MPL3115_STATUS, self.STA_reg)
85+
read_attempts += 1
8486

8587
if(self.STA_reg[0] == 0):
8688
time.sleep(0.01)
@@ -90,6 +92,11 @@ def _read_status(self):
9092
else:
9193
return False
9294

95+
# If we get here the sensor isn't responding. Reset it so next time in it should work
96+
self.i2c.writeto_mem(MPL3115_I2CADDR, MPL3115_CTRL_REG1, bytes([0x00])) # put into standby
97+
self.i2c.writeto_mem(MPL3115_I2CADDR, MPL3115_CTRL_REG1, bytes([0x04])) # reset
98+
return False
99+
93100
def pressure(self):
94101
if self.mode == ALTITUDE:
95102
raise MPL3115A2exception("Incorrect Measurement Mode MPL3115A2")

shields/lib/SI7006A20.py

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,18 @@ class SI7006A20:
2222

2323
SI7006A20_I2C_ADDR = const(0x40)
2424

25+
# I2C commands
2526
TEMP_NOHOLDMASTER = const(0xF3)
2627
HUMD_NOHOLDMASTER = const(0xF5)
28+
WRITE_USER_REG1 = const(0xE6)
29+
READ_USER_REG1 = const(0xE7)
30+
WRITE_HEATER_CTRL_REG = const(0x51)
31+
READ_HEATER_CTRL_REG = const(0x11)
32+
33+
# Register masks and offsets
34+
USER_REG1_HTR_ENABLE_MASK = const(0b00000100)
35+
USER_REG1_HTR_ENABLE_OFFSET = const(0x02)
36+
HTR_CTRL_REG_MASK = const(0b00001111)
2737

2838
def __init__(self, pysense = None, sda = 'P22', scl = 'P21'):
2939
if pysense is not None:
@@ -55,18 +65,32 @@ def humidity(self):
5565

5666
def read_user_reg(self):
5767
""" reading the user configuration register """
58-
self.i2c.writeto(SI7006A20_I2C_ADDR, bytearray([0xE7]))
68+
self.i2c.writeto(SI7006A20_I2C_ADDR, bytearray([READ_USER_REG1]))
5969
time.sleep(0.5)
6070
data = self.i2c.readfrom(SI7006A20_I2C_ADDR, 1)
6171
return data[0]
6272

6373
def read_heater_reg(self):
6474
""" reading the heater configuration register """
65-
self.i2c.writeto(SI7006A20_I2C_ADDR, bytearray([0x11]))
75+
self.i2c.writeto(SI7006A20_I2C_ADDR, bytearray([READ_HEATER_CTRL_REG]))
6676
time.sleep(0.5)
6777
data = self.i2c.readfrom(SI7006A20_I2C_ADDR, 1)
6878
return data[0]
6979

80+
def write_heater_reg(self, heater_value):
81+
""" writing the heater configuration register """
82+
# We should only set the bottom four bits of this register
83+
heater_setting = heater_value & HTR_CTRL_REG_MASK
84+
self.write_reg(WRITE_HEATER_CTRL_REG, heater_setting)
85+
86+
def heater_control(self, on_off):
87+
""" turn the heater on or off """
88+
# Get current settings for everything else
89+
user_reg = self.read_user_reg()
90+
# Set the heater bit
91+
user_reg = (user_reg & ~USER_REG1_HTR_ENABLE_MASK) | (on_off << USER_REG1_HTR_ENABLE_OFFSET)
92+
self.write_reg(WRITE_USER_REG1, user_reg)
93+
7094
def read_electronic_id(self):
7195
""" reading electronic identifier """
7296
self.i2c.writeto(SI7006A20_I2C_ADDR, bytearray([0xFA]) + bytearray([0x0F]))

shields/pysense_2.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@
4848

4949

5050
lt = LTR329ALS01(py)
51-
print("Light (channel Blue lux, channel Red lux): " + str(lt.light()))
51+
print("Light (channel Blue, channel Red): " + str(lt.light()," Lux: ", str(lt.lux()), "lx"))
5252

5353
li = LIS2HH12(py)
5454
print("Acceleration: " + str(li.acceleration()))

0 commit comments

Comments
 (0)