Skip to content

Improve speed of temperature retrieval and allow non-blocking measurement #19

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

Merged
merged 4 commits into from
Aug 2, 2022

Conversation

eirinnm
Copy link
Contributor

@eirinnm eirinnm commented Aug 2, 2022

According to the MAX31856 datasheet, a one-shot temperature measurement takes approximately 160ms, and the ongoing status of the measurement can be queried by looking at the CRO register. However the current implementation in this library doesn't do this; it simply calls sleep(0.250) to block for 250ms upon triggering a measurement. Naturally this results in low performance if multiple sensors are used in parallel.

This PR improves performance by making the .temperature retrieval property return as soon as the measurement is complete.

Internally this is accomplished with new functions:

  • .initiate_one_shot_measurement(), which returns immediately;
  • .oneshot_pending, which queries the CRO register;
  • ._wait_for_oneshot(), which checks .oneshot_pending in a loop every 10ms until the value is False;
  • .unpack_temperature(), which retrieves the measurement from the register.

A developer may use these functions to read multiple MAX31856 sensors in parallel simultaneously because one is not blocking the other.

The original .temperature property still performs a blocking temperature retrieval to maintain expected behaviour, but it is refactored to use these functions, and now takes ~160ms instead of 250ms.

Tested on a Feather ESP32-S2 with two MAX31856 sensors.

@ladyada
Copy link
Member

ladyada commented Aug 2, 2022

hiya! thanks so much for submitting a PR! we can review & merge PRs once they have passed continuous integration (CI). that means that code is 'linted' - that means it is formatted and passes basic structure tests, so that we maintain the same text formatting for all new code
if your code isnt passing, check the CI output (click on the red X next to the PR to scroll through the log and find where the error is

here is a tutorial on pylint and black: https://learn.adafruit.com/improve-your-code-with-pylint

and overall how to contribute PRs to circuitpython: https://learn.adafruit.com/contribute-to-circuitpython-with-git-and-github

once you get that green checkmark that indicates CI has passed, please comment reply to this post so we know its time for another review (we may not get notified on CI pass and miss that its time to look!)

@eirinnm
Copy link
Contributor Author

eirinnm commented Aug 2, 2022

Hi! Happy to contribute to this library. I have two more PRs to file after this, adding more features from the datasheet: multi-sample averaging, and 50/60hz mains filtering.

@ladyada ladyada merged commit ba94e36 into adafruit:main Aug 2, 2022
adafruit-adabot added a commit to adafruit/Adafruit_CircuitPython_Bundle that referenced this pull request Aug 4, 2022
Updating https://github.com/adafruit/Adafruit_CircuitPython_MAX31856 to 0.10.0 from 0.9.11:
  > Added Black formatting badge
  > Merge pull request adafruit/Adafruit_CircuitPython_MAX31856#19 from eirinnm/non-blocking
  > Changed .env to .venv in README.rst

Updating https://github.com/adafruit/Adafruit_CircuitPython_Requests to 1.12.5 from 1.12.4:
  > Merge pull request adafruit/Adafruit_CircuitPython_Requests#112 from Neradoc/fix-send-content-length
  > Added Black formatting badge
  > Changed .env to .venv in README.rst
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.

2 participants