Skip to content
Discussion options

You must be logged in to vote

I would use a ThreadSafeQueue. This simplifies the code and avoids needless RAM allocations. See the doc. Something along these lines:

from machine import Pin, ADC, Timer
import array
import uasyncio as asyncio
from threadsafe import ThreadSafeQueue

adc = ADC(Pin(1))
timer = Timer(0)

buffer_size = 100
buffer = array.array('H')

tsq = ThreadSafeQueue(buffer)

timer.init(freq=10_000, mode=Timer.PERIODIC, callback= isr)

def isr(_):  # Interrupt handler
    tsq.put_sync(adc.read_u16())  # Put ADC value on queue

async def run():
    async for item in tsq:
        # process the data item

asyncio.run(run())

This offers no protection against items arriving faster than you can process them. T…

Replies: 2 comments 7 replies

Comment options

You must be logged in to vote
1 reply
@saraverbeecke
Comment options

Comment options

You must be logged in to vote
6 replies
@peterhinch
Comment options

@saraverbeecke
Comment options

@peterhinch
Comment options

@SeregaZ2004
Comment options

@peterhinch
Comment options

Answer selected by saraverbeecke
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
4 participants