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;
77 size_t readBytes(uint8_t *data,
size_t len)
override {
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;
175 bool is_active =
false;
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;
246 for (
int j = 0; j < channels; j++) {
247 self->buffer->
read(sample);
248 int pin = self->config.pins_data[j];
249 analogWrite(pin, sample);
259 Pins& pins = config.pins();
260 if (pins.size()<config.channels){
261 LOGE(
"Only pins %d of %d defined", pins.size(), config.channels);
266 if (config.rx_tx_mode == RX_MODE) {
267 LOGI(
"rx start_pin: %d", config.start_pin);
269 for (
int j = 0; j < config.channels; j++) {
270 int pin = config.pins_data [j];
272 LOGD(
"pinMode(%d, INPUT)", pin);
275 if (config.is_auto_center_read) {
277 for (
int j = 0; j < 1024; j++) {
278 updateMinMax(analogRead(config.pins_data[0]));
280 LOGI(
"Avg Signal was %d", avg_value);
282 }
else if (config.rx_tx_mode == TX_MODE) {
284 for (
int j = 0; j < config.channels; j++) {
285 int pin = config.pins_data[j];
286 LOGI(
"tx pin %d: %d", j, pin);
287 pinMode(pin, OUTPUT);
288 LOGD(
"pinMode(%d, OUTPUT)", pin);
294 void updateMinMax(
int value) {
295 if (value < min) min = value;
296 if (value > max) max = value;
297 if (count++ == 1024) updateAvg();
301 avg_value = (max + min) / 2;
310 return config.sample_rate >= config.max_sample_rate;
314 bool isCombinedChannel() {
return is_combined_channels; }
320 if (config.sample_rate <= config.max_sample_rate)
return 1;
321 for (
int j = 2; j < 6; j += 2) {
322 if (config.sample_rate / j <= config.max_sample_rate) {