diff --git a/main/ESPAudio.cpp b/main/ESPAudio.cpp index 031c5eb95..48a097b0e 100644 --- a/main/ESPAudio.cpp +++ b/main/ESPAudio.cpp @@ -37,10 +37,10 @@ static TaskHandle_t dactid = NULL; uint8_t Audio::_tonemode; -uint16_t Audio::vario_mode_volume; -uint16_t Audio::s2f_mode_volume; -uint16_t &Audio::speaker_volume = Audio::vario_mode_volume; -uint16_t Audio::current_volume; +float Audio::vario_mode_volume; +float Audio::s2f_mode_volume; +float &Audio::speaker_volume = Audio::vario_mode_volume; +float Audio::current_volume; dac_channel_t Audio::_ch; bool Audio::_chopping = false; @@ -73,7 +73,7 @@ int Audio::prev_scale = -1; int Audio::_tonemode_back = 0; int Audio::tick = 0; int Audio::volume_change=0; -int Audio::_step = 2; +float Audio::_step = 2; bool Audio::dac_enable=false; bool Audio::amplifier_enable=false; bool Audio::_haveCAT5171=false; @@ -81,7 +81,7 @@ bool Audio::_haveCAT5171=false; const int clk_8m_div = 7; // RTC 8M clock divider (division is by clk_8m_div+1, i.e. 0 means 8MHz frequency) const float freq_step = RTC_FAST_CLK_FREQ_APPROX / (65536 * 8 ); // div = 0x07 typedef struct lookup { uint8_t div; uint8_t step; } t_lookup_entry; -typedef struct volume { uint16_t vol; uint8_t scale; uint8_t wiper; } t_scale_wip; +//typedef struct volume { uint16_t vol; uint8_t scale; uint8_t wiper; } t_scale_wip; #define FADING_STEPS 6 // steps used for fade in/out at chopping #define FADING_TIME 3 // factor for volume changes fade over smoothing @@ -182,17 +182,17 @@ void Audio::begin( dac_channel_t ch ) delay(10); } -uint16_t Audio::equal_volume( uint16_t volume ){ +float Audio::equal_volume( float volume ){ float freq_resp = ( 1 - (((current_frequency-minf) / (maxf-minf)) * (frequency_response.get()/100.0) ) ); float new_vol = volume * freq_resp; if( equalizerSpline ) new_vol = new_vol * (float)(*equalizerSpline)( (double)current_frequency ); - if( new_vol >= DigitalPoti->getRange()*(max_volume.get()/100) ) - new_vol = DigitalPoti->getRange()*(max_volume.get()/100); + if( new_vol >= max_volume.get() ) + new_vol = max_volume.get(); if( new_vol <= 0 ) new_vol = 0; // ESP_LOGI(FNAME,"Vol: %d Scaled: %f F: %.0f spline: %.3f", volume, new_vol, current_frequency, (float)(*equalizerSpline)( (double)current_frequency )); - return (uint16_t)new_vol; + return new_vol; } bool Audio::selfTest(){ @@ -220,23 +220,22 @@ bool Audio::selfTest(){ { ESP_LOGI(FNAME,"MCP4018 digital Poti found"); } - _step = DigitalPoti->getStep(); - uint16_t setwiper = ( 0.01 * default_volume.get() * DigitalPoti->getRange()); + float setvolume = default_volume.get(); speaker_volume = vario_mode_volume; - vario_mode_volume = s2f_mode_volume = setwiper; - ESP_LOGI(FNAME,"default volume/wiper: %d", (speaker_volume) ); - ESP_LOGI(FNAME, "selfTest wiper: %d", vario_mode_volume ); + vario_mode_volume = s2f_mode_volume = setvolume; + ESP_LOGI(FNAME,"default volume/wiper: %f", (speaker_volume) ); + ESP_LOGI(FNAME, "selfTest wiper: %f", vario_mode_volume ); _alarm_mode = true; - writeVolume( 1 ); - uint16_t getwiper; - bool ret = DigitalPoti->readWiper( getwiper ); + writeVolume( 50.0 ); + float getvolume; + bool ret = DigitalPoti->readVolume( getvolume ); // gets 49.8049... if( ret == false ) { ESP_LOGI(FNAME,"readWiper returned error"); return false; } - if( getwiper != 1 ) + if( (int)getvolume != 49 ) { - ESP_LOGI(FNAME,"readWiper returned wrong setting set=%d get=%d", setwiper, getwiper ); + ESP_LOGI(FNAME,"readWiper returned wrong setting set=%f get=%f", setvolume, getvolume ); ret = false; } else @@ -250,15 +249,15 @@ bool Audio::selfTest(){ setFrequency( f ); current_frequency = f; if( !fadein ){ - int volume = 3; - for( int i=0; iwriteWiper( volume ); // max volume + DigitalPoti->writeVolume( volume ); // max volume else - DigitalPoti->writeWiper( equal_volume(volume) ); // take care frequency response + DigitalPoti->writeVolume( equal_volume(volume) ); // take care frequency response current_volume = volume; } @@ -548,7 +547,7 @@ void Audio::dactask(void* arg ) calculateFrequency(); // Amplifier and Volume control if( !_testmode && !(tick%2) ) { - // ESP_LOGI(FNAME, "sound dactask tick:%d volume:%d te:%f db:%d", tick, (speaker_volume), _te, inDeadBand(_te) ); + // ESP_LOGI(FNAME, "sound dactask tick:%d volume:%f te:%f db:%d", tick, (speaker_volume), _te, inDeadBand(_te) ); if( !(tick%10) ){ bool mode = calcS2Fmode(); if( _s2f_mode != mode ){ @@ -575,28 +574,28 @@ void Audio::dactask(void* arg ) // Optionally disable Sound when in Menu if( audio_disable.get() && gflags.inSetup ) sound = false; - //ESP_LOGI(FNAME, "sound %d, ht %d, te %2.1f vc:%d cw:%d ", sound, hightone, _te, volume_change, current_volume ); + //ESP_LOGI(FNAME, "sound %d, ht %d, te %2.1f vc:%d cw:%f ", sound, hightone, _te, volume_change, current_volume ); if( sound ){ if( !deadband_active && amplifier_shutdown.get() ){ enableAmplifier( true ); } // Blend over gracefully volume changes if( (current_volume != (speaker_volume)) && volume_change ){ - // ESP_LOGI(FNAME, "volume change, new volume: %d, current_volume %d", (speaker_volume), current_volume ); - int delta = 1; + // ESP_LOGI(FNAME, "volume change, new volume: %f, current_volume %f", (speaker_volume), current_volume ); + float delta = 1; dacEnable(); if( (speaker_volume) > current_volume ){ - for( int i=current_volume; i<(speaker_volume); i+=delta ) { - writeVolume( i ); - delta = _step+i/FADING_TIME; - // ESP_LOGI(FNAME, "volume inc, new volume: %d", i ); + for( float f=current_volume; f<(speaker_volume); f+=delta ) { + writeVolume( f ); + delta = _step+f/FADING_TIME; + // ESP_LOGI(FNAME, "volume inc, new volume: %f", f ); delay(1); }} else{ - for( int i=current_volume; i>(speaker_volume); i-=delta ) { - writeVolume( i ); - // ESP_LOGI(FNAME, "volume dec, new volume: %d", i ); - delta = _step+i/FADING_TIME; + for( float f=current_volume; f>(speaker_volume); f-=delta ) { + writeVolume( f ); + // ESP_LOGI(FNAME, "volume dec, new volume: %f", f ); + delta = _step+f/FADING_TIME; delay(1); } } @@ -614,7 +613,7 @@ void Audio::dactask(void* arg ) } else{ float volume=3; - for( int i=0; i=0; i++ ) { + for( int i=0; i 0; i++ ) { //ESP_LOGI(FNAME, "fade out sound, volume: %3.1f", volume ); writeVolume( volume ); volume = volume*0.75; + if (volume < 3.0) + volume = 0; } delay(1); writeVolume( 0 ); diff --git a/main/ESPAudio.h b/main/ESPAudio.h index 4ca694afe..1ef6b5b0b 100644 --- a/main/ESPAudio.h +++ b/main/ESPAudio.h @@ -32,9 +32,9 @@ class Audio { static void setFrequency( float f ); static void setup(); - static void setVolume( int vol ); + static void setVolume( float vol ); - static void alarm( bool enable, int volume=100, e_audio_alarm_type_t alarmType=AUDIO_ALARM_STALL ); + static void alarm( bool enable, float volume=100, e_audio_alarm_type_t alarmType=AUDIO_ALARM_STALL ); static bool selfTest(); static inline void setTestmode( bool mode ) { _testmode = mode; } static void shutdown(); // frue ON, false OFF @@ -56,9 +56,9 @@ class Audio { static bool inDeadBand( float te ); static bool lookup( float f, int& div, int &step ); static void enableAmplifier( bool enable ); // frue ON, false OFF - static uint16_t equal_volume( uint16_t volume ); + static float equal_volume( float volume ); static void calculateFrequency(); - static void writeVolume( uint16_t volume ); + static void writeVolume( float volume ); static dac_channel_t _ch; static float _te; @@ -68,10 +68,10 @@ class Audio { static bool _testmode; static bool sound; static float _range; - static uint16_t &speaker_volume; - static uint16_t vario_mode_volume; - static uint16_t s2f_mode_volume; - static uint16_t current_volume; + static float &speaker_volume; + static float vario_mode_volume; + static float s2f_mode_volume; + static float current_volume; static float maxf; static float minf; static int prev_div; @@ -96,7 +96,7 @@ class Audio { static unsigned long next_scedule; static int mtick; static float current_frequency; - static int _step; + static float _step; static bool dac_enable; static bool amplifier_enable; static bool _haveCAT5171; diff --git a/main/Flarm.cpp b/main/Flarm.cpp index 0919e3f0a..0183ff032 100644 --- a/main/Flarm.cpp +++ b/main/Flarm.cpp @@ -435,7 +435,7 @@ void Flarm::drawFlarmWarning(){ if( _tick > 500 ) // age FLARM alarm in case there is no more input 50 per second = 10 sec AlarmLevel = 0; xSemaphoreTake(spiMutex,portMAX_DELAY ); - int volume=0; + float volume=0; e_audio_alarm_type_t alarm = AUDIO_ALARM_FLARM_1; if( AlarmLevel == 3 ) { // highest, impact 0-8 seconds volume = flarm_volume.get(); diff --git a/main/Poti.h b/main/Poti.h index 99c521667..459082ef7 100644 --- a/main/Poti.h +++ b/main/Poti.h @@ -11,12 +11,14 @@ class Poti virtual ~Poti() {}; virtual bool begin() = 0; virtual void setBus( I2C_t *theBus ) = 0; - virtual bool readWiper( uint16_t& val ) = 0; - virtual bool writeWiper( uint16_t val ) = 0; - virtual bool incWiper() = 0; - virtual bool decWiper() = 0; + virtual bool readVolume( float& val ) = 0; + virtual bool writeVolume( float val ) = 0; virtual bool haveDevice() = 0; +private: virtual int getRange() = 0; + virtual float getInvRange() = 0; + virtual bool readWiper( int& val ) = 0; + virtual bool writeWiper( int val ) = 0; virtual int getStep() = 0; }; diff --git a/main/SetupMenu.cpp b/main/SetupMenu.cpp index 2f15d2277..f211f5824 100644 --- a/main/SetupMenu.cpp +++ b/main/SetupMenu.cpp @@ -377,7 +377,7 @@ int bug_adj( SetupMenuValFloat * p ){ } int vol_adj( SetupMenuValFloat * p ){ - // Audio::setVolume( (int)(*(p->_value)) ); + // Audio::setVolume( (*(p->_value)) ); return 0; } @@ -516,6 +516,8 @@ void SetupMenu::down(int count){ float vol = audio_volume.get(); for( int i=0; i drawing_prio; extern uint8_t g_col_background; extern uint8_t g_col_highlight; -extern int last_volume; +extern float last_volume; // is this used? void change_ballast(); void change_mc(); diff --git a/main/cat5171.cpp b/main/cat5171.cpp index cf912de9b..400bef08c 100644 --- a/main/cat5171.cpp +++ b/main/cat5171.cpp @@ -7,21 +7,22 @@ CAT5171::CAT5171() { errorcount=0; _noDevice = false; - wiper = 63; + wiper = CAT5171RANGE/2; bus = 0; } bool CAT5171::begin() { errorcount=0; - if( readWiper( wiper ) ) { + int local_wiper; + if( readWiper( local_wiper ) ) { + wiper = local_wiper; ESP_LOGI(FNAME,"CAT5171 wiper=%d", wiper ); return(true); } - else { - ESP_LOGE(FNAME,"CAT5171 Error reading wiper!"); - return( false ); - } + // else + ESP_LOGE(FNAME,"CAT5171 Error reading wiper!"); + return( false ); } //destroy instance @@ -30,46 +31,32 @@ CAT5171::~CAT5171() } bool CAT5171::haveDevice() { - ESP_LOGI(FNAME,"CAT5171 haveDevice"); - esp_err_t err = bus->testConnection(CAT5171_I2C_ADDR); - if( err == ESP_OK ) { - ESP_LOGI(FNAME,"CAT5171 haveDevice: OK"); - return true; - } - else{ - ESP_LOGI(FNAME,"CAT5171 haveDevice: NONE"); - return false; - } -} - -bool CAT5171::incWiper(){ - if( wiper 1 ) - wiper--; - return( writeWiper(wiper) ); + ESP_LOGI(FNAME,"CAT5171 haveDevice"); + esp_err_t err = bus->testConnection(CAT5171_I2C_ADDR); + if( err == ESP_OK ) { + ESP_LOGI(FNAME,"CAT5171 haveDevice: OK"); + return true; + } + // else + ESP_LOGI(FNAME,"CAT5171 haveDevice: NONE"); + return false; } - -bool CAT5171::readWiper( uint16_t &val ) { - esp_err_t err = bus->read8bit(CAT5171_I2C_ADDR, &val ); +bool CAT5171::readWiper( int &val ) { + uint16_t i16val; + esp_err_t err = bus->read8bit(CAT5171_I2C_ADDR, &i16val ); if( err == ESP_OK ){ - // ESP_LOGI(FNAME,"CAT5171 read wiper val=%d OK", val ); + // ESP_LOGI(FNAME,"CAT5171 read wiper val=%d OK", i16val ); + val = i16val; return true; } - else - { - ESP_LOGE(FNAME,"CAT5171 Error reading wiper, error count %d", errorcount); - errorcount++; - return false; - } + // else + ESP_LOGE(FNAME,"CAT5171 Error reading wiper, error count %d", errorcount); + errorcount++; + return false; } -bool CAT5171::writeWiper( uint16_t val ) { +bool CAT5171::writeWiper( int val ) { // ESP_LOGI(FNAME,"CAT5171 write wiper %d", val ); esp_err_t err = bus->write2bytes( CAT5171_I2C_ADDR, 0, (uint8_t)val ); // 0x40 = RS = midscale if( err != ESP_OK ){ @@ -78,10 +65,10 @@ bool CAT5171::writeWiper( uint16_t val ) { errorcount++; return false; } - uint16_t b; + int b; bool ret=readWiper( b ); - if( (b != (uint8_t)val) || !ret ){ + if( (b != val) || !ret ){ ESP_LOGE(FNAME,"CAT5171 Error writing wiper, error count %d, write %d != read %d", errorcount, val, b ); } @@ -89,6 +76,21 @@ bool CAT5171::writeWiper( uint16_t val ) { return true; } +bool CAT5171::readVolume( float &val ) { + int ival; + if ( readWiper( ival ) ) { + val = (float)(100 * ival) * getInvRange(); + return true; + } + return false; +} + +bool CAT5171::writeVolume( float val ) { + int ival = (int)(val * getRange()); + ival /= 100; + return writeWiper( ival ); +} + bool CAT5171::reset() { ESP_LOGI(FNAME,"CAT5171 reset"); diff --git a/main/cat5171.h b/main/cat5171.h index 51bbba916..6275bc4b8 100644 --- a/main/cat5171.h +++ b/main/cat5171.h @@ -28,20 +28,22 @@ class CAT5171: public Poti */ ~CAT5171(); - bool readWiper( uint16_t& val ); - bool writeWiper( uint16_t val ); + bool readVolume( float& val ); + bool writeVolume( float val ); bool reset(); - bool incWiper(); - bool decWiper(); bool haveDevice(); - inline int getRange() { return 255; }; - inline int getStep() { return 3; }; private: + bool readWiper( int& val ); + bool writeWiper( int val ); +#define CAT5171RANGE 255 + inline int getRange() { return CAT5171RANGE; }; + inline float getInvRange() { return (1.0/CAT5171RANGE); }; + inline int getStep() { return 3; }; I2C_t *bus; int errorcount; bool _noDevice; - uint16_t wiper; // only bit 0..7 supported + int wiper; // only bit 0..7 supported }; #endif diff --git a/main/mcp4018.cpp b/main/mcp4018.cpp index 44dd79d6b..ac98fff02 100644 --- a/main/mcp4018.cpp +++ b/main/mcp4018.cpp @@ -7,14 +7,16 @@ MCP4018::MCP4018() { errorcount=0; _noDevice = false; - wiper = 63; + wiper = MCP4018RANGE/2; bus = 0; } bool MCP4018::begin() { errorcount=0; - if( readWiper( wiper ) ) { + int local_wiper; + if( readWiper( local_wiper ) ) { + wiper = local_wiper; ESP_LOGI(FNAME,"MCP4018 wiper=%d", wiper ); return(true); } @@ -42,23 +44,12 @@ bool MCP4018::haveDevice() { } } -bool MCP4018::incWiper(){ - if( wiper <127 ) - wiper++; - return( writeWiper(wiper) ); -} - -bool MCP4018::decWiper(){ - if( wiper >1 ) - wiper--; - return( writeWiper(wiper) ); -} - - -bool MCP4018::readWiper( uint16_t &val ) { - esp_err_t err = bus->read8bit(MPC4018_I2C_ADDR, &val ); +bool MCP4018::readWiper( int &val ) { + uint16_t i16val; + esp_err_t err = bus->read8bit(MPC4018_I2C_ADDR, &i16val ); if( err == ESP_OK ){ - ESP_LOGI(FNAME,"MCP4018 read wiper val=%d OK", val ); + //ESP_LOGI(FNAME,"MCP4018 read wiper val=%d OK", i16val ); + val = i16val; return true; } else @@ -69,9 +60,9 @@ bool MCP4018::readWiper( uint16_t &val ) { } } -bool MCP4018::writeWiper( uint16_t val ) { +bool MCP4018::writeWiper( int val ) { // ESP_LOGI(FNAME,"MCP4018 write wiper %d", val ); - esp_err_t err = bus->write8bit(MPC4018_I2C_ADDR, val ); + esp_err_t err = bus->write8bit(MPC4018_I2C_ADDR, (uint16_t)val ); if( err == ESP_OK ){ // ESP_LOGV(FNAME,"MCP4018 write wiper OK"); return true; @@ -84,6 +75,20 @@ bool MCP4018::writeWiper( uint16_t val ) { } } +bool MCP4018::readVolume( float &val ) { + int ival; + if ( readWiper( ival ) ) { + val = (float)(100 * ival) * getInvRange(); + return true; + } + else + { + return false; + } +} - - +bool MCP4018::writeVolume( float val ) { + int ival = (int)(val * getRange()); + ival /= 100; + return writeWiper( ival ); +} diff --git a/main/mcp4018.h b/main/mcp4018.h index fa7691e31..90fbad53b 100644 --- a/main/mcp4018.h +++ b/main/mcp4018.h @@ -28,19 +28,21 @@ class MCP4018: public Poti */ ~MCP4018(); - bool readWiper( uint16_t& val ); - bool writeWiper( uint16_t val ); - bool incWiper(); - bool decWiper(); + bool readVolume( float& val ); + bool writeVolume( float val ); bool haveDevice(); - inline int getRange() { return 127; }; // 7 bit 0..127 - inline int getStep() { return 2; }; private: + bool readWiper( int& val ); + bool writeWiper( int val ); +#define MCP4018RANGE 127 + inline int getRange() { return MCP4018RANGE; }; + inline float getInvRange() { return (1.0/MCP4018RANGE); }; + inline int getStep() { return 2; }; I2C_t *bus; int errorcount; bool _noDevice; - uint16_t wiper; // only bit 0..7 supported + int wiper; // only bit 0..7 supported }; #endif diff --git a/main/sensor.cpp b/main/sensor.cpp index 5c0576c89..d8188b600 100644 --- a/main/sensor.cpp +++ b/main/sensor.cpp @@ -361,7 +361,7 @@ void drawDisplay(void *pvParameters){ if( gload_mode.get() != GLOAD_OFF ){ if( (float)accelG[0] > gload_pos_limit.get() || (float)accelG[0] < gload_neg_limit.get() ){ if( !gflags.gload_alarm ) { - Audio::alarm( true, DigitalPoti->getRange()*(gload_alarm_volume.get()/100) ); + Audio::alarm( true, gload_alarm_volume.get() ); gflags.gload_alarm = true; } }else diff --git a/main/sound.cpp b/main/sound.cpp index 40bf76dc0..b8033cdfb 100644 --- a/main/sound.cpp +++ b/main/sound.cpp @@ -77,9 +77,9 @@ void Sound::playSound( e_sound a_sound, bool end ){ sound = a_sound; ESP_LOGI(FNAME,"Start play sound"); Audio::setTestmode(true); - uint16_t volume; - _poti->readWiper( volume ); - _poti->writeWiper( 50 ); + float volume; + _poti->readVolume( volume ); + _poti->writeVolume( 50.0 ); dac_output_enable(DAC_CHANNEL_1); dac_output_voltage(DAC_CHANNEL_1,127); sleep(0.05); @@ -91,7 +91,7 @@ void Sound::playSound( e_sound a_sound, bool end ){ timer_disable_intr(TIMER_GROUP_0, TIMER_0); ESP_LOGI(FNAME,"play Sound end"); if( end ) { - _poti->writeWiper( volume ); + _poti->writeVolume( volume ); Audio::restart(); } ESP_LOGI(FNAME,"play Sound task end");