Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

memory_optimization #52

Merged
merged 7 commits into from
Jun 3, 2021
Merged

memory_optimization #52

merged 7 commits into from
Jun 3, 2021

Conversation

jposada202020
Copy link
Contributor

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
Copy link
Contributor Author

@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

Copy link
Member

@tannewt tannewt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of having copies of code in advanced.py please have is subclass the basic version to add additional features. Thanks!

Comment on lines 71 to 96
"""iir_filter values"""
IIR_FILTER_DISABLE = const(0)

"""overscan values for temperature, pressure, and humidity"""
OVERSCAN_DISABLE = const(0x00)
OVERSCAN_X1 = const(0x01)
OVERSCAN_X2 = const(0x02)
OVERSCAN_X4 = const(0x03)
OVERSCAN_X8 = const(0x04)
OVERSCAN_X16 = const(0x05)

_BME280_OVERSCANS = {
OVERSCAN_DISABLE: 0,
OVERSCAN_X1: 1,
OVERSCAN_X2: 2,
OVERSCAN_X4: 4,
OVERSCAN_X8: 8,
OVERSCAN_X16: 16,
}

"""mode values"""
MODE_SLEEP = const(0x00)
MODE_FORCE = const(0x01)
MODE_NORMAL = const(0x03)

_BME280_MODES = (MODE_SLEEP, MODE_FORCE, MODE_NORMAL)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd probably move these to advanced. In the internal init just use the value. These cost memory for each variable name.

@jposada202020
Copy link
Contributor Author

will do

@jposada202020
Copy link
Contributor Author

jposada202020 commented May 26, 2021

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 jposada202020 marked this pull request as ready for review May 26, 2021 18:53
@jposada202020
Copy link
Contributor Author

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

@jposada202020
Copy link
Contributor Author

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

@jposada202020
Copy link
Contributor Author

jposada202020 commented May 30, 2021

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

  • Itsybitsy M4
  • Metro M4
  • Raspberry Pico
  • Metro EXP32S2
  • FeatherS2
  • Feather RP2040
  • Feather nRFS2840

and with the simpletest on

  • QtPy M0

Copy link
Contributor

@jerryneedell jerryneedell left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I ran both the simpletest and normal_mode on a feather_m0_rfm9x -- I created .mpys of the advanced.py and basic.py.
Both tests ran normally.
All run with 7.0 alpha.2

@jerryneedell
Copy link
Contributor

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
Copy link
Contributor

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

@jposada202020
Copy link
Contributor Author

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

@jerryneedell
Copy link
Contributor

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

@jposada202020
Copy link
Contributor Author

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
Copy link
Contributor Author

@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

Copy link
Member

@tannewt tannewt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you! This looks great!

@tannewt tannewt merged commit d5d36ea into adafruit:master Jun 3, 2021
adafruit-adabot added a commit to adafruit/Adafruit_CircuitPython_Bundle that referenced this pull request Jun 8, 2021
Updating https://github.com/adafruit/Adafruit_CircuitPython_BME280 to 2.6.5 from 2.6.4:
  > Moved default branch to main
  > Merge pull request adafruit/Adafruit_CircuitPython_BME280#52 from jposada202020/memory_otimization
  > Moved CI to Python 3.7

Updating https://github.com/adafruit/Adafruit_CircuitPython_HTU31D to 1.1.1 from 1.1.0:
  > Merge pull request adafruit/Adafruit_CircuitPython_HTU31D#6 from gmparis/main
  > Moved CI to Python 3.7
  > Added help text and problem matcher
  > Added pull request template

Updating https://github.com/adafruit/Adafruit_CircuitPython_ICM20X to 2.0.8 from 2.0.7:
  > Linted
  > Moved default branch to main
  > Moved CI to Python 3.7
  > Added help text and problem matcher
  > Added pull request template

Updating https://github.com/adafruit/Adafruit_CircuitPython_IRRemote to 4.1.0 from 4.0.6:
  > Moved default branch to main
  > Merge pull request adafruit/Adafruit_CircuitPython_IRRemote#42 from danielballan/nonblocking
  > Moved CI to Python 3.7
  > Added help text and problem matcher
  > Added pull request template

Updating https://github.com/adafruit/Adafruit_CircuitPython_L3GD20 to 2.3.6 from 2.3.5:
  > Merge pull request adafruit/Adafruit_CircuitPython_L3GD20#23 from jposada202020/correcting_measuremnt_units
  > Moved default branch to main
  > Moved CI to Python 3.7
  > Added help text and problem matcher
  > Added pull request template

Updating https://github.com/adafruit/Adafruit_CircuitPython_SGP40 to 1.1.0 from 1.0.3:
  > Merge pull request adafruit/Adafruit_CircuitPython_SGP40#4 from CrakeNotSnowman/voc_algorithm_dev
  > Moved CI to Python 3.7
  > Added help text and problem matcher
  > Added pull request template

Updating https://github.com/adafruit/Adafruit_CircuitPython_SHTC3 to 1.1.2 from 1.1.1:
  > Moved default branch to main
  > Merge pull request adafruit/Adafruit_CircuitPython_SHTC3#11 from jposada202020/master
  > Moved CI to Python 3.7
  > Added help text and problem matcher
  > Added pull request template

Updating https://github.com/adafruit/Adafruit_CircuitPython_SI4713 to 1.3.2 from 1.3.1:
  > Moved default branch to main
  > Merge pull request adafruit/Adafruit_CircuitPython_SI4713#20 from jposada202020/solving_station_information

Updating https://github.com/adafruit/Adafruit_CircuitPython_SSD1306 to 2.11.6 from 2.11.5:
  > Corrected url
  > Merge pull request adafruit/Adafruit_CircuitPython_SSD1306#64 from jposada202020/correcting_example_link

Updating https://github.com/adafruit/Adafruit_CircuitPython_Colorsys to 2.0.1 from 2.0.0:
  > Moved default branch to main
  > Merge pull request adafruit/Adafruit_CircuitPython_Colorsys#17 from GomiHgy/patch-1
  > Moved CI to Python 3.7
  > Added help text and problem matcher
  > Added pull request template

Updating https://github.com/adafruit/Adafruit_CircuitPython_FunHouse to 2.1.4 from 2.1.3:
  > Merge pull request adafruit/Adafruit_CircuitPython_FunHouse#17 from jposada202020/adding_new_guides
@jposada202020 jposada202020 deleted the memory_otimization branch January 20, 2023 01:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants