From 847750e233dd154590e100774225b0a65fea7e9f Mon Sep 17 00:00:00 2001 From: Silvano Seva Date: Tue, 15 Oct 2024 17:33:58 +0200 Subject: [PATCH] Added volume level to device state data structure. Added filtered volume level to device state. Value is updated at 10Hz and filtering is done by averaging the current value with the new sample. --- openrtx/include/core/state.h | 1 + openrtx/src/core/state.c | 9 +++++++++ openrtx/src/ui/default/ui_main.c | 2 +- platform/drivers/audio/audio_MDx.cpp | 7 +++---- 4 files changed, 14 insertions(+), 5 deletions(-) diff --git a/openrtx/include/core/state.h b/openrtx/include/core/state.h index 47d5f74c..b69efdcb 100644 --- a/openrtx/include/core/state.h +++ b/openrtx/include/core/state.h @@ -40,6 +40,7 @@ typedef struct uint16_t v_bat; uint8_t charge; rssi_t rssi; + uint8_t volume; uint8_t ui_screen; uint8_t tuner_mode; diff --git a/openrtx/src/core/state.c b/openrtx/src/core/state.c index 4404d94a..31a91050 100644 --- a/openrtx/src/core/state.c +++ b/openrtx/src/core/state.c @@ -72,6 +72,7 @@ void state_init() state.v_bat = platform_getVbat(); state.charge = battery_getCharge(state.v_bat); state.rssi = -127.0f; + state.volume = platform_getVolumeLevel(); state.channel_index = 0; // Set default channel index (it is 0-based) state.bank_enabled = false; @@ -126,6 +127,14 @@ void state_task() state.v_bat += (vbat * 2) / 100; #endif + /* + * Update volume level, as a 50% average between previous value and a new + * read of the knob position. This gives a good reactivity while preventing + * the volume level to jitter when the knob is not being moved. + */ + uint16_t vol = platform_getVolumeLevel() + state.volume; + state.volume = vol / 2; + state.charge = battery_getCharge(state.v_bat); state.rssi = rtx_getRssi(); diff --git a/openrtx/src/ui/default/ui_main.c b/openrtx/src/ui/default/ui_main.c index cfb8e962..9f7d7ae3 100644 --- a/openrtx/src/ui/default/ui_main.c +++ b/openrtx/src/ui/default/ui_main.c @@ -260,7 +260,7 @@ void _ui_drawMainBottom() // Squelch bar rssi_t rssi = last_state.rssi; uint8_t squelch = last_state.settings.sqlLevel; - uint8_t volume = platform_getVolumeLevel(); + uint8_t volume = last_state.volume; uint16_t meter_width = CONFIG_SCREEN_WIDTH - 2 * layout.horizontal_pad; uint16_t meter_height = layout.bottom_h; point_t meter_pos = { layout.horizontal_pad, diff --git a/platform/drivers/audio/audio_MDx.cpp b/platform/drivers/audio/audio_MDx.cpp index 190686fc..3d8f043e 100644 --- a/platform/drivers/audio/audio_MDx.cpp +++ b/platform/drivers/audio/audio_MDx.cpp @@ -87,14 +87,13 @@ static void *audio_thread(void *arg) { Cx000dac_task(); - uint8_t volume = platform_getVolumeLevel(); - if(volume != oldVolume) + if(state.volume != oldVolume) { // Apply new volume level, map 0 - 255 range into -31 to 31 - int8_t gain = ((int8_t) (volume / 4)) - 31; + int8_t gain = ((int8_t) (state.volume / 4)) - 31; C6000.setDacGain(gain); - oldVolume = volume; + oldVolume = state.volume; } now += 4;