Skip to content

audio: New LevelDetectorSPL instance blocks at getValue() #503

@microbit-carlos

Description

@microbit-carlos

This programme creates a new instance of LevelDetectorSPL from the raw splitter (the same way as done in uBit.audio constructor), and every call to localLevelSPL->getValue() gets stuck. It seems to be yielding, so likely locked at the internal resourceLock.wait():

#include "MicroBit.h"

MicroBit uBit;

static LevelDetectorSPL *localLevelSPL = NULL;
const int DEVICE_ID_UNUSED = 50;

int localLevelGetValue() {
    if (localLevelSPL == NULL) {
        localLevelSPL = new LevelDetectorSPL(*(uBit.audio.rawSplitter->createChannel()), 85.0, 65.0, 16.0, 35.0f, DEVICE_ID_UNUSED);
    }
    uBit.serial.send("Before localLevelSPL->getValue()\n");
    int localLevelValue = localLevelSPL->getValue();
    uBit.serial.send("After localLevelSPL->getValue()\n");
    return localLevelValue;
}

int main() {
    uBit.init();

    uBit.messageBus.listen(MICROBIT_ID_BUTTON_A, MICROBIT_BUTTON_EVT_DOWN, [](MicroBitEvent) {
        uBit.serial.send("Button A: ", SYNC_SPINWAIT);
        uBit.serial.printf("%d\n", localLevelGetValue());
    });
    uBit.messageBus.listen(MICROBIT_ID_BUTTON_B, MICROBIT_BUTTON_EVT_DOWN, [](MicroBitEvent) {
        uBit.serial.send("Button B: ", SYNC_SPINWAIT);
        uBit.serial.printf("%d\n", localLevelGetValue());
    });

    while (true) {
        uBit.serial.send("Loop: ", SYNC_SPINWAIT);
        uBit.serial.printf("%d; %d\n",
            (int)uBit.audio.levelSPL->getValue(),
            localLevelGetValue()
        );
        uBit.sleep(200);
    }
}

The main() function infinite loop calls getValue, so do the button handlers, so we can see this output after pressing button A and B, and nothing more is printed. The uBit.serial.send("After localLevelSPL->getValue()") is never reached:

Loop: Before localLevelSPL->getValue()
Button A: Before localLevelSPL->getValue()
Button B: Before localLevelSPL->getValue()

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions