Skip to content
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

Unblocking analogbufio #9225

Closed
timchinowsky opened this issue May 4, 2024 · 2 comments
Closed

Unblocking analogbufio #9225

timchinowsky opened this issue May 4, 2024 · 2 comments

Comments

@timchinowsky
Copy link

Currently analogbufio.BufferedIn blocks until the number of samples requested have been collected. I'm working on a non-blocking ring buffered version which will enable continuous data transfer. It could work like this:

>>> length = 60
>>> mybuffer = array.array("H", [0x0000] * length)
>>> rate = 10
>>> # create a RingIn object and start reading the ADC into it via DMA
>>> adcbuf = analogbufio.RingIn(board.GP26, sample_rate=rate, mybuffer) 
>>> adcbuf.count
25
>>> adcbuf.count  # this will keep going up and up at 10 Hz
37
>>> adcbuf.last  # this is the last index of the buffer that was written to
55 
>>> adcbuf.last  # it will wrap around according to the length of the buffer
10
>>> mybuffer[adcbuf.last]  # this will return the most recent ADC conversion (as of the time that adcbuf.last executes)
124 
>>> del mybuffer # needs to tolerate this, so we make sure the RingIn still refers to the buffer object
>>> adcbuf.ring[adcbuf.last] # maybe like this
>>> 127

Or the RingIn could allocate the array itself, e.g. adcbuf = analogbufio.RingIn(board.GP26, sample_rate=rate, length=length) Wondering what advice you could offer regarding creation and management of persistent objects like this?

@dhalbert dhalbert added this to the Long term milestone May 13, 2024
@tannewt
Copy link
Member

tannewt commented May 13, 2024

Generally I like the model of allocating the large chunk of memory outside of the object. That way you can reuse it in a different object if you really want to.

In the Python API, I would expose the indices though. Instead, make an iterator available to get the next value. Take a look at PulseIn for an example.

I'd also suggest adding this to a new module so we don't need to include it everywhere analogbufio is.

@timchinowsky
Copy link
Author

Closing in favor of #2676.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants