Skip to content

Commit

Permalink
Merge branch 'master' into pimoroni_plasma
Browse files Browse the repository at this point in the history
  • Loading branch information
earlephilhower authored Dec 19, 2024
2 parents 0a06955 + eecbcdf commit dfbead6
Show file tree
Hide file tree
Showing 10 changed files with 87 additions and 35 deletions.
4 changes: 2 additions & 2 deletions .readthedocs.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ sphinx:
# fail_on_warning: true

# Optionally build your docs in additional formats such as PDF and ePub
# formats:
# - pdf
formats:
- pdf
# - epub

# Optional but recommended, declare the Python requirements required
Expand Down
21 changes: 21 additions & 0 deletions cores/rp2040/AudioOutputBase.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Abstract class for audio output devices to allow easy swapping between output devices

#pragma once

#include <Print.h>

class AudioOutputBase : public Print {
public:
virtual ~AudioOutputBase() { }
virtual bool setBuffers(size_t buffers, size_t bufferWords, int32_t silenceSample = 0) = 0;
virtual bool setBitsPerSample(int bps) = 0;
virtual bool setFrequency(int freq) = 0;
virtual bool setStereo(bool stereo = true) = 0;
virtual bool begin() = 0;
virtual bool end() = 0;
virtual bool getUnderflow() = 0;
virtual void onTransmit(void(*)(void *), void *) = 0;
// From Print
virtual size_t write(const uint8_t *buffer, size_t size) = 0;
virtual int availableForWrite() = 0;
};
1 change: 0 additions & 1 deletion libraries/AudioBufferManager/src/AudioBufferManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,6 @@ class AudioBufferManager {
delete ab;
}

