adafruit / Adafruit_ADS1X15

Driver for TI's ADS1015: 12-bit Differential or Single-Ended ADC with PGA and Comparator
Other
289 stars 301 forks source link

Refactoring, and exposing continuous mode #76

Closed matthewlai closed 2 years ago

matthewlai commented 2 years ago

A few changes:

  1. Refactored copy and pasted code out of the readADC functions.
  2. Exposed interface to support both non-blocking conversions (startADCReading() => conversionComplete() => getLastConversionResults()), and continuous conversions. The ALERT/RDY pin is set to RDY mode to allow for interrupt based reads in continuous mode, and this is the only way to achieve the maximum conversion rate of 860sps supported by the chips.

All these changes are backward compatible.

ladyada commented 2 years ago

hiya! thanks so much for submitting a PR! we can review & merge PRs once they have passed continuous integration (CI). that means that code compiles cleanly for all tested platforms, is clang formatted so we maintain the same text formatting for all new code, and is doxygen documented. 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 doxygen: https://learn.adafruit.com/the-well-automated-arduino-library/doxygen

and clang-format: https://learn.adafruit.com/the-well-automated-arduino-library/formatting-with-clang-format

and overall how to contribute PRs to libraries: https://learn.adafruit.com/contribute-to-arduino-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!)

ladyada commented 2 years ago

can you split the freq update and the refactor into 2 PRs? for freq update we have to be careful because other i2c devices may not be 400khz so maybe make the freq an argument or function :)

matthewlai commented 2 years ago

Done!

ladyada commented 2 years ago

ok @caternuson is the expert on this libray and can review it next week most likely! thank yu :)

caternuson commented 2 years ago

@matthewlai Thanks! This looks like a good refactor. Can you also add two new example sketches to demonstrate (1) continuous reading and (2) non-blocking reading. That'll help a lot to demonstrate using the new capabilities.

matthewlai commented 2 years ago

Done! They do compile for both ESP32 and AVR, but unfortunately I cannot test them as the only chip I have is now embedded in a wall :) My actual application uses the chip like in the continuous example though.

caternuson commented 2 years ago

Thanks for the examples. Tested both using an Itsy M4 and they work as expected. One change for each, can use computeVolts : https://github.com/adafruit/Adafruit_ADS1X15/blob/da24495248886e9dfce5f5cec5359797fd586998/Adafruit_ADS1X15.h#L158 instead of using the hardcoded multiplier. Something like:

  Serial.print("Differential: "); Serial.print(results); Serial.print("("); Serial.print(ads.computeVolts(results)); Serial.println("mV)");
matthewlai commented 2 years ago

Done!

caternuson commented 2 years ago

Awesome. Thanks!