Skip to content

Commit

Permalink
codal_port/microbit_pinmode: Make audio and speech use "audio" pin mode.
Browse files Browse the repository at this point in the history
This distinguishes "music" and "audio" as separate modes as returned by
pin.get_mode(), making v2 more compatible with micro:bit v1.

Fixes issue #137.

Signed-off-by: Damien George <[email protected]>
  • Loading branch information
dpgeorge committed Nov 7, 2022
1 parent d2130d8 commit fd99f40
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 15 deletions.
7 changes: 5 additions & 2 deletions src/codal_port/microbit_pinaudio.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
// The currently selected pin output for the audio.
STATIC const microbit_pin_obj_t *audio_routed_pin = NULL;

void microbit_pin_audio_select(mp_const_obj_t select) {
void microbit_pin_audio_select(mp_const_obj_t select, const microbit_pinmode_t *pinmode) {
// Work out which pins are requested for the audio output.
const microbit_pin_obj_t *pin_selected;
if (select == mp_const_none) {
Expand All @@ -50,9 +50,12 @@ void microbit_pin_audio_select(mp_const_obj_t select) {
if (audio_routed_pin == NULL) {
microbit_hal_audio_select_pin(-1);
} else {
microbit_obj_pin_acquire(audio_routed_pin, microbit_pin_mode_music);
microbit_obj_pin_acquire(audio_routed_pin, pinmode);
microbit_hal_audio_select_pin(audio_routed_pin->name);
}
} else if (audio_routed_pin != NULL) {
// Update the pin acquisition mode, to make sure pin.get_mode() reflects the current mode.
microbit_pin_set_mode(audio_routed_pin, pinmode);
}
}

Expand Down
14 changes: 7 additions & 7 deletions src/codal_port/microbit_pinmode.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,15 @@ const microbit_pinmode_t *microbit_pin_get_mode(const microbit_pin_obj_t *pin) {
return &microbit_pinmodes[pinmode];
}

static void set_mode(uint32_t pin, const microbit_pinmode_t *mode) {
void microbit_pin_set_mode(const microbit_pin_obj_t *pin, const microbit_pinmode_t *mode) {
uint32_t index = mode - &microbit_pinmodes[0];
microbit_pinmode_indices[pin] = index;
microbit_pinmode_indices[pin->number] = index;
return;
}

void microbit_obj_pin_free(const microbit_pin_obj_t *pin) {
if (pin != NULL) {
set_mode(pin->number, microbit_pin_mode_unused);
microbit_pin_set_mode(pin, microbit_pin_mode_unused);
}
}

Expand All @@ -66,7 +66,7 @@ bool microbit_obj_pin_acquire(const microbit_pin_obj_t *pin, const microbit_pinm

if (current_mode != new_mode) {
current_mode->release(pin);
set_mode(pin->number, new_mode);
microbit_pin_set_mode(pin, new_mode);
return true;
} else {
return false;
Expand All @@ -86,7 +86,7 @@ static void analog_release(const microbit_pin_obj_t *pin) {
// TODO: pwm_release()
}

static void music_release(const microbit_pin_obj_t *pin) {
static void audio_music_release(const microbit_pin_obj_t *pin) {
if (microbit_audio_is_playing() || microbit_music_is_playing()) {
pinmode_error(pin);
} else {
Expand All @@ -101,8 +101,8 @@ const microbit_pinmode_t microbit_pinmodes[] = {
[MODE_WRITE_DIGITAL] = { MP_QSTR_write_digital, noop },
[MODE_DISPLAY] = { MP_QSTR_display, pinmode_error },
[MODE_BUTTON] = { MP_QSTR_button, pinmode_error },
[MODE_MUSIC] = { MP_QSTR_music, music_release },
[MODE_AUDIO_PLAY] = { MP_QSTR_audio, noop },
[MODE_MUSIC] = { MP_QSTR_music, audio_music_release },
[MODE_AUDIO_PLAY] = { MP_QSTR_audio, audio_music_release },
[MODE_TOUCH] = { MP_QSTR_touch, noop },
[MODE_I2C] = { MP_QSTR_i2c, pinmode_error },
[MODE_SPI] = { MP_QSTR_spi, pinmode_error }
Expand Down
2 changes: 1 addition & 1 deletion src/codal_port/modaudio.c
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ void microbit_audio_play_source(mp_obj_t src, mp_obj_t pin_select, bool wait, ui
microbit_audio_stop();
}
audio_init(sample_rate);
microbit_pin_audio_select(pin_select);
microbit_pin_audio_select(pin_select, microbit_pin_mode_audio_play);

const char *sound_expr_data = NULL;
if (mp_obj_is_type(src, &microbit_sound_type)) {
Expand Down
6 changes: 5 additions & 1 deletion src/codal_port/modmicrobit.h
Original file line number Diff line number Diff line change
Expand Up @@ -220,11 +220,15 @@ bool microbit_obj_pin_can_be_acquired(const microbit_pin_obj_t *pin);
// Not safe to call in an interrupt as it may raise if pin can't be acquired.
bool microbit_obj_pin_acquire(const microbit_pin_obj_t *pin, const microbit_pinmode_t *mode);

// Change the mode of a pin. This does not check the existing mode, nor release any
// resources, and should only be used if pin resources are managed by the caller.
void microbit_pin_set_mode(const microbit_pin_obj_t *pin, const microbit_pinmode_t *mode);

const microbit_pinmode_t *microbit_pin_get_mode(const microbit_pin_obj_t *pin);
void pinmode_error(const microbit_pin_obj_t *pin);

void microbit_pin_audio_speaker_enable(bool enable);
void microbit_pin_audio_select(mp_const_obj_t select);
void microbit_pin_audio_select(mp_const_obj_t select, const microbit_pinmode_t *pinmode);
void microbit_pin_audio_free(void);

MP_DECLARE_CONST_FUN_OBJ_0(microbit_reset_obj);
Expand Down
6 changes: 3 additions & 3 deletions src/codal_port/modmusic.c
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ STATIC mp_obj_t microbit_music_stop(mp_uint_t n_args, const mp_obj_t *args) {
pin = args[0];
}
// Raise exception if the pin we are trying to stop is not in a compatible mode.
microbit_pin_audio_select(pin);
microbit_pin_audio_select(pin, microbit_pin_mode_music);

// Stop any ongoing background music
music_data->async_state = ASYNC_MUSIC_STATE_IDLE;
Expand Down Expand Up @@ -326,7 +326,7 @@ STATIC mp_obj_t microbit_music_play(mp_uint_t n_args, const mp_obj_t *pos_args,
music_data->async_state = ASYNC_MUSIC_STATE_IDLE;

// get the pin to play on
microbit_pin_audio_select(args[1].u_obj);
microbit_pin_audio_select(args[1].u_obj, microbit_pin_mode_music);

// start the tune running in the background
music_data->async_wait_ticks = mp_hal_ticks_ms();
Expand Down Expand Up @@ -372,7 +372,7 @@ STATIC mp_obj_t microbit_music_pitch(mp_uint_t n_args, const mp_obj_t *pos_args,
music_data->async_state = ASYNC_MUSIC_STATE_IDLE;

// Update pin modes
microbit_pin_audio_select(args[2].u_obj);
microbit_pin_audio_select(args[2].u_obj, microbit_pin_mode_music);

bool wait = args[3].u_bool;
music_output_amplitude(MUSIC_OUTPUT_AMPLITUDE_ON);
Expand Down
2 changes: 1 addition & 1 deletion src/codal_port/modspeech.c
Original file line number Diff line number Diff line change
Expand Up @@ -421,7 +421,7 @@ STATIC mp_obj_t articulate(mp_obj_t phonemes, mp_uint_t n_args, const mp_obj_t *

#if USE_DEDICATED_AUDIO_CHANNEL
sam_output_reset(NULL);
microbit_pin_audio_select(args[7].u_obj);
microbit_pin_audio_select(args[7].u_obj, microbit_pin_mode_audio_play);
microbit_hal_audio_speech_init(sample_rate);
#else
speech_iterator_t *src = make_speech_iter();
Expand Down

0 comments on commit fd99f40

Please sign in to comment.