adafruit / Adafruit_CircuitPython_BME280

CircuitPython driver for the BME280
MIT License
63 stars 42 forks source link

memory_optimization #52

Closed jposada202020 closed 3 years ago

jposada202020 commented 3 years ago

Initial test

Register and Bus_device mpy test code

Test Code

import gc
print("Loading only gc", gc.mem_free())
import time
print("After importing time module", gc.mem_free())
import board
print("After importing board module", gc.mem_free())
import adafruit_bme280
print("After importing BME280 module", gc.mem_free())
i2c = board.I2C()
print("After initiating i2c module", gc.mem_free())
bme280 = adafruit_bme280.Adafruit_BME280_I2C(i2c)
print("After defining bm280 sensor", gc.mem_free())

bme280.sea_level_pressure = 1013.25
print("After defining sea level pressure", gc.mem_free())

print("\nTemperature: %0.1f C" % bme280.temperature)
print("Humidity: %0.1f %%" % bme280.relative_humidity)
print("Pressure: %0.1f hPa" % bme280.pressure)
print("Altitude = %0.2f meters" % bme280.altitude)
print("After print instructions", gc.mem_free())

Results

>>> import bme280_simpletest
Loading only gc 19136
After importing time module 19136
After importing board module 19120
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "bme280_simpletest.py", line 7, in <module>
MemoryError: 

Test 2 Doing a gc before loading the bme280 module

>>> import bme280_simpletest
Loading only gc 19056
After importing time module 19056
After importing board module 19040
After gc collect 19280
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "bme280_simpletest.py", line 9, in <module>
MemoryError: memory allocation failed, allocating 79 bytes

Test 3 trying to load the module from the REPL

>>> import adafruit_bme280
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
MemoryError: memory allocation failed, allocating 117 bytes

Test 4 New module without the I2C and SPI classes

Adafruit CircuitPython 6.2.0 on 2021-04-05; Adafruit QT Py M0 with samd21e18
>>> import basic
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
MemoryError: 

Test 5. Lean Basic file and using mpy file

>>> import bme280_simpletest
Loading only gc 17872
After importing BME280 module 9408
After importing time module 9392
After importing board module 9392
After gc collect 9392
After initiating i2c module 9376
After defining bm280 sensor 5168
After defining sea level pressure 5168

Temperature: 23.2 C
Humidity: 43.6 %
Pressure: 1015.9 hPa
Altitude = -22.22 meters
After print instructions 528

Test 6 using basic.mpy and Using GC after each print statement

>>> import bme280_simpletest
Loading only gc 19024
After importing BME280 module 9472
After importing time module 9456
After importing board module 9456
After gc collect 9456
After initiating i2c module 9440
After defining bm280 sensor 5264
After defining sea level pressure 5264

Temperature: 23.3 C
Humidity: 43.8 %
Pressure: 1016.0 hPa
Altitude = -22.41 meters
After print instructions 6416
jposada202020 commented 3 years ago

@tannewt please review and comments, and then I wil proceed to all the code cleanup and examples, and references check. I include all the messages for the heap size analysis

Thanks

jposada202020 commented 3 years ago

will do

jposada202020 commented 3 years ago

TEST 7 Removing all the variable in the basic file and only working with values as per suggestion

>>> import bme280_simpletest
Loading only gc 18800
After importing BME280 module 12784
After importing time module 12768
After importing board module 12768
After gc collect 12784
After initiating i2c module 12768
After defining bm280 sensor 8544
After defining sea level pressure 8544

Temperature: 26.3 C
Humidity: 43.2 %
Pressure: 1002.5 hPa
Altitude = 89.93 meters
After print instructions 9728

Improvement From 9.5k to 6k

jposada202020 commented 3 years ago

I decided for maintenability purposes to keep the Variables definitions, that way we could use the functions in both basic and advanced.

jposada202020 commented 3 years ago

before doing test in some boards please review :) Thanks.

jposada202020 commented 3 years ago

I tested this on for the simpletest and normal_mode in the examples:

and with the simpletest on

jerryneedell commented 3 years ago

Ran the test code from the top of this PR (using basic)

Adafruit CircuitPython 7.0.0-alpha.2-705-gc80e25307 on 2021-05-30; Adafruit Feather M0 RFM9x with samd21g18
>>> 
>>> import test
Loading only gc 19136
After importing time module 19136
After importing board module 19120
After importing BME280 module 13536
After initiating i2c module 13520
After defining bm280 sensor 12080
After defining sea level pressure 12080

Temperature: 24.3 C
Humidity: 41.3 %
Pressure: 1024.1 hPa
Altitude = -90.18 meters
After print instructions 7632
>>> 

I'm not really below sea-level ;-)

jerryneedell commented 3 years ago

Also ran the demo using RFM9x radio and BME280 together-- see #37

jposada202020 commented 3 years ago

@jerryneedell interesting I will take a look at the altitude. Thanks for testing.

jerryneedell commented 3 years ago

I'm not worried about it -- I ddi not set the offset.

jposada202020 commented 3 years ago

This was tested on RP 4 using Pyton 3.7.3 and blinka

Also this was tested on Raspberry Pi Pico with RP2040 Using Blinka and the bme280_simpletest_pico.py

MicroPython v1.15 on 2021-04-18; Raspberry Pi Pico with RP2040
Type "help()" for more information.
>>> %Run -c $EDITOR_CONTENT

Temperature: 25.8 C
Humidity: 44.6 %
Pressure: 1017.1 hPa
Altitude = -32.61 meters
jposada202020 commented 3 years ago

@tannewt I think we are ready, I will need to modify the learning guide for the library and the Micropython with Blinka learning guide also, as it includes this sensor. If you are ok with the changes I will marge when I do the learning guide changes. Thanks as always