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;
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++) {
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++) {
247 int pin = self->config.pins_data[
j];
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) {