Skip to content

Commit 18eb9de

Browse files
committed
codal_port: Add SoundEvent class and move LOUD/QUIET constants there.
To capture the current 'loud' and 'quiet' options and give the possibility to expand it in the future to other types of events. Usage is now: microphone.was_event(SoundEvent.LOUD)
1 parent 0d1c10b commit 18eb9de

File tree

5 files changed

+78
-13
lines changed

5 files changed

+78
-13
lines changed

src/codal_port/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ SRC_C += \
6565
microbit_microphone.c \
6666
microbit_pin.c \
6767
microbit_pinmode.c \
68+
microbit_soundevent.c \
6869
microbit_spi.c \
6970
microbit_uart.c \
7071
modaudio.c \

src/codal_port/microbit_microphone.c

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,10 @@ typedef struct _microbit_microphone_obj_t {
3838
mp_obj_base_t base;
3939
} microbit_microphone_obj_t;
4040

41-
static const qstr sound_event_name_map[] = {
42-
[SOUND_EVENT_NONE] = MP_QSTR_,
43-
[SOUND_EVENT_LOUD] = MP_QSTR_loud,
44-
[SOUND_EVENT_QUIET] = MP_QSTR_quiet,
41+
static const mp_const_obj_t sound_event_obj_map[] = {
42+
[SOUND_EVENT_NONE] = MP_ROM_NONE,
43+
[SOUND_EVENT_LOUD] = MP_ROM_PTR(&microbit_soundevent_loud_obj),
44+
[SOUND_EVENT_QUIET] = MP_ROM_PTR(&microbit_soundevent_quiet_obj),
4545
};
4646

4747
static uint8_t sound_event_current = 0;
@@ -72,10 +72,9 @@ STATIC void microphone_init(void) {
7272
microbit_hal_microphone_init();
7373
}
7474

75-
STATIC uint8_t sound_event_from_obj(mp_obj_t sound_in) {
76-
qstr sound = mp_obj_str_get_qstr(sound_in);
77-
for (uint8_t i = 0; i < MP_ARRAY_SIZE(sound_event_name_map); ++i) {
78-
if (sound == sound_event_name_map[i]) {
75+
STATIC uint8_t sound_event_from_obj(mp_obj_t sound) {
76+
for (uint8_t i = 0; i < MP_ARRAY_SIZE(sound_event_obj_map); ++i) {
77+
if (sound == sound_event_obj_map[i]) {
7978
return i;
8079
}
8180
}
@@ -110,7 +109,7 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_1(microbit_microphone_sound_level_obj, microbit_m
110109
STATIC mp_obj_t microbit_microphone_current_event(mp_obj_t self_in) {
111110
(void)self_in;
112111
microphone_init();
113-
return MP_OBJ_NEW_QSTR(sound_event_name_map[sound_event_current]);
112+
return (mp_obj_t)sound_event_obj_map[sound_event_current];
114113
}
115114
STATIC MP_DEFINE_CONST_FUN_OBJ_1(microbit_microphone_current_event_obj, microbit_microphone_current_event);
116115

@@ -145,7 +144,7 @@ STATIC mp_obj_t microbit_microphone_get_events(mp_obj_t self_in) {
145144
mp_obj_tuple_t *o = (mp_obj_tuple_t *)mp_obj_new_tuple(sound_event_history_index, NULL);
146145
for (size_t i = 0; i < sound_event_history_index; ++i) {
147146
uint8_t sound = sound_event_history_array[i];
148-
o->items[i] = MP_OBJ_NEW_QSTR(sound_event_name_map[sound]);
147+
o->items[i] = (mp_obj_t)sound_event_obj_map[sound];
149148
}
150149
sound_event_history_index = 0;
151150
return o;
@@ -159,9 +158,6 @@ STATIC const mp_rom_map_elem_t microbit_microphone_locals_dict_table[] = {
159158
{ MP_ROM_QSTR(MP_QSTR_is_event), MP_ROM_PTR(&microbit_microphone_is_event_obj) },
160159
{ MP_ROM_QSTR(MP_QSTR_was_event), MP_ROM_PTR(&microbit_microphone_was_event_obj) },
161160
{ MP_ROM_QSTR(MP_QSTR_get_events), MP_ROM_PTR(&microbit_microphone_get_events_obj) },
162-
163-
{ MP_ROM_QSTR(MP_QSTR_LOUD), MP_ROM_QSTR(MP_QSTR_loud) },
164-
{ MP_ROM_QSTR(MP_QSTR_QUIET), MP_ROM_QSTR(MP_QSTR_quiet) },
165161
};
166162
STATIC MP_DEFINE_CONST_DICT(microbit_microphone_locals_dict, microbit_microphone_locals_dict_table);
167163

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/*
2+
* This file is part of the MicroPython project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2020 Damien P. George
7+
*
8+
* Permission is hereby granted, free of charge, to any person obtaining a copy
9+
* of this software and associated documentation files (the "Software"), to deal
10+
* in the Software without restriction, including without limitation the rights
11+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12+
* copies of the Software, and to permit persons to whom the Software is
13+
* furnished to do so, subject to the following conditions:
14+
*
15+
* The above copyright notice and this permission notice shall be included in
16+
* all copies or substantial portions of the Software.
17+
*
18+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24+
* THE SOFTWARE.
25+
*/
26+
27+
#include "py/runtime.h"
28+
#include "modmicrobit.h"
29+
30+
struct _microbit_soundevent_obj_t {
31+
mp_obj_base_t base;
32+
qstr name;
33+
};
34+
35+
const microbit_soundevent_obj_t microbit_soundevent_loud_obj = {
36+
{ &microbit_soundevent_type },
37+
MP_QSTR_loud,
38+
};
39+
40+
const microbit_soundevent_obj_t microbit_soundevent_quiet_obj = {
41+
{ &microbit_soundevent_type },
42+
MP_QSTR_quiet,
43+
};
44+
45+
STATIC void microbit_soundevent_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
46+
microbit_soundevent_obj_t *self = (microbit_soundevent_obj_t *)self_in;
47+
mp_printf(print, "SoundEvent('%q')", self->name);
48+
}
49+
50+
STATIC const mp_rom_map_elem_t microbit_soundevent_locals_dict_table[] = {
51+
{ MP_ROM_QSTR(MP_QSTR_LOUD), MP_ROM_PTR(&microbit_soundevent_loud_obj) },
52+
{ MP_ROM_QSTR(MP_QSTR_QUIET), MP_ROM_PTR(&microbit_soundevent_quiet_obj) },
53+
};
54+
STATIC MP_DEFINE_CONST_DICT(microbit_soundevent_locals_dict, microbit_soundevent_locals_dict_table);
55+
56+
const mp_obj_type_t microbit_soundevent_type = {
57+
{ &mp_type_type },
58+
.name = MP_QSTR_MicroBitSoundEvent,
59+
.print = microbit_soundevent_print,
60+
.locals_dict = (mp_obj_dict_t *)&microbit_soundevent_locals_dict,
61+
};

src/codal_port/modmicrobit.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ STATIC const mp_rom_map_elem_t microbit_module_globals_table[] = {
8686
{ MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_microbit) },
8787

8888
{ MP_ROM_QSTR(MP_QSTR_Image), (mp_obj_t)&microbit_image_type },
89+
{ MP_ROM_QSTR(MP_QSTR_SoundEvent), (mp_obj_t)&microbit_soundevent_type },
8990

9091
{ MP_ROM_QSTR(MP_QSTR_display), MP_ROM_PTR(&microbit_display_obj) },
9192
{ MP_ROM_QSTR(MP_QSTR_button_a), (mp_obj_t)&microbit_button_a_obj },

src/codal_port/modmicrobit.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,12 +69,15 @@ typedef struct _pinmode {
6969
release_func release; /* Call this function to release pin */
7070
} microbit_pinmode_t;
7171

72+
typedef struct _microbit_soundevent_obj_t microbit_soundevent_obj_t;
73+
7274
extern const microbit_pinmode_t microbit_pinmodes[];
7375

7476
extern const mp_obj_type_t microbit_ad_pin_type;
7577
extern const mp_obj_type_t microbit_dig_pin_type;
7678
extern const mp_obj_type_t microbit_touch_pin_type;
7779
extern const mp_obj_type_t microbit_touch_only_pin_type;
80+
extern const mp_obj_type_t microbit_soundevent_type;
7881

7982
extern const struct _microbit_pin_obj_t microbit_p0_obj;
8083
extern const struct _microbit_pin_obj_t microbit_p1_obj;
@@ -169,6 +172,9 @@ extern const struct _monochrome_5by5_t microbit_const_image_skull_obj;
169172
extern const struct _monochrome_5by5_t microbit_const_image_umbrella_obj;
170173
extern const struct _monochrome_5by5_t microbit_const_image_snake_obj;
171174

175+
extern const microbit_soundevent_obj_t microbit_soundevent_loud_obj;
176+
extern const microbit_soundevent_obj_t microbit_soundevent_quiet_obj;
177+
172178
extern struct _microbit_display_obj_t microbit_display_obj;
173179
extern const struct _microbit_accelerometer_obj_t microbit_accelerometer_obj;
174180
extern const struct _microbit_compass_obj_t microbit_compass_obj;

0 commit comments

Comments
 (0)