-
Notifications
You must be signed in to change notification settings - Fork 23
Introduce StructArray that can be used for repeated register #10
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
Conversation
structures. This is better than a tuple of objects because it creates fewer objects by default and is a fixed cost per device independent of the number of entries in the array.
I wonder how feasible would be using this for the displays. |
its worth thinking about. I did something similar in the experimental is31fl3731 drivers here: https://github.com/adafruit/Adafruit_CircuitPython_IS31FL3731/blob/experimental/adafruit_is31fl3731.py#L132 It doesn't handle 2D slices as far as I know though. |
Speaking of is31fl3731, I think we should make it use the framebuf module, once it's available in all ports. |
|
||
class _BoundStructArray: | ||
""" | ||
Actual array object that `StructArray` constructs on demand for |
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.
Broken sentence?
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.
Fixed.
|
||
def _header(self, index): | ||
"""Shared bounds checking and buffer creation.""" | ||
if index < 0 or index >= self.count: |
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.
I would prefer if not 0 <= index < self.count:
here.
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.
Done
# We create the buffer every time instead of keeping the buffer (which is 32 bytes at least) | ||
# around forever. | ||
buf = bytearray(size + 1) | ||
buf[0] = self.first_register + size * index |
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.
Not sure this is correct. Chips that have 16 or 32 bit registers don't usually address them with gaps. If the first_register
is 40, then the second will be 41, even if the register is 16-bit.
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.
I added a note warning about this.
self.obj = obj | ||
self.count = count | ||
|
||
def _header(self, index): |
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.
Maybe this would be better called _get_buffer
or something like that? _header
doesn't really say much.
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.
Done. Good idea
self.format = struct_format | ||
self.address = register_address | ||
self.count = count | ||
self.array_id = "_structarray" + str(register_address) |
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.
.format()
would be more readable here.
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.
Done
return getattr(obj, self.array_id) | ||
|
||
def __set__(self, obj, value): | ||
raise RuntimeError() |
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.
I think you can simply not define this, and then the property will be read-only.
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.
Done. That makes it a non-data descriptor (according to this) but that should be ok.
@deshipu please take another look! |
@deshipu gaves the thumbs up over Discord. |
Updating https://github.com/adafruit/Adafruit_CircuitPython_PCA9685 to 3.1.0 from 3.0.0: > Merge pull request adafruit/Adafruit_CircuitPython_PCA9685#10 from tannewt/struct_array Updating https://github.com/adafruit/Adafruit_CircuitPython_Register to 1.2.0 from 1.1.0: > Merge pull request adafruit/Adafruit_CircuitPython_Register#9 from tannewt/struct_opt > Merge pull request adafruit/Adafruit_CircuitPython_Register#10 from tannewt/struct_array
structures. This is better than a tuple of objects because it
creates fewer objects by default and is a fixed cost per device
independent of the number of entries in the array.