add looping to rp2040 analogbufio.BufferedIn #9438
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This PR contains part of the changes necessary to implement the processing of live sampled audio as described in #2676. Here
analogbufio.BufferedIn
is modified to add aloop
keyword argument toBufferedIn.readinto()
. If called without the keyword or withloop=False
, behavior is identical to the currentreadinto()
. If called withloop=True
,readinto()
sets up chained DMA which continuously writes ADC values into the buffer.The contrast between the two modes can be seen by plotting the output of test code listed here:
Here is a plot of the data output by the first print loop when pin
A0
is driven by a 1 kHz sine wave:The buffer is plotted 4 times, but you can only see one (green) series, because when
loop=False
the buffer is static. Compare the plot generated from the second print loop:The slight scatter between the series shows that the data in the buffer is different in each of the 4 print sequences. If the print loop is repeated again, the plot is similar, but has drifted in phase due to small differences between the clock rates of the ADC and the signal generator:
These plots are deceptively simple, because the signal frequency was chosen to put exactly one period of the signal in each buffer. If you change the frequency to 500 Hz instead the complications can be seen:
During the ~241 ms required to print the buffer 4 times, it has been refilled by DMA 241 times, alternating between the upward-going half of the sine wave and the downward-going half. Which half shows up at each printed sample will depend upon when exactly in the sequence that particular sample value was observed.
The other difference between
loop=False
andloop=True
is that whilereadinto(buffer, loop=False)
blocks until DMA is complete, then scales the ADC values to 16 bits before returning,readinto(buffer, loop=True)
returns immediately after starting DMA, and the buffer values are raw unscaled ADC data.