Skip to content

Commit

Permalink
codal_port/microbit_pinmode: Allow grabbing a pin in music mode if idle.
Browse files Browse the repository at this point in the history
If a pin is in music mode but there is no audio/music currently active,
then allow it to be grabbed for use as any other mode.  Otherwise raise an
exception that it is in use in music mode.

Addresses issue #50.

Signed-off-by: Damien George <[email protected]>
  • Loading branch information
dpgeorge committed Jun 29, 2021
1 parent 6790bb5 commit bca4dc5
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 3 deletions.
12 changes: 11 additions & 1 deletion src/codal_port/microbit_pinmode.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@
*/

#include "py/runtime.h"
#include "modaudio.h"
#include "modmicrobit.h"
#include "modmusic.h"

uint8_t microbit_pinmode_indices[32] = { 0 };

Expand Down Expand Up @@ -84,14 +86,22 @@ static void analog_release(const microbit_pin_obj_t *pin) {
// TODO: pwm_release()
}

static void music_release(const microbit_pin_obj_t *pin) {
if (microbit_audio_is_playing() || microbit_music_is_playing()) {
pinmode_error(pin);
} else {
microbit_pin_audio_free();
}
}

const microbit_pinmode_t microbit_pinmodes[] = {
[MODE_UNUSED] = { MP_QSTR_unused, noop },
[MODE_WRITE_ANALOG] = { MP_QSTR_write_analog, analog_release },
[MODE_READ_DIGITAL] = { MP_QSTR_read_digital, noop },
[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, pinmode_error },
[MODE_MUSIC] = { MP_QSTR_music, music_release },
[MODE_AUDIO_PLAY] = { MP_QSTR_audio, noop },
[MODE_TOUCH] = { MP_QSTR_touch, noop },
[MODE_I2C] = { MP_QSTR_i2c, pinmode_error },
Expand Down
4 changes: 2 additions & 2 deletions src/codal_port/modaudio.c
Original file line number Diff line number Diff line change
Expand Up @@ -205,11 +205,11 @@ STATIC mp_obj_t play(mp_uint_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_ar
MP_DEFINE_CONST_FUN_OBJ_KW(microbit_audio_play_obj, 0, play);

bool microbit_audio_is_playing(void) {
return audio_running;
return audio_running || microbit_hal_audio_is_expression_active();
}

mp_obj_t is_playing(void) {
return mp_obj_new_bool(audio_running);
return mp_obj_new_bool(microbit_audio_is_playing());
}
MP_DEFINE_CONST_FUN_OBJ_0(microbit_audio_is_playing_obj, is_playing);

Expand Down
4 changes: 4 additions & 0 deletions src/codal_port/modmusic.c
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,10 @@ STATIC int music_output_period_us(uint32_t period) {
return microbit_hal_pin_set_analog_period_us(MICROBIT_HAL_PIN_MIXER, period);
}

bool microbit_music_is_playing(void) {
return music_data != NULL && music_data->async_state != ASYNC_MUSIC_STATE_IDLE;
}

// This runs on a hardware interrupt.
void microbit_music_tick(void) {
if (music_data == NULL) {
Expand Down
1 change: 1 addition & 0 deletions src/codal_port/modmusic.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ extern const struct _mp_obj_tuple_t microbit_music_tune_power_up_obj;
extern const struct _mp_obj_tuple_t microbit_music_tune_power_down_obj;

void microbit_music_volume_changed(void);
bool microbit_music_is_playing(void);
void microbit_music_tick(void);

#endif // MICROPY_INCLUDED_MICROBIT_MUSIC_H

0 comments on commit bca4dc5

Please sign in to comment.