Skip to content

Commit

Permalink
AnalogDriverESP32 cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
pschatzmann committed Sep 8, 2023
1 parent 2dea409 commit 0b23223
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 73 deletions.
78 changes: 10 additions & 68 deletions src/AudioAnalog/AnalogAudioBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,27 +34,18 @@ class AnalogConfig : public AudioInfo {
bool auto_clear = I2S_AUTO_CLEAR;
bool uninstall_driver_on_end = true;
int mode_internal;

AnalogConfig() {
sample_rate = 44100;
bits_per_sample = 16;
channels = 2;
this->rx_tx_mode = TX_MODE;
// enable both channels
mode_internal = (I2S_MODE_MASTER | I2S_MODE_TX | I2S_MODE_DAC_BUILT_IN);
}
int adc_pin;

/// Default constructor
AnalogConfig(RxTxMode rxtxMode) {
AnalogConfig(RxTxMode rxtxMode=TX_MODE) {
sample_rate = 44100;
bits_per_sample = 16;
channels = 2;
rx_tx_mode = rxtxMode;
if (rx_tx_mode == RX_MODE) {
mode_internal = (I2S_MODE_MASTER | I2S_MODE_RX | I2S_MODE_ADC_BUILT_IN);
setInputPin1(PIN_ADC1);
adc_pin = PIN_ADC1;
auto_clear = false;
// setInputPin2(PIN_ADC2);
LOGI("I2S_MODE_ADC_BUILT_IN");
} else {
mode_internal = (I2S_MODE_MASTER | I2S_MODE_TX | I2S_MODE_DAC_BUILT_IN);
Expand All @@ -65,72 +56,23 @@ class AnalogConfig : public AudioInfo {
/// Copy constructor
AnalogConfig(const AnalogConfig &cfg) = default;

/// Defines an alternative input pin (for the left channel)
void setInputPin1(int pin=PIN_ADC1){
setInputPin1(pin,0);
}

void logInfo() {
AudioInfo::logInfo();
if (rx_tx_mode == TX_MODE){
LOGI("analog left output pin: %d", 25);
LOGI("analog right output pin: %d", 26);
} else {
LOGI("input pin1: %d", adc_pin[0]);
if (channels==2){
LOGI("input pin2: %d", adc_pin[1]);
}
LOGI("input pin1: %d", adc_pin);
}
}

protected:
// input values
int adc_pin[2];
adc_unit_t adc_unit[2];
adc1_channel_t adc_channel[2];

/// Defines the current ADC pin. The following GPIO pins are supported: GPIO32-GPIO39
void setInputPin1(int gpio, int channelIdx){
this->adc_pin[channelIdx] = gpio;
switch(gpio){
case 32:
adc_unit[channelIdx] = ADC_UNIT_1;
adc_channel[channelIdx] = ADC1_GPIO32_CHANNEL;
break;
case 33:
adc_unit[channelIdx] = ADC_UNIT_1;
adc_channel[channelIdx] = ADC1_GPIO33_CHANNEL;
break;
case 34:
adc_unit[channelIdx] = ADC_UNIT_1;
adc_channel[channelIdx] = ADC1_GPIO34_CHANNEL;
break;
case 35:
adc_unit[channelIdx] = ADC_UNIT_1;
adc_channel[channelIdx] = ADC1_GPIO35_CHANNEL;
break;
case 36:
adc_unit[channelIdx] = ADC_UNIT_1;
adc_channel[channelIdx] = ADC1_GPIO36_CHANNEL;
break;
case 37:
adc_unit[channelIdx] = ADC_UNIT_1;
adc_channel[channelIdx] = ADC1_GPIO37_CHANNEL;
break;
case 38:
adc_unit[channelIdx] = ADC_UNIT_1;
adc_channel[channelIdx] = ADC1_GPIO38_CHANNEL;
break;
case 39:
adc_unit[channelIdx] = ADC_UNIT_1;
adc_channel[channelIdx] = ADC1_GPIO39_CHANNEL;
break;
/// Defines an alternative input pin (for the left channel)
void setInputPin1(int pin){
this->adc_pin = pin;
}

default:
LOGE( "%s - pin GPIO%d is not supported", __func__,gpio);
}
}
#else

AnalogConfig() {
sample_rate = 44100;
bits_per_sample = 16;
Expand All @@ -143,7 +85,7 @@ class AnalogConfig : public AudioInfo {
AnalogConfig(RxTxMode rxtxMode) : AnalogConfig() {
rx_tx_mode = rxtxMode;
}
int start_pin = PIN_ANALOG_START;
int start_pin = PIN_ANALOG_START;

#endif

Expand Down
53 changes: 50 additions & 3 deletions src/AudioAnalog/AnalogAudioESP32.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,7 @@ static inline uint16_t convert8DAC(int64_t value, int value_bits_per_sample){
class AnalogDriverESP32 : public AnalogDriverBase {
public:
/// Default constructor
AnalogDriverESP32() {
}
AnalogDriverESP32() = default;

/// Destructor
virtual ~AnalogDriverESP32() {
Expand Down Expand Up @@ -93,8 +92,10 @@ class AnalogDriverESP32 : public AnalogDriverBase {
switch (cfg.rx_tx_mode) {
case RX_MODE:
LOGI("RX_MODE");

setupInputPin(cfg.adc_pin);

if (i2s_set_adc_mode(cfg.adc_unit[0], cfg.adc_channel[0])!=ESP_OK) {
if (i2s_set_adc_mode(adc_unit, adc_channel)!=ESP_OK) {
LOGE( "%s - %s", __func__, "i2s_driver_install");
return false;
}
Expand Down Expand Up @@ -207,6 +208,52 @@ class AnalogDriverESP32 : public AnalogDriverBase {
bool is_driver_installed = false;
size_t result=0;

// input values
adc_unit_t adc_unit;
adc1_channel_t adc_channel;

/// Defines the current ADC pin. The following GPIO pins are supported: GPIO32-GPIO39
void setupInputPin(int gpio){
TRACED();

switch(gpio){
case 32:
adc_unit = ADC_UNIT_1;
adc_channel = ADC1_GPIO32_CHANNEL;
break;
case 33:
adc_unit = ADC_UNIT_1;
adc_channel = ADC1_GPIO33_CHANNEL;
break;
case 34:
adc_unit = ADC_UNIT_1;
adc_channel = ADC1_GPIO34_CHANNEL;
break;
case 35:
adc_unit = ADC_UNIT_1;
adc_channel = ADC1_GPIO35_CHANNEL;
break;
case 36:
adc_unit = ADC_UNIT_1;
adc_channel = ADC1_GPIO36_CHANNEL;
break;
case 37:
adc_unit = ADC_UNIT_1;
adc_channel = ADC1_GPIO37_CHANNEL;
break;
case 38:
adc_unit = ADC_UNIT_1;
adc_channel = ADC1_GPIO38_CHANNEL;
break;
case 39:
adc_unit = ADC_UNIT_1;
adc_channel = ADC1_GPIO39_CHANNEL;
break;

default:
LOGE( "%s - pin GPIO%d is not supported", __func__,gpio);
}
}

// The internal DAC only supports 8 bit values - so we need to convert the data
size_t outputStereo(const void *src, size_t size_bytes) {
Expand Down
2 changes: 0 additions & 2 deletions src/AudioConfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,6 @@
#define SOFT_MUTE_VALUE 0
#define PIN_CS SS
#define PIN_ADC1 34
#define PIN_ADC2 14

#define I2S_AUTO_CLEAR true

Expand Down Expand Up @@ -275,7 +274,6 @@ typedef uint32_t eps32_i2s_sample_rate_type;
#define SOFT_MUTE_VALUE 0
#define PIN_CS SS
#define PIN_ADC1 21
#define PIN_ADC2 22

#define I2S_AUTO_CLEAR true

Expand Down

0 comments on commit 0b23223

Please sign in to comment.