Black Lives Matter - Action and Equality. ... Adafruit is open and shipping.
0

CircularBuffer equivalent in CircuitPython?
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

CircularBuffer equivalent in CircuitPython?

by kevinjwalters on Fri Apr 03, 2020 11:25 am

I was looking at the Arudino example for the plotting recently and noticed it using the CircuitBuffer library. I ended up doing something similar but more messy using array.array for underlying storage.

A discussion came up on this when I was implementing the receive parts of the MIDI library as the buffer it uses ends up doing a lot of growing and shrinking as it parses messages from the incoming data. I think this may be being reworked to use a one-off allocated, fixed size buffer and a "no copy/move data" approach like the CircularBuffer.

I think data in one of the ulab examples is also implementing the same thing but is probably still doing a copy albeit with greater efficiency as the implementation is C.

Does CircuitPython have anything like CircularBuffer? Has anyone else noted a need for this?

kevinjwalters
 
Posts: 634
Joined: Sun Oct 01, 2017 3:15 pm

Re: CircularBuffer equivalent in CircuitPython?

by dastels on Fri Apr 03, 2020 2:53 pm

This (https://gist.github.com/edwintcloud/f998e15d839e17ebcae2b8e2bb1d8d8c) is a decent looking implementation, I've loaded it as is into CircuitPython (on a Feather M4 Express) and it works fine as is.

Dave

dastels
 
Posts: 2829
Joined: Tue Oct 20, 2015 3:22 pm

Re: CircularBuffer equivalent in CircuitPython?

by tannewt on Fri Apr 03, 2020 4:00 pm

I've been thinking about this. I think the CPython equivalent is a `deque`: https://docs.python.org/3.8/library/col ... ions.deque

I don't believe it implements the buffer protocol though which would be nice. That way it could be used in places where data is read into a buffer.

tannewt
 
Posts: 1685
Joined: Thu Oct 06, 2016 8:48 pm

Re: CircularBuffer equivalent in CircuitPython?

by kevinjwalters on Sat Apr 04, 2020 7:46 am

@dastels thanks for the example.

Might be worth trying to gauge existing use and potential demand for this to see if it's worth implementing within the interpreter. Something where the storage can be chosen and/or passed in would be good. For choice, a list of numbers or bytes or array.array and ndarray/ulab-efficient compatibility sounds useful. All worth some more thought and wider input, particularly for real, existing use cases.

kevinjwalters
 
Posts: 634
Joined: Sun Oct 01, 2017 3:15 pm

Please be positive and constructive with your questions and comments.