Skip to content

Commit 3dbb221

Browse files
committed
pybricks.common.Speaker: Fix awaitable.
The initialization code ran only on boot, which could cause it to get in a bad state the second time.
1 parent 0a62fa5 commit 3dbb221

File tree

1 file changed

+6
-12
lines changed

1 file changed

+6
-12
lines changed

pybricks/common/pb_type_speaker.c

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727

2828
typedef struct {
2929
mp_obj_base_t base;
30-
bool initialized;
3130

3231
// State of awaitable sound
3332
mp_obj_t notes_generator;
@@ -44,8 +43,6 @@ typedef struct {
4443
uint16_t sample_attenuator;
4544
} pb_type_Speaker_obj_t;
4645

47-
STATIC pb_type_Speaker_obj_t pb_type_Speaker_singleton;
48-
4946
STATIC uint16_t waveform_data[128];
5047

5148
STATIC mp_obj_t pb_type_Speaker_volume(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
@@ -111,15 +108,12 @@ STATIC void pb_type_Speaker_stop_beep(void) {
111108
}
112109

113110
STATIC mp_obj_t pb_type_Speaker_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args) {
114-
pb_type_Speaker_obj_t *self = &pb_type_Speaker_singleton;
115-
if (!self->initialized) {
116-
self->base.type = &pb_type_Speaker;
117-
self->initialized = true;
118-
119-
// List of awaitables associated with speaker. By keeping track,
120-
// we can cancel them as needed when a new sound is started.
121-
self->awaitables = mp_obj_new_list(0, NULL);
122-
}
111+
112+
pb_type_Speaker_obj_t *self = mp_obj_malloc(pb_type_Speaker_obj_t, type);
113+
114+
// List of awaitables associated with speaker. By keeping track,
115+
// we can cancel them as needed when a new sound is started.
116+
self->awaitables = mp_obj_new_list(0, NULL);
123117

124118
// REVISIT: If a user creates two Speaker instances, this will reset the volume settings for both.
125119
// If done only once per singleton, however, altered volume settings would be persisted between program runs.

0 commit comments

Comments
 (0)