Skip to content

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

Merged
merged 4 commits into from
Jan 29, 2018

Conversation

tannewt
Copy link
Member

@tannewt tannewt commented Jan 16, 2018

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.

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.
@deshipu
Copy link

deshipu commented Jan 17, 2018

I wonder how feasible would be using this for the displays.

@tannewt
Copy link
Member Author

tannewt commented Jan 18, 2018

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.

@deshipu
Copy link

deshipu commented Jan 18, 2018

Speaking of is31fl3731, I think we should make it use the framebuf module, once it's available in all ports.

@tannewt
Copy link
Member Author

tannewt commented Jan 19, 2018

@ladyada has ideas for a framebuf alternative that we'll move our display drivers to once we get to it.

@deshipu would you mind reviewing this PR too? :-)


class _BoundStructArray:
"""
Actual array object that `StructArray` constructs on demand for
Copy link

Choose a reason for hiding this comment

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

Broken sentence?

Copy link
Member Author

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:
Copy link

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.

Copy link
Member Author

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
Copy link

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.

Copy link
Member Author

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):
Copy link

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.

Copy link
Member Author

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)
Copy link

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.

Copy link
Member Author

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()
Copy link

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.

Copy link
Member Author

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.

@tannewt
Copy link
Member Author

tannewt commented Jan 25, 2018

@deshipu please take another look!

@tannewt
Copy link
Member Author

tannewt commented Jan 29, 2018

@deshipu gaves the thumbs up over Discord.

@tannewt tannewt merged commit a4bc90c into adafruit:master Jan 29, 2018
tannewt pushed a commit to adafruit/Adafruit_CircuitPython_Bundle that referenced this pull request Feb 7, 2018
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