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/Buffers.h"
51 if (config.rx_tx_mode == RXTX_MODE) {
52 LOGE(
"RXTX not supported");
61 if (!setupTx())
return false;
63 if (!setupBuffer())
return false;
69 void end()
override { timer.end(); }
71 int available()
override {
72 if (config.rx_tx_mode == TX_MODE)
return 0;
73 return buffer ==
nullptr ? 0 : buffer->
available() * 2;
78 if (config.rx_tx_mode == TX_MODE)
return 0;
79 if (buffer ==
nullptr)
return 0;
80 int bytes = len / frame_size * frame_size;
84 int availableForWrite()
override {
85 if (config.rx_tx_mode == RX_MODE)
return 0;
86 if (buffer ==
nullptr)
return 0;
87 return config.is_blocking_write ? config.buffer_size
91 size_t write(
const uint8_t *data,
size_t len)
override {
92 LOGD(
"write: %d", (
int)len);
93 if (config.rx_tx_mode == RX_MODE)
return 0;
95 len = len / frame_size * frame_size;
97 if (isCombinedChannel()) {
99 len = cr.convert((uint8_t *)data, len);
100 LOGD(
"ChannelReducer len: %d", (
int)len);
105 len = dec.convert((uint8_t *)data, len);
106 LOGD(
"Decimate len: %d for factor %d", (
int)len, decim);
110 if (config.is_blocking_write) {
111 LOGD(
"Waiting for buffer to be available");
123 size_t samples = len / 2;
124 int16_t *p16 = (int16_t*)data;
125 for (
int j=0;j<samples;j++){
126 uint8_t sample =
map(p16[j],-32768, 32767,0,255);
127 if (buffer->
write(sample)){
135 size_t samples = len / 3;
136 int24_t *p24 = (int24_t*)data;
137 for (
int j=0;j<samples;j++){
138 uint8_t sample =
map(p24[j],-8388608, 8388607,0,255);
139 if (buffer->
write(sample)){
148 size_t samples = len / 4;
149 int32_t *p32 = (int32_t*)data;
150 for (
int j=0;j<samples;j++){
151 uint8_t sample =
map(p32[j],-2147483648, 2147483647,0,255);
152 if (buffer->
write(sample)){
163 return result * result_factor;
167 AnalogConfigStd config;
168 TimerAlarmRepeating timer;
169 BaseBuffer<uint8_t> *buffer =
nullptr;
170 int avg_value, min, max, count;
171 bool is_combined_channels =
false;
172 uint16_t frame_size = 0;
173 int result_factor = 1;
178 if (config.rx_tx_mode == TX_MODE) {
180 if (config.channels > ANALOG_MAX_OUT_CHANNELS) {
181 if (config.channels == 2) {
182 is_combined_channels =
true;
185 LOGE(
"Unsupported channels");
191 decim = decimation();
192 result_factor = result_factor * decim;
194 if (isCombinedChannel()) {
195 LOGI(
"Combining channels");
196 result_factor = result_factor * 2;
203 if (buffer ==
nullptr) {
205 buffer =
new RingBuffer<uint8_t>(config.buffer_size * config.buffer_count);
206 if (buffer ==
nullptr) {
207 LOGE(
"Not enough memory for buffer");
216 : config.sample_rate;
217 LOGI(
"sample_rate: %d", sample_rate);
218 timer.setCallbackParameter(
this);
219 return timer.begin(
callback, sample_rate, TimeUnit::HZ);
227 if (self->buffer ==
nullptr)
return;
230 if (self->config.rx_tx_mode == RX_MODE) {
231 int channels = self->config.
channels;
232 for (
int j = 0;
j < channels;
j++) {
235 if (self->config.is_auto_center_read) {
236 self->updateMinMax(value);
238 value = (value - self->avg_value) * 16;
239 self->buffer->
write(value);
242 }
else if (self->config.rx_tx_mode == TX_MODE) {
243 int channels = self->config.
channels;
244 for (
int j = 0;
j < channels;
j++) {
246 int pin = self->config.pins_data[
j];
257 Pins& pins = config.pins();
258 if (pins.size()<config.channels){
259 LOGE(
"Only pins %d of %d defined", pins.size(), config.channels);
264 if (config.rx_tx_mode == RX_MODE) {
265 LOGI(
"rx start_pin: %d", config.start_pin);
267 for (
int j = 0;
j < config.channels;
j++) {
268 int pin = config.pins_data [
j];
270 LOGD(
"pinMode(%d, INPUT)", pin);
273 if (config.is_auto_center_read) {
275 for (
int j = 0;
j < 1024;
j++) {
276 updateMinMax(
analogRead(config.pins_data[0]));
278 LOGI(
"Avg Signal was %d", avg_value);
280 }
else if (config.rx_tx_mode == TX_MODE) {
282 for (
int j = 0;
j < config.channels;
j++) {
283 int pin = config.pins_data[
j];
284 LOGI(
"tx pin %d: %d",
j, pin);
285 pinMode(pin, OUTPUT);
286 LOGD(
"pinMode(%d, OUTPUT)", pin);
292 void updateMinMax(
int value) {
293 if (value < min) min = value;
294 if (value > max) max = value;
295 if (count++ == 1024) updateAvg();
299 avg_value = (max + min) / 2;
308 return config.sample_rate >= config.max_sample_rate;
312 bool isCombinedChannel() {
return is_combined_channels; }
318 if (config.sample_rate <= config.max_sample_rate)
return 1;
319 for (
int j = 2;
j < 6;
j += 2) {
320 if (config.sample_rate /
j <= config.max_sample_rate) {