This repository was archived by the owner on Dec 20, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbmp581.py
More file actions
295 lines (240 loc) · 9.18 KB
/
bmp581.py
File metadata and controls
295 lines (240 loc) · 9.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
# SPDX-FileCopyrightText: Copyright (c) 2023 Jose D. Montoya
#
# SPDX-License-Identifier: MIT
"""
`bmp581`
================================================================================
CircuitPython Driver for the Bosch BMP581 pressure sensor
* Author(s): Jose D. Montoya
"""
from micropython import const
from adafruit_bus_device import i2c_device
from adafruit_register.i2c_struct import ROUnaryStruct
from adafruit_register.i2c_bits import RWBits, ROBits
try:
from busio import I2C
except ImportError:
pass
__version__ = "0.0.0+auto.0"
__repo__ = "https://github.com/jposada202020/CircuitPython_BMP581.git"
_REG_WHOAMI = const(0x01)
_OSR_CONF = const(0x36)
_ODR_CONFIG = const(0x37)
# Power Modes
STANDBY = const(0x00)
NORMAL = const(0x01)
FORCED = const(0x02)
NON_STOP = const(0x03)
power_mode_values = (STANDBY, NORMAL, FORCED, NON_STOP)
# Oversample Rate
OSR1 = const(0x00)
OSR2 = const(0x01)
OSR4 = const(0x02)
OSR8 = const(0x03)
OSR16 = const(0x04)
OSR32 = const(0x05)
OSR64 = const(0x06)
OSR128 = const(0x07)
pressure_oversample_rate_values = (OSR1, OSR2, OSR4, OSR8, OSR16, OSR32, OSR64, OSR128)
temperature_oversample_rate_values = (
OSR1,
OSR2,
OSR4,
OSR8,
OSR16,
OSR32,
OSR64,
OSR128,
)
class BMP581:
"""Driver for the BMP581 Sensor connected over I2C.
:param ~busio.I2C i2c_bus: The I2C bus the BMP581 is connected to.
:param int address: The I2C device address. Defaults to :const:`0x47`
:raises RuntimeError: if the sensor is not found
**Quickstart: Importing and using the device**
Here is an example of using the :class:`BMP581` class.
First you will need to import the libraries to use the sensor
.. code-block:: python
import board
import bmp581
Once this is done you can define your `board.I2C` object and define your sensor object
.. code-block:: python
i2c = board.I2C() # uses board.SCL and board.SDA
bmp = bmp581.BMP581(i2c)
Now you have access to the attributes
.. code-block:: python
press = bmp.pressure
"""
_device_id = ROUnaryStruct(_REG_WHOAMI, "B")
_power_mode = RWBits(2, _ODR_CONFIG, 0)
_temperature_oversample_rate = RWBits(3, _OSR_CONF, 0)
_pressure_oversample_rate = RWBits(3, _OSR_CONF, 3)
_output_data_rate = RWBits(5, _ODR_CONFIG, 2)
_pressure_enabled = RWBits(1, _OSR_CONF, 6)
_temperature = ROBits(24, 0x1D, 0, 3)
_pressure = ROBits(24, 0x20, 0, 3)
def __init__(self, i2c_bus: I2C, address: int = 0x47) -> None:
self.i2c_device = i2c_device.I2CDevice(i2c_bus, address)
if self._device_id != 0x50:
raise RuntimeError("Failed to find BMP581")
self._power_mode = NORMAL
self._pressure_enabled = True
self.sea_level_pressure = 101.325
@property
def power_mode(self) -> str:
"""
Sensor power_mode
+-----------------------------+------------------+
| Mode | Value |
+=============================+==================+
| :py:const:`bmp581.STANDBY` | :py:const:`0x00` |
+-----------------------------+------------------+
| :py:const:`bmp581.NORMAL` | :py:const:`0x01` |
+-----------------------------+------------------+
| :py:const:`bmp581.FORCED` | :py:const:`0x02` |
+-----------------------------+------------------+
| :py:const:`bmp581.NON_STOP` | :py:const:`0X03` |
+-----------------------------+------------------+
"""
values = (
"STANDBY",
"NORMAL",
"FORCED",
"NON_STOP",
)
return values[self._power_mode]
@power_mode.setter
def power_mode(self, value: int) -> None:
if value not in power_mode_values:
raise ValueError("Value must be a valid power_mode setting")
self._power_mode = value
@property
def pressure_oversample_rate(self) -> str:
"""
Sensor pressure_oversample_rate
+---------------------------+------------------+
| Mode | Value |
+===========================+==================+
| :py:const:`bmp581.OSR1` | :py:const:`0x00` |
+---------------------------+------------------+
| :py:const:`bmp581.OSR2` | :py:const:`0x01` |
+---------------------------+------------------+
| :py:const:`bmp581.OSR4` | :py:const:`0x02` |
+---------------------------+------------------+
| :py:const:`bmp581.OSR8` | :py:const:`0x03` |
+---------------------------+------------------+
| :py:const:`bmp581.OSR16` | :py:const:`0x04` |
+---------------------------+------------------+
| :py:const:`bmp581.OSR32` | :py:const:`0x05` |
+---------------------------+------------------+
| :py:const:`bmp581.OSR64` | :py:const:`0x06` |
+---------------------------+------------------+
| :py:const:`bmp581.OSR128` | :py:const:`0x07` |
+---------------------------+------------------+
"""
values = (
"OSR1",
"OSR2",
"OSR4",
"OSR8",
"OSR16",
"OSR32",
"OSR64",
"OSR128",
)
return values[self._pressure_oversample_rate]
@pressure_oversample_rate.setter
def pressure_oversample_rate(self, value: int) -> None:
if value not in pressure_oversample_rate_values:
raise ValueError("Value must be a valid pressure_oversample_rate setting")
self._pressure_oversample_rate = value
@property
def temperature_oversample_rate(self) -> str:
"""
Sensor temperature_oversample_rate
+---------------------------+------------------+
| Mode | Value |
+===========================+==================+
| :py:const:`bmp581.OSR1` | :py:const:`0x00` |
+---------------------------+------------------+
| :py:const:`bmp581.OSR2` | :py:const:`0x01` |
+---------------------------+------------------+
| :py:const:`bmp581.OSR4` | :py:const:`0x02` |
+---------------------------+------------------+
| :py:const:`bmp581.OSR8` | :py:const:`0x03` |
+---------------------------+------------------+
| :py:const:`bmp581.OSR16` | :py:const:`0x04` |
+---------------------------+------------------+
| :py:const:`bmp581.OSR32` | :py:const:`0x05` |
+---------------------------+------------------+
| :py:const:`bmp581.OSR64` | :py:const:`0x06` |
+---------------------------+------------------+
| :py:const:`bmp581.OSR128` | :py:const:`0x07` |
+---------------------------+------------------+
"""
values = (
"OSR1",
"OSR2",
"OSR4",
"OSR8",
"OSR16",
"OSR32",
"OSR64",
"OSR128",
)
return values[self._temperature_oversample_rate]
@temperature_oversample_rate.setter
def temperature_oversample_rate(self, value: int) -> None:
if value not in temperature_oversample_rate_values:
raise ValueError(
"Value must be a valid temperature_oversample_rate setting"
)
self._temperature_oversample_rate = value
@property
def output_data_rate(self) -> int:
"""
Sensor output_data_rate. for a complete list of values please see the datasheet
"""
return self._output_data_rate
@output_data_rate.setter
def output_data_rate(self, value: int) -> None:
if value not in range(0, 32, 1):
raise ValueError("Value must be a valid output_data_rate setting")
self._output_data_rate = value
@property
def temperature(self) -> float:
"""
The temperature sensor in C
:return: Temperature
"""
raw_temp = self._temperature
return self._twos_comp(raw_temp, 24) / 2**16.0
@property
def pressure(self) -> float:
"""
The sensor pressure in kPa
:return: Pressure in kPa
"""
raw_pressure = self._pressure
return self._twos_comp(raw_pressure, 24) / 2**6.0 / 1000
@property
def altitude(self):
"""
With the measured pressure p and the pressure at sea level p0 e.g. 1013.25hPa,
the altitude in meters can be calculated with the international barometric formula
With the measured pressure p and the absolute altitude the pressure at sea level
can be calculated too. See the altitude setter for this calculation
"""
# updated to Bosch’s formula in bmp180 datasheet, and eliminate excessive rounding of altitude
altitude = 44330.0 * (
1.0 - ((self.pressure / self.sea_level_pressure) ** (1.0 / 5.255))
)
return altitude
@altitude.setter
def altitude(self, value: float) -> None:
self.sea_level_pressure = self.pressure / (1.0 - value / 44330.0) ** 5.255
@staticmethod
def _twos_comp(val: int, bits: int) -> int:
if val & (1 << (bits - 1)) != 0:
return val - (1 << bits)
return val