Skip to content

Commit 9e0cf30

Browse files
committed
Rename Pins to DriverPins
1 parent a830676 commit 9e0cf30

File tree

9 files changed

+200
-55
lines changed

9 files changed

+200
-55
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ Supported audio codec chips are e.g
1616
- ES7243
1717
- etc
1818

19-
While you can use this library stand alone, I recommend to use it together with my [AudioTools](https://github.com/pschatzmann/arduino-audio-tools) project which provides a nice integration with it's I2SCodecStream class.
19+
While you can use this library stand alone, I recommend to use it together with my [AudioTools](https://github.com/pschatzmann/arduino-audio-tools) project which provides a nice integration with it's [I2SCodecStream](https://pschatzmann.github.io/arduino-audio-tools/classaudio__tools_1_1_i2_s_codec_stream.html) class.
2020

2121
## AudioTools
2222

@@ -75,8 +75,8 @@ You can also easily define your custom boards by defining the __driver__ and you
7575

7676
#include "AudioBoard.h"
7777

78-
Pins my_pins;
79-
AudioBoard board(my_pins, &AudioDriverES8388);
78+
DriverPins my_pins;
79+
AudioBoard board(&AudioDriverES8388, my_pins);
8080

8181
void setup() {
8282
// add i2c codec pins: scl, sda, port
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/**
2+
* @brief We define a custom board with the i2c and i2s pins and output a sine
3+
* with the help of the AudioTools I2SCodecStream
4+
* @author phil schatzmann
5+
*/
6+
7+
#include "AudioTools.h" // install https://github.com/pschatzmann/arduino-audio-tools
8+
#include "AudioLibs/I2SCodecStream.h"
9+
10+
AudioInfo info(44100, 2, 16);
11+
SineWaveGenerator<int16_t> sineWave(32000);
12+
GeneratedSoundStream<int16_t> sound(sineWave);
13+
DriverPins my_pins;
14+
AudioBoard board(&AudioDriverES8388, my_pins);
15+
I2SCodecStream out(board);
16+
StreamCopy copier(out, sound);
17+
18+
void setup() {
19+
// Setup logging
20+
Serial.begin(115200);
21+
AudioLogger::instance().begin(Serial, AudioLogger::Info);
22+
LOGLEVEL_AUDIODRIVER = AudioDriverInfo;
23+
24+
// setup pins
25+
// - add i2c codec pins: scl, sda, port, (I2C object)
26+
my_pins.addI2C(CODEC, 32, 22, 0x20, 100000, Wire);
27+
// - add i2s pins: mclk, bclk, ws, data_out, data_in
28+
my_pins.addI2S(CODEC, 0, 14, 15, 22);
29+
30+
31+
// start I2S & codec with i2c and i2s configured above
32+
Serial.println("starting I2S...");
33+
auto config = out.defaultConfig();
34+
config.copyFrom(info);
35+
out.begin(config);
36+
37+
// Setup sine wave
38+
sineWave.begin(info, N_B4);
39+
}
40+
41+
// Arduino loop - copy sound to out
42+
void loop() { copier.copy(); }
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/**
2+
* @brief We define a custom board w/o any pins and output a sine tone
3+
* with the help of the AudioTools I2SCodecStream
4+
* @author phil schatzmann
5+
*/
6+
7+
#include "AudioTools.h" // install https://github.com/pschatzmann/arduino-audio-tools
8+
#include "AudioLibs/I2SCodecStream.h"
9+
10+
AudioInfo info(44100, 2, 16);
11+
SineWaveGenerator<int16_t> sineWave(32000);
12+
GeneratedSoundStream<int16_t> sound(sineWave);
13+
AudioBoard board(&AudioDriverES8388);
14+
I2SCodecStream out(board);
15+
StreamCopy copier(out, sound);
16+
17+
void setup() {
18+
// Setup logging
19+
Serial.begin(115200);
20+
AudioLogger::instance().begin(Serial, AudioLogger::Info);
21+
LOGLEVEL_AUDIODRIVER = AudioDriverInfo;
22+
23+
// initialize i2c because board has no i2c definition
24+
Wire.begin();
25+
26+
// start I2S & codec
27+
Serial.println("starting I2S...");
28+
auto config = out.defaultConfig();
29+
config.copyFrom(info);
30+
// define your i2s pins if you dont want to use the default pins
31+
config.pin_bck = 14;
32+
config.pin_ws = 15;
33+
config.pin_data = 22;
34+
out.begin(config);
35+
36+
// Setup sine wave
37+
sineWave.begin(info, N_B4);
38+
}
39+
40+
// Arduino loop - copy sound to out
41+
void loop() { copier.copy(); }
Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,38 @@
1+
/**
2+
* @brief We use a predefined AudioKitEs8388V1 board and output a sine wave via i2s
3+
* using the AudioTools I2SCodecStream
4+
* @author phil schatzmann
5+
*/
6+
17
#include "AudioTools.h" // install https://github.com/pschatzmann/arduino-audio-tools
28
#include "AudioLibs/I2SCodecStream.h"
39

4-
AudioInfo info(32000, 2, 16);
5-
SineWaveGenerator<int16_t> sineWave(32000); // subclass of SoundGenerator with max amplitude of 32000
6-
GeneratedSoundStream<int16_t> sound(sineWave); // Stream generated from sine wave
10+
AudioInfo info(44100, 2, 16);
11+
SineWaveGenerator<int16_t> sineWave(32000);
12+
GeneratedSoundStream<int16_t>sound(sineWave);
713
I2SCodecStream out(AudioKitEs8388V1);
8-
StreamCopy copier(out, sound); // copies sound into i2s
14+
StreamCopy copier(out, sound);
915

1016
// Arduino Setup
11-
void setup(void) {
12-
// Open Serial
17+
void setup(void) {
18+
// Setup logging
1319
Serial.begin(115200);
1420
AudioLogger::instance().begin(Serial, AudioLogger::Info);
15-
// logging for driver
16-
LOGLEVEL_AUDIODRIVER = AudioDriverInfo;
17-
21+
LOGLEVEL_AUDIODRIVER = AudioDriverInfo;
1822

19-
// start I2S
23+
// start I2S & codec
2024
Serial.println("starting I2S...");
21-
auto config = out.defaultConfig(TX_MODE);
22-
config.copyFrom(info);
25+
auto config = out.defaultConfig();
26+
config.copyFrom(info);
2327
out.begin(config);
2428

29+
// set volume
30+
out.setVolume(1.0);
31+
2532
// Setup sine wave
2633
sineWave.begin(info, N_B4);
2734
Serial.println("started...");
2835
}
2936

30-
// Arduino loop - copy sound to out
31-
void loop() {
32-
copier.copy();
33-
}
37+
// Arduino loop - copy sound to out
38+
void loop() { copier.copy(); }

examples/custom-max/custom-max.ino

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/**
2+
* @brief We just set up the codec and the i2c pins, so that the wire is
3+
* automatically initialized with the indicated pins.
4+
* After this you can set up and use i2s
5+
* @author phil schatzmann
6+
*/
7+
8+
#include "AudioBoard.h"
9+
10+
AudioBoard board(&AudioDriverES8388);
11+
12+
void setup() {
13+
// add i2c codec pins: scl, sda, port
14+
my_pins.addI2C(CODEC, 32, 22, 0x20);
15+
// example add other pins: PA on gpio 21
16+
my_pins.addPin(PA, 21);
17+
18+
// configure codec
19+
CodecConfig cfg;
20+
cfg.adc_input = ADC_INPUT_LINE1;
21+
cfg.dac_output = DAC_OUTPUT_ALL;
22+
cfg.i2s.bits = BIT_LENGTH_16BITS;
23+
cfg.i2s.rate = RATE_44K;
24+
// cfg.i2s.fmt = I2S_NORMAL;
25+
// cfg.i2s.mode = MODE_SLAVE;
26+
board.begin(cfg);
27+
}
28+
29+
void loop() {}

examples/custom-min/custom-min.ino

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/**
2+
* @brief We just set up the codec. Because I2C was not defined we need to
3+
* initilize it ourself After this you can set up and use i2s
4+
* @author phil schatzmann
5+
*/
6+
7+
#include "AudioBoard.h"
8+
9+
AudioBoard board(&AudioDriverES8388, my_pins);
10+
11+
void setup() {
12+
// start I2C for the communication with the codec
13+
Wire.begin();
14+
// configure codec
15+
CodecConfig cfg;
16+
cfg.adc_input = ADC_INPUT_LINE1;
17+
cfg.dac_output = DAC_OUTPUT_ALL;
18+
cfg.i2s.bits = BIT_LENGTH_16BITS;
19+
cfg.i2s.rate = RATE_44K;
20+
// cfg.i2s.fmt = I2S_NORMAL;
21+
// cfg.i2s.mode = MODE_SLAVE;
22+
board.begin(cfg);
23+
}
24+
25+
void loop() {}

src/AudioBoard.h

Lines changed: 25 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,18 @@
44

55
namespace audio_driver {
66

7+
/// @ingroup audio_driver @brief Pins need to be set up in the sketch
8+
static DriverPins NoPins;
9+
710
/**
811
* @brief Defitintion for audio board pins and an audio driver
912
* @ingroup audio_driver
1013
*/
1114
class AudioBoard {
1215
public:
13-
AudioBoard(Pins pins, AudioDriver *driver) {
16+
AudioBoard(AudioDriver *driver, DriverPins pins=NoPins) {
1417
this->pins = pins;
15-
this->driver =driver;
18+
this->driver = driver;
1619
}
1720

1821
bool begin(){
@@ -30,7 +33,7 @@ class AudioBoard {
3033
bool setMute(bool enable) { return driver->setMute(enable); }
3134
bool setVolume(int volume) { return driver->setVolume(volume); }
3235
int getVolume() { return driver->getVolume(); }
33-
Pins& getPins() { return pins; }
36+
DriverPins& getPins() { return pins; }
3437
bool setPAPower(bool enable) { return driver->setPAPower(enable); }
3538
/// set volume for adc: this is only supported on some defined codecs
3639
bool setInputVolume(int volume) {return driver->setInputVolume(volume);}
@@ -40,51 +43,51 @@ class AudioBoard {
4043
}
4144

4245
protected:
43-
Pins pins;
46+
DriverPins pins;
4447
CodecConfig codec_cfg;
4548
AudioDriver* driver = nullptr;
4649
};
4750

4851
// -- Drivers
4952
/// @ingroup audio_driver
50-
AudioDriverES8388Class AudioDriverES8388;
53+
static AudioDriverES8388Class AudioDriverES8388;
5154
/// @ingroup audio_driver
52-
AudioDriverAC101Class AudioDriverAC101;
55+
static AudioDriverAC101Class AudioDriverAC101;
5356
/// @ingroup audio_driver
54-
AudioDriverCS43l22Class AudioDriverCS43l22;
57+
static AudioDriverCS43l22Class AudioDriverCS43l22;
5558
/// @ingroup audio_driver
56-
AudioDriverES8311Class AudioDriverES8311;
59+
static AudioDriverES8311Class AudioDriverES8311;
5760
/// @ingroup audio_driver
58-
AudioDriverES7243Class AudioDriverES7243;
61+
static AudioDriverES7243Class AudioDriverES7243;
5962
/// @ingroup audio_driver
60-
AudioDriverLyratMiniClass AudioDriverLyratMini;
63+
static AudioDriverLyratMiniClass AudioDriverLyratMini;
6164

6265
// -- Pins
6366
/// @ingroup audio_driver
64-
PinsLyrat43Class PinsLyrat43;
67+
static PinsLyrat43Class PinsLyrat43;
6568
/// @ingroup audio_driver
66-
PinsLyrat42Class PinsLyrat42;
69+
static PinsLyrat42Class PinsLyrat42;
6770
/// @ingroup audio_driver
68-
PinsLyratMiniClass PinsLyratMini;
71+
static PinsLyratMiniClass PinsLyratMini;
6972
/// @ingroup audio_driver
70-
PinsAudioKitEs8388v1Class PinsAudioKitEs8388v1;
73+
static PinsAudioKitEs8388v1Class PinsAudioKitEs8388v1;
7174
/// @ingroup audio_driver
72-
PinsAudioKitEs8388v2Class PinsAudioKitEs8388v2;
75+
static PinsAudioKitEs8388v2Class PinsAudioKitEs8388v2;
7376
/// @ingroup audio_driver
74-
PinsAudioKitAC101Class PinsAudioKitAC101;
77+
static PinsAudioKitAC101Class PinsAudioKitAC101;
7578

7679
// -- Boards
7780
/// @ingroup audio_driver
78-
AudioBoard AudioKitEs8388V1{PinsAudioKitEs8388v1, &AudioDriverES8388};
81+
static AudioBoard AudioKitEs8388V1{&AudioDriverES8388, PinsAudioKitEs8388v1};
7982
/// @ingroup audio_driver
80-
AudioBoard AudioKitEs8388V2{PinsAudioKitEs8388v2, &AudioDriverES8388};
83+
static AudioBoard AudioKitEs8388V2{&AudioDriverES8388, PinsAudioKitEs8388v2};
8184
/// @ingroup audio_driver
82-
AudioBoard AudioKitAC101{PinsAudioKitAC101, &AudioDriverAC101};
85+
static AudioBoard AudioKitAC101{&AudioDriverAC101, PinsAudioKitAC101};
8386
/// @ingroup audio_driver
84-
AudioBoard LyratV43{PinsLyrat43, &AudioDriverES8388};
87+
static AudioBoard LyratV43{&AudioDriverES8388, PinsLyrat43};
8588
/// @ingroup audio_driver
86-
AudioBoard LyratV42{PinsLyrat42, &AudioDriverES8388};
89+
static AudioBoard LyratV42{&AudioDriverES8388, PinsLyrat42};
8790
/// @ingroup audio_driver
88-
AudioBoard LyratMini{PinsLyratMini, &AudioDriverLyratMini};
91+
static AudioBoard LyratMini{&AudioDriverLyratMini, PinsLyratMini};
8992

9093
}

src/Driver.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ class CodecConfig : public codec_config_t {
6161
*/
6262
class AudioDriver {
6363
public:
64-
virtual bool begin(CodecConfig codecCfg, Pins &pins) {
64+
virtual bool begin(CodecConfig codecCfg, DriverPins &pins) {
6565
codec_cfg = codecCfg;
6666
p_pins = &pins;
6767
if (!init(codec_cfg)){
@@ -89,7 +89,7 @@ class AudioDriver {
8989
virtual bool isVolumeSupported() { return true; }
9090
virtual bool isInputVolumeSupported() { return false; }
9191

92-
Pins &pins() { return *p_pins; }
92+
DriverPins &pins() { return *p_pins; }
9393

9494
/// Sets the PA Power pin to active or inactive
9595
bool setPAPower(bool enable) {
@@ -103,7 +103,7 @@ class AudioDriver {
103103

104104
protected:
105105
CodecConfig codec_cfg;
106-
Pins *p_pins = nullptr;
106+
DriverPins *p_pins = nullptr;
107107

108108
virtual bool init(codec_config_t codec_cfg) { return false; }
109109
virtual bool deinit() { return false; }
@@ -249,7 +249,7 @@ class AudioDriverES7243Class : public AudioDriver {
249249
*/
250250
class AudioDriverLyratMiniClass : public AudioDriver {
251251
public:
252-
bool begin(CodecConfig codecCfg, Pins &pins) {
252+
bool begin(CodecConfig codecCfg, DriverPins &pins) {
253253
int rc = 0;
254254
if (codecCfg.dac_output != DAC_OUTPUT_NONE)
255255
rc += !dac.begin(codecCfg, pins);
@@ -318,7 +318,7 @@ class AudioDriverCS43l22Class : public AudioDriver {
318318

319319
void setI2CAddress(uint16_t adr) { deviceAddr = adr; }
320320

321-
virtual bool begin(CodecConfig codecCfg, Pins &pins) {
321+
virtual bool begin(CodecConfig codecCfg, DriverPins &pins) {
322322
codec_cfg = codecCfg;
323323
// manage reset pin -> acive high
324324
setPAPower(true);

0 commit comments

Comments
 (0)