-
Notifications
You must be signed in to change notification settings - Fork 60
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
Conversation
…e port to use. Adding .begin Add wirePort as a parameter for .begin and .init Add _i2cPort (Private) Change Wire. to _i2cPort->
There was a problem hiding this 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)); |
There was a problem hiding this comment.
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 ?
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
else unnecessary after return
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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); |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure. OK. Resolved here:
2b5747d#diff-e98a2dd452878f22bfa9405f77a816ec8c6add4277c3d61c18ef3ed2a98ad598R91-R96
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() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
wrong indentation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pesky whitespace! Resolved here:
2b5747d#diff-fedaeb63fea75cce65adad3c7c284f5f797befa455548788021315c68d0f423cR55
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); |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
…ISED. .init returns true even if the sesnor version is unrecognised.
…el according to the sensor version.
…e sensor version if required
Hi @PaulZC, it appears that your PR is failing, can you rebase it over master branch ? |
Done! |
Hi @patrickelectric , |
Bump @patrickelectric |
Hi @PaulZC! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Merged manually.
No problem @patrickelectric (& @jaxxzer) - thank you! |
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:
init(TwoWire &wirePort = Wire);
. wirePort is optional and will default to Wire so all existing code will continue to work normally.begin
which simply calls initI 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