mirror of
https://github.com/betaflight/betaflight.git
synced 2025-07-23 00:05:33 +03:00
Merge pull request #4442 from codecae/vtx_settings_config_refactor
Refactored vtx_settings_config to reduce duplication and improve readability
This commit is contained in:
commit
cd66bcc8cb
1 changed files with 79 additions and 76 deletions
|
@ -35,43 +35,86 @@ PG_RESET_TEMPLATE(vtxSettingsConfig_t, vtxSettingsConfig,
|
||||||
.freq = 0
|
.freq = 0
|
||||||
);
|
);
|
||||||
|
|
||||||
|
static bool vtxChangePending = false;
|
||||||
|
|
||||||
|
static void vtxChangeSetting(bool changed)
|
||||||
|
{
|
||||||
|
if (changed && !vtxChangePending) {
|
||||||
|
vtxChangePending = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static bool vtxSetBand(uint8_t band)
|
||||||
|
{
|
||||||
|
if (band != vtxSettingsConfig()->band) {
|
||||||
|
vtxSettingsConfigMutable()->band = band;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
static bool vtxSetChannel(uint8_t channel)
|
||||||
|
{
|
||||||
|
if (channel != vtxSettingsConfig()->channel) {
|
||||||
|
vtxSettingsConfigMutable()->channel = channel;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
static bool vtxSetFreq(uint16_t freq)
|
||||||
|
{
|
||||||
|
if (freq && freq != vtxSettingsConfig()->freq) {
|
||||||
|
uint8_t band;
|
||||||
|
uint8_t channel;
|
||||||
|
if (vtx58_Freq2Bandchan(freq, &band, &channel)) {
|
||||||
|
vtxChangeSetting(vtxSetBand(band));
|
||||||
|
vtxChangeSetting(vtxSetChannel(channel));
|
||||||
|
} else {
|
||||||
|
vtxChangeSetting(vtxSetBand(0));
|
||||||
|
}
|
||||||
|
vtxSettingsConfigMutable()->freq = freq;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
static bool vtxSetPower(uint8_t powerIndex)
|
||||||
|
{
|
||||||
|
if (powerIndex != vtxSettingsConfig()->power) {
|
||||||
|
vtxSettingsConfigMutable()->power = powerIndex;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void vtxCommitChanges(void)
|
||||||
|
{
|
||||||
|
if (vtxChangePending) {
|
||||||
|
saveConfigAndNotify();
|
||||||
|
}
|
||||||
|
vtxChangePending = false;
|
||||||
|
}
|
||||||
|
|
||||||
//Saves the given band/channel values to configuration settings.
|
//Saves the given band/channel values to configuration settings.
|
||||||
// band: Band value (1 to 5).
|
// band: Band value (1 to 5).
|
||||||
// channel: Channel value (1 to 8).
|
// channel: Channel value (1 to 8).
|
||||||
void vtxSettingsSaveBandAndChannel(uint8_t band, uint8_t channel)
|
void vtxSettingsSaveBandAndChannel(uint8_t band, uint8_t channel)
|
||||||
{
|
{
|
||||||
bool modFlag = false;
|
vtxChangeSetting(vtxSetBand(band));
|
||||||
if (band != vtxSettingsConfigMutable()->band) {
|
vtxChangeSetting(vtxSetChannel(channel));
|
||||||
vtxSettingsConfigMutable()->band = band;
|
if (band) {
|
||||||
modFlag = true;
|
vtxChangeSetting(vtxSetFreq(vtx58_Bandchan2Freq(band, channel)));
|
||||||
}
|
|
||||||
if (channel != vtxSettingsConfigMutable()->channel) {
|
|
||||||
vtxSettingsConfigMutable()->channel = channel;
|
|
||||||
modFlag = true;
|
|
||||||
}
|
|
||||||
if (band != 0) { //enter freq value to band/channel
|
|
||||||
uint16_t freq;
|
|
||||||
if ((freq = vtx58_Bandchan2Freq(band, channel)) != 0 &&
|
|
||||||
freq != vtxSettingsConfigMutable()->freq) {
|
|
||||||
vtxSettingsConfigMutable()->freq = freq;
|
|
||||||
modFlag = true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (modFlag) {
|
|
||||||
// need to save config so vtx settings in place after reboot
|
|
||||||
saveConfigAndNotify();
|
|
||||||
}
|
}
|
||||||
|
vtxCommitChanges();
|
||||||
}
|
}
|
||||||
|
|
||||||
//Saves the given power-index value to the configuration setting.
|
//Saves the given power-index value to the configuration setting.
|
||||||
// index: Power-index value.
|
// index: Power-index value.
|
||||||
void vtxSettingsSavePowerByIndex(uint8_t index)
|
void vtxSettingsSavePowerByIndex(uint8_t index)
|
||||||
{
|
{
|
||||||
if (index != vtxSettingsConfigMutable()->power) {
|
vtxChangeSetting(vtxSetPower(index));
|
||||||
vtxSettingsConfigMutable()->power = index;
|
vtxCommitChanges();
|
||||||
// need to save config so vtx settings in place after reboot
|
|
||||||
saveConfigAndNotify();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//Saves the given band/channel/power values to configuration settings.
|
//Saves the given band/channel/power values to configuration settings.
|
||||||
|
@ -80,51 +123,21 @@ void vtxSettingsSavePowerByIndex(uint8_t index)
|
||||||
// index: Power-index value.
|
// index: Power-index value.
|
||||||
void vtxSettingsSaveBandChanAndPower(uint8_t band, uint8_t channel, uint8_t index)
|
void vtxSettingsSaveBandChanAndPower(uint8_t band, uint8_t channel, uint8_t index)
|
||||||
{
|
{
|
||||||
bool modFlag = false;
|
vtxChangeSetting(vtxSetBand(band));
|
||||||
if (band != vtxSettingsConfigMutable()->band) {
|
vtxChangeSetting(vtxSetChannel(channel));
|
||||||
vtxSettingsConfigMutable()->band = band;
|
vtxChangeSetting(vtxSetPower(index));
|
||||||
modFlag = true;
|
if (band) {
|
||||||
}
|
vtxChangeSetting(vtxSetFreq(vtx58_Bandchan2Freq(band, channel)));
|
||||||
if (channel != vtxSettingsConfigMutable()->channel) {
|
|
||||||
vtxSettingsConfigMutable()->channel = channel;
|
|
||||||
modFlag = true;
|
|
||||||
}
|
|
||||||
if (index != vtxSettingsConfigMutable()->power) {
|
|
||||||
vtxSettingsConfigMutable()->power = index;
|
|
||||||
modFlag = true;
|
|
||||||
}
|
|
||||||
if (band != 0) { //enter freq value to band/channel
|
|
||||||
uint16_t freq;
|
|
||||||
if ((freq=vtx58_Bandchan2Freq(band, channel)) != 0 &&
|
|
||||||
freq != vtxSettingsConfigMutable()->freq) {
|
|
||||||
vtxSettingsConfigMutable()->freq = freq;
|
|
||||||
modFlag = true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (modFlag) {
|
|
||||||
// need to save config so vtx settings in place after reboot
|
|
||||||
saveConfigAndNotify();
|
|
||||||
}
|
}
|
||||||
|
vtxCommitChanges();
|
||||||
}
|
}
|
||||||
|
|
||||||
//Saves the given frequency value to the configuration setting.
|
//Saves the given frequency value to the configuration setting.
|
||||||
// freq: Frequency value in MHz.
|
// freq: Frequency value in MHz.
|
||||||
void vtxSettingsSaveFrequency(uint16_t freq)
|
void vtxSettingsSaveFrequency(uint16_t freq)
|
||||||
{
|
{
|
||||||
bool modFlag = false;
|
vtxChangeSetting(vtxSetFreq(freq));
|
||||||
if (freq != vtxSettingsConfigMutable()->freq) {
|
vtxCommitChanges();
|
||||||
vtxSettingsConfigMutable()->freq = freq;
|
|
||||||
modFlag = true;
|
|
||||||
}
|
|
||||||
if (vtxSettingsConfigMutable()->band != 0) {
|
|
||||||
// set band=0 so freq setting will be used
|
|
||||||
vtxSettingsConfigMutable()->band = 0;
|
|
||||||
modFlag = true;
|
|
||||||
}
|
|
||||||
if (modFlag) {
|
|
||||||
// need to save config so vtx settings in place after reboot
|
|
||||||
saveConfigAndNotify();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//Saves the given frequency/power values to configuration settings.
|
//Saves the given frequency/power values to configuration settings.
|
||||||
|
@ -132,19 +145,9 @@ void vtxSettingsSaveFrequency(uint16_t freq)
|
||||||
// index: Power-index value.
|
// index: Power-index value.
|
||||||
void vtxSettingsSaveFreqAndPower(uint16_t freq, uint8_t index)
|
void vtxSettingsSaveFreqAndPower(uint16_t freq, uint8_t index)
|
||||||
{
|
{
|
||||||
bool modFlag = false;
|
vtxChangeSetting(vtxSetFreq(freq));
|
||||||
if (freq != vtxSettingsConfigMutable()->freq) {
|
vtxChangeSetting(vtxSetPower(index));
|
||||||
vtxSettingsConfigMutable()->freq = freq;
|
vtxCommitChanges();
|
||||||
modFlag = true;
|
|
||||||
}
|
|
||||||
if (index != vtxSettingsConfigMutable()->power) {
|
|
||||||
vtxSettingsConfigMutable()->power = index;
|
|
||||||
modFlag = true;
|
|
||||||
}
|
|
||||||
if (modFlag) {
|
|
||||||
// need to save config so vtx settings in place after reboot
|
|
||||||
saveConfigAndNotify();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif //VTX_SETTINGS_CONFIG
|
#endif //VTX_SETTINGS_CONFIG
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue