-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Add default buses to board #372
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
Comments
I gently want to suggest that we revisit the idea of I'll take
you can do:
But suppose you later decide you want to use different pins? The singleton call doesn't take pin args, so you don't do this (which is too verbose anyway):
Instead you need to go back to the original way and rewrite it as You might argue that instead one should just assign the value:
but in that case, I think the better API is to have
so that you can just do:
It's true you lose the convenience of the singleton, but you don't need to specify the pin names or remember their order, and you don't need to The motivation for the original is issue #340, which was much more about Tagging @matt-land, @tannewt, @deshipu, @caternuson, and @tannewt for comments. (This bothered me enough that it woke me up. On the other hand, my middle-of-the-night thoughts might not be so coherent.) |
Of course I would much prefer the solution proposed here by @dhalbert. Not only will it use less memory when you are not using I2C, not only adding it will result in smaller code size, but also it has the advantage of using an existing Python mechanism, which everybody will have to learn about anyways, instead of being something entirely new created specially for the purpose, which has to be learned separately from the documentation. |
One case this is designed to ease is initializing multiple I2C devices on the same bus, which is common when using multiple feather wings. With a singleton in d1 = device1.Device1()
d2 = device2.Device2() With the proposed default args we'd need: bus = busio.I2C()
d1 = device1.Device1(bus)
d2 = device2.Device2(bus) The second option does make it easier to change the pins but how common will this be? With featherwings in particular, this is highly unlikely. Are we ok with the extra step of the second option? Is the first too magical? I think it'd be tricky to have a shared default bus like the first option without having it built into a core module such as |
Can't we cache the bus, so that creating a bus with the same parameters just gets you the same bus back? I think that's what MicroPython does, anyways. |
I was composing a message with the same idea the @deshipu just posted. Suppose
will throw a "Pin xxx in use" right now. We could have |
Ok, that's fine with me. I think it'll be a bit tricky because the memory is allocated in |
Yes, this would be easier to implement in Python than C 😃 |
As an easier solution, we could cache only the argument-less call. |
That might be kind of confusing, since then sometimes a double call would work and sometimes not. I think it might not be too hard to keep track. The I2C objects already keep the pins, so we just have to look over the existing objects. Just have to keep a list of them. We could even use list primitives to manage the storage if we don't want to manage it in a C way on the heap. |
And expose the list as |
I'm now thinking about the details of implementing cached bus objects. The |
Throwing an error on mismatched settings is ok with me. |
After thinking about the implementation of this for several days and talking with @tannewt, I've changed my mind and decided that I will add some documentation in the various Having the singletons come from Example from CircuitPython Essentials Guide:
becomes
|
Fixed by #841, #844, #845. Thanks @matt-land!! |
From the discussion raised in this issue, w.r.t I2C bus:
#340
The text was updated successfully, but these errors were encountered: