Skip to content

Commit ef0eef6

Browse files
committed
Refactor to remove dependency on the enum class
Removed references to the enum class, while keeping the same functionality Updated the 'normal mode' example
1 parent a8c12e7 commit ef0eef6

File tree

2 files changed

+85
-73
lines changed

2 files changed

+85
-73
lines changed

adafruit_bme280.py

Lines changed: 74 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929
"""
3030
import math
3131
from time import sleep
32-
from enum import Enum
3332
from micropython import const
3433
try:
3534
import struct
@@ -68,42 +67,49 @@
6867
_BME280_HUMIDITY_MIN = const(0)
6968
_BME280_HUMIDITY_MAX = const(100)
7069

71-
class IIR_FILTER(Enum):
72-
"""Enum class for iir_filter values"""
73-
DISABLE = 0
74-
X_2 = 1
75-
X_4 = 2
76-
X_8 = 3
77-
X_16 = 4
78-
79-
class OVERSCAN(Enum):
80-
"""Enum class for overscan values for temperature, pressure, and humidity"""
81-
DISABLE = 0
82-
X_1 = 1
83-
X_2 = 2
84-
X_4 = 3
85-
X_8 = 4
86-
X_16 = 5
87-
88-
class MODE(Enum):
89-
"""Enum class for mode values"""
90-
SLEEP = 0
91-
FORCE = 1
92-
NORMAL = 3
93-
94-
class STANDBY(Enum):
95-
"""
96-
Enum class for standby values
97-
TC_X[_Y] where X=milliseconds and Y=tenths of a millisecond
98-
"""
99-
TC_0_5 = 0 #0.5ms
100-
TC_10 = 6 #10ms
101-
TC_20 = 7 #20ms
102-
TC_62_5 = 1 #62.5ms
103-
TC_125 = 2 #125ms
104-
TC_250 = 3 #250ms
105-
TC_500 = 4 #500ms
106-
TC_1000 = 5 #1000ms
70+
"""iir_filter values"""
71+
IIR_FILTER_DISABLE = const(0)
72+
IIR_FILTER_X2 = const(0x01)
73+
IIR_FILTER_X4 = const(0x02)
74+
IIR_FILTER_X8 = const(0x03)
75+
IIR_FILTER_X16 = const(0x04)
76+
77+
_BME280_IIR_FILTERS = frozenset((IIR_FILTER_DISABLE, IIR_FILTER_X2,
78+
IIR_FILTER_X4, IIR_FILTER_X8, IIR_FILTER_X16))
79+
80+
"""overscan values for temperature, pressure, and humidity"""
81+
OVERSCAN_DISABLE = const(0x00)
82+
OVERSCAN_X1 = const(0x01)
83+
OVERSCAN_X2 = const(0x02)
84+
OVERSCAN_X4 = const(0x03)
85+
OVERSCAN_X8 = const(0x04)
86+
OVERSCAN_X16 = const(0x05)
87+
88+
_BME280_OVERSCANS = frozenset((OVERSCAN_DISABLE, OVERSCAN_X1, OVERSCAN_X2,
89+
OVERSCAN_X4, OVERSCAN_X8, OVERSCAN_X16))
90+
91+
"""mode values"""
92+
MODE_SLEEP = const(0x00)
93+
MODE_FORCE = const(0x01)
94+
MODE_NORMAL = const(0x03)
95+
96+
_BME280_MODES = frozenset((MODE_SLEEP, MODE_FORCE, MODE_NORMAL))
97+
"""
98+
standby timeconstant values
99+
TC_X[_Y] where X=milliseconds and Y=tenths of a millisecond
100+
"""
101+
STANDBY_TC_0_5 = const(0x00) #0.5ms
102+
STANDBY_TC_10 = const(0x06) #10ms
103+
STANDBY_TC_20 = const(0x07) #20ms
104+
STANDBY_TC_62_5 = const(0x01) #62.5ms
105+
STANDBY_TC_125 = const(0x02) #125ms
106+
STANDBY_TC_250 = const(0x03) #250ms
107+
STANDBY_TC_500 = const(0x04) #500ms
108+
STANDBY_TC_1000 = const(0x05) #1000ms
109+
110+
_BME280_STANDBY_TCS = frozenset((STANDBY_TC_0_5, STANDBY_TC_10, STANDBY_TC_20,
111+
STANDBY_TC_62_5, STANDBY_TC_125, STANDBY_TC_250,
112+
STANDBY_TC_500, STANDBY_TC_1000))
107113

108114
class Adafruit_BME280:
109115
"""Driver from BME280 Temperature, Humidity and Barometic Pressure sensor"""
@@ -115,12 +121,12 @@ def __init__(self):
115121
if _BME280_CHIPID != chip_id:
116122
raise RuntimeError('Failed to find BME280! Chip ID 0x%x' % chip_id)
117123
#Set some reasonable defaults.
118-
self._iir_filter = IIR_FILTER.DISABLE
119-
self._overscan_humidity = OVERSCAN.X_2
120-
self._overscan_temperature = OVERSCAN.X_2
121-
self._overscan_pressure = OVERSCAN.X_16
122-
self._t_standby = STANDBY.TC_0_5
123-
self._mode = MODE.SLEEP
124+
self._iir_filter = IIR_FILTER_DISABLE
125+
self._overscan_humidity = OVERSCAN_X1
126+
self._overscan_temperature = OVERSCAN_X1
127+
self._overscan_pressure = OVERSCAN_X16
128+
self._t_standby = STANDBY_TC_125
129+
self._mode = MODE_SLEEP
124130
self._reset()
125131
self._read_coefficients()
126132
self._write_ctrl_meas()
@@ -131,8 +137,8 @@ def __init__(self):
131137

132138
def _read_temperature(self):
133139
# perform one measurement
134-
if self.mode != MODE.NORMAL:
135-
self.mode = MODE.FORCE
140+
if self.mode != MODE_NORMAL:
141+
self.mode = MODE_FORCE
136142
# Wait for conversion to complete
137143
while self._get_status() & 0x08:
138144
sleep(0.002)
@@ -174,25 +180,25 @@ def _read_config(self):
174180
def _write_config(self):
175181
"""Write the value to the config register in the device """
176182
normal_flag = False
177-
if self._mode == MODE.NORMAL:
183+
if self._mode == MODE_NORMAL:
178184
#Writes to the config register may be ignored while in Normal mode
179185
normal_flag = True
180-
self.mode = MODE.SLEEP #So we switch to Sleep mode first
186+
self.mode = MODE_SLEEP #So we switch to Sleep mode first
181187
self._write_register_byte(_BME280_REGISTER_CONFIG, self._config)
182188
if normal_flag:
183-
self.mode = MODE.NORMAL
189+
self.mode = MODE_NORMAL
184190

185191
@property
186192
def mode(self):
187193
"""
188194
Operation mode
189-
Allowed values are set in the MODE enum class
195+
Allowed values are the constants MODE_*
190196
"""
191197
return self._mode
192198

193199
@mode.setter
194200
def mode(self, value):
195-
if not value in MODE:
201+
if not value in _BME280_MODES:
196202
raise ValueError('Mode \'%s\' not supported' % (value))
197203
if self._mode == value:
198204
return
@@ -203,13 +209,13 @@ def mode(self, value):
203209
def standby_period(self):
204210
"""
205211
Control the inactive period when in Normal mode
206-
Allowed standby periods are set the STANDBY enum class
212+
Allowed standby periods are the constants STANDBY_TC_*
207213
"""
208214
return self._t_standby
209215

210216
@standby_period.setter
211217
def standby_period(self, value):
212-
if not value in STANDBY:
218+
if not value in _BME280_STANDBY_TCS:
213219
raise ValueError('Standby Period \'%s\' not supported' % (value))
214220
if self._t_standby == value:
215221
return
@@ -218,13 +224,15 @@ def standby_period(self, value):
218224

219225
@property
220226
def overscan_humidity(self):
221-
"""Humidity Oversampling
222-
Allowed values are set in the OVERSCAN enum class """
227+
"""
228+
Humidity Oversampling
229+
Allowed values are the constants OVERSCAN_*
230+
"""
223231
return self._overscan_humidity
224232

225233
@overscan_humidity.setter
226234
def overscan_humidity(self, value):
227-
if not value in OVERSCAN:
235+
if not value in _BME280_OVERSCANS:
228236
raise ValueError('Overscan value \'%s\' not supported' % (value))
229237
self._overscan_humidity = value
230238
self._write_ctrl_meas()
@@ -233,13 +241,13 @@ def overscan_humidity(self, value):
233241
def overscan_temperature(self):
234242
"""
235243
Temperature Oversampling
236-
Allowed values are set in the OVERSCAN enum class
244+
Allowed values are the constants OVERSCAN_*
237245
"""
238246
return self._overscan_temperature
239247

240248
@overscan_temperature.setter
241249
def overscan_temperature(self, value):
242-
if not value in OVERSCAN:
250+
if not value in _BME280_OVERSCANS:
243251
raise ValueError('Overscan value \'%s\' not supported' % (value))
244252
self._overscan_temperature = value
245253
self._write_ctrl_meas()
@@ -248,13 +256,13 @@ def overscan_temperature(self, value):
248256
def overscan_pressure(self):
249257
"""
250258
Pressure Oversampling
251-
Allowed values are set in the OVERSCAN enum class
259+
Allowed values are the constants OVERSCAN_*
252260
"""
253261
return self._overscan_pressure
254262

255263
@overscan_pressure.setter
256264
def overscan_pressure(self, value):
257-
if not value in OVERSCAN:
265+
if not value in _BME280_OVERSCANS:
258266
raise ValueError('Overscan value \'%s\' not supported' % (value))
259267
self._overscan_pressure = value
260268
self._write_ctrl_meas()
@@ -263,13 +271,13 @@ def overscan_pressure(self, value):
263271
def iir_filter(self):
264272
"""
265273
Controls the time constant of the IIR filter
266-
Allowed values are set in the IIR_FILTER enum class
274+
Allowed values are the constants IIR_FILTER_*
267275
"""
268276
return self._iir_filter
269277

270278
@iir_filter.setter
271279
def iir_filter(self, value):
272-
if not value in IIR_FILTER:
280+
if not value in _BME280_IIR_FILTERS:
273281
raise ValueError('IIR Filter \'%s\' not supported' % (value))
274282
self._iir_filter = value
275283
self._write_config()
@@ -278,20 +286,20 @@ def iir_filter(self, value):
278286
def _config(self):
279287
"""Value to be written to the device's config register """
280288
config = 0
281-
if self.mode == MODE.NORMAL:
289+
if self.mode == MODE_NORMAL:
282290
config += (self._t_standby << 5)
283291
if self._iir_filter:
284-
config += (self._iir_filter.value << 2)
292+
config += (self._iir_filter << 2)
285293
if isinstance(self, Adafruit_BME280_SPI):
286294
config += 1 #enable SPI interface
287295
return config
288296

