kopia lustrzana https://github.com/micropython/micropython-lib
micropython/drivers/sensor/pasco2: Cleanup and adding code comments.
Signed-off-by: NikhitaR-IFX <Nikhita.Rajasekhar@infineon.com>pull/644/head
rodzic
601cc7c37c
commit
11c1fe2c27
|
@ -2,16 +2,11 @@ from machine import I2C
|
|||
from utime import sleep, sleep_ms
|
||||
|
||||
# Sensor Register Address Stack
|
||||
#_PASCO2_REG_PROD_ID = const(0x00) # Product and revision ID register address
|
||||
_PASCO2_REG_SENS_STS = const(0x01) # Sensor status register address
|
||||
_PASCO2_REG_MEAS_RATE_H = const(0x02) # Measurement period MSB configuration register address
|
||||
#_PASCO2_REG_MEAS_RATE_L = const(0x03) # Measurement period LSB register address
|
||||
_PASCO2_REG_MEAS_CFG = const(0x04) # Measurement mode configuration register address
|
||||
_PASCO2_REG_CO2PPM_H = const(0x05) # CO2 concentration result MSB register address
|
||||
#_PASCO2_REG_CO2PPM_L = const(0x06) # CO2 concentration result LSB register address
|
||||
_PASCO2_REG_MEAS_STS = const(0x07) # Measurement status register address
|
||||
#_PASCO2_REG_INT_CFG = const(0x08) # Interrupt pin configuration register address
|
||||
#_PASCO2_REG_ALARM_TH_H = const(0x09) # Alarm threshold MSB register address
|
||||
_PASCO2_REG_CO2PPM_H = const(0x05) # CO2 concentration result MSB register address
|
||||
_PASCO2_REG_MEAS_STS = const(0x07) # Measurement status register address
|
||||
_PASCO2_REG_SENS_RST = const(0x10) # Soft reset register address
|
||||
|
||||
#Error codes
|
||||
|
@ -25,34 +20,44 @@ i2c = I2C(0)
|
|||
|
||||
class PASCO2:
|
||||
"""IFX - XENSIV PAS CO2 sensor driver"""
|
||||
regMap = { # RegAddr, Mask
|
||||
'REG_SENS_STS_BITF_SENS_RDY' : [_PASCO2_REG_SENS_STS, 0x80],
|
||||
'REG_MEAS_CFG_BITF_OP_MODE' : [_PASCO2_REG_MEAS_CFG, 0x03],
|
||||
# RegAddr, Mask, <Later extend with register access type, bit position etc.>
|
||||
regMap = {
|
||||
'REG_SENS_STS_BITF_SENS_RDY' : [_PASCO2_REG_SENS_STS, 0x80], # Sensor status bit
|
||||
'REG_MEAS_CFG_BITF_OP_MODE' : [_PASCO2_REG_MEAS_CFG, 0x03], # Operation Mode type bit
|
||||
'REG_MEAS_STS_BITF_DATA_RDY' : [_PASCO2_REG_MEAS_STS, 0x10], # Data ready status bit
|
||||
'REG_CO2PPM_H_BITF_CO2_PPM_H': [_PASCO2_REG_CO2PPM_H, 0xFF],
|
||||
'REG_CO2PPM_H_BITF_CO2_PPM_H': [_PASCO2_REG_CO2PPM_H, 0xFF], # Stored CO2 value bit
|
||||
}
|
||||
|
||||
def __init__(self, bus, measInterval=10, sensorAddr=0x28):
|
||||
"""" Intialize the sensor and required dependencies """
|
||||
self.bus = bus
|
||||
self.sensorAddr = sensorAddr
|
||||
self.measInterval = measInterval
|
||||
self.softResetCode = b"\xa3"
|
||||
|
||||
def _read_reg(self, regAddr, bytesToRead=1):
|
||||
""" Internal function to read data from the sensor register and returns it raw """
|
||||
readVal = self.bus.readfrom_mem(self.sensorAddr, regAddr, bytesToRead)
|
||||
return readVal
|
||||
|
||||
def _write_reg(self, regAddr, writeData):
|
||||
""" Internal function to write data to sensor register """
|
||||
self.bus.writeto_mem(self.sensorAddr, regAddr, writeData)
|
||||
|
||||
def _is_sensor_ready(self):
|
||||
""" Helper function to check the sensor status """
|
||||
reg = self.regMap['REG_SENS_STS_BITF_SENS_RDY']
|
||||
return (self._read_reg(reg[addr])[0] & reg[mask])
|
||||
|
||||
def _soft_reset(self):
|
||||
""" Helper function to perform soft reset of the sensor """
|
||||
self._write_reg(_PASCO2_REG_SENS_RST, self.softResetCode)
|
||||
|
||||
def _set_mode(self, mode):
|
||||
""" Helper function to set the mode of sensor. Currently supported modes:
|
||||
1. Idle
|
||||
2. Continuous
|
||||
"""
|
||||
if mode == 'idle': modeVal = 0x00
|
||||
if mode == 'continuous': modeVal = 0x02
|
||||
|
||||
|
@ -63,6 +68,7 @@ class PASCO2:
|
|||
|
||||
|
||||
def initialize(self):
|
||||
""" Public function to initialize the sensor """
|
||||
try:
|
||||
# wait for sensor to be ready
|
||||
sensor_ready = self._is_sensor_ready()
|
||||
|
@ -80,7 +86,7 @@ class PASCO2:
|
|||
|
||||
# reset operation mode to idle mode
|
||||
self._set_mode('idle')
|
||||
|
||||
|
||||
# start continuous mode
|
||||
self._set_mode('continuous')
|
||||
|
||||
|
@ -90,6 +96,7 @@ class PASCO2:
|
|||
return _PASCO2_ERROR
|
||||
|
||||
def getCO2Value(self):
|
||||
""" Public function to get the CO2 value """
|
||||
while True:
|
||||
try:
|
||||
# get meas status
|
||||
|
|
Ładowanie…
Reference in New Issue