Skip to content

Commit 96d956c

Browse files
committed
Copied some stuff from hts221
1 parent b768adc commit 96d956c

File tree

1 file changed

+150
-2
lines changed

1 file changed

+150
-2
lines changed

adafruit_ms8607.py

Lines changed: 150 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,155 @@
3030
# * Adafruit's Register library: https://github.com/adafruit/Adafruit_CircuitPython_Register
3131
"""
3232

33-
# imports
34-
3533
__version__ = "0.0.0-auto.0"
3634
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_MS8607.git"
35+
36+
from micropython import const
37+
import adafruit_bus_device.i2c_device as i2cdevice
38+
from adafruit_register.i2c_struct import ROUnaryStruct
39+
from adafruit_register.i2c_bits import RWBits, ROBits
40+
from adafruit_register.i2c_bit import RWBit, ROBit
41+
42+
_WHO_AM_I = const(0x0F)
43+
44+
P_T_RESET = const(0x1E) # Pressure and temperature sensor reset
45+
CONVERT_D1_OSR_256 = const(0x40) # Sampling rate 256
46+
CONVERT_D1_OSR_512 = const(0x42) # Sampling rate 512
47+
CONVERT_D1_OSR_1024 = const(0x44) # Sampling rate 1024
48+
CONVERT_D1_OSR_2048 = const(0x46) # Sampling rate 2048
49+
CONVERT_D1_OSR_4096 = const(0x48) # Sampling rate 4096
50+
CONVERT_D1_OSR_8192 = const(0x4A) # Sampling rate 8192
51+
CONVERT_D2_OSR_256 = const(0x50) # Sampling rate 256
52+
CONVERT_D2_OSR_512 = const(0x52) # Sampling rate 512
53+
CONVERT_D2_OSR_1024 = const(0x54) # Sampling rate 1024
54+
CONVERT_D2_OSR_2048 = const(0x56) # Sampling rate 2048
55+
CONVERT_D2_OSR_4096 = const(0x58) # Sampling rate 4096
56+
CONVERT_D2_OSR_8192 = const(0x5A) # Sampling rate 8192
57+
ADC_READ = const(0x00) # Command to read from ADC
58+
59+
HUM_RESET = const(0xFE) # Humidity sensor reset
60+
HUM_WRITE_REGISTER = const(0xE6) # Humidity sensor write register
61+
HUM_READ_REGISTER = const(0xE7) # Humidity sensor read register
62+
HUM_MEASURE_RH_HOLD = const(0xE5) # Humidity sensor measure relative humidity hold master
63+
HUM_MEASURE_RH_NO_HOLD = const(0xF5) # Humidity sensor measure relative humidity no hold master
64+
65+
_MS8607_CHIP_ID = const("0xXX")
66+
_MS8607_DEFAULT_PT_ADDRESS = 0x76
67+
_MS8607_DEFAULT_RH_ADDRESS = 0x40
68+
69+
70+
71+
72+
class CV:
73+
"""struct helper"""
74+
75+
@classmethod
76+
def add_values(cls, value_tuples):
77+
"creates CV entires"
78+
cls.string = {}
79+
cls.lsb = {}
80+
81+
for value_tuple in value_tuples:
82+
name, value, string, lsb = value_tuple
83+
setattr(cls, name, value)
84+
cls.string[value] = string
85+
cls.lsb[value] = lsb
86+
87+
@classmethod
88+
def is_valid(cls, value):
89+
"Returns true if the given value is a member of the CV"
90+
return value in cls.string
91+
92+
93+
class Rate(CV):
94+
pass # pylint: disable=unnecessary-pass
95+
96+
class MS8607: # pylint: disable=too-many-instance-attributes
97+
"""Library for the TE MS8607 Pressure, Humidity, and Temperature Sensor
98+
99+
:param ~busio.I2C i2c_bus: The I2C bus the MS8607 is connected to.
100+
101+
"""
102+
103+
def __init__(self, i2c_bus):
104+
self.i2c_device = i2cdevice.I2CDevice(i2c_bus, _MS8607_DEFAULT_ADDRESS)
105+
if not self._chip_id in [_MS8607_CHIP_ID]:
106+
raise RuntimeError(
107+
"Failed to find MS8607! Found chip ID 0x%x" % self._chip_id
108+
)
109+
110+
# This is the closest thing to a software reset. It re-loads the calibration values from flash
111+
def _boot(self):
112+
self._boot_bit = True
113+
# wait for the reset to finish
114+
while self._boot_bit:
115+
pass
116+
117+
@property
118+
def relative_humidity(self):
119+
"""The current relative humidity measurement in %rH"""
120+
calibrated_value_delta = self.calib_hum_value_1 - self.calib_hum_value_0
121+
calibrated_measurement_delta = self.calib_hum_meas_1 - self.calib_hum_meas_0
122+
123+
calibration_value_offset = self.calib_hum_value_0
124+
calibrated_measurement_offset = self.calib_hum_meas_0
125+
zeroed_measured_humidity = self._raw_humidity - calibrated_measurement_offset
126+
127+
correction_factor = calibrated_value_delta / calibrated_measurement_delta
128+
129+
adjusted_humidity = (
130+
zeroed_measured_humidity * correction_factor + calibration_value_offset
131+
)
132+
133+
return adjusted_humidity
134+
135+
@property
136+
def temperature(self):
137+
"""The current temperature measurement in degrees C"""
138+
139+
calibrated_value_delta = self.calibrated_value_1 - self.calib_temp_value_0
140+
calibrated_measurement_delta = self.calib_temp_meas_1 - self.calib_temp_meas_0
141+
142+
calibration_value_offset = self.calib_temp_value_0
143+
calibrated_measurement_offset = self.calib_temp_meas_0
144+
zeroed_measured_temp = self._raw_temperature - calibrated_measurement_offset
145+
146+
correction_factor = calibrated_value_delta / calibrated_measurement_delta
147+
148+
adjusted_temp = (
149+
zeroed_measured_temp * correction_factor
150+
) + calibration_value_offset
151+
152+
return adjusted_temp
153+
154+
@property
155+
def data_rate(self):
156+
"""The rate at which the sensor measures ``relative_humidity`` and ``temperature``.
157+
``data_rate`` should be set to one of the values of ``adafruit_ms8607.Rate``. Note that
158+
setting ``data_rate`` to ``Rate.ONE_SHOT`` will cause ``relative_humidity`` and
159+
``temperature`` measurements to only update when ``take_measurements`` is called."""
160+
return self._data_rate
161+
162+
@data_rate.setter
163+
def data_rate(self, value):
164+
if not Rate.is_valid(value):
165+
raise AttributeError("data_rate must be a `Rate`")
166+
167+
self._data_rate = value
168+
169+
@property
170+
def humidity_data_ready(self):
171+
"""Returns true if a new relative humidity measurement is available to be read"""
172+
return self._humidity_status_bit
173+
174+
@property
175+
def temperature_data_ready(self):
176+
"""Returns true if a new temperature measurement is available to be read"""
177+
return self._temperature_status_bit
178+
179+
def take_measurements(self):
180+
"""Update the value of ``relative_humidity`` and ``temperature`` by taking a single
181+
measurement. Only meaningful if ``data_rate`` is set to ``ONE_SHOT``"""
182+
self._one_shot_bit = True
183+
while self._one_shot_bit:
184+
pass

0 commit comments

Comments
 (0)