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.
4431STATIC 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
9333void 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
12057void microbit_pin_audio_free (void ) {
0 commit comments