Skip to content

Commit 6398fd4

Browse files
authored
Merge pull request #15 from jposada202020/light_class
memory_reduction
2 parents 659ca5f + f2464bb commit 6398fd4

File tree

11 files changed

+410
-215
lines changed

11 files changed

+410
-215
lines changed

README.rst

Lines changed: 34 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ Introduction
1313
:target: https://github.com/adafruit/Adafruit_CircuitPython_DPS310/actions
1414
:alt: Build Status
1515

16+
.. image:: https://img.shields.io/badge/code%20style-black-000000.svg
17+
:target: https://github.com/psf/black
18+
:alt: Code Style: Black
19+
1620
Library for the DPS310 Precision Barometric Pressure Sensor
1721

1822

@@ -53,41 +57,56 @@ To install in a virtual environment in your current project:
5357
source .env/bin/activate
5458
pip3 install adafruit-circuitpython-dps310
5559
60+
61+
Installing to a connected CircuitPython Device
62+
==============================================
63+
Some devices, eg. the QT-PY, are very limited in memory. The DPS310 library contains
64+
two variants - basic and advanced - which give different levels of functionality.
65+
66+
Installing the DPS310 library could have the following outcomes:
67+
68+
* It installs successfully and your code runs successfully. Woo-hoo! Continue with
69+
your amazing project.
70+
* It installs successfully and your code fails to run with a memory allocation
71+
error. Try one of the following:
72+
73+
* If your ``code.py`` is large, especially if it has lots of comments, you
74+
can shrink it into a ``.mpy`` file instead. See the Adafruit
75+
`Learn Guide <https://learn.adafruit.com/Memory-saving-tips-for-CircuitPython/non-volatile-not-enough-disk-space>`_
76+
on shrinking your code.
77+
* Only use the basic DPS310 implementation, and remove the following file:
78+
``<CIRCUITPY>/lib/adafruit_dps310/advanced.mpy`` where <CIRCUITPY> is the
79+
mounted location of your device. Make sure that your code only uses the basic
80+
implementation.
81+
82+
5683
Usage Example
5784
=============
5885

5986
.. code-block:: python3
6087
6188
import time
6289
import board
63-
import adafruit_dps310
90+
from adafruit_dps310.basic import DPS310
6491
6592
i2c = board.I2C() # uses board.SCL and board.SDA
6693
67-
dps310 = adafruit_dps310.DPS310(i2c)
94+
dps310 = DPS310(i2c)
6895
6996
while True:
7097
print("Temperature = %.2f *C"%dps310.temperature)
7198
print("Pressure = %.2f hPa"%dps310.pressure)
7299
print("")
73100
time.sleep(1.0)
74101
75-
Caveat: by default the library initializes the IC with constant temperature and pressure measurements at 64Hz with 64 samples. It is not possible to change the IC's mode, temperature_oversample_count or pressure_oversample_count on-the-fly so resetting the IC and operation parameteres is required. For instance, to set the mode to continuous pressure measurement at 1Hz with 2 samples:
76-
77-
.. code-block:: python3
78-
79-
dps310.reset()
80-
dps310.pressure_oversample_count = adafruit_dps310.SampleCount.COUNT_2
81-
dps310.pressure_rate = adafruit_dps310.Rate.RATE_1_HZ
82-
dps310.mode = adafruit_dps310.Mode.CONT_PRESSURE
83-
dps310.wait_pressure_ready()
84-
85-
86102
87103
Known Issues
88104
============
89-
Library extensive features might not be compatible with memory limited boards. Library might not
90-
load in SAMD21 boards and others with 32KB of RAM or less.
105+
Library extensive features might not be compatible with memory limited boards. For these kind of
106+
boards you need to use the ``adafruit_dps310/basic.mpy``, the file needs to be in the ``mpy``
107+
format in order to fit in memory.
108+
For boards with more memory available you could use the code present
109+
in ``adafruit_dps310/advanced.py``. For usage refer to ``dps310_simpletest_advanced.py``
91110

92111

93112
Documentation

adafruit_dps310/__init__.py

Whitespace-only changes.

adafruit_dps310.py renamed to adafruit_dps310/advanced.py

Lines changed: 28 additions & 186 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,13 @@
33
# SPDX-License-Identifier: MIT
44

