5 #include "AnalogConfigStd.h"
6 #include "AudioTools/CoreAudio/AudioAnalog/AnalogDriverBase.h"
7 #include "AudioTools/CoreAudio/AudioTimer/AudioTimer.h"
8 #include "AudioTools/CoreAudio/AudioStreams.h"
9 #include "AudioTools/CoreAudio/AudioTypes.h"
10 #include "AudioTools/CoreAudio/Buffers.h"
52 if (config.rx_tx_mode == RXTX_MODE) {
53 LOGE(
"RXTX not supported");
62 if (!setupTx())
return false;
64 if (!setupBuffer())
return false;
70 void end()
override { timer.end(); }
72 int available()
override {
73 if (config.rx_tx_mode == TX_MODE)
return 0;
74 return buffer ==
nullptr ? 0 : buffer->
available() * 2;
78 size_t readBytes(uint8_t *data,
size_t len)
override {
79 if (config.rx_tx_mode == TX_MODE)
return 0;
80 if (buffer ==
nullptr)
return 0;
81 int bytes = len / frame_size * frame_size;
85 int availableForWrite()
override {
86 if (config.rx_tx_mode == RX_MODE)
return 0;
87 if (buffer ==
nullptr)
return 0;
88 return config.is_blocking_write ? config.buffer_size
92 size_t write(
const uint8_t *data,
size_t len)
override {
93 LOGD(
"write: %d", (
int)len);
94 if (config.rx_tx_mode == RX_MODE)
return 0;
96 len = len / frame_size * frame_size;
98 if (isCombinedChannel()) {
100 len = cr.convert((uint8_t *)data, len);
101 LOGD(
"ChannelReducer len: %d", (
int)len);
106 len = dec.convert((uint8_t *)data, len);
107 LOGD(
"Decimate len: %d for factor %d", (
int)len, decim);
111 if (config.is_blocking_write) {
112 LOGD(
"Waiting for buffer to be available");
124 size_t samples = len / 2;
125 int16_t *p16 = (int16_t*)data;
126 for (
int j=0;j<samples;j++){
127 uint8_t sample =
map(p16[j],-32768, 32767,0,255);
128 if (buffer->
write(sample)){
136 size_t samples = len / 3;
137 int24_t *p24 = (int24_t*)data;
138 for (
int j=0;j<samples;j++){
139 uint8_t sample =
map(p24[j],-8388608, 8388607,0,255);
140 if (buffer->
write(sample)){
149 size_t samples = len / 4;
150 int32_t *p32 = (int32_t*)data;
151 for (
int j=0;j<samples;j++){
152 uint8_t sample =
map(p32[j],-2147483648, 2147483647,0,255);
153 if (buffer->
write(sample)){
164 return result * result_factor;
168 AnalogConfigStd config;
169 TimerAlarmRepeating timer;
170 BaseBuffer<uint8_t> *buffer =
nullptr;
171 int avg_value, min, max, count;
172 bool is_combined_channels =
false;
173 uint16_t frame_size = 0;
174 int result_factor = 1;
179 if (config.rx_tx_mode == TX_MODE) {
181 if (config.channels > ANALOG_MAX_OUT_CHANNELS) {
182 if (config.channels == 2) {
183 is_combined_channels =
true;
186 LOGE(
"Unsupported channels");
192 decim = decimation();
193 result_factor = result_factor * decim;
195 if (isCombinedChannel()) {
196 LOGI(
"Combining channels");
197 result_factor = result_factor * 2;
204 if (buffer ==
nullptr) {
206 buffer =
new RingBuffer<uint8_t>(config.buffer_size * config.buffer_count);
207 if (buffer ==
nullptr) {
208 LOGE(
"Not enough memory for buffer");
217 : config.sample_rate;
218 LOGI(
"sample_rate: %d", sample_rate);
219 timer.setCallbackParameter(
this);
220 return timer.begin(
callback, sample_rate, TimeUnit::HZ);
228 if (self->buffer ==
nullptr)
return;
231 if (self->config.rx_tx_mode == RX_MODE) {
232 int channels =
self->config.
channels;
233 for (
int j = 0; j < channels; j++) {
235 value = analogRead(self->config.pins_data[j]);
236 if (self->config.is_auto_center_read) {
237 self->updateMinMax(value);
239 value = (value -
self->avg_value) * 16;
240 self->buffer->write(value);
243 }
else if (self->config.rx_tx_mode == TX_MODE) {
244 int channels =
self->config.channels;
245 for (
int j = 0; j < channels; j++) {
246 int16_t sample =
self->buffer->read();
247 int pin =
self->config.pins_data[j];
248 analogWrite(pin, sample);
258 Pins& pins = config.pins();
259 if (pins.size()<config.channels){
260 LOGE(
"Only pins %d of %d defined", pins.size(), config.channels);
265 if (config.rx_tx_mode == RX_MODE) {
266 LOGI(
"rx start_pin: %d", config.start_pin);
268 for (
int j = 0; j < config.channels; j++) {
269 int pin = config.pins_data [j];
271 LOGD(
"pinMode(%d, INPUT)", pin);
274 if (config.is_auto_center_read) {
276 for (
int j = 0; j < 1024; j++) {
277 updateMinMax(analogRead(config.pins_data[0]));
279 LOGI(
"Avg Signal was %d", avg_value);
281 }
else if (config.rx_tx_mode == TX_MODE) {
283 for (
int j = 0; j < config.channels; j++) {
284 int pin = config.pins_data[j];
285 LOGI(
"tx pin %d: %d", j, pin);
286 pinMode(pin, OUTPUT);
287 LOGD(
"pinMode(%d, OUTPUT)", pin);
293 void updateMinMax(
int value) {
294 if (value < min) min = value;
295 if (value > max) max = value;
296 if (count++ == 1024) updateAvg();
300 avg_value = (max + min) / 2;
309 return config.sample_rate >= config.max_sample_rate;
313 bool isCombinedChannel() {
return is_combined_channels; }
319 if (config.sample_rate <= config.max_sample_rate)
return 1;
320 for (
int j = 2; j < 6; j += 2) {
321 if (config.sample_rate / j <= config.max_sample_rate) {