Skip to content
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

Implement microphone.sound_level_db() method. #221

Merged
merged 2 commits into from
Sep 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/codal_app/microbithal.h
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,7 @@ int microbit_hal_compass_get_heading(void);
void microbit_hal_microphone_init(void);
void microbit_hal_microphone_set_threshold(int kind, int value);
int microbit_hal_microphone_get_level(void);
float microbit_hal_microphone_get_level_db(void);

const uint8_t *microbit_hal_get_font_data(char c);

Expand Down
7 changes: 7 additions & 0 deletions src/codal_app/microbithal_microphone.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,11 @@ int microbit_hal_microphone_get_level(void) {
return value;
}

float microbit_hal_microphone_get_level_db(void) {
uBit.audio.levelSPL->setUnit(LEVEL_DETECTOR_SPL_DB);
float value = uBit.audio.levelSPL->getValue();
uBit.audio.levelSPL->setUnit(LEVEL_DETECTOR_SPL_8BIT);
return value;
}

}
8 changes: 8 additions & 0 deletions src/codal_port/microbit_microphone.c
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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(&microbit_microphone_set_threshold_obj) },
{ MP_ROM_QSTR(MP_QSTR_sound_level), MP_ROM_PTR(&microbit_microphone_sound_level_obj) },
{ MP_ROM_QSTR(MP_QSTR_sound_level_db), MP_ROM_PTR(&microbit_microphone_sound_level_db_obj) },
{ MP_ROM_QSTR(MP_QSTR_current_event), MP_ROM_PTR(&microbit_microphone_current_event_obj) },
{ MP_ROM_QSTR(MP_QSTR_is_event), MP_ROM_PTR(&microbit_microphone_is_event_obj) },
{ MP_ROM_QSTR(MP_QSTR_was_event), MP_ROM_PTR(&microbit_microphone_was_event_obj) },
Expand Down
Loading