microbit-foundation / micropython-microbit-v2

Temporary home for MicroPython for micro:bit v2 as we stablise it before pushing upstream
MIT License
43 stars 24 forks source link

microphone.is_event() stops responding after a few events #33

Closed martinwork closed 3 years ago

martinwork commented 3 years ago

This program sees LOUD at startup. After 3 more loud events, it's stuck on QUIET.

from microbit import *

display.clear()

while True:
    if microphone.is_event(SoundEvent.LOUD):
        display.show(Image.SQUARE)
    elif microphone.is_event(SoundEvent.QUIET):
        display.show(Image.SQUARE_SMALL)
    else:
        display.clear()
microbit-carlos commented 3 years ago

Thanks Martin, I can replicate this in MicroPython 2.0.0-beta.1.

Could you try the same programme in CODAL C++ and see if it replicates there as well?

dpgeorge commented 3 years ago

This is related to the semantics of microphone events, #36 . The point is that the event history (L from that issue) gets full and so no more events come in and it gets stuck (until microphone.get_events() is called).

To make this work try:

from microbit import *

display.clear()

while True:
    if microphone.was_event(SoundEvent.LOUD):
        display.show(Image.SQUARE)
    elif microphone.was_event(SoundEvent.QUIET):
        display.show(Image.SQUARE_SMALL)
martinwork commented 3 years ago

I don't know how to replicate this in CODAL. I've only seen the LEVEL_THRESHOLD_HIGH/LOW events and can test "is loud" with levelDetector->getValue() > levelDetector->getHighThreshold().

I was testing is_event, so can't use was_event instead!

I was expecting is_event and was _event to behave like button is_pressed and was_pressed, but looking at #36, I think the test shows is_event is behaving as intended.

dpgeorge commented 3 years ago

This should be fixed by c57910ce1d6957dda43d5a8e4596d511be20b446

martinwork commented 3 years ago

Thanks @dpgeorge . It doesn't get stuck now.