55
"""
6-
`adafruit_dps310`
6+
`adafruit_dps310.advanced`
77
================================================================================
88
9-
Library for the DPS310 Precision Barometric Pressure Sensor
9+
Library for the DPS310 Precision Barometric Pressure Sensor. This is the advanced
10+
version. Includes some features not present in `adafruit_dps310.basic`
1011
11-
* Author(s): Bryan Siepert
12+
* Author(s): Bryan Siepert, Jose David M.
1213
1314
Implementation Notes
1415
--------------------
@@ -34,26 +35,10 @@
3435
__version__ = "0.0.0-auto.0"
3536
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_DPS310.git"
3637

37-
# Common imports; remove if unused or pylint will complain
38-
import math
3938
from time import sleep
40-
from adafruit_bus_device import i2c_device
41-
from adafruit_register.i2c_struct import UnaryStruct, ROUnaryStruct
42-
from adafruit_register.i2c_bit import RWBit, ROBit
43-
from adafruit_register.i2c_bits import RWBits, ROBits
44-
45-
_DPS310_DEFAULT_ADDRESS = 0x77 # DPS310 default i2c address
46-
_DPS310_DEVICE_ID = 0x10 # DPS310 device identifier
47-
48-
_DPS310_PRSB2 = 0x00 # Highest byte of pressure data
49-
_DPS310_TMPB2 = 0x03 # Highest byte of temperature data
50-
_DPS310_PRSCFG = 0x06 # Pressure configuration
51-
_DPS310_TMPCFG = 0x07 # Temperature configuration
52-
_DPS310_MEASCFG = 0x08 # Sensor configuration
53-
_DPS310_CFGREG = 0x09 # Interrupt/FIFO configuration
54-
_DPS310_RESET = 0x0C # Soft reset
55-
_DPS310_PRODREVID = 0x0D # Register that contains the part ID
56-
_DPS310_TMPCOEFSRCE = 0x28 # Temperature calibration src
39+
from micropython import const
40+
from adafruit_register.i2c_bits import RWBits
41+
from adafruit_dps310.basic import DPS310
5742

5843
# pylint: disable=no-member,unnecessary-pass
5944

@@ -159,29 +144,45 @@ class SampleCount(CV):
159144
)
160145
)
161146
# pylint: enable=unnecessary-pass
162-
class DPS310:
147+
148+
_DPS310_DEFAULT_ADDRESS = const(0x77) # DPS310 default i2c address
149+
# _DPS310_DEVICE_ID = const(0x10) # DPS310 device identifier
150+
151+
# _DPS310_PRSB2 = const(0x00) # Highest byte of pressure data
152+
# _DPS310_TMPB2 = const(0x03) # Highest byte of temperature data
153+
_DPS310_PRSCFG = const(0x06) # Pressure configuration
154+
_DPS310_TMPCFG = const(0x07) # Temperature configuration
155+
# _DPS310_MEASCFG = const(0x08) # Sensor configuration
156+
# _DPS310_CFGREG = const(0x09) # Interrupt/FIFO configuration
157+
# _DPS310_RESET = const(0x0C) # Soft reset
158+
# _DPS310_PRODREVID = const(0x0D) # Register that contains the part ID
159+
# _DPS310_TMPCOEFSRCE = const(0x28) # Temperature calibration src
160+
161+
162+
class DPS310_Advanced(DPS310):
163163
# pylint: disable=too-many-instance-attributes
164164
"""Library for the DPS310 Precision Barometric Pressure Sensor.
165+
This class contains some of other configurable features
165166
166167
:param ~busio.I2C i2c_bus: The I2C bus the DPS310 is connected to.
167168
:param int address: The I2C device address. Defaults to :const:`0x77`
168169
169170
**Quickstart: Importing and using the DPS310**
170171
171-
Here is an example of using the :class:`DPS310` class.
172+
Here is an example of using the :class:`DPS310_Advanced` class.
172173
First you will need to import the libraries to use the sensor
173174
174175
.. code-block:: python
175176
176177
import board
177-
import adafruit_dps310
178+
from adafruit_dps310.dps310_advanced import DPS310_Advanced as DPS310
178179
179180
Once this is done you can define your `board.I2C` object and define your sensor object
180181
181182
.. code-block:: python
182183
183184
i2c = board.I2C() # uses board.SCL and board.SDA
184-
dps310 = adafruit_dps310.DPS310(i2c)
185+
dps310 = DPS310(i2c)
185186
186187
Now you have access to the :attr:`temperature` and :attr:`pressure` attributes.
187188
@@ -192,66 +193,11 @@ class DPS310:
192193
193194
"""
194195
# Register definitions
195-
_device_id = ROUnaryStruct(_DPS310_PRODREVID, ">B")
196-
_reset_register = UnaryStruct(_DPS310_RESET, ">B")
197-
_mode_bits = RWBits(3, _DPS310_MEASCFG, 0)
198-
199196
_pressure_ratebits = RWBits(3, _DPS310_PRSCFG, 4)
200-
_pressure_osbits = RWBits(4, _DPS310_PRSCFG, 0)
201-
202197
_temp_ratebits = RWBits(3, _DPS310_TMPCFG, 4)
203-
_temp_osbits = RWBits(4, _DPS310_TMPCFG, 0)
204-
205-
_temp_measurement_src_bit = RWBit(_DPS310_TMPCFG, 7)
206-
207-
_pressure_shiftbit = RWBit(_DPS310_CFGREG, 2)
208-
_temp_shiftbit = RWBit(_DPS310_CFGREG, 3)
209-
210-
_coefficients_ready = RWBit(_DPS310_MEASCFG, 7)
211-
_sensor_ready = RWBit(_DPS310_MEASCFG, 6)
212-
_temp_ready = RWBit(_DPS310_MEASCFG, 5)
213-
_pressure_ready = RWBit(_DPS310_MEASCFG, 4)
214-
215-
_raw_pressure = ROBits(24, _DPS310_PRSB2, 0, 3, lsb_first=False)
216-
_raw_temperature = ROBits(24, _DPS310_TMPB2, 0, 3, lsb_first=False)
217-
218-
_calib_coeff_temp_src_bit = ROBit(_DPS310_TMPCOEFSRCE, 7)
219-
220-
_reg0e = RWBits(8, 0x0E, 0)
221-
_reg0f = RWBits(8, 0x0F, 0)
222-
_reg62 = RWBits(8, 0x62, 0)
223198

224199
def __init__(self, i2c_bus, address=_DPS310_DEFAULT_ADDRESS):
225-
self.i2c_device = i2c_device.I2CDevice(i2c_bus, address)
226-
227-
if self._device_id != _DPS310_DEVICE_ID:
228-
raise RuntimeError("Failed to find DPS310 - check your wiring!")
229-
self._pressure_scale = None
230-
self._temp_scale = None
231-
self._c0 = None
232-
self._c1 = None
233-
self._c00 = None
234-
self._c00 = None
235-
self._c10 = None
236-
self._c10 = None
237-
self._c01 = None
238-
self._c11 = None
239-
self._c20 = None
240-
self._c21 = None
241-
self._c30 = None
242-
self._oversample_scalefactor = (
243-
524288,
244-
1572864,
245-
3670016,
246-
7864320,
247-
253952,
248-
516096,
249-
1040384,
250-
2088960,
251-
)
252-
self.sea_level_pressure = 1013.25
253-
"""Pressure in hectoPascals at sea level. Used to calibrate :attr:`altitude`."""
254-
self.initialize()
200+
super().__init__(i2c_bus, _DPS310_DEFAULT_ADDRESS)
255201

256202
def initialize(self):
257203
"""Initialize the sensor to continuous measurement"""
@@ -268,69 +214,6 @@ def initialize(self):
268214
self.wait_temperature_ready()
269215
self.wait_pressure_ready()
270216

271-
# (https://github.com/Infineon/DPS310-Pressure-Sensor#temperature-measurement-issue)
272-
# similar to DpsClass::correctTemp(void) from infineon's c++ library
273-
def _correct_temp(self):
274-
"""Correct temperature readings on ICs with a fuse bit problem"""
275-
self._reg0e = 0xA5
276-
self._reg0f = 0x96
277-
self._reg62 = 0x02
278-
self._reg0e = 0
279-
self._reg0f = 0
280-
281-
# perform a temperature measurement
282-
# the most recent temperature will be saved internally
283-
# and used for compensation when calculating pressure
284-
_unused = self._raw_temperature
285-
286-
def reset(self):
287-
"""Reset the sensor"""
288-
self._reset_register = 0x89
289-
# wait for hardware reset to finish
290-
sleep(0.010)
291-
while not self._sensor_ready:
292-
sleep(0.001)
293-
self._correct_temp()
294-
self._read_calibration()
295-
# make sure we're using the temperature source used for calibration
296-
self._temp_measurement_src_bit = self._calib_coeff_temp_src_bit
297-
298-
@property
299-
def pressure(self):
300-
"""Returns the current pressure reading in hPA"""
301-
302-
temp_reading = self._raw_temperature
303-
raw_temperature = self._twos_complement(temp_reading, 24)
304-
pressure_reading = self._raw_pressure
305-
raw_pressure = self._twos_complement(pressure_reading, 24)
306-
_scaled_rawtemp = raw_temperature / self._temp_scale
307-
308-
_temperature = _scaled_rawtemp * self._c1 + self._c0 / 2.0
309-
310-
p_red = raw_pressure / self._pressure_scale
311-
312-
pres_calc = (
313-
self._c00
314-
+ p_red * (self._c10 + p_red * (self._c20 + p_red * self._c30))
315-
+ _scaled_rawtemp * (self._c01 + p_red * (self._c11 + p_red * self._c21))
316-
)
317-
318-
final_pressure = pres_calc / 100
319-
return final_pressure
320-
321-
@property
322-
def altitude(self):
323-
"""The altitude based on the sea level pressure (:attr:`sea_level_pressure`) -
324-
which you must enter ahead of time)"""
325-
return 44330 * (1.0 - math.pow(self.pressure / self.sea_level_pressure, 0.1903))
326-
327-
@property
328-
def temperature(self):
329-
"""The current temperature reading in degrees Celsius"""
330-
_scaled_rawtemp = self._raw_temperature / self._temp_scale
331-
_temperature = _scaled_rawtemp * self._c1 + self._c0 / 2.0
332-
return _temperature
333-
334217
@property
335218
def temperature_ready(self):
336219
"""Returns true if there is a temperature reading ready"""
@@ -434,44 +317,3 @@ def temperature_oversample_count(self, value):
434317
self._temp_osbits = value
435318
self._temp_scale = self._oversample_scalefactor[value]
436319
self._temp_shiftbit = value > SampleCount.COUNT_8
437-
438-
@staticmethod
439-
def _twos_complement(val, bits):
440-
if val & (1 << (bits - 1)):
441-
val -= 1 << bits
442-
443-
return val
444-
445-
def _read_calibration(self):
446-
447-
while not self._coefficients_ready:
448-
sleep(0.001)
449-
450-
buffer = bytearray(19)
451-
coeffs = [None] * 18
452-
for offset in range(18):
453-
buffer = bytearray(2)
454-
buffer[0] = 0x10 + offset
455-
456-
with self.i2c_device as i2c:
457-
458-
i2c.write_then_readinto(buffer, buffer, out_end=1, in_start=1)
459-
460-
coeffs[offset] = buffer[1]
461-
462-
self._c0 = (coeffs[0] << 4) | ((coeffs[1] >> 4) & 0x0F)
463-
self._c0 = self._twos_complement(self._c0, 12)
464-
465-
self._c1 = self._twos_complement(((coeffs[1] & 0x0F) << 8) | coeffs[2], 12)
466-
467-
self._c00 = (coeffs[3] << 12) | (coeffs[4] << 4) | ((coeffs[5] >> 4) & 0x0F)
468-
self._c00 = self._twos_complement(self._c00, 20)
469-
470-
self._c10 = ((coeffs[5] & 0x0F) << 16) | (coeffs[6] << 8) | coeffs[7]
471-
self._c10 = self._twos_complement(self._c10, 20)
472-
473-
self._c01 = self._twos_complement((coeffs[8] << 8) | coeffs[9], 16)
474-
self._c11 = self._twos_complement((coeffs[10] << 8) | coeffs[11], 16)
475-
self._c20 = self._twos_complement((coeffs[12] << 8) | coeffs[13], 16)
476-
self._c21 = self._twos_complement((coeffs[14] << 8) | coeffs[15], 16)
477-
self._c30 = self._twos_complement((coeffs[16] << 8) | coeffs[17], 16)

0 commit comments

Comments
 (0)