Execute a function with an infinite loop via ir_rx #11094
Replies: 15 comments 8 replies
-
Could you please put three back ticks before and after the code? |
Beta Was this translation helpful? Give feedback.
-
Which In this case, the code looks OK except for if q == 44 where this comparison would throw an exception. You'd need something like async def blink(q):
while True:
if not q.empty():
print(await q.get()) |
Beta Was this translation helpful? Give feedback.
-
I used the queue module, but now I want to test the primitives one. Which 'primitives' do you have to install on the Pico? Edit: If I then the error comes File "/lib/primitives/init.py", line 2, in |
Beta Was this translation helpful? Give feedback.
-
I don't recognise
|
Beta Was this translation helpful? Give feedback.
-
Where do I enter the commands? |
Beta Was this translation helpful? Give feedback.
-
The command mp or mpr are synonyms of mpremote. They are executed on the PC where the device is connected. |
Beta Was this translation helpful? Give feedback.
-
Ahh ok. |
Beta Was this translation helpful? Give feedback.
-
OK that worked, but the code doesn't work like this yet.
The edition: hello world |
Beta Was this translation helpful? Give feedback.
-
[EDIT] Please see below. |
Beta Was this translation helpful? Give feedback.
-
There is also a more subtle issue here. The callback from the micropython_ir library runs in a soft ISR context because it is launched by a soft timer callback [code]. This means it can pre-empt The callback should not launch tasks. Further, the standard from threadsafe import ThreadSafeQueue
from machine import Pin
from ir_rx import NEC_16
def callback(data, addr, ctrl, qu): # Runs in a soft ISR context
if data > 0: # NEC protocol sends repeat codes.
print('Data {:02x} Addr {:04x}'.format(data, addr))
if not qu.full():
qu.put_sync(data)
async def blink(q):
async for data in q: # Task will pause until data available
print("Hello world")
print(data)
async def main():
q = ThreadSafeQueue([0 for _ in range(20)])
await q.put(1)
ir = NEC_16(Pin(16, Pin.IN), callback, q)
await blink(q)
uasyncio.run(main()) |
Beta Was this translation helpful? Give feedback.
-
Since I get the error that int does not contain 'len': Traceback (most recent call last): |
Beta Was this translation helpful? Give feedback.
-
Thanks, that worked fine. |
Beta Was this translation helpful? Give feedback.
-
292 / 5.000 My goal is to have different functions where some of them also have an infinite loop.
|
Beta Was this translation helpful? Give feedback.
-
I'm still not entirely sure what you have in mind. Something like this? tasks = []
async def check_queue(q): # Runs forever
async for data in q:
if data == 70:
for task in tasks:
task.cancel()
async def blink():
while True:
await asyncio.sleep(1)
print("Hello world")
async def main():
q = ThreadSafeQueue([0 for _ in range(20)])
await q.put(1)
ir = NEC_16(Pin(6, Pin.IN), callback, q)
tasks.append(asyncio.create_task(blink) # Could add other tasks
await check_queue(q) |
Beta Was this translation helpful? Give feedback.
-
Thank you very much, that's exactly what I was looking for.
It probably works in python3 too, doesn't it? |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Hello, I would like to use ir_rx in the callback function to call a function that has an endless loop, but send another command to the function depending on the state of the key pressed.
My code shown below doesn't work.
How can you do that?
Beta Was this translation helpful? Give feedback.
All reactions