Skip to content

Conversation

dok-net
Copy link
Contributor

@dok-net dok-net commented May 28, 2022

It's usual for Arduino driver libs to have a constructor suitable for storage class static, and a begin() method that's called from setup(). The I2C address can thus be set in either way.
This PR adds that function.

@blemasle
Copy link
Owner

Hi, and thanks for the contribution 🙂

I'm not sure about this one though. As stated in the README

Unlike most Arduino library, no default instance is created when the library is included

The usual begin method is here to compensate for the lack of a proper constructor because libraries usually declare a unique, static instance.

I agree that the I2C address can be set either way with your PR, but it also means that you can create an instance and start using it without ever setting the address, which would be error prone. It also means that you can set this address using the constructor, and then use begin() without any argument which will overwrite that value.

Moreover, most of the libs I've seen use begin() to actually begin the communication with the device/port. Eg Serial.begin actually opens the serial port. Here, nothing would "begin" when you call that function.

What do you think ? 🙂

@dok-net
Copy link
Contributor Author

dok-net commented May 31, 2022

Hi. Obviously, I have a different take :-)
First, the current pattern isn't obsoleted by my patch.
Second, there are libraries don't create a global default instance and still offer begin().
Third, I don't subscribe to the danger argument, as when using the ctor without a specified address value,
the default address gets set, that is, the base I2C address when the address lines are pulled down, which I would
like to believed is used a lot in applications. It's only after begin() or init() that there is an actual attempt to access the device by that address.

As for

It also means that you can set this address using the constructor, and then use begin() without any argument which will overwrite that value.

You're right, I missed that, I have fixed that now.

Is this PR better suited for inclusion now?

Copy link
Owner

@blemasle blemasle left a comment

Choose a reason for hiding this comment

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

Alright, you convinced me 🙂 Despite the fact that I still think the begin() pattern used by a lot of libraries is misleading and confusing, I get the "everything else works that way" argument.

Would you review the suggestions I made though ?

Cascade from `begin` with I2C address argument to `begin` w/o argument, it's clearer than calling `init()` from both places.

Co-authored-by: Bertrand Lemasle <[email protected]>
@dok-net
Copy link
Contributor Author

dok-net commented Jun 2, 2022

Thanks for the nice review feedback.

@blemasle blemasle merged commit 05f4c6d into blemasle:master Jun 4, 2022
@dok-net dok-net deleted the addbegin branch June 5, 2022 10:36
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