From 26066cd8fad4a9e61e9b1e529c43b527042c0b8c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Eckhard=20V=C3=B6llm?= Date: Fri, 26 Jan 2024 23:29:04 +0100 Subject: [PATCH] Use 50% scale for ESP32 sine wave generator for lower distortion --- main/ESPAudio.cpp | 23 ++++++++++++++++------- main/Poti.h | 1 + main/SetupMenu.cpp | 10 +++++----- main/cat5171.cpp | 6 +++--- main/cat5171.h | 2 ++ main/mcp4018.cpp | 5 +++-- main/mcp4018.h | 2 ++ 7 files changed, 32 insertions(+), 17 deletions(-) diff --git a/main/ESPAudio.cpp b/main/ESPAudio.cpp index de07ab8f7..d7ce4b785 100644 --- a/main/ESPAudio.cpp +++ b/main/ESPAudio.cpp @@ -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; @@ -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= XCVARIO_21 ? dac_scale_set(_ch, 1 ) : dac_scale_set(_ch, 2 ); // new hardware can accept 50% scale -> better sound enableAmplifier( true ); dacEnable(); } diff --git a/main/Poti.h b/main/Poti.h index 459082ef7..97fab7fdb 100644 --- a/main/Poti.h +++ b/main/Poti.h @@ -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; diff --git a/main/SetupMenu.cpp b/main/SetupMenu.cpp index e3d6444a3..cbeeb9c83 100644 --- a/main/SetupMenu.cpp +++ b/main/SetupMenu.cpp @@ -557,8 +557,8 @@ void SetupMenu::down(int count){ else{ // Volume float vol = audio_volume.get(); for( int i=0; i max_volume.get() ) vol = max_volume.get(); audio_volume.set( vol ); diff --git a/main/cat5171.cpp b/main/cat5171.cpp index 400bef08c..b09bb749f 100644 --- a/main/cat5171.cpp +++ b/main/cat5171.cpp @@ -9,6 +9,7 @@ CAT5171::CAT5171() _noDevice = false; wiper = CAT5171RANGE/2; bus = 0; + _scale = 100; } bool CAT5171::begin() @@ -79,7 +80,7 @@ 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; @@ -87,11 +88,10 @@ bool CAT5171::readVolume( float &val ) { 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 diff --git a/main/cat5171.h b/main/cat5171.h index 6275bc4b8..d10eac015 100644 --- a/main/cat5171.h +++ b/main/cat5171.h @@ -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 ); @@ -44,6 +45,7 @@ class CAT5171: public Poti int errorcount; bool _noDevice; int wiper; // only bit 0..7 supported + int _scale; }; #endif diff --git a/main/mcp4018.cpp b/main/mcp4018.cpp index ac98fff02..9932cfddc 100644 --- a/main/mcp4018.cpp +++ b/main/mcp4018.cpp @@ -9,6 +9,7 @@ MCP4018::MCP4018() _noDevice = false; wiper = MCP4018RANGE/2; bus = 0; + _scale = 100; } bool MCP4018::begin() @@ -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 @@ -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 ); } diff --git a/main/mcp4018.h b/main/mcp4018.h index 90fbad53b..a01a3ac32 100644 --- a/main/mcp4018.h +++ b/main/mcp4018.h @@ -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 ); @@ -43,6 +44,7 @@ class MCP4018: public Poti int errorcount; bool _noDevice; int wiper; // only bit 0..7 supported + int _scale; }; #endif