arduino-audio-tools
Loading...
Searching...
No Matches
R2ROutput.h
Go to the documentation of this file.
1#pragma once
2
3#include "AudioToolsConfig.h"
9namespace audio_tools {
10
19 public:
20 virtual void setupPins(Vector<int> &channel1_pins,
21 Vector<int> &channel2_pins) = 0;
22
23 virtual void writePins(int channels, int channel, unsigned uvalue) = 0;
24};
25
34class R2RDriver : public R2RDriverBase {
35 public:
36 void setupPins(Vector<int> &channel1_pins,
37 Vector<int> &channel2_pins) override {
38 TRACED();
39 p_channel1_pins = &channel1_pins;
40 p_channel2_pins = &channel2_pins;
41
42 for (auto pin : channel1_pins) {
43 LOGI("Setup channel1 pin %d", pin);
44 pinMode(pin, OUTPUT);
45 }
46 for (int pin : channel2_pins) {
47 LOGI("Setup channel2 pin %d", pin);
48 pinMode(pin, OUTPUT);
49 }
50 };
51
52 void writePins(int channels, int channel, unsigned uvalue) override {
53 switch (channel) {
54 case 0:
55 for (int j = 0; j < (*p_channel1_pins).size(); j++) {
56 int pin = (*p_channel1_pins)[j];
57 if (pin >= 0) digitalWrite(pin, (uvalue >> j) & 1);
58 }
59 break;
60 case 1:
61 for (int j = 0; j < (*p_channel2_pins).size(); j++) {
62 int pin = (*p_channel2_pins)[j];
63 if (pin >= 0) digitalWrite(pin, (uvalue >> j) & 1);
64 }
65 break;
66 }
67 }
68
69 protected:
73
91
105class R2ROutput : public AudioOutput {
106 public:
108 R2RConfig r;
109 return r;
110 }
111
113 TRACED();
114 cfg = c;
115 rcfg = c;
116 return begin();
117 }
118
119 bool begin() override {
120 TRACED();
121 if (cfg.channels == 0 || cfg.channels > 2) {
122 LOGE("channels is %d", cfg.channels);
123 return false;
124 }
125 if (rcfg.channel1_pins.size() == 0) {
126 LOGE("channel1_pins not defined");
127 return false;
128 }
129 if (cfg.channels == 2 &&
131 LOGE("channel2_pins not defined");
132 return false;
133 }
134 if (rcfg.buffer_size * rcfg.buffer_count == 0) {
135 LOGE("buffer_size or buffer_count is 0");
136 return false;
137 }
140
141 // setup timer
143 timer.setIsSave(true);
146 }
147
148 size_t write(const uint8_t *data, size_t len) override {
149 LOGD("write: %d", len);
150 size_t result = 0;
151 // if buffer has not been allocated (buffer_size==0)
152 if (len > rcfg.buffer_size) {
153 LOGE("buffer_size %d too small for write size: %d", rcfg.buffer_size,
154 len);
155 return len;
156 }
157
158 if (rcfg.is_blocking){
159 // write of all bytes
160 int open = len;
161 while(open > 0){
162 int written = buffer.writeArray(data + result, open);
163 open -= written;
164 result += written;
165 if (open > 0){
167 }
168 }
169 } else {
170 // write as much as possible
171 result = buffer.writeArray(data, len);
172 }
173 // activate output when buffer is half full
175 LOGI("is_active = true");
176 is_active = true;
177 }
178
179 return result;
180 }
181
182 protected:
184 // Double buffer
187
188 void writeValue(int channel) {
189 switch (cfg.bits_per_sample) {
190 case 8:
191 return writeValueT<int8_t>(channel);
192 case 16:
193 return writeValueT<int16_t>(channel);
194 case 24:
195 return writeValueT<int24_t>(channel);
196 case 32:
197 return writeValueT<int32_t>(channel);
198 }
199 }
200
201 template <typename T>
202 void writeValueT(int channel) {
203 // don't do anything if we do not have enough data
204 if (buffer.available() < sizeof(T)) return;
205
206 // get next value from buffer
207 T value = 0;
208 buffer.readArray((uint8_t *)&value, sizeof(T));
209 // convert to unsigned
210 unsigned uvalue = (int)value + NumberConverter::maxValueT<T>() + 1;
211 // scale value
212 uvalue = uvalue >> ((sizeof(T) * 8) - rcfg.channel1_pins.size());
213 // Serial.println(uvalue);
214
215 // output pins
217 }
218
219 static void r2r_timer_callback(void *ptr) {
220 R2ROutput *self = (R2ROutput *)ptr;
221 if (self->is_active) {
222 // output channel 1
223 self->writeValue(0);
224 // output channel 2
225 if (self->cfg.channels == 2) self->writeValue(1);
226 };
227 }
228};
229
230} // namespace audio_tools
#define TRACED()
Definition AudioLoggerIDF.h:31
#define LOGI(...)
Definition AudioLoggerIDF.h:28
#define LOGD(...)
Definition AudioLoggerIDF.h:27
#define LOGE(...)
Definition AudioLoggerIDF.h:30
#define OUTPUT
Definition NoArduino.h:42
void pinMode(int pin, int mode)
Definition NoArduino.h:210
void digitalWrite(int pin, int value)
Definition NoArduino.h:206
#define DEFAULT_BUFFER_SIZE
Definition avr.h:20
Abstract Audio Ouptut class.
Definition AudioOutput.h:25
AudioInfo cfg
Definition AudioOutput.h:88
bool is_active
Definition AudioOutput.h:90
virtual int readArray(T data[], int len)
reads multiple values
Definition Buffers.h:33
virtual int writeArray(const T data[], int len)
Fills the buffer data.
Definition Buffers.h:55
A lock free N buffer. If count=2 we create a DoubleBuffer, if count=3 a TripleBuffer etc.
Definition Buffers.h:663
int available()
determines the available entries for the current read buffer
Definition Buffers.h:708
virtual int bufferCountFilled()
Provides the number of entries that are available to read.
Definition Buffers.h:768
virtual bool resize(int bytes)
Resizes the buffer if supported: returns false if not supported.
Definition Buffers.h:773
R2R configuration.
Definition R2ROutput.h:80
Vector< int > channel2_pins
Definition R2ROutput.h:83
Vector< int > channel1_pins
Definition R2ROutput.h:82
R2RDriverBase * driver
Definition R2ROutput.h:86
int timer_id
Definition R2ROutput.h:89
uint16_t buffer_count
Definition R2ROutput.h:85
bool is_blocking
Definition R2ROutput.h:87
uint16_t buffer_size
Definition R2ROutput.h:84
int blocking_retry_delay_ms
Definition R2ROutput.h:88
R2R driver base class.
Definition R2ROutput.h:18
virtual void setupPins(Vector< int > &channel1_pins, Vector< int > &channel2_pins)=0
virtual void writePins(int channels, int channel, unsigned uvalue)=0
R2R driver which uses the Arduino API to setup and write to the digital pins.
Definition R2ROutput.h:34
void setupPins(Vector< int > &channel1_pins, Vector< int > &channel2_pins) override
Definition R2ROutput.h:36
Vector< int > * p_channel2_pins
Definition R2ROutput.h:71
void writePins(int channels, int channel, unsigned uvalue) override
Definition R2ROutput.h:52
Vector< int > * p_channel1_pins
Definition R2ROutput.h:70
Output to R-2R DAC. You need to define the used digital pins in the configuration....
Definition R2ROutput.h:105
R2RConfig rcfg
Definition R2ROutput.h:186
void writeValue(int channel)
Definition R2ROutput.h:188
bool begin(R2RConfig c)
Definition R2ROutput.h:112
R2RConfig defaultConfig()
Definition R2ROutput.h:107
NBuffer< uint8_t > buffer
Definition R2ROutput.h:185
size_t write(const uint8_t *data, size_t len) override
Definition R2ROutput.h:148
TimerAlarmRepeating timer
Definition R2ROutput.h:183
static void r2r_timer_callback(void *ptr)
Definition R2ROutput.h:219
void writeValueT(int channel)
Definition R2ROutput.h:202
bool begin() override
Definition R2ROutput.h:119
Common Interface definition for TimerAlarmRepeating.
Definition AudioTimer.h:29
virtual void setTimer(int timer)
Definition AudioTimer.h:60
void setIsSave(bool is_save)
Definition AudioTimer.h:66
void setCallbackParameter(void *obj)
Definition AudioTimer.h:56
bool begin(repeating_timer_callback_t callback_f, uint32_t time, TimeUnit unit=MS)
Definition AudioTimer.h:43
Vector implementation which provides the most important methods as defined by std::vector....
Definition Vector.h:21
int size()
Definition Vector.h:178
@ HZ
Definition AudioTypes.h:48
audio_tools::R2RDriver r2r_driver
Generic Implementation of sound input and output for desktop environments using portaudio.
Definition AudioCodecsBase.h:10
void delay(unsigned long ms)
Definition Time.h:23
size_t writeData(Print *p_out, T *data, int samples, int maxSamples=512)
Definition AudioTypes.h:512
Basic Audio information which drives e.g. I2S.
Definition AudioTypes.h:55
sample_rate_t sample_rate
Sample Rate: e.g 44100.
Definition AudioTypes.h:57
uint16_t channels
Number of channels: 2=stereo, 1=mono.
Definition AudioTypes.h:59
uint8_t bits_per_sample
Number of bits per sample (int16_t = 16 bits)
Definition AudioTypes.h:61