Moved code for radio state update to a dedicated function

This commit is contained in:
Silvano Seva 2022-02-27 09:00:49 +01:00
parent 91c9408f32
commit 3f2df608c9
3 changed files with 62 additions and 57 deletions

View File

@ -77,7 +77,6 @@ m17_t;
*/
typedef struct
{
bool radioStateUpdated;
curTime_t time;
uint16_t v_bat;
uint8_t charge;
@ -131,17 +130,21 @@ extern state_t state;
void state_init();
/**
* Write default values to OpenRTX settings and VFO Channel configuration
* Writes out to flash and calls state_init again to reload it immediately
* Write default values to OpenRTX settings and VFO Channel configuration.
* Writes out to flash and calls state_init again to reload it immediately.
*/
void defaultSettingsAndVfo();
/**
* This function terminates the Radio state,
* Saving persistent settings to flash.
* This function terminates the radio state saving persistent settings to flash.
*/
void state_terminate();
/**
* Update radio state fetching data from device drivers.
*/
void state_update();
/**
* The RTC and state.time are set to UTC time
* Use this function to get local time from UTC time based on timezone setting

View File

@ -65,10 +65,9 @@ void state_init()
/*
* Initialise remaining fields
*/
state.radioStateUpdated = true;
#ifdef HAS_RTC
#ifdef HAS_RTC
state.time = rtc_getTime();
#endif
#endif
state.v_bat = platform_getVbat();
state.charge = battery_getCharge(state.v_bat);
state.rssi = rtx_getRssi();
@ -95,6 +94,25 @@ void state_terminate()
nvm_writeSettingsAndVfo(&state.settings, &state.channel);
}
void state_update()
{
/*
* Low-pass filtering with a time constant of 10s when updated at 1Hz
* Original computation: state.v_bat = 0.02*vbat + 0.98*state.v_bat
* Peak error is 18mV when input voltage is 49mV.
*/
uint16_t vbat = platform_getVbat();
state.v_bat -= (state.v_bat * 2) / 100;
state.v_bat += (vbat * 2) / 100;
state.charge = battery_getCharge(state.v_bat);
state.rssi = rtx_getRssi();
#ifdef HAS_RTC
state.time = rtc_getTime();
#endif
}
curTime_t state_getLocalTime(curTime_t utc_time)
{
curTime_t local_time = utc_time;

View File

@ -225,23 +225,7 @@ void *dev_task(void *arg)
{
// Lock mutex and update internal state
pthread_mutex_lock(&state_mutex);
#ifdef HAS_RTC
state.time = rtc_getTime();
#endif
/*
* Low-pass filtering with a time constant of 10s when updated at 1Hz
* Original computation: state.v_bat = 0.02*vbat + 0.98*state.v_bat
* Peak error is 18mV when input voltage is 49mV.
*/
uint16_t vbat = platform_getVbat();
state.v_bat -= (state.v_bat * 2) / 100;
state.v_bat += (vbat * 2) / 100;
state.charge = battery_getCharge(state.v_bat);
state.rssi = rtx_getRssi();
state_update();
pthread_mutex_unlock(&state_mutex);
// Signal state update to UI thread