int _bitsPerSample;
size_t _wordsPerBuffer;
size_t _bufferCount;
enum dma_channel_transfer_size _dmaSize;
Expand Down
3 changes: 3 additions & 0 deletions libraries/BluetoothAudio/src/A2DPSource.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,8 @@ void A2DPSource::clearPairing() {
size_t A2DPSource::write(const uint8_t *buffer, size_t size) {
BluetoothLock b;

size = std::min((size_t)availableForWrite(), size);

size_t count = 0;
size /= 2;

Expand Down Expand Up @@ -260,6 +262,7 @@ int A2DPSource::availableForWrite() {
} else {
avail = _pcmBufferSize - _pcmWriter + _pcmReader - 1;
}
avail /= sizeof(uint32_t); // availableForWrite always 32b sample pairs in this core...
return avail;
}

Expand Down
27 changes: 22 additions & 5 deletions libraries/BluetoothAudio/src/A2DPSource.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,20 @@
#include <BluetoothHCI.h>
#include <BluetoothLock.h>
#include "BluetoothMediaConfigurationSBC.h"
#include <AudioOutputBase.h>
#include <functional>
#include <list>
#include <memory>


class A2DPSource : public Stream {
class A2DPSource : public Stream, public AudioOutputBase {
public:
A2DPSource() {
}

bool setFrequency(uint32_t rate) {
virtual ~A2DPSource() { }

virtual bool setFrequency(int rate) override {
if (_running || ((rate != 44100) && (rate != 48000))) {
return false;
}
Expand All @@ -51,7 +54,14 @@ class A2DPSource : public Stream {
return true;
}

void onTransmit(void (*cb)(void *), void *cbData = nullptr) {
virtual bool setBitsPerSample(int bps) override {
return bps == 16;
}
virtual bool setStereo(bool stereo = true) override {
return stereo;
}

virtual void onTransmit(void (*cb)(void *), void *cbData = nullptr) override {
_transmitCB = cb;
_transmitData = cbData;
}
Expand Down Expand Up @@ -84,7 +94,11 @@ class A2DPSource : public Stream {
return true;
}

bool getUnderflow() {
virtual bool setBuffers(size_t buffers, size_t bufferWords, int32_t silenceSample = 0) override {
return setBufferSize(buffers * bufferWords * sizeof(int32_t));
}

virtual bool getUnderflow() override {
BluetoothLock b;
if (!_running) {
return false;
Expand All @@ -102,7 +116,10 @@ class A2DPSource : public Stream {
}
}

bool begin();
virtual bool begin() override;
virtual bool end() override {
return false; // We can't actually stop bluetooth on this device
}

std::vector<BTDeviceInfo> scan(uint32_t mask = BluetoothHCI::speaker_cod, int scanTimeSec = 5, bool async = false) {
return _hci.scan(mask, scanTimeSec, async);
Expand Down
8 changes: 3 additions & 5 deletions libraries/I2S/examples/SimpleTone/SimpleTone.ino
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@

#include <I2S.h>

// Create the I2S port using a PIO state machine
I2S i2s(OUTPUT);

// GPIO pin numbers
#define pBCLK 20
#define pWS (pBCLK+1)
#define pDOUT 22

// Create the I2S port using a PIO state machine
I2S i2s(OUTPUT, pBCLK, pDOUT);

const int frequency = 440; // frequency of square wave in Hz
const int amplitude = 500; // amplitude of square wave
const int sampleRate = 16000; // minimum for UDA1334A
Expand All @@ -43,8 +43,6 @@ void setup() {
Serial.begin(115200);
Serial.println("I2S simple tone");

i2s.setBCLK(pBCLK);
i2s.setDATA(pDOUT);
i2s.setBitsPerSample(16);

// start I2S at the sample rate with 16-bits per sample
Expand Down
11 changes: 6 additions & 5 deletions libraries/I2S/src/I2S.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,14 @@
#include <pico/stdlib.h>


I2S::I2S(PinMode direction) {
I2S::I2S(PinMode direction, pin_size_t bclk, pin_size_t data, pin_size_t mclk) {
_running = false;
_bps = 16;
_writtenHalf = false;
_isOutput = direction == OUTPUT;
_pinBCLK = 26;
_pinDOUT = 28;
_pinMCLK = 25;
_pinBCLK = bclk;
_pinDOUT = data;
_pinMCLK = mclk;
_MCLKenabled = false;
#ifdef PIN_I2S_BCLK
_pinBCLK = PIN_I2S_BCLK;
Expand Down Expand Up @@ -287,7 +287,7 @@ bool I2S::begin() {
return true;
}

void I2S::end() {
bool I2S::end() {
if (_running) {
if (_MCLKenabled) {
pio_sm_set_enabled(_pioMCLK, _smMCLK, false);
Expand All @@ -301,6 +301,7 @@ void I2S::end() {
delete _i2s;
_i2s = nullptr;
}
return true;
}

int I2S::available() {
Expand Down
23 changes: 15 additions & 8 deletions libraries/I2S/src/I2S.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,23 @@

#pragma once
#include <Arduino.h>
#include "AudioBufferManager.h"
#include <AudioBufferManager.h>
#include <AudioOutputBase.h>

class I2S : public Stream {
class I2S : public Stream, public AudioOutputBase {
public:
I2S(PinMode direction = OUTPUT);
I2S(PinMode direction = OUTPUT, pin_size_t bclk = 26, pin_size_t data = 28, pin_size_t mclk = 25);
virtual ~I2S();

bool setBCLK(pin_size_t pin);
bool setDATA(pin_size_t pin);
bool setMCLK(pin_size_t pin);
bool setBitsPerSample(int bps);
bool setBuffers(size_t buffers, size_t bufferWords, int32_t silenceSample = 0);
bool setFrequency(int newFreq);
virtual bool setBitsPerSample(int bps) override;
virtual bool setBuffers(size_t buffers, size_t bufferWords, int32_t silenceSample = 0) override;
virtual bool setFrequency(int newFreq) override;
virtual bool setStereo(bool stereo = true) override {
return stereo;
}
bool setLSBJFormat();
bool setTDMFormat();
bool setTDMChannels(int channels);
Expand All @@ -46,8 +50,11 @@ class I2S : public Stream {
return begin();
}

bool begin();
void end();
virtual bool begin() override;
virtual bool end() override;
virtual bool getUnderflow() override {
return getOverUnderflow();
}

// from Stream
virtual int available() override;
Expand Down
6 changes: 4 additions & 2 deletions libraries/PWMAudio/src/PWMAudio.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ PWMAudio::~PWMAudio() {
end();
}

bool PWMAudio::setBuffers(size_t buffers, size_t bufferWords) {
bool PWMAudio::setBuffers(size_t buffers, size_t bufferWords, int32_t silence) {
(void) silence;
if (_running || (buffers < 3) || (bufferWords < 8)) {
return false;
}
Expand Down Expand Up @@ -176,7 +177,7 @@ bool PWMAudio::begin() {
return true;
}

void PWMAudio::end() {
bool PWMAudio::end() {
if (_running) {
_running = false;
pinMode(_pin, OUTPUT);
Expand All @@ -189,6 +190,7 @@ void PWMAudio::end() {
dma_timer_unclaim(_pacer);
_pacer = -1;
}
return true;
}

int PWMAudio::available() {
Expand Down
18 changes: 11 additions & 7 deletions libraries/PWMAudio/src/PWMAudio.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,24 @@

#pragma once
#include <Arduino.h>
#include "AudioBufferManager.h"
#include <AudioOutputBase.h>
#include <AudioBufferManager.h>

class PWMAudio : public Stream {
class PWMAudio : public Stream, public AudioOutputBase {
public:
PWMAudio(pin_size_t pin = 0, bool stereo = false);
virtual ~PWMAudio();

bool setBuffers(size_t buffers, size_t bufferWords);
virtual bool setBuffers(size_t buffers, size_t bufferWords, int32_t silenceSample = 0) override;
/*Sets the frequency of the PWM in hz*/
bool setPWMFrequency(int newFreq);
/*Sets the sample rate frequency in hz*/
bool setFrequency(int frequency);
virtual bool setFrequency(int frequency) override;
bool setPin(pin_size_t pin);
bool setStereo(bool stereo = true);
virtual bool setStereo(bool stereo = true) override;
virtual bool setBitsPerSample(int bits) override {
return bits == 16;
}

bool begin(long sampleRate) {
_sampleRate = sampleRate;
Expand All @@ -47,8 +51,8 @@ class PWMAudio : public Stream {
return begin();
}

bool begin();
void end();
virtual bool begin() override;
virtual bool end() override;

// from Stream
virtual int available() override;
Expand Down

0 comments on commit dfbead6

Please sign in to comment.