Skip to content

Commit cac1c0c

Browse files
committed
codal_port/microbit_speaker: Add microbit.speaker object.
Changes are: - add microbit.speaker object - move pin_speaker disable/enable methods to speaker off/on - change pin_speaker type to a normal digital pin Signed-off-by: Damien George <[email protected]>
1 parent cbc3cfc commit cac1c0c

File tree

5 files changed

+68
-34
lines changed

5 files changed

+68
-34
lines changed

src/codal_port/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ SRC_C += \
7171
microbit_pinmode.c \
7272
microbit_sound.c \
7373
microbit_soundevent.c \
74+
microbit_speaker.c \
7475
microbit_spi.c \
7576
microbit_uart.c \
7677
modaudio.c \

src/codal_port/microbit_pin.c

Lines changed: 2 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ const microbit_pin_obj_t microbit_p19_obj = {{&microbit_dig_pin_type}, 19, MICR
4949
const microbit_pin_obj_t microbit_p20_obj = {{&microbit_dig_pin_type}, 20, MICROBIT_HAL_PIN_P20, MODE_I2C};
5050

5151
const microbit_pin_obj_t microbit_pin_logo_obj = {{&microbit_touch_only_pin_type}, 30, MICROBIT_HAL_PIN_FACE, MODE_UNUSED};
52-
const microbit_pin_obj_t microbit_pin_speaker_obj = {{&microbit_speaker_pin_type}, 31, MICROBIT_HAL_PIN_SPEAKER, MODE_UNUSED};
52+
const microbit_pin_obj_t microbit_pin_speaker_obj = {{&microbit_dig_pin_type}, 31, MICROBIT_HAL_PIN_SPEAKER, MODE_UNUSED};
5353

5454
static mp_obj_t microbit_pin_get_mode_func(mp_obj_t self_in) {
5555
microbit_pin_obj_t *self = (microbit_pin_obj_t*)self_in;
@@ -177,20 +177,6 @@ mp_obj_t microbit_pin_set_touch_mode(mp_obj_t self_in, mp_obj_t mode_in) {
177177
}
178178
MP_DEFINE_CONST_FUN_OBJ_2(microbit_pin_set_touch_mode_obj, microbit_pin_set_touch_mode);
179179

180-
STATIC mp_obj_t microbit_pin_speaker_disable(mp_obj_t self_in) {
181-
(void)self_in;
182-
microbit_pin_audio_speaker_enable(false);
183-
return mp_const_none;
184-
}
185-
STATIC MP_DEFINE_CONST_FUN_OBJ_1(microbit_pin_speaker_disable_obj, microbit_pin_speaker_disable);
186-
187-
STATIC mp_obj_t microbit_pin_speaker_enable(mp_obj_t self_in) {
188-
(void)self_in;
189-
microbit_pin_audio_speaker_enable(true);
190-
return mp_const_none;
191-
}
192-
STATIC MP_DEFINE_CONST_FUN_OBJ_1(microbit_pin_speaker_enable_obj, microbit_pin_speaker_enable);
193-
194180
#define PULL_CONSTANTS \
195181
{ MP_ROM_QSTR(MP_QSTR_PULL_UP), MP_ROM_INT(MICROBIT_HAL_PIN_PULL_UP) }, \
196182
{ MP_ROM_QSTR(MP_QSTR_PULL_DOWN), MP_ROM_INT(MICROBIT_HAL_PIN_PULL_DOWN) }, \
@@ -278,26 +264,9 @@ const mp_obj_type_t microbit_touch_only_pin_type = {
278264
.locals_dict = (mp_obj_dict_t *)&microbit_touch_only_pin_locals_dict,
279265
};
280266

281-
STATIC const mp_rom_map_elem_t microbit_speaker_pin_locals_dict_table[] = {
282-
{ MP_ROM_QSTR(MP_QSTR_write_digital), MP_ROM_PTR(&microbit_pin_write_digital_obj) },
283-
{ MP_ROM_QSTR(MP_QSTR_write_analog), MP_ROM_PTR(&microbit_pin_write_analog_obj) },
284-
{ MP_ROM_QSTR(MP_QSTR_set_analog_period), MP_ROM_PTR(&microbit_pin_set_analog_period_obj) },
285-
{ MP_ROM_QSTR(MP_QSTR_set_analog_period_microseconds), MP_ROM_PTR(&microbit_pin_set_analog_period_microseconds_obj) },
286-
{ MP_ROM_QSTR(MP_QSTR_get_analog_period_microseconds), MP_ROM_PTR(&microbit_pin_get_analog_period_microseconds_obj) },
287-
{ MP_ROM_QSTR(MP_QSTR_disable), MP_ROM_PTR(&microbit_pin_speaker_disable_obj) },
288-
{ MP_ROM_QSTR(MP_QSTR_enable), MP_ROM_PTR(&microbit_pin_speaker_enable_obj) },
289-
};
290-
STATIC MP_DEFINE_CONST_DICT(microbit_speaker_pin_locals_dict, microbit_speaker_pin_locals_dict_table);
291-
292-
const mp_obj_type_t microbit_speaker_pin_type = {
293-
{ &mp_type_type },
294-
.name = MP_QSTR_MicroBitSpeakerPin,
295-
.locals_dict = (mp_obj_dict_t *)&microbit_speaker_pin_locals_dict,
296-
};
297-
298267
const microbit_pin_obj_t *microbit_obj_get_pin(mp_obj_t o) {
299268
const mp_obj_type_t *type = mp_obj_get_type(o);
300-
if (type == &microbit_touch_pin_type || type == &microbit_ad_pin_type || type == &microbit_dig_pin_type || type == &microbit_speaker_pin_type) {
269+
if (type == &microbit_touch_pin_type || type == &microbit_ad_pin_type || type == &microbit_dig_pin_type) {
301270
return (microbit_pin_obj_t*)o;
302271
} else {
303272
mp_raise_TypeError("expecting a pin");

src/codal_port/microbit_speaker.c

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/*
2+
* This file is part of the MicroPython project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2020 Damien P. George
7+
*
8+
* Permission is hereby granted, free of charge, to any person obtaining a copy
9+
* of this software and associated documentation files (the "Software"), to deal
10+
* in the Software without restriction, including without limitation the rights
11+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12+
* copies of the Software, and to permit persons to whom the Software is
13+
* furnished to do so, subject to the following conditions:
14+
*
15+
* The above copyright notice and this permission notice shall be included in
16+
* all copies or substantial portions of the Software.
17+
*
18+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24+
* THE SOFTWARE.
25+
*/
26+
27+
#include "py/runtime.h"
28+
#include "microbithal.h"
29+
#include "modmicrobit.h"
30+
31+
typedef struct _microbit_speaker_obj_t {
32+
mp_obj_base_t base;
33+
} microbit_speaker_obj_t;
34+
35+
STATIC mp_obj_t microbit_speaker_off(mp_obj_t self_in) {
36+
(void)self_in;
37+
microbit_hal_audio_select_speaker(false);
38+
return mp_const_none;
39+
}
40+
STATIC MP_DEFINE_CONST_FUN_OBJ_1(microbit_speaker_off_obj, microbit_speaker_off);
41+
42+
STATIC mp_obj_t microbit_speaker_on(mp_obj_t self_in) {
43+
(void)self_in;
44+
microbit_hal_audio_select_speaker(true);
45+
return mp_const_none;
46+
}
47+
STATIC MP_DEFINE_CONST_FUN_OBJ_1(microbit_speaker_on_obj, microbit_speaker_on);
48+
49+
STATIC const mp_rom_map_elem_t microbit_speaker_locals_dict_table[] = {
50+
{ MP_ROM_QSTR(MP_QSTR_off), MP_ROM_PTR(&microbit_speaker_off_obj) },
51+
{ MP_ROM_QSTR(MP_QSTR_on), MP_ROM_PTR(&microbit_speaker_on_obj) },
52+
};
53+
STATIC MP_DEFINE_CONST_DICT(microbit_speaker_locals_dict, microbit_speaker_locals_dict_table);
54+
55+
STATIC const mp_obj_type_t microbit_speaker_type = {
56+
{ &mp_type_type },
57+
.name = MP_QSTR_MicroBitSpeakerPin,
58+
.locals_dict = (mp_obj_dict_t *)&microbit_speaker_locals_dict,
59+
};
60+
61+
const microbit_speaker_obj_t microbit_speaker_obj = {
62+
{ &microbit_speaker_type },
63+
};

src/codal_port/modmicrobit.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ STATIC const mp_rom_map_elem_t microbit_module_globals_table[] = {
103103
{ MP_ROM_QSTR(MP_QSTR_button_b), (mp_obj_t)&microbit_button_b_obj },
104104
{ MP_ROM_QSTR(MP_QSTR_accelerometer), MP_ROM_PTR(&microbit_accelerometer_obj) },
105105
{ MP_ROM_QSTR(MP_QSTR_compass), MP_ROM_PTR(&microbit_compass_obj) },
106+
{ MP_ROM_QSTR(MP_QSTR_speaker), MP_ROM_PTR(&microbit_speaker_obj) },
106107
{ MP_ROM_QSTR(MP_QSTR_microphone), MP_ROM_PTR(&microbit_microphone_obj) },
107108
{ MP_ROM_QSTR(MP_QSTR_audio), MP_ROM_PTR(&audio_module) },
108109

src/codal_port/modmicrobit.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,6 @@ extern const mp_obj_type_t microbit_ad_pin_type;
8282
extern const mp_obj_type_t microbit_dig_pin_type;
8383
extern const mp_obj_type_t microbit_touch_pin_type;
8484
extern const mp_obj_type_t microbit_touch_only_pin_type;
85-
extern const mp_obj_type_t microbit_speaker_pin_type;
8685
extern const mp_obj_type_t microbit_sound_type;
8786
extern const mp_obj_type_t microbit_soundevent_type;
8887

@@ -184,6 +183,7 @@ extern const microbit_soundevent_obj_t microbit_soundevent_quiet_obj;
184183
extern struct _microbit_display_obj_t microbit_display_obj;
185184
extern const struct _microbit_accelerometer_obj_t microbit_accelerometer_obj;
186185
extern const struct _microbit_compass_obj_t microbit_compass_obj;
186+
extern const struct _microbit_speaker_obj_t microbit_speaker_obj;
187187
extern const struct _microbit_microphone_obj_t microbit_microphone_obj;
188188
extern const struct _microbit_button_obj_t microbit_button_a_obj;
189189
extern const struct _microbit_button_obj_t microbit_button_b_obj;

0 commit comments

Comments
 (0)