Skip to content

Add type annotation protocols for LEDs #11

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

Merged
merged 2 commits into from
Apr 7, 2022

Conversation

tekktrik
Copy link
Member

Useful for typing LEDs without having to import them. Spawned from typing adafruit_esp32spi.

@tekktrik tekktrik added documentation Improvements or additions to documentation enhancement New feature or request labels Mar 25, 2022
@tekktrik tekktrik requested a review from a team March 25, 2022 13:33
@tekktrik
Copy link
Member Author

If this isn't widely used enough, I could also just type it in the relevant library

@dhalbert
Copy link
Contributor

If this isn't widely used enough, I could also just type it in the relevant library

If it's used in more than one library, it's good to have it in a central place. Otherwise you can put the definitions inside the try block for the typing imports. They will take space in a library, which is one reason not to include them in libraries.

@tekktrik
Copy link
Member Author

If they get caught (or missed, really) because of the try/except block, that should prevent them from using memory though, right? Either way, I'll try to raise it on a case-by-case basis then

@dhalbert
Copy link
Contributor

Right, it's static code size, either in the filesystem, or in a frozen module.

@tekktrik tekktrik marked this pull request as draft March 28, 2022 15:26
@tekktrik
Copy link
Member Author

Converted to draft for now. There may be other uses of this in the libraries but I'm not sure... I'll take a look and see if it's worth putting here or not!

@tekktrik tekktrik marked this pull request as ready for review March 28, 2022 18:33
@tekktrik
Copy link
Member Author

Okay, I found at least a few libraries that use Protocol for LEDs in their library. Basing those types here seems to be a good idea in this case.

Copy link
Contributor

@FoamyGuy FoamyGuy left a comment

Choose a reason for hiding this comment

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

Makes sense to me to have this as a Protocol, I believe I've seen this concept used in multiple places and I'm sure there are likely more that I am unaware of.

Looks good to me.

I am more familiar with the ones that use fill(). I don't recall seeing any that use color() but they're probably among the ones I am unaware of.

I'm curious if you know about square bracket accessing in protocols? I believe neopixel and similar LEDs with a fill() function also typically support things like:

led_strip_obj[0] = 0x00ff00

to set an individual LED by it's index to a given color. I don't know if it's possible to express this in a typing protocol but if so it might be nice to include that as well.

@tekktrik
Copy link
Member Author

tekktrik commented Apr 4, 2022

Looks like you'd need to define __getitem__ and/or __setitem__ in the protocol so yup!

@tekktrik
Copy link
Member Author

tekktrik commented Apr 6, 2022

@FoamyGuy do you know of any libraries that would need that set in the Protocol? I don't necessarily want to over constrain the protocols if where they are doesn't require that specific behavior (ex: the only need to generically describe fill()/color().)

Copy link
Contributor

@FoamyGuy FoamyGuy left a comment

Choose a reason for hiding this comment

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

https://github.com/adafruit/Adafruit_CircuitPython_led_animation

and the board specific helper libraries like:
https://github.com/adafruit/Adafruit_CircuitPython_PyBadger
https://github.com/adafruit/Adafruit_CircuitPython_CircuitPlayground
https://github.com/adafruit/Adafruit_CircuitPython_NeoTrellis

are the ones that come to mind that use neopixels. I'm not sure that any of them would specifically need this functionality though. The places I did see it typed so far looking around have it as NeoPixel. I think it makes sense to use that in these contexts instead of a protocol. So yeah, probably not worth adding the set_item. We can always do it later if a need arises.

@tekktrik
Copy link
Member Author

tekktrik commented Apr 7, 2022

Sounds good!

@tekktrik tekktrik merged commit 982dee3 into adafruit:main Apr 7, 2022
@tekktrik tekktrik deleted the dev/led-types branch December 1, 2022 03:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants