From 703abc864df235945d54ede403c496c90760fc49 Mon Sep 17 00:00:00 2001 From: Damien George Date: Mon, 9 Sep 2024 12:07:45 +1000 Subject: [PATCH] codal_port/microbit_microphone: Add microphone.sound_level_db() method. This returns a floating-point number in dB. Signed-off-by: Damien George --- src/codal_port/microbit_microphone.c | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/codal_port/microbit_microphone.c b/src/codal_port/microbit_microphone.c index 338f2e0..cf37069 100644 --- a/src/codal_port/microbit_microphone.c +++ b/src/codal_port/microbit_microphone.c @@ -109,6 +109,13 @@ static mp_obj_t microbit_microphone_sound_level(mp_obj_t self_in) { } static MP_DEFINE_CONST_FUN_OBJ_1(microbit_microphone_sound_level_obj, microbit_microphone_sound_level); +static mp_obj_t microbit_microphone_sound_level_db(mp_obj_t self_in) { + (void)self_in; + microphone_init(); + return mp_obj_new_float_from_f(microbit_hal_microphone_get_level_db()); +} +static MP_DEFINE_CONST_FUN_OBJ_1(microbit_microphone_sound_level_db_obj, microbit_microphone_sound_level_db); + static mp_obj_t microbit_microphone_current_event(mp_obj_t self_in) { (void)self_in; microphone_init(); @@ -154,6 +161,7 @@ static MP_DEFINE_CONST_FUN_OBJ_1(microbit_microphone_get_events_obj, microbit_mi static const mp_rom_map_elem_t microbit_microphone_locals_dict_table[] = { { MP_ROM_QSTR(MP_QSTR_set_threshold), MP_ROM_PTR(µbit_microphone_set_threshold_obj) }, { MP_ROM_QSTR(MP_QSTR_sound_level), MP_ROM_PTR(µbit_microphone_sound_level_obj) }, + { MP_ROM_QSTR(MP_QSTR_sound_level_db), MP_ROM_PTR(µbit_microphone_sound_level_db_obj) }, { MP_ROM_QSTR(MP_QSTR_current_event), MP_ROM_PTR(µbit_microphone_current_event_obj) }, { MP_ROM_QSTR(MP_QSTR_is_event), MP_ROM_PTR(µbit_microphone_is_event_obj) }, { MP_ROM_QSTR(MP_QSTR_was_event), MP_ROM_PTR(µbit_microphone_was_event_obj) },