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

Spot when pressure sensor has stalled and reset it. #95

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions pysense/lib/LTR329ALS01.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,14 @@ class LTR329ALS01:
ALS_GAIN_8X = const(0x03)
ALS_GAIN_48X = const(0x06)
ALS_GAIN_96X = const(0x07)
ALS_GAIN_VALUES = {
ALS_GAIN_1X: 1,
ALS_GAIN_2X: 2,
ALS_GAIN_4X: 4,
ALS_GAIN_8X: 8,
ALS_GAIN_48X: 48,
ALS_GAIN_96X: 96
}

ALS_INT_50 = const(0x01)
ALS_INT_100 = const(0x00)
Expand All @@ -27,6 +35,16 @@ class LTR329ALS01:
ALS_INT_300 = const(0x06)
ALS_INT_350 = const(0x07)
ALS_INT_400 = const(0x03)
ALS_INT_VALUES = {
ALS_INT_50: 0.5,
ALS_INT_100: 1,
ALS_INT_150: 1.5,
ALS_INT_200: 2,
ALS_INT_250: 2.5,
ALS_INT_300: 3,
ALS_INT_350: 3.5,
ALS_INT_400: 4
}

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

self.gain = gain
self.integration = integration

contr = self._getContr(gain)
self.i2c.writeto_mem(ALS_I2CADDR, ALS_CONTR_REG, bytearray([contr]))

Expand Down Expand Up @@ -68,3 +89,19 @@ def light(self):
data0 = int(self._getWord(ch0high[0], ch0low[0]))

return (data0, data1)

def lux(self):
# Calculate Lux value from formular in Appendix A of the datasheet
light_level = self.light()
if light_level[0]+light_level[1] > 0:
ratio = light_level[1]/(light_level[0]+light_level[1])
if ratio < 0.45:
return (1.7743 * light_level[0] + 1.1059 * light_level[1]) / self.ALS_GAIN_VALUES[self.gain] / self.ALS_INT_VALUES[self.integration]
elif ratio < 0.64 and ratio >= 0.45:
return (4.2785 * light_level[0] - 1.9548 * light_level[1]) / self.ALS_GAIN_VALUES[self.gain] / self.ALS_INT_VALUES[self.integration]
elif ratio < 0.85 and ratio >= 0.64:
return (0.5926 * light_level[0] + 0.1185 * light_level[1]) / self.ALS_GAIN_VALUES[self.gain] / self.ALS_INT_VALUES[self.integration]
else:
return 0
else:
return 0
9 changes: 8 additions & 1 deletion pysense/lib/MPL3115A2.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,10 @@ def __init__(self, pysense = None, sda = 'P22', scl = 'P21', mode = PRESSURE):
raise MPL3115A2exception("Error with MPL3115A2")

def _read_status(self):
while True:
read_attempts = 0
while read_attempts < 500:
self.i2c.readfrom_mem_into(MPL3115_I2CADDR, MPL3115_STATUS, self.STA_reg)
read_attempts += 1

if(self.STA_reg[0] == 0):
time.sleep(0.01)
Expand All @@ -80,6 +82,11 @@ def _read_status(self):
else:
return False

# If we get here the sensor isn't responding. Reset it so next time in it should work
self.i2c.writeto_mem(MPL3115_I2CADDR, MPL3115_CTRL_REG1, bytes([0x00])) # put into standby
self.i2c.writeto_mem(MPL3115_I2CADDR, MPL3115_CTRL_REG1, bytes([0x04])) # reset
return False

def pressure(self):
if self.mode == ALTITUDE:
raise MPL3115A2exception("Incorrect Measurement Mode MPL3115A2")
Expand Down
28 changes: 26 additions & 2 deletions pysense/lib/SI7006A20.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,18 @@ class SI7006A20:

SI7006A20_I2C_ADDR = const(0x40)

# I2C commands
TEMP_NOHOLDMASTER = const(0xF3)
HUMD_NOHOLDMASTER = const(0xF5)
WRITE_USER_REG1 = const(0xE6)
READ_USER_REG1 = const(0xE7)
WRITE_HEATER_CTRL_REG = const(0x51)
READ_HEATER_CTRL_REG = const(0x11)

# Register masks and offsets
USER_REG1_HTR_ENABLE_MASK = const(0b00000100)
USER_REG1_HTR_ENABLE_OFFSET = const(0x02)
HTR_CTRL_REG_MASK = const(0b00001111)

def __init__(self, pysense = None, sda = 'P22', scl = 'P21'):
if pysense is not None:
Expand Down Expand Up @@ -45,18 +55,32 @@ def humidity(self):

def read_user_reg(self):
""" reading the user configuration register """
self.i2c.writeto(SI7006A20_I2C_ADDR, bytearray([0xE7]))
self.i2c.writeto(SI7006A20_I2C_ADDR, bytearray([READ_USER_REG1]))
time.sleep(0.5)
data = self.i2c.readfrom(SI7006A20_I2C_ADDR, 1)
return data[0]

def read_heater_reg(self):
""" reading the heater configuration register """
self.i2c.writeto(SI7006A20_I2C_ADDR, bytearray([0x11]))
self.i2c.writeto(SI7006A20_I2C_ADDR, bytearray([READ_HEATER_CTRL_REG]))
time.sleep(0.5)
data = self.i2c.readfrom(SI7006A20_I2C_ADDR, 1)
return data[0]

def write_heater_reg(self, heater_value):
""" writing the heater configuration register """
# We should only set the bottom four bits of this register
heater_setting = heater_value & HTR_CTRL_REG_MASK
self.write_reg(WRITE_HEATER_CTRL_REG, heater_setting)

def heater_control(self, on_off):
""" turn the heater on or off """
# Get current settings for everything else
user_reg = self.read_user_reg()
# Set the heater bit
user_reg = (user_reg & ~USER_REG1_HTR_ENABLE_MASK) | (on_off << USER_REG1_HTR_ENABLE_OFFSET)
self.write_reg(WRITE_USER_REG1, user_reg)

def read_electronic_id(self):
""" reading electronic identifier """
self.i2c.writeto(SI7006A20_I2C_ADDR, bytearray([0xFA]) + bytearray([0x0F]))
Expand Down