Skip to content

Commit 0c4287c

Browse files
committed
codal_port/microbit_pinaudio: Only allow one pin for audio selection.
The audio, music and speech modules can now only take either None ore a single pin object to specify the output for the audio. Note that this is in addition to the speaker output which is now a completely independent audio entity, and to which audio will always be directed (speaker output can be disabled via speaker.off()). Signed-off-by: Damien George <[email protected]>
1 parent fb682ea commit 0c4287c

File tree

3 files changed

+11
-75
lines changed

3 files changed

+11
-75
lines changed

src/codal_port/microbit_pin.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,7 @@ const mp_obj_type_t microbit_touch_only_pin_type = {
264264
.locals_dict = (mp_obj_dict_t *)&microbit_touch_only_pin_locals_dict,
265265
};
266266

267-
const microbit_pin_obj_t *microbit_obj_get_pin(mp_obj_t o) {
267+
const microbit_pin_obj_t *microbit_obj_get_pin(mp_const_obj_t o) {
268268
const mp_obj_type_t *type = mp_obj_get_type(o);
269269
if (type == &microbit_touch_pin_type || type == &microbit_ad_pin_type || type == &microbit_dig_pin_type) {
270270
return (microbit_pin_obj_t*)o;

src/codal_port/microbit_pinaudio.c

Lines changed: 7 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -27,74 +27,17 @@
2727
#include "microbithal.h"
2828
#include "modmicrobit.h"
2929

30-
const mp_obj_tuple_t microbit_pin_default_audio_obj = {
31-
{ &mp_type_tuple },
32-
2,
33-
{
34-
(mp_obj_t)MP_ROM_PTR(&microbit_p0_obj),
35-
(mp_obj_t)MP_ROM_PTR(&microbit_pin_speaker_obj),
36-
}
37-
};
38-
39-
// Whether the speaker is enabled or not. If true, this overrides any selection
40-
// of the speaker to act as though it was not selected.
41-
STATIC bool audio_speaker_enabled = true;
42-
43-
// The currently selected pin and/or speaker output for the audio.
30+
// The currently selected pin output for the audio.
4431
STATIC const microbit_pin_obj_t *audio_routed_pin = NULL;
45-
STATIC bool audio_routed_speaker = false;
46-
47-
STATIC void microbit_pin_audio_get_pins(mp_const_obj_t select, const microbit_pin_obj_t **pin_selected, bool *speaker_selected) {
48-
// Convert the "select" object into an array of items (which should be pins).
49-
size_t len;
50-
mp_obj_t *items;
51-
if (mp_obj_is_type(select, &mp_type_tuple)) {
52-
mp_obj_get_array((mp_obj_t)select, &len, &items);
53-
if (len > 2) {
54-
mp_raise_ValueError(MP_ERROR_TEXT("maximum of 2 pins allowed"));
55-
}
56-
} else {
57-
len = 1;
58-
items = (mp_obj_t *)&select;
59-
}
60-
61-
// Work out which pins are selected for the audio output.
62-
*pin_selected = NULL;
63-
*speaker_selected = false;
64-
if (len == 1 || len == 2) {
65-
if (items[0] == &microbit_pin_speaker_obj) {
66-
*speaker_selected = true;
67-
} else {
68-
*pin_selected = microbit_obj_get_pin(items[0]);
69-
}
70-
if (len == 2) {
71-
if (items[1] == &microbit_pin_speaker_obj) {
72-
if (*speaker_selected) {
73-
mp_raise_ValueError(MP_ERROR_TEXT("speaker selected twice"));
74-
}
75-
*speaker_selected = true;
76-
} else {
77-
if (*pin_selected != NULL) {
78-
mp_raise_ValueError(MP_ERROR_TEXT("can only select one non-speaker pin"));
79-
}
80-
*pin_selected = microbit_obj_get_pin(items[1]);
81-
}
82-
}
83-
}
84-
85-
// Apply global override to enable/disable speaker.
86-
*speaker_selected = *speaker_selected && audio_speaker_enabled;
87-
}
88-
89-
void microbit_pin_audio_speaker_enable(bool enable) {
90-
audio_speaker_enabled = enable;
91-
}
9232

9333
void microbit_pin_audio_select(mp_const_obj_t select) {
94-
// Work out which pins are selected.
34+
// Work out which pins are requested for the audio output.
9535
const microbit_pin_obj_t *pin_selected;
96-
bool speaker_selected;
97-
microbit_pin_audio_get_pins(select, &pin_selected, &speaker_selected);
36+
if (select == mp_const_none) {
37+
pin_selected = NULL;
38+
} else {
39+
pin_selected = microbit_obj_get_pin((mp_obj_t)select);
40+
}
9841

9942
// Change the pin if needed.
10043
if (pin_selected != audio_routed_pin) {
@@ -109,12 +52,6 @@ void microbit_pin_audio_select(mp_const_obj_t select) {
10952
microbit_hal_audio_select_pin(audio_routed_pin->name);
11053
}
11154
}
112-
113-
// Change the speaker mode if needed.
114-
if (speaker_selected != audio_routed_speaker) {
115-
audio_routed_speaker = speaker_selected;
116-
microbit_hal_audio_select_speaker(audio_routed_speaker);
117-
}
11855
}
11956

12057
void microbit_pin_audio_free(void) {

src/codal_port/modmicrobit.h

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@
5555
#define microbit_pin_mode_i2c (&microbit_pinmodes[MODE_I2C])
5656
#define microbit_pin_mode_spi (&microbit_pinmodes[MODE_SPI])
5757

58+
#define microbit_pin_default_audio_obj (microbit_p0_obj)
59+
5860
typedef struct _microbit_pin_obj_t {
5961
mp_obj_base_t base;
6062
uint8_t number; // The pin number on microbit board
@@ -188,10 +190,7 @@ extern const struct _microbit_microphone_obj_t microbit_microphone_obj;
188190
extern const struct _microbit_button_obj_t microbit_button_a_obj;
189191
extern const struct _microbit_button_obj_t microbit_button_b_obj;
190192

191-
//extern uint16_t microbit_volume_global;
192-
extern const mp_obj_tuple_t microbit_pin_default_audio_obj;
193-
194-
const microbit_pin_obj_t *microbit_obj_get_pin(mp_obj_t o);
193+
const microbit_pin_obj_t *microbit_obj_get_pin(mp_const_obj_t o);
195194

196195
// Release pin for use by other modes. Safe to call in an interrupt.
197196
// If pin is NULL or pin already unused, then this is a no-op

0 commit comments

Comments
 (0)