Skip to content

Commit 0e1c7ea

Browse files
committed
PWMAudioStreamPico
1 parent ece3afb commit 0e1c7ea

File tree

5 files changed

+32
-41
lines changed

5 files changed

+32
-41
lines changed

src/AudioPWM/PWMAudioBase.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -221,10 +221,10 @@ class PWMAudioStreamBase : public AudioPrint, public AudioBaseInfoDependent {
221221
// blocking write for an array: we expect a singed value and convert it into a unsigned
222222
virtual size_t write(const uint8_t *wrt_buffer, size_t size){
223223
size_t available = min((size_t)availableForWrite(),size);
224-
LOGD("write: %zu bytes -> %zu", size, available);
224+
LOGD("write: %u bytes -> %u", (unsigned int)size, (unsigned int)available);
225225
size_t result = buffer->writeArray(wrt_buffer, available);
226226
if (result!=available){
227-
LOGW("Could not write all data: %d -> %d", size, result);
227+
LOGW("Could not write all data: %u -> %d", (unsigned int) size, result);
228228
}
229229
// activate the timer now - if not already done
230230
startTimer();

src/AudioPWM/PWMAudioMBED.h

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@
22
#pragma once
33
#if defined(__arm__) && __has_include("mbed.h")
44
#include "AudioPWM/PWMAudioBase.h"
5+
#include "AudioTimer/AudioTimer.h"
56
#include "mbed.h"
67

78
namespace audio_tools {
89

910
// forward declaration
1011
class PWMAudioStreamMBED;
1112
typedef PWMAudioStreamMBED PWMAudioStream;
12-
static PWMAudioStreamMBED *accessAudioPWM = nullptr;
1313

1414
/**
1515
* @brief Audio output to PWM pins for MBED based Arduino implementations
@@ -18,19 +18,17 @@ static PWMAudioStreamMBED *accessAudioPWM = nullptr;
1818
*/
1919

2020
class PWMAudioStreamMBED : public PWMAudioStreamBase {
21-
friend void defaultPWMAudioOutputCallback();
2221

2322
public:
2423

2524
PWMAudioStreamMBED(){
2625
LOGD("PWMAudioStreamMBED");
27-
accessAudioPWM = this;
2826
}
2927

3028
// Ends the output
31-
virtual void end(){
29+
virtual void end() override {
3230
LOGD(LOG_METHOD);
33-
ticker.detach(); // it does not hurt to call this even if it has not been started
31+
ticker.end(); // it does not hurt to call this even if it has not been started
3432
is_timer_started = false;
3533

3634
// stop and release pins
@@ -48,14 +46,15 @@ class PWMAudioStreamMBED : public PWMAudioStreamBase {
4846

4947
protected:
5048
Vector<mbed::PwmOut*> pins;
51-
mbed::Ticker ticker; // calls a callback repeatedly with a timeout
49+
TimerAlarmRepeating ticker; // calls a callback repeatedly with a timeout
5250

5351
/// when we get the first write -> we activate the timer to start with the output of data
54-
virtual void startTimer(){
52+
virtual void startTimer() override {
5553
if (!is_timer_started){
5654
LOGD(LOG_METHOD);
5755
long wait_time = 1000000l / audio_config.sample_rate;
58-
ticker.attach_us(defaultPWMAudioOutputCallback, wait_time);
56+
ticker.setCallbackParameter(this);
57+
ticker.begin(defaultPWMAudioOutputCallback, wait_time, US);
5958
is_timer_started = true;
6059
}
6160
}
@@ -99,15 +98,17 @@ class PWMAudioStreamMBED : public PWMAudioStreamBase {
9998
float float_value = static_cast<float>(value) / maxOutputValue();
10099
pins[channel]->write(float_value); // pwm the value is between 0.0 and 1.0
101100
}
102-
101+
102+
/// timer callback: write the next frame to the pins
103+
static inline void defaultPWMAudioOutputCallback(void *obj) {
104+
PWMAudioStreamMBED* accessAudioPWM = (PWMAudioStreamMBED*) obj;
105+
if (accessAudioPWM!=nullptr){
106+
accessAudioPWM->playNextFrame();
107+
}
108+
}
109+
103110
};
104111

105-
/// timer callback: write the next frame to the pins
106-
void defaultPWMAudioOutputCallback() {
107-
if (accessAudioPWM!=nullptr){
108-
accessAudioPWM->playNextFrame();
109-
}
110-
}
111112

112113
} // Namespace
113114

src/AudioPWM/PWMAudioRP2040.h

Lines changed: 12 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ namespace audio_tools {
1212
// forwrd declaratioin of callback
1313
class PWMAudioStreamPico;
1414
typedef PWMAudioStreamPico PWMAudioStream;
15-
//bool defaultPWMAudioOutputCallbackPico(repeating_timer* ptr);
1615

1716
/**
1817
* @brief Rasperry Pico Channel to pin assignments
@@ -47,10 +46,8 @@ class PWMAudioStreamPico : public PWMAudioStreamBase {
4746
/// Ends the output -> resets the timer and the pins
4847
void end() override {
4948
LOGD(LOG_METHOD);
49+
ticker.end(); // it does not hurt to call this even if it has not been started
5050
is_timer_started = false;
51-
if (!cancel_repeating_timer(&timer)){
52-
LOGE("cancel_repeating_timer failed")
53-
}
5451
for(auto pin : pins) {
5552
if (pin.gpio!=-1){
5653
pwm_set_enabled(pin.slice, false);
@@ -60,10 +57,16 @@ class PWMAudioStreamPico : public PWMAudioStreamBase {
6057

6158
protected:
6259
Vector<PicoChannelOut> pins;
63-
repeating_timer_t timer;
60+
TimerAlarmRepeating ticker;
6461

6562
virtual void startTimer() override {
66-
//LOGI(LOG_METHOD);
63+
if (!is_timer_started){
64+
LOGD(LOG_METHOD);
65+
long wait_time = 1000000l / audio_config.sample_rate;
66+
ticker.setCallbackParameter(this);
67+
ticker.begin(defaultPWMAudioOutputCallbackPico, wait_time, US);
68+
is_timer_started = true;
69+
}
6770
is_timer_started = true;
6871
}
6972

@@ -128,15 +131,6 @@ class PWMAudioStreamPico : public PWMAudioStreamBase {
128131
}
129132

130133
void setupTimer() override {
131-
LOGD(LOG_METHOD);
132-
// setup timer
133-
uint32_t time = 1000000UL / audio_config.sample_rate;
134-
LOGI("->Timer value %ld us", time);
135-
if (!add_repeating_timer_us(-time, &defaultPWMAudioOutputCallbackPico, this, &timer)){
136-
LOGE("Error: alarm_pool_add_repeating_timer_us failed; no alarm slots available");
137-
} else {
138-
LOGI("repeating timer has started");
139-
}
140134
}
141135

142136
/// The pico supports max 16 pwm pins
@@ -155,12 +149,9 @@ class PWMAudioStreamPico : public PWMAudioStreamBase {
155149
}
156150

157151
// timed output executed at the sampleRate
158-
static bool defaultPWMAudioOutputCallbackPico(repeating_timer* ptr) {
159-
PWMAudioStreamPico *self = (PWMAudioStreamPico*) ptr->user_data;
160-
//if (self!=nullptr){
161-
self->playNextFrame();
162-
//}
163-
return true;
152+
inline static void defaultPWMAudioOutputCallbackPico(void* ptr) {
153+
PWMAudioStreamPico *self = (PWMAudioStreamPico*) ptr;
154+
self->playNextFrame();
164155
}
165156

166157
};

src/AudioTimer/AudioTimerRP2040.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,12 +69,11 @@ class TimerAlarmRepeatingRP2040 : public TimerAlarmRepeatingDef{
6969
protected:
7070
alarm_pool_t *ap = nullptr;
7171
repeating_timer_t timer;
72-
my_repeating_timer_callback_t instanceCallback;
72+
my_repeating_timer_callback_t instanceCallback=nullptr;
7373
};
7474

7575
typedef TimerAlarmRepeatingRP2040 TimerAlarmRepeating;
7676

77-
7877
}
7978

8079

src/AudioTools/AudioStreams.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,7 @@ class GeneratedSoundStream : public AudioStreamX, public AudioBaseInfoSource {
259259

260260
/// privide the data as byte stream
261261
size_t readBytes(uint8_t *buffer, size_t length) override {
262-
LOGD("GeneratedSoundStream::readBytes: %zu", length);
262+
LOGD("GeneratedSoundStream::readBytes: %u", (unsigned int)length);
263263
return generator_ptr->readBytes(buffer, length);
264264
}
265265

0 commit comments

Comments
 (0)