Skip to content

Commit 4f369bb

Browse files
committed
codal_app: Use uBit.audio.level for sound level detector.
And only register the event callback when the microphone is needed. This way the microphone is not initialised until needed by the code. And the settings are the standard ones defined by uBit. Fixes issue #107. Signed-off-by: Damien George <damien@micropython.org>
1 parent ca108e7 commit 4f369bb

File tree

2 files changed

+16
-28
lines changed

2 files changed

+16
-28
lines changed

src/codal_app/main.cpp

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ extern "C" void mp_main(void);
3232
extern "C" void m_printf(...);
3333
extern "C" void microbit_hal_timer_callback(void);
3434
extern "C" void microbit_hal_gesture_callback(int);
35-
extern "C" void microbit_hal_level_detector_callback(int);
3635
extern "C" void microbit_hal_sound_synth_callback(int);
3736
extern "C" void microbit_radio_irq_handler(void);
3837

@@ -46,10 +45,6 @@ void gesture_event_handler(Event evt) {
4645
microbit_hal_gesture_callback(evt.value);
4746
}
4847

49-
void level_detector_event_handler(Event evt) {
50-
microbit_hal_level_detector_callback(evt.value);
51-
}
52-
5348
void sound_synth_event_handler(Event evt) {
5449
microbit_hal_sound_synth_callback(evt.value);
5550
}
@@ -68,7 +63,6 @@ int main() {
6863
uBit.messageBus.listen(MICROPY_TIMER_EVENT, DEVICE_EVT_ANY, timer_handler, MESSAGE_BUS_LISTENER_IMMEDIATE);
6964
uBit.messageBus.listen(DEVICE_ID_SERIAL, CODAL_SERIAL_EVT_DELIM_MATCH, serial_interrupt_handler, MESSAGE_BUS_LISTENER_IMMEDIATE);
7065
uBit.messageBus.listen(DEVICE_ID_GESTURE, DEVICE_EVT_ANY, gesture_event_handler);
71-
uBit.messageBus.listen(DEVICE_ID_SYSTEM_LEVEL_DETECTOR, DEVICE_EVT_ANY, level_detector_event_handler);
7266
uBit.messageBus.listen(DEVICE_ID_SOUND_EMOJI_SYNTHESIZER_0, DEVICE_EVT_ANY, sound_synth_event_handler);
7367

7468
// 6ms follows the micro:bit v1 value

src/codal_app/microbithal_microphone.cpp

Lines changed: 16 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -28,44 +28,38 @@
2828
#include "microbithal.h"
2929
#include "MicroBitDevice.h"
3030

31-
extern "C" {
32-
3331
#define SOUND_LEVEL_MAXIMUM (20000)
3432

35-
static NRF52ADCChannel *mic = NULL;
36-
static StreamNormalizer *processor = NULL;
37-
static LevelDetector *level = NULL;
33+
extern "C" void microbit_hal_level_detector_callback(int);
3834

39-
void microbit_hal_microphone_init(void) {
40-
if (mic == NULL) {
41-
mic = uBit.adc.getChannel(uBit.io.microphone);
42-
mic->setGain(7, 0);
35+
static void level_detector_event_handler(Event evt) {
36+
microbit_hal_level_detector_callback(evt.value);
37+
}
4338

44-
processor = new StreamNormalizer(mic->output, 0.05, true, DATASTREAM_FORMAT_8BIT_SIGNED);
45-
level = new LevelDetector(processor->output, 600, 200);
39+
extern "C" {
4640

47-
uBit.io.runmic.setDigitalValue(1);
48-
uBit.io.runmic.setHighDrive(true);
41+
static bool microphone_init_done = false;
42+
43+
void microbit_hal_microphone_init(void) {
44+
if (!microphone_init_done) {
45+
microphone_init_done = true;
46+
uBit.messageBus.listen(DEVICE_ID_SYSTEM_LEVEL_DETECTOR, DEVICE_EVT_ANY, level_detector_event_handler);
4947
}
5048
}
5149

5250
void microbit_hal_microphone_set_threshold(int kind, int value) {
5351
value = value * SOUND_LEVEL_MAXIMUM / 255;
5452
if (kind == 0) {
55-
level->setLowThreshold(value);
53+
uBit.audio.level->setLowThreshold(value);
5654
} else {
57-
level->setHighThreshold(value);
55+
uBit.audio.level->setHighThreshold(value);
5856
}
5957
}
6058

6159
int microbit_hal_microphone_get_level(void) {
62-
if (level == NULL) {
63-
return -1;
64-
} else {
65-
int l = level->getValue();
66-
l = min(255, l * 255 / SOUND_LEVEL_MAXIMUM);
67-
return l;
68-
}
60+
int l = uBit.audio.level->getValue();
61+
l = min(255, l * 255 / SOUND_LEVEL_MAXIMUM);
62+
return l;
6963
}
7064

7165
}

0 commit comments

Comments
 (0)