Skip to content

Commit b24d9e5

Browse files
committed
codal_app: Add microbit_hal_audio_select_pin/speaker functions.
1 parent b260810 commit b24d9e5

File tree

4 files changed

+28
-9
lines changed

4 files changed

+28
-9
lines changed

src/codal_app/main.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
#include "MicroBit.h"
3030

3131
extern MicroBit uBit;
32+
extern NRF52Pin *const pin_obj[];
3233

3334
void serial_interrupt_handler(Event evt);
3435

src/codal_app/microbithal.cpp

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,7 @@
2828
#include "MicroBitDevice.h"
2929
#include "neopixel.h"
3030

31-
extern "C" {
32-
33-
#include "microbithal.h"
34-
35-
static NRF52Pin *const pin_obj[] = {
31+
NRF52Pin *const pin_obj[] = {
3632
&uBit.io.P0,
3733
&uBit.io.P1,
3834
&uBit.io.P2,
@@ -66,7 +62,6 @@ static NRF52Pin *const pin_obj[] = {
6662
&uBit.io.usbTx,
6763
&uBit.io.usbRx,
6864
&uBit.io.irq1,
69-
&uBit.io.speaker, // MICROBIT_HAL_PIN_AUDIO
7065
};
7166

7267
static Button *const button_obj[] = {
@@ -83,6 +78,10 @@ static const PullMode pin_pull_mode_mapping[] = {
8378
static uint8_t pin_pull_state[32 + 6];
8479
static uint16_t button_state[2];
8580

81+
extern "C" {
82+
83+
#include "microbithal.h"
84+
8685
void microbit_hal_background_processing(void) {
8786
// This call takes about 200us.
8887
Event(DEVICE_ID_SCHEDULER, DEVICE_SCHEDULER_EVT_IDLE);
@@ -115,7 +114,7 @@ int microbit_hal_pin_get_pull(int pin) {
115114
}
116115

117116
int microbit_hal_pin_set_analog_period_us(int pin, int period) {
118-
if (pin == MICROBIT_HAL_PIN_AUDIO) {
117+
if (pin == MICROBIT_HAL_PIN_MIXER) {
119118
uBit.audio.virtualOutputPin.setAnalogPeriodUs(period);
120119
return 0;
121120
}
@@ -148,7 +147,7 @@ int microbit_hal_pin_read_analog_u10(int pin) {
148147
}
149148

150149
void microbit_hal_pin_write_analog_u10(int pin, int value) {
151-
if (pin == MICROBIT_HAL_PIN_AUDIO) {
150+
if (pin == MICROBIT_HAL_PIN_MIXER) {
152151
uBit.audio.virtualOutputPin.setAnalogValue(value);
153152
return;
154153
}

src/codal_app/microbithal.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@
3030
extern "C" {
3131
#endif
3232

33+
#include <stdbool.h>
34+
#include <stddef.h>
35+
#include <stdint.h>
36+
3337
// These numbers refer to indices in the (private) pin_obj table.
3438
#define MICROBIT_HAL_PIN_P0 (0)
3539
#define MICROBIT_HAL_PIN_P1 (1)
@@ -54,7 +58,7 @@ extern "C" {
5458
#define MICROBIT_HAL_PIN_SPEAKER (20)
5559
#define MICROBIT_HAL_PIN_USB_TX (30)
5660
#define MICROBIT_HAL_PIN_USB_RX (31)
57-
#define MICROBIT_HAL_PIN_AUDIO (33)
61+
#define MICROBIT_HAL_PIN_MIXER (33)
5862

5963
// These match the micro:bit v1 constants.
6064
#define MICROBIT_HAL_PIN_PULL_UP (0)
@@ -128,6 +132,8 @@ int microbit_hal_microphone_get_level(void);
128132

129133
const uint8_t *microbit_hal_get_font_data(char c);
130134

135+
void microbit_hal_audio_select_pin(int pin);
136+
void microbit_hal_audio_select_speaker(bool enable);
131137
void microbit_hal_audio_set_volume(int value);
132138
void microbit_hal_audio_play_expression_by_name(const char *name);
133139

src/codal_app/microbithal_audio.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,19 @@ extern "C" {
5959

6060
#include "microbithal.h"
6161

62+
void microbit_hal_audio_select_pin(int pin) {
63+
if (pin < 0) {
64+
uBit.audio.setPinEnabled(false);
65+
} else {
66+
uBit.audio.setPinEnabled(true);
67+
uBit.audio.setPin(*pin_obj[pin]);
68+
}
69+
}
70+
71+
void microbit_hal_audio_select_speaker(bool enable) {
72+
uBit.audio.setSpeakerEnabled(enable);
73+
}
74+
6275
// Input value has range 0-255 inclusive.
6376
void microbit_hal_audio_set_volume(int value) {
6477
if (value >= 255) {

0 commit comments

Comments
 (0)