Skip to content

read_pulses() still blocks #32

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

Closed
mfeif opened this issue Feb 29, 2020 · 6 comments
Closed

read_pulses() still blocks #32

mfeif opened this issue Feb 29, 2020 · 6 comments

Comments

@mfeif
Copy link
Contributor

mfeif commented Feb 29, 2020

I've read through #18 after having trouble reading repeat commands from my IR remote. Here's some code to illustrate what I'm seeing, which is that while the remote is "clicked" on anything, the loop gets stuck:

(this is a light change from the bundled example file in this repo)

import pulseio
import board
import adafruit_irremote

pin = board.D17

pulsein = pulseio.PulseIn(pin, maxlen=120, idle_state=True)
decoder = adafruit_irremote.GenericDecode()

while True:
    pulses = decoder.read_pulses(pulsein, blocking=False)
    if pulses:
        print("\nHeard", len(pulses), "Pulses:", pulses, flush=True)
        try:
            code = decoder.decode_bits(pulses)
            print("Decoded:", code)
        except adafruit_irremote.IRNECRepeatException:
            pass
        except adafruit_irremote.IRDecodeException as e:
            pass
    else:
        print('.', end='', flush=True)

If you run this, you'll see a flood of "." filling the console, as expected, but as soon as you put some activity on the pin, the flood will pause. This will pause indefinitely... I held down for 10+ seconds, and it blocked. When I let go, only one event was returned.

I tried to reason through _read_pulses_non_blocking() in adafruit_irremote.py but I'm having trouble, since I don't really know what it's trying to do, so I can't spot the bug in the logic there.

I've also seen Kevin's work here that includes a timeout feature that seems to taking a stab at the same thing. It works, and even supports the repeat exception that in the example code.

@mfeif
Copy link
Contributor Author

mfeif commented Mar 25, 2020

I now have an ItsyBitsy M4, and I'm seeing the same thing as I saw above on my rPi.

It still blocks while a button is pressed down.

@majuss
Copy link

majuss commented Jun 6, 2020

Same problem, sadly this lib is unusable in the current state.

@danielballan
Copy link
Contributor

I can reproduce this. A continuous stream of pulses (caused by holding down a button the remote) causes the read_pulses to block even when blocking=False is set. I modified the irremote_simple.py to add timing output as follows

$ diff examples/irremote_simpletest.py /media/dallan/CIRCUITPY/code.py 
8a9
> import time
15c16,22
<     pulses = decoder.read_pulses(pulsein)
---
>     t0 = time.monotonic()
>     pulses = decoder.read_pulses(pulsein, blocking=False)
>     t1 = time.monotonic()
>     if t1 - t0 > 0.02:  # 20 ms
>         print(f"Blocked for {t1 - t0:.2} seconds")
>     if pulses is None:
>         continue

The output from a short button press looks OK:

code.py output:
Blocked for 0.4 seconds
Heard 41 Pulses: [2422, 579, 1224, 569, 626, 568, 1224, 568, 1224, 568, 1225, 567, 1225, 568, 627, 567, 628, 566, 1226, 567, 628, 566, 1226, 566, 1226, 567, 628, 566, 1226, 566, 629, 566, 629, 565, 630, 564, 1228, 565, 1227, 565, 1227]
Decoded: [67, 75, 8]
----------------------------

but the output from holding down the button reproduces the issue described above.

Blocked for 7.8 seconds
Heard 41 Pulses: [2434, 568, 1223, 570, 625, 569, 1234, 559, 1233, 559, 1233, 560, 1233, 560, 634, 560, 625, 569, 1234, 559, 636, 559, 1233, 559, 1233, 560, 635, 560, 1232, 561, 634, 560, 635, 560, 625, 569, 1223, 569, 1233, 559, 1233]
Decoded: [67, 75, 8]
----------------------------

I have an idea of how to fix this. PR forthcoming...

@danielballan
Copy link
Contributor

My fix works well. See #42.

@danielballan
Copy link
Contributor

I think it safe to close this now that #42 is in.

@tannewt
Copy link
Member

tannewt commented Jun 22, 2021

Thanks @danielballan!

@tannewt tannewt closed this as completed Jun 22, 2021
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

5 participants