Skip to content

Commit

Permalink
Merge pull request #270 from moshe-braner/encapsulate
Browse files Browse the repository at this point in the history
Encapsulate "wiper" inside Poti
  • Loading branch information
iltis42 authored Dec 14, 2023
2 parents 9e0af03 + 101cc55 commit a019e19
Show file tree
Hide file tree
Showing 13 changed files with 160 additions and 144 deletions.
89 changes: 45 additions & 44 deletions main/ESPAudio.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -73,15 +73,15 @@ 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;

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
Expand Down Expand Up @@ -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(){
Expand Down Expand Up @@ -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
Expand All @@ -250,15 +249,15 @@ bool Audio::selfTest(){
setFrequency( f );
current_frequency = f;
if( !fadein ){
int volume = 3;
for( int i=0; i<FADING_STEPS && volume <= (int)setwiper; i++ ) {
float volume = 3;
for( int i=0; i<FADING_STEPS && volume <= setvolume; i++ ) {
writeVolume( volume );
volume = volume*1.75;
delay(1);
fadein = true;
}
}
writeVolume( setwiper );
writeVolume( setvolume );
delay(20);
esp_task_wdt_reset();
}
Expand Down Expand Up @@ -339,7 +338,7 @@ void Audio::dac_scale_set(dac_channel_t channel, int scale)
}
}

void Audio::alarm( bool enable, int volume, e_audio_alarm_type_t style ){ // non blocking
void Audio::alarm( bool enable, float volume, e_audio_alarm_type_t style ){ // non blocking
if( enable && !_alarm_mode ){ // Begin of alarm
_vol_back_s2f = s2f_mode_volume;
_vol_back_vario = vario_mode_volume;
Expand All @@ -361,7 +360,7 @@ void Audio::alarm( bool enable, int volume, e_audio_alarm_type_t style ){ // no
writeVolume( vario_mode_volume );
}
if( enable ) { // tune alarm
// ESP_LOGI(FNAME,"Alarm sound enable volume: %d style: %d", volume, style );
// ESP_LOGI(FNAME,"Alarm sound enable volume: %f style: %d", volume, style );
if( style == AUDIO_ALARM_STALL ){
_te = 3.0;
_tonemode = ATM_DUAL_TONE;
Expand Down Expand Up @@ -437,7 +436,7 @@ void Audio::dac_invert_set(dac_channel_t channel, int invert)
}


void Audio::setVolume( int vol ) {
void Audio::setVolume( float vol ) {
speaker_volume = vol;
volume_change = 100;
};
Expand Down Expand Up @@ -495,12 +494,12 @@ void Audio::calculateFrequency(){
// ESP_LOGI(FNAME, "New Freq: (%0.1f) TE:%0.2f exp_fac:%0.1f", current_frequency, _te, mult );
}

void Audio::writeVolume( uint16_t volume ){
// ESP_LOGI(FNAME, "set volume: %d", volume);
void Audio::writeVolume( float volume ){
// ESP_LOGI(FNAME, "set volume: %f", volume);
if( _alarm_mode )
DigitalPoti->writeWiper( 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;
}

Expand Down Expand Up @@ -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 ){
Expand All @@ -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);
}
}
Expand All @@ -614,7 +613,7 @@ void Audio::dactask(void* arg )
}
else{
float volume=3;
for( int i=0; i<FADING_STEPS && (int)volume <=(speaker_volume); i++ ) {
for( int i=0; i<FADING_STEPS && volume <=(speaker_volume); i++ ) {
// ESP_LOGI(FNAME, "fade in sound, volume: %3.1f", volume );
writeVolume( volume );
volume = volume*1.75;
Expand All @@ -637,10 +636,12 @@ void Audio::dactask(void* arg )
writeVolume( 0 );
}else{
float volume = (float)(speaker_volume);
for( int i=0; i<FADING_STEPS && (int)volume >=0; i++ ) {
for( int i=0; i<FADING_STEPS && volume > 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 );
Expand Down
18 changes: 9 additions & 9 deletions main/ESPAudio.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion main/Flarm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
10 changes: 6 additions & 4 deletions main/Poti.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};

Expand Down
4 changes: 3 additions & 1 deletion main/SetupMenu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down Expand Up @@ -516,6 +516,8 @@ void SetupMenu::down(int count){
float vol = audio_volume.get();
for( int i=0; i<count; i++ )
vol = vol * 0.83;
if( vol<3.0 )
vol=0;
audio_volume.set( vol );
// ESP_LOGI(FNAME,"NEW DN VOL: %f", vol );
}
Expand Down
4 changes: 2 additions & 2 deletions main/SetupNG.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,10 +89,10 @@ void resetCWindAge() {
CircleWind::resetAge();
}

int last_volume=0;
float last_volume=0; // is this used?

void change_volume() {
Audio::setVolume( (int)audio_volume.get() );
Audio::setVolume( audio_volume.get() );
}

void flap_act() {
Expand Down
2 changes: 1 addition & 1 deletion main/SetupNG.h
Original file line number Diff line number Diff line change
Expand Up @@ -670,7 +670,7 @@ extern SetupNG<int> 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();
Loading

0 comments on commit a019e19

Please sign in to comment.