Skip to content

Commit

Permalink
Use 50% scale for ESP32 sine wave generator for lower distortion
Browse files Browse the repository at this point in the history
  • Loading branch information
iltis42 committed Jan 26, 2024
1 parent 9ac5146 commit 26066cd
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 17 deletions.
23 changes: 16 additions & 7 deletions main/ESPAudio.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -219,9 +219,11 @@ bool Audio::selfTest(){
{
ESP_LOGI(FNAME,"MCP4018 digital Poti found");
}
float setvolume = default_volume.get();
speaker_volume = vario_mode_volume = s2f_mode_volume = setvolume;
ESP_LOGI(FNAME,"default volume: %f", speaker_volume );
float setvolume = 50.0;

if( hardwareRevision.get() >= XCVARIO_21 )
DigitalPoti->setHalfScale(); // double amplitude from ESP32 for better sound

_alarm_mode = true;
writeVolume( 50.0 );
float getvolume;
Expand All @@ -231,15 +233,23 @@ bool Audio::selfTest(){
ESP_LOGI(FNAME,"readWiper returned error");
return false;
}
if( (int)getvolume != 49 )
if( std::abs( getvolume - setvolume ) > 2.0 )
{
ESP_LOGI(FNAME,"readWiper returned wrong setting set=%f get=%f", setvolume, getvolume );
ret = false;
}
else
else{
ESP_LOGI(FNAME,"readWiper: OK");
ret = true;
}

bool fadein=false;
_alarm_mode = false;

setvolume = default_volume.get();
speaker_volume = vario_mode_volume = s2f_mode_volume = setvolume;
ESP_LOGI(FNAME,"default volume: %f", speaker_volume );
writeVolume( setvolume );
// while(1){ // uncomment for continuous self test
ESP_LOGI(FNAME,"Min F %f, Max F %f", minf, maxf );
for( float f=minf; f<maxf*1.25; f=f*1.03){
Expand Down Expand Up @@ -490,7 +500,6 @@ void Audio::calcS2Fmode( bool recalc ){
}
}


void Audio::evaluateChopping(){
if(
(chopping_mode.get() == BOTH_CHOP) ||
Expand Down Expand Up @@ -793,7 +802,7 @@ void Audio::restart()
dac_cosine_enable(_ch);
dac_offset_set(_ch, 0 );
dac_invert_set(_ch, 2 ); // invert MSB to get sine waveform
dac_scale_set(_ch, 2 );
hardwareRevision.get() >= XCVARIO_21 ? dac_scale_set(_ch, 1 ) : dac_scale_set(_ch, 2 ); // new hardware can accept 50% scale -> better sound
enableAmplifier( true );
dacEnable();
}
Expand Down
1 change: 1 addition & 0 deletions main/Poti.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ class Poti
virtual bool readVolume( float& val ) = 0;
virtual bool writeVolume( float val ) = 0;
virtual bool haveDevice() = 0;
virtual void setHalfScale() = 0;
private:
virtual int getRange() = 0;
virtual float getInvRange() = 0;
Expand Down
10 changes: 5 additions & 5 deletions main/SetupMenu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -557,8 +557,8 @@ void SetupMenu::down(int count){
else{ // Volume
float vol = audio_volume.get();
for( int i=0; i<count; i++ )
vol = vol * 0.83;
if( vol<3.0 )
vol = vol * 0.77;
if( vol<1.5 ) // allow smaller volumes to better support new 50% scale mode of ESP32 sine generator (default is 25%)
vol=0;
audio_volume.set( vol );
// ESP_LOGI(FNAME,"NEW DN VOL: %f", vol );
Expand Down Expand Up @@ -598,10 +598,10 @@ void SetupMenu::up(int count){
}
else{ // Volume
float vol = audio_volume.get();
if( vol<3.0 )
vol=3.0;
if( vol<1.5 )
vol=1.5;
for( int i=0; i<count; i++ )
vol = vol * 1.17;
vol = vol * 1.33;
if( vol > max_volume.get() )
vol = max_volume.get();
audio_volume.set( vol );
Expand Down
6 changes: 3 additions & 3 deletions main/cat5171.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ CAT5171::CAT5171()
_noDevice = false;
wiper = CAT5171RANGE/2;
bus = 0;
_scale = 100;
}

bool CAT5171::begin()
Expand Down Expand Up @@ -79,19 +80,18 @@ bool CAT5171::writeWiper( int val ) {
bool CAT5171::readVolume( float &val ) {
int ival;
if ( readWiper( ival ) ) {
val = (float)(100 * ival) * getInvRange();
val = (float)(_scale * ival) * getInvRange();
return true;
}
return false;
}

bool CAT5171::writeVolume( float val ) {
int ival = (int)(val * getRange());
ival /= 100;
ival /= _scale;
return writeWiper( ival );
}


bool CAT5171::reset() {
ESP_LOGI(FNAME,"CAT5171 reset");
esp_err_t err = bus->write2bytes( CAT5171_I2C_ADDR, 0x60, 128 ); // 0x40 = RS = midscale
Expand Down
2 changes: 2 additions & 0 deletions main/cat5171.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ class CAT5171: public Poti
bool writeVolume( float val );
bool reset();
bool haveDevice();
void setHalfScale() { _scale = 200; };

private:
bool readWiper( int& val );
Expand All @@ -44,6 +45,7 @@ class CAT5171: public Poti
int errorcount;
bool _noDevice;
int wiper; // only bit 0..7 supported
int _scale;
};

#endif
5 changes: 3 additions & 2 deletions main/mcp4018.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ MCP4018::MCP4018()
_noDevice = false;
wiper = MCP4018RANGE/2;
bus = 0;
_scale = 100;
}

bool MCP4018::begin()
Expand Down Expand Up @@ -78,7 +79,7 @@ bool MCP4018::writeWiper( int val ) {
bool MCP4018::readVolume( float &val ) {
int ival;
if ( readWiper( ival ) ) {
val = (float)(100 * ival) * getInvRange();
val = (float)(_scale * ival) * getInvRange();
return true;
}
else
Expand All @@ -89,6 +90,6 @@ bool MCP4018::readVolume( float &val ) {

bool MCP4018::writeVolume( float val ) {
int ival = (int)(val * getRange());
ival /= 100;
ival /= _scale;
return writeWiper( ival );
}
2 changes: 2 additions & 0 deletions main/mcp4018.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ class MCP4018: public Poti
bool readVolume( float& val );
bool writeVolume( float val );
bool haveDevice();
void setHalfScale() { _scale = 200; };

private:
bool readWiper( int& val );
Expand All @@ -43,6 +44,7 @@ class MCP4018: public Poti
int errorcount;
bool _noDevice;
int wiper; // only bit 0..7 supported
int _scale;
};

#endif

0 comments on commit 26066cd

Please sign in to comment.