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

Changes to allow code to run on SparkFun OpenLog Artemis #22

Closed
wants to merge 15 commits into from

Conversation

PaulZC
Copy link
Contributor

@PaulZC PaulZC commented Jan 27, 2021

Hi @jaxxzer ,

We really like the BAR02 sensor and want to add support for it on OpenLog Artemis.
To do that, we need to remove the hardwiring to the Wire interface. This PR does that.

Please note: I haven't updated the version in library.properties. You might want to do that after merging.

Changes:

I haven't included @senceryazici 's PR #16 to support the other OverSample Rates but his changes look good. You might want to merge those too.

Enjoy!

Paul

Copy link
Member

@patrickelectric patrickelectric left a comment

Choose a reason for hiding this comment

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

Hi @PaulZC!
Thanks for the great contribution, it indeed is a great thing that you are doing!
Just some small corrections and a couple of suggestions to get it merged :)

@@ -19,110 +19,140 @@ MS5837::MS5837() {
fluidDensity = 1029;
}

bool MS5837::init() {
bool MS5837::begin(TwoWire &wirePort) {
return (init(wirePort));
Copy link
Member

Choose a reason for hiding this comment

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

You could add wirePort.begin() here also.
Also, is there a reason for you do choose this approach and not to add TwoWire as a class constructor argument ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hi Patrick (@patrickelectric),
Thanks for the feedback!
We don't call Wire.begin inside the library. We expect the user to call Wire.begin externally. It saves problems with (e.g.) (re)setting the clock speed. Please see this post (scroll down to the Don’t call Wire.begin() in the library section).
https://www.sparkfun.com/news/3245
Yes, true, you could use a class constructor argument. We choose not to do it that way.
Paul

}

bool MS5837::init(TwoWire &wirePort) {
_i2cPort = &wirePort; //Grab which port the user wants us to use
Copy link
Member

Choose a reason for hiding this comment

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

You could move _i2cPort from ptr to reference if you do the initialization of it in the constructor

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, true, but we choose not to do it that way. All the SparkFun libraries accept the wirePort as an parameter in .begin. There are some devices that can communicate over both I2C or SPI and we deal with those by overloading .begin.

src/MS5837.cpp Outdated
_model = MS5837_02BA;
return true;
}
else if (version == 0x15) // MS5837-02BA21
Copy link
Member

Choose a reason for hiding this comment

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

else unnecessary after return

Copy link
Contributor Author

@PaulZC PaulZC Feb 5, 2021

Choose a reason for hiding this comment

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

Ah, yes, you're right. I've changed this and have added more improvements in the latest commits:

  • I've defined a const uint8_t for each sensor version (based on the values in the datasheets)
  • I've added MS5837_UNRECOGNISED as a possible value for _model
  • .init sets _model according to the sensor version it extracts from PROM Word 0
  • if the sensor version is unrecognised, .init still returns true but sets _model to MS5837_UNRECOGNISED
  • the code is still backward-compatible. You can still call setModel to override _model if required. (E.g. if TE has used a different sensor version ID which they have forgotten to tell us about!)

The main reason I've added all of this is that we support the MS5637 on OpenLog Artemis. It has the same address as the MS5837 and has the same memory structure. It passes the same CRC test. Before I made these changes, the MS5837 was appearing as an MS5637. The MS5637 datasheet doesn't define what the 'sensor version' bits are, so I can't update the MS5637 library with the same changes... These changes mean I can test for an MS5837 first. If _model is MS5837_UNRECOGNISED then I know I'm almost certainly talking to a MS5637.

Anyway, I think we've ended up with a nice solution. Everything is backward-compatible. .init sets the _model automatically, but you can override it if you want to.

Cheers!

Paul

src/MS5837.cpp Outdated
_model = MS5837_02BA;
return true;
}
else if (version == 0x1A) // MS5837-30BA26
Copy link
Member

Choose a reason for hiding this comment

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

You could also break it to a else case

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Please see the comment above...

Wire.beginTransmission(MS5837_ADDR);
Wire.write(MS5837_CONVERT_D1_8192);
Wire.endTransmission();
_i2cPort->beginTransmission(MS5837_ADDR);
Copy link
Member

Choose a reason for hiding this comment

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

Check if i2cPort is not nullptr

Copy link
Contributor Author

Choose a reason for hiding this comment

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

src/MS5837.h Outdated
@@ -50,14 +51,16 @@ class MS5837 {

MS5837();

bool init();
bool init(TwoWire &wirePort = Wire);
bool begin(TwoWire &wirePort = Wire); // Calls init()
Copy link
Member

Choose a reason for hiding this comment

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

wrong indentation

Copy link
Contributor Author

Choose a reason for hiding this comment

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

src/MS5837.h Outdated

/** Set model of MS5837 sensor. Valid options are MS5837::MS5837_30BA (default)
* and MS5837::MS5837_02BA.
*/
void setModel(uint8_t model);
uint8_t getModel(void);
Copy link
Member

Choose a reason for hiding this comment

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

void in constructor is unnecessary in C++ code

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@patrickelectric
Copy link
Member

Hi @PaulZC, it appears that your PR is failing, can you rebase it over master branch ?

@PaulZC
Copy link
Contributor Author

PaulZC commented Feb 8, 2021

Done!
Best wishes,
Paul

@PaulZC
Copy link
Contributor Author

PaulZC commented Feb 17, 2021

Hi @patrickelectric ,
Are you able to merge this PR now?
Best wishes,
Paul

@jaxxzer
Copy link
Member

jaxxzer commented Mar 2, 2021

Bump @patrickelectric

@patrickelectric
Copy link
Member

Hi @PaulZC!
I did the final tests and everything appears to be working, to avoid more work from your part, I did a couple of corrections and organization on the commit history, they are already on our master branch.
Thanks for you contribution and sorry for the delay!

Copy link
Member

@patrickelectric patrickelectric left a comment

Choose a reason for hiding this comment

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

Merged manually.

@PaulZC
Copy link
Contributor Author

PaulZC commented Mar 3, 2021

No problem @patrickelectric (& @jaxxzer) - thank you!
All the best,
Paul

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