Skip to content

Commit

Permalink
codal_port/microbit_soundevent: Add SoundEvent.CLAP object.
Browse files Browse the repository at this point in the history
The CODAL will emit a clap event if a "clap" is detected, and this new
SoundEvent.CLAP object can be used to detect that.

Signed-off-by: Damien George <[email protected]>
  • Loading branch information
dpgeorge committed May 20, 2024
1 parent 41245c8 commit 8b8c1d3
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/codal_app/microbithal.h
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ extern "C" {
// Microphone events, passed to microbit_hal_level_detector_callback().
#define MICROBIT_HAL_MICROPHONE_EVT_THRESHOLD_LOW (1)
#define MICROBIT_HAL_MICROPHONE_EVT_THRESHOLD_HIGH (2)
#define MICROBIT_HAL_MICROPHONE_EVT_CLAP (8)

// Threshold kind, passed to microbit_hal_microphone_set_threshold().
#define MICROBIT_HAL_MICROPHONE_SET_THRESHOLD_LOW (0)
Expand Down
4 changes: 4 additions & 0 deletions src/codal_port/microbit_microphone.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@

#define SOUND_EVENT_QUIET (0)
#define SOUND_EVENT_LOUD (1)
#define SOUND_EVENT_CLAP (2)

typedef struct _microbit_microphone_obj_t {
mp_obj_base_t base;
Expand All @@ -40,6 +41,7 @@ typedef struct _microbit_microphone_obj_t {
static const mp_const_obj_t sound_event_obj_map[] = {
[SOUND_EVENT_QUIET] = MP_ROM_PTR(&microbit_soundevent_quiet_obj),
[SOUND_EVENT_LOUD] = MP_ROM_PTR(&microbit_soundevent_loud_obj),
[SOUND_EVENT_CLAP] = MP_ROM_PTR(&microbit_soundevent_clap_obj),
};

static uint8_t sound_event_current = SOUND_EVENT_QUIET;
Expand All @@ -54,6 +56,8 @@ void microbit_hal_level_detector_callback(int value) {
ev = SOUND_EVENT_QUIET;
} else if (value == MICROBIT_HAL_MICROPHONE_EVT_THRESHOLD_HIGH) {
ev = SOUND_EVENT_LOUD;
} else if (value == MICROBIT_HAL_MICROPHONE_EVT_CLAP) {
ev = SOUND_EVENT_CLAP;
} else {
// Ignore unknown events.
return;
Expand Down
6 changes: 6 additions & 0 deletions src/codal_port/microbit_soundevent.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ const microbit_soundevent_obj_t microbit_soundevent_quiet_obj = {
MP_QSTR_quiet,
};

const microbit_soundevent_obj_t microbit_soundevent_clap_obj = {
{ &microbit_soundevent_type },
MP_QSTR_clap,
};

STATIC void microbit_soundevent_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
microbit_soundevent_obj_t *self = (microbit_soundevent_obj_t *)self_in;
mp_printf(print, "SoundEvent('%q')", self->name);
Expand All @@ -50,6 +55,7 @@ STATIC void microbit_soundevent_print(const mp_print_t *print, mp_obj_t self_in,
STATIC const mp_rom_map_elem_t microbit_soundevent_locals_dict_table[] = {
{ MP_ROM_QSTR(MP_QSTR_LOUD), MP_ROM_PTR(&microbit_soundevent_loud_obj) },
{ MP_ROM_QSTR(MP_QSTR_QUIET), MP_ROM_PTR(&microbit_soundevent_quiet_obj) },
{ MP_ROM_QSTR(MP_QSTR_CLAP), MP_ROM_PTR(&microbit_soundevent_clap_obj) },
};
STATIC MP_DEFINE_CONST_DICT(microbit_soundevent_locals_dict, microbit_soundevent_locals_dict_table);

Expand Down
1 change: 1 addition & 0 deletions src/codal_port/modmicrobit.h
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,7 @@ extern const struct _monochrome_5by5_t microbit_const_image_scissors_obj;

extern const microbit_soundevent_obj_t microbit_soundevent_loud_obj;
extern const microbit_soundevent_obj_t microbit_soundevent_quiet_obj;
extern const microbit_soundevent_obj_t microbit_soundevent_clap_obj;

extern struct _microbit_display_obj_t microbit_display_obj;
extern const struct _microbit_accelerometer_obj_t microbit_accelerometer_obj;
Expand Down

0 comments on commit 8b8c1d3

Please sign in to comment.