mirror of
https://github.com/betaflight/betaflight.git
synced 2025-07-23 16:25:31 +03:00
Pit mode on a switch
This commit is contained in:
parent
0d89fce800
commit
b13e73c9bb
7 changed files with 110 additions and 33 deletions
|
@ -23,6 +23,7 @@
|
|||
#if defined(VTX_COMMON)
|
||||
|
||||
#include "common/time.h"
|
||||
#include "common/maths.h"
|
||||
|
||||
#include "config/parameter_group.h"
|
||||
#include "config/parameter_group_ids.h"
|
||||
|
@ -31,6 +32,7 @@
|
|||
|
||||
#include "fc/config.h"
|
||||
#include "fc/runtime_config.h"
|
||||
#include "fc/rc_modes.h"
|
||||
|
||||
#include "io/vtx.h"
|
||||
#include "io/vtx_string.h"
|
||||
|
@ -43,7 +45,8 @@ PG_RESET_TEMPLATE(vtxSettingsConfig_t, vtxSettingsConfig,
|
|||
.channel = VTX_SETTINGS_DEFAULT_CHANNEL,
|
||||
.power = VTX_SETTINGS_DEFAULT_POWER,
|
||||
.freq = VTX_SETTINGS_DEFAULT_FREQ,
|
||||
.lowPowerDisarm = 0,
|
||||
.pitModeFreq = VTX_SETTINGS_DEFAULT_PITMODE_FREQ,
|
||||
.lowPowerDisarm = VTX_SETTINGS_DEFAULT_LOW_POWER_DISARM,
|
||||
);
|
||||
|
||||
#define VTX_PARAM_CYCLE_TIME_US 100000 // 10Hz
|
||||
|
@ -51,6 +54,7 @@ PG_RESET_TEMPLATE(vtxSettingsConfig_t, vtxSettingsConfig,
|
|||
typedef enum {
|
||||
VTX_PARAM_BANDCHAN = 0,
|
||||
VTX_PARAM_POWER,
|
||||
VTX_PARAM_PITMODE,
|
||||
VTX_PARAM_CONFIRM,
|
||||
VTX_PARAM_COUNT
|
||||
} vtxScheduleParams_e;
|
||||
|
@ -61,27 +65,70 @@ static uint8_t vtxParamSchedule[VTX_PARAM_COUNT];
|
|||
void vtxInit(void)
|
||||
{
|
||||
uint8_t index = 0;
|
||||
bool settingsUpdated = false;
|
||||
|
||||
vtxParamSchedule[index++] = VTX_PARAM_BANDCHAN;
|
||||
vtxParamSchedule[index++] = VTX_PARAM_POWER;
|
||||
vtxParamSchedule[index++] = VTX_PARAM_PITMODE;
|
||||
vtxParamSchedule[index++] = VTX_PARAM_CONFIRM;
|
||||
|
||||
vtxParamScheduleCount = index;
|
||||
|
||||
// sync frequency in parameter group when band/channel are specified
|
||||
const uint16_t freq = vtx58_Bandchan2Freq(vtxSettingsConfig()->band, vtxSettingsConfig()->channel);
|
||||
if (vtxSettingsConfig()->band && freq != vtxSettingsConfig()->freq) {
|
||||
vtxSettingsConfigMutable()->freq = freq;
|
||||
settingsUpdated = true;
|
||||
}
|
||||
|
||||
#if defined(VTX_SETTINGS_FREQCMD)
|
||||
// constrain pit mode frequency
|
||||
if (vtxSettingsConfig()->pitModeFreq) {
|
||||
const uint16_t constrainedPitModeFreq = MAX(vtxSettingsConfig()->pitModeFreq, VTX_SETTINGS_MIN_USER_FREQ);
|
||||
if (constrainedPitModeFreq != vtxSettingsConfig()->pitModeFreq) {
|
||||
vtxSettingsConfigMutable()->pitModeFreq = constrainedPitModeFreq;
|
||||
settingsUpdated = true;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
if (settingsUpdated) {
|
||||
saveConfigAndNotify();
|
||||
}
|
||||
}
|
||||
|
||||
static vtxSettingsConfig_t vtxGetSettings(void) {
|
||||
vtxSettingsConfig_t settings = {
|
||||
.band = vtxSettingsConfig()->band,
|
||||
.channel = vtxSettingsConfig()->channel,
|
||||
.power = vtxSettingsConfig()->power,
|
||||
.freq = vtxSettingsConfig()->freq,
|
||||
.pitModeFreq = vtxSettingsConfig()->pitModeFreq,
|
||||
.lowPowerDisarm = vtxSettingsConfig()->lowPowerDisarm,
|
||||
};
|
||||
|
||||
#if defined(VTX_SETTINGS_FREQCMD)
|
||||
if (IS_RC_MODE_ACTIVE(BOXVTXPITMODE) && isModeActivationConditionPresent(BOXVTXPITMODE) && settings.pitModeFreq) {
|
||||
settings.band = 0;
|
||||
settings.freq = settings.pitModeFreq;
|
||||
settings.power = VTX_SETTINGS_DEFAULT_POWER;
|
||||
}
|
||||
#endif
|
||||
|
||||
if (!ARMING_FLAG(ARMED) && settings.lowPowerDisarm) {
|
||||
settings.power = VTX_SETTINGS_DEFAULT_POWER;
|
||||
}
|
||||
|
||||
return settings;
|
||||
}
|
||||
|
||||
static bool vtxProcessBandAndChannel(void) {
|
||||
if(!ARMING_FLAG(ARMED)) {
|
||||
uint8_t vtxBand;
|
||||
uint8_t vtxChan;
|
||||
if (vtxCommonGetBandAndChannel(&vtxBand, &vtxChan)) {
|
||||
if (vtxSettingsConfig()->band != vtxBand || vtxSettingsConfig()->channel != vtxChan) {
|
||||
vtxCommonSetBandAndChannel(vtxSettingsConfig()->band, vtxSettingsConfig()->channel);
|
||||
const vtxSettingsConfig_t settings = vtxGetSettings();
|
||||
if (vtxBand != settings.band || vtxChan != settings.channel) {
|
||||
vtxCommonSetBandAndChannel(settings.band, settings.channel);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
@ -94,8 +141,9 @@ static bool vtxProcessFrequency(void) {
|
|||
if(!ARMING_FLAG(ARMED)) {
|
||||
uint16_t vtxFreq;
|
||||
if (vtxCommonGetFrequency(&vtxFreq)) {
|
||||
if (vtxSettingsConfig()->freq != vtxFreq) {
|
||||
vtxCommonSetFrequency(vtxSettingsConfig()->freq);
|
||||
const vtxSettingsConfig_t settings = vtxGetSettings();
|
||||
if (vtxFreq != settings.freq) {
|
||||
vtxCommonSetFrequency(settings.freq);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
@ -106,29 +154,43 @@ static bool vtxProcessFrequency(void) {
|
|||
|
||||
static bool vtxProcessPower(void) {
|
||||
uint8_t vtxPower;
|
||||
uint8_t newPower;
|
||||
if (vtxCommonGetPowerIndex(&vtxPower)) {
|
||||
if (!ARMING_FLAG(ARMED) && vtxSettingsConfig()->lowPowerDisarm) {
|
||||
newPower = VTX_SETTINGS_DEFAULT_POWER;
|
||||
} else {
|
||||
newPower = vtxSettingsConfig()->power;
|
||||
}
|
||||
if (vtxPower != newPower) {
|
||||
vtxCommonSetPowerByIndex(newPower);
|
||||
const vtxSettingsConfig_t settings = vtxGetSettings();
|
||||
if (vtxPower != settings.power) {
|
||||
vtxCommonSetPowerByIndex(settings.power);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
static bool vtxProcessPitMode(void) {
|
||||
uint8_t pitOnOff;
|
||||
if (!ARMING_FLAG(ARMED) && vtxCommonGetPitMode(&pitOnOff)) {
|
||||
if (IS_RC_MODE_ACTIVE(BOXVTXPITMODE)) {
|
||||
#if defined(VTX_SETTINGS_FREQCMD)
|
||||
if (vtxSettingsConfig()->pitModeFreq) {
|
||||
return false;
|
||||
}
|
||||
#endif
|
||||
if (isModeActivationConditionPresent(BOXVTXPITMODE)) {
|
||||
if (!pitOnOff) {
|
||||
vtxCommonSetPitMode(true);
|
||||
return true;
|
||||
}
|
||||
} else {
|
||||
if (pitOnOff) {
|
||||
vtxCommonSetPitMode(false);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
static bool vtxProcessStateUpdate(void) {
|
||||
const vtxSettingsConfig_t vtxSettingsState = {
|
||||
.band = vtxSettingsConfig()->band,
|
||||
.channel = vtxSettingsConfig()->channel,
|
||||
.power = vtxSettingsConfig()->power,
|
||||
.freq = vtxSettingsConfig()->freq,
|
||||
.lowPowerDisarm = vtxSettingsConfig()->lowPowerDisarm,
|
||||
};
|
||||
const vtxSettingsConfig_t vtxSettingsState = vtxGetSettings();
|
||||
vtxSettingsConfig_t vtxState = vtxSettingsState;
|
||||
|
||||
if (vtxSettingsState.band) {
|
||||
|
@ -152,11 +214,12 @@ void vtxProcessSchedule(timeUs_t currentTimeUs)
|
|||
|
||||
if (vtxCommonDeviceRegistered()) {
|
||||
const uint8_t currentSchedule = vtxParamSchedule[scheduleIndex];
|
||||
const vtxSettingsConfig_t settings = vtxGetSettings();
|
||||
// Process VTX changes from the parameter group at 10Hz
|
||||
if (currentTimeUs > lastCycleTimeUs + VTX_PARAM_CYCLE_TIME_US) {
|
||||
switch (currentSchedule) {
|
||||
case VTX_PARAM_BANDCHAN:
|
||||
if (vtxSettingsConfig()->band) {
|
||||
if (settings.band) {
|
||||
vtxUpdatePending = vtxProcessBandAndChannel();
|
||||
#if defined(VTX_SETTINGS_FREQCMD)
|
||||
} else {
|
||||
|
@ -167,6 +230,9 @@ void vtxProcessSchedule(timeUs_t currentTimeUs)
|
|||
case VTX_PARAM_POWER:
|
||||
vtxUpdatePending = vtxProcessPower();
|
||||
break;
|
||||
case VTX_PARAM_PITMODE:
|
||||
vtxUpdatePending = vtxProcessPitMode();
|
||||
break;
|
||||
case VTX_PARAM_CONFIRM:
|
||||
vtxUpdatePending = vtxProcessStateUpdate();
|
||||
break;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue