arduino-audio-tools
Loading...
Searching...
No Matches
R2ROutput.h
Go to the documentation of this file.
1#pragma once
2
8#include "AudioToolsConfig.h"
9
10namespace audio_tools {
11
20 public:
21 virtual void setupPins(Vector<digital_pin_t>& channel1_pins,
22 Vector<digital_pin_t>& channel2_pins) = 0;
23
24 virtual void writePins(int channels, int channel, unsigned uvalue) = 0;
25};
26
41class R2RDriver : public R2RDriverBase {
42 public:
43 void setupPins(Vector<digital_pin_t>& channel1_pins,
44 Vector<digital_pin_t>& channel2_pins) override {
45 TRACED();
46 p_channel1_pins = &channel1_pins;
47 p_channel2_pins = &channel2_pins;
48
49 for (auto pin : channel1_pins) {
50 LOGI("Setup channel1 pin %d", pin);
51 pinMode(pin, OUTPUT);
52 }
53 for (auto pin : channel2_pins) {
54 LOGI("Setup channel2 pin %d", pin);
55 pinMode(pin, OUTPUT);
56 }
57 };
58
59 void writePins(int channels, int channel, unsigned uvalue) override {
60 switch (channel) {
61 case 0:
62 for (int j = 0; j < (*p_channel1_pins).size(); j++) {
63 digital_pin_t pin = (*p_channel1_pins)[j];
64 if (pin != GPIO_NONE) digitalWrite(pin, (uvalue >> j) & 1);
65 }
66 break;
67 case 1:
68 for (int j = 0; j < (*p_channel2_pins).size(); j++) {
69 digital_pin_t pin = (*p_channel2_pins)[j];
70 if (pin != GPIO_NONE) digitalWrite(pin, (uvalue >> j) & 1);
71 }
72 break;
73 }
74 }
75
76 protected:
79} ;
80
83
101
115class R2ROutput : public AudioOutput {
116 public:
118 R2RConfig r;
119 return r;
120 }
121
123 TRACED();
124 cfg = c;
125 rcfg = c;
126 return begin();
127 }
128
129 bool begin() override {
130 TRACED();
131 if (cfg.channels == 0 || cfg.channels > 2) {
132 LOGE("channels is %d", cfg.channels);
133 return false;
134 }
135 if (rcfg.channel1_pins.size() == 0) {
136 LOGE("channel1_pins not defined");
137 return false;
138 }
139 if (cfg.channels == 2 &&
141 LOGE("channel2_pins not defined");
142 return false;
143 }
144 if (rcfg.buffer_size * rcfg.buffer_count == 0) {
145 LOGE("buffer_size or buffer_count is 0");
146 return false;
147 }
150
151 // setup timer
153 timer.setIsSave(true);
156 }
157
158 size_t write(const uint8_t* data, size_t len) override {
159 LOGD("write: %d", len);
160 size_t result = 0;
161 // if buffer has not been allocated (buffer_size==0)
162 if (len > rcfg.buffer_size) {
163 LOGE("buffer_size %d too small for write size: %d", rcfg.buffer_size,
164 len);
165 return len;
166 }
167
168 if (rcfg.is_blocking) {
169 // write of all bytes
170 int open = len;
171 while (open > 0) {
172 int written = buffer.writeArray(data + result, open);
173 open -= written;
174 result += written;
175 if (open > 0) {
177 }
178 }
179 } else {
180 // write as much as possible
181 result = buffer.writeArray(data, len);
182 }
183 // activate output when buffer is half full
185 LOGI("is_active = true");
186 is_active = true;
187 }
188
189 return result;
190 }
191
192 protected:
194 // Double buffer
197
198 void writeValue(int channel) {
199 switch (cfg.bits_per_sample) {
200 case 8:
201 return writeValueT<int8_t>(channel);
202 case 16:
203 return writeValueT<int16_t>(channel);
204 case 24:
205 return writeValueT<int24_t>(channel);
206 case 32:
207 return writeValueT<int32_t>(channel);
208 }
209 }
210
211 template <typename T>
212 void writeValueT(int channel) {
213 // don't do anything if we do not have enough data
214 if (buffer.available() < sizeof(T)) return;
215
216 // get next value from buffer
217 T value = 0;
218 buffer.readArray((uint8_t*)&value, sizeof(T));
219 // convert to unsigned
220 unsigned uvalue = (int)value + NumberConverter::maxValueT<T>() + 1;
221 // scale value
222 uvalue = uvalue >> ((sizeof(T) * 8) - rcfg.channel1_pins.size());
223 // Serial.println(uvalue);
224
225 // output pins
227 }
228
229 static void r2r_timer_callback(void* ptr) {
230 R2ROutput* self = (R2ROutput*)ptr;
231 if (self->is_active) {
232 // output channel 1
233 self->writeValue(0);
234 // output channel 2
235 if (self->cfg.channels == 2) self->writeValue(1);
236 };
237 }
238};
239
240} // namespace audio_tools
#define OUTPUT
Definition Arduino.h:38
#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 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
Common Interface definition for AudioTimer.
Definition AudioTimer.h:30
virtual void setTimer(int timer)
Definition AudioTimer.h:61
void setIsSave(bool is_save)
Definition AudioTimer.h:67
void setCallbackParameter(void *obj)
Definition AudioTimer.h:57
bool begin(repeating_timer_callback_t callback_f, uint32_t time, TimeUnit unit=MS)
Definition AudioTimer.h:44
virtual int writeArray(const T data[], int len)
Fills the buffer data.
Definition Buffers.h:56
A lock free N buffer. If count=2 we create a DoubleBuffer, if count=3 a TripleBuffer etc.
Definition Buffers.h:675
virtual bool resize(size_t bytes)
Resizes the buffer if supported: returns false if not supported.
Definition Buffers.h:808
int available()
determines the available entries for the current read buffer
Definition Buffers.h:733
virtual int bufferCountFilled()
Provides the number of entries that are available to read.
Definition Buffers.h:803
int readArray(T data[], int len) override
Definition Buffers.h:690
R2R configuration.
Definition R2ROutput.h:90
Vector< digital_pin_t > channel2_pins
Definition R2ROutput.h:93
R2RDriverBase * driver
Definition R2ROutput.h:96
Vector< digital_pin_t > channel1_pins
Definition R2ROutput.h:92
int timer_id
Definition R2ROutput.h:99
uint16_t buffer_count
Definition R2ROutput.h:95
bool is_blocking
Definition R2ROutput.h:97
uint16_t buffer_size
Definition R2ROutput.h:94
int blocking_retry_delay_ms
Definition R2ROutput.h:98
R2R driver base class.
Definition R2ROutput.h:19
virtual void setupPins(Vector< digital_pin_t > &channel1_pins, Vector< digital_pin_t > &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:41
Vector< digital_pin_t > * p_channel2_pins
Definition R2ROutput.h:78
Vector< digital_pin_t > * p_channel1_pins
Definition R2ROutput.h:77
void setupPins(Vector< digital_pin_t > &channel1_pins, Vector< digital_pin_t > &channel2_pins) override
Definition R2ROutput.h:43
void writePins(int channels, int channel, unsigned uvalue) override
Definition R2ROutput.h:59
Output to R-2R DAC. You need to define the used digital pins in the configuration....
Definition R2ROutput.h:115
R2RConfig rcfg
Definition R2ROutput.h:196
void writeValue(int channel)
Definition R2ROutput.h:198
bool begin(R2RConfig c)
Definition R2ROutput.h:122
R2RConfig defaultConfig()
Definition R2ROutput.h:117
NBuffer< uint8_t > buffer
Definition R2ROutput.h:195
size_t write(const uint8_t *data, size_t len) override
Definition R2ROutput.h:158
AudioTimer timer
Definition R2ROutput.h:193
static void r2r_timer_callback(void *ptr)
Definition R2ROutput.h:229
void writeValueT(int channel)
Definition R2ROutput.h:212
bool begin() override
Definition R2ROutput.h:129
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:44
Generic Implementation of sound input and output for desktop environments using portaudio.
Definition AudioCodecsBase.h:10
gpio_dt_spec digital_pin_t
Zephyr GPIO spec as digital_pin_t.
Definition Arduino.h:266
void digitalWrite(digital_pin_t pin, bool value)
Definition Arduino.h:313
static gpio_dt_spec GPIO_NONE
GPIO_NONE is no pin defined.
Definition Arduino.h:269
static R2RDriver r2r_driver
Default driver instance.
Definition R2ROutput.h:82
void delay(uint32_t ms)
Definition Arduino.h:255
void pinMode(digital_pin_t pin, int mode)
Definition Arduino.h:282
size_t writeData(Print *p_out, T *data, int samples, int maxSamples=512)
Definition AudioTypes.h:508
Basic Audio information which drives e.g. I2S.
Definition AudioTypes.h:51
sample_rate_t sample_rate
Sample Rate: e.g 44100.
Definition AudioTypes.h:53
uint16_t channels
Number of channels: 2=stereo, 1=mono.
Definition AudioTypes.h:55
uint8_t bits_per_sample
Number of bits per sample (int16_t = 16 bits)
Definition AudioTypes.h:57