289297
@property
290298
def _ctrl_meas(self):
291299
"""Value to be written to the device's ctrl_meas register """
292-
ctrl_meas = (self.overscan_temperature.value << 5)
293-
ctrl_meas += (self.overscan_pressure.value << 2)
294-
ctrl_meas += self.mode.value
300+
ctrl_meas = (self.overscan_temperature << 5)
301+
ctrl_meas += (self.overscan_pressure << 2)
302+
ctrl_meas += self.mode
295303
return ctrl_meas
296304

297305
@property

examples/bme280_normal_mode.py

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
"""
2+
Example showing how the BME280 library can be used to set the various
3+
parameters supported by the sensor.
4+
Refer to the BME280 datasheet to understand what these parameters do
5+
"""
16
import time
27

38
import board
@@ -15,13 +20,12 @@
1520

1621
# change this to match the location's pressure (hPa) at sea level
1722
bme280.sea_level_pressure = 1013.25
18-
#refer to the BMP280 datasheet to understand what these do
19-
bme280.mode = adafruit_bme280.MODE.NORMAL
20-
bme280.standby_period = adafruit_bme280.STANDBY.TC_500
21-
bme280.iir_filter = adafruit_bme280.IIR_FILTER.X_16
22-
bme280.overscan_pressure = adafruit_bme280.OVERSCAN.X_16
23-
bme280.overscan_humidity = adafruit_bme280.OVERSCAN.X_1
24-
bme280.overscan_temperature = adafruit_bme280.OVERSCAN.X_2
23+
bme280.mode = adafruit_bme280.MODE_NORMAL
24+
bme280.standby_period = adafruit_bme280.STANDBY_TC_500
25+
bme280.iir_filter = adafruit_bme280.IIR_FILTER_X16
26+
bme280.overscan_pressure = adafruit_bme280.OVERSCAN_X16
27+
bme280.overscan_humidity = adafruit_bme280.OVERSCAN_X1
28+
bme280.overscan_temperature = adafruit_bme280.OVERSCAN_X2
2529
#The sensor will need a moment to gather inital readings
2630
time.sleep(1)
2731

0 commit comments

Comments
 (0)