Skip to content

Commit

Permalink
Allow VTX to be enabled/disabled via AUX channel. Idea by Keiren
Browse files Browse the repository at this point in the history
Black/Fraser Oneil/Calum Richings/Graeme Matthews/Liam Noble (Team
Scotland) & Dominic Clifton.
  • Loading branch information
hydra committed Nov 29, 2016
1 parent 60b2064 commit 31fa9d1
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 3 deletions.
1 change: 1 addition & 0 deletions src/main/fc/boot.c
Expand Up @@ -135,6 +135,7 @@ extern uint8_t motorControlEnable;
serialPort_t *loopbackPort;
#endif

bool isUsingVTXSwitch(void);
void mixerUsePWMIOConfiguration(pwmIOConfiguration_t *pwmIOConfiguration);
void rxInit(modeActivationCondition_t *modeActivationConditions);

Expand Down
3 changes: 3 additions & 0 deletions src/main/fc/cleanflight_fc.c
Expand Up @@ -626,6 +626,9 @@ void processRx(void)
}
#endif

#ifdef VTX
updateVTXState();
#endif
}

void filterRc(void){
Expand Down
6 changes: 6 additions & 0 deletions src/main/fc/config.c
Expand Up @@ -78,6 +78,8 @@
#define DEFAULT_RX_FEATURE FEATURE_RX_PARALLEL_PWM
#endif

void initVTXState(void);


// Default settings
STATIC_UNIT_TESTED void resetConf(void)
Expand Down Expand Up @@ -180,6 +182,10 @@ static void activateConfig(void)
recalculateMagneticDeclination();
#endif

#ifdef VTX
initVTXState();
#endif

static imuRuntimeConfig_t imuRuntimeConfig;
imuRuntimeConfig.dcm_kp = imuConfig()->dcm_kp / 10000.0f;
imuRuntimeConfig.dcm_ki = imuConfig()->dcm_ki / 10000.0f;
Expand Down
8 changes: 7 additions & 1 deletion src/main/fc/msp_server_fc.c
Expand Up @@ -164,6 +164,7 @@ static const box_t boxes[CHECKBOX_ITEM_COUNT] = {
{ "BLACKBOX", BOXBLACKBOX, 26 },
{ "FAILSAFE", BOXFAILSAFE, 27 },
{ "AIR MODE", BOXAIRMODE, 28 },
{ "VTX", BOXVTX, 29 },
};

// mask of enabled IDs, calculated on start based on enabled features. boxId_e is used as bit index.
Expand Down Expand Up @@ -368,6 +369,10 @@ static void initActiveBoxIds(void)
ena |= 1 << BOXGTUNE;
#endif

#ifdef VTX
ena |= 1 << BOXVTX;
#endif

// check that all enabled IDs are in boxes array (check is skipped when using findBoxBy<id>() functions
for(boxId_e boxId = 0; boxId < CHECKBOX_ITEM_COUNT; boxId++)
if((ena & (1 << boxId))
Expand Down Expand Up @@ -403,7 +408,7 @@ uint32_t packFlightModeFlags(void)
#define BM(x) (1 << (x))
const uint32_t rcModeCopyMask = BM(BOXHEADADJ) | BM(BOXCAMSTAB) | BM(BOXCAMTRIG) | BM(BOXBEEPERON)
| BM(BOXLEDMAX) | BM(BOXLEDLOW) | BM(BOXLLIGHTS) | BM(BOXCALIB) | BM(BOXGOV) | BM(BOXOSD)
| BM(BOXTELEMETRY) | BM(BOXGTUNE) | BM(BOXBLACKBOX) | BM(BOXAIRMODE) ;
| BM(BOXTELEMETRY) | BM(BOXGTUNE) | BM(BOXBLACKBOX) | BM(BOXAIRMODE) | BM(BOXVTX);
for(unsigned i = 0; i < sizeof(rcModeCopyMask) * 8; i++) {
if((rcModeCopyMask & BM(i)) == 0)
continue;
Expand All @@ -415,6 +420,7 @@ uint32_t packFlightModeFlags(void)
if(ARMING_FLAG(ARMED))
boxEnabledMask |= 1 << BOXARM;


// map boxId_e enabled bits to MSP status indexes
// only active boxIds are sent in status over MSP, other bits are not counted
uint32_t mspBoxEnabledMask = 0;
Expand Down
1 change: 1 addition & 0 deletions src/main/fc/rc_controls.h
Expand Up @@ -47,6 +47,7 @@ typedef enum {
BOXBLACKBOX,
BOXFAILSAFE,
BOXAIRMODE,
BOXVTX,
CHECKBOX_ITEM_COUNT
} boxId_e;

Expand Down
42 changes: 40 additions & 2 deletions src/main/io/vtx.c
Expand Up @@ -24,6 +24,7 @@
#include "config/parameter_group.h"
#include "config/parameter_group_ids.h"

#include "fc/rc_controls.h"
#include "common/utils.h"

#include "drivers/system.h"
Expand Down Expand Up @@ -143,6 +144,11 @@ void handleVTXControlButton(void)
#endif
}

bool isUsingVTXSwitch(void)
{
return rcModeIsActivationConditionPresent(modeActivationProfile()->modeActivationConditions, BOXVTX);
}

void vtxSaveState(void)
{
vtxConfig()->channel = vtxState.channel;
Expand All @@ -158,8 +164,40 @@ void vtxInit(void)
vtxState.band = vtxConfig()->band;
vtxState.rfPower = vtxConfig()->rfPower;

if (vtxConfig()->enabledOnBoot) {
vtxEnable();
if (!isUsingVTXSwitch()) {
if (vtxConfig()->enabledOnBoot) {
vtxEnable();
}
}
}

static bool vtxModeActivationConditionPresent = false;

void initVTXState(void)
{
vtxModeActivationConditionPresent = isUsingVTXSwitch();
}

void updateVTXState(void)
{
static bool vtxEnabled = false;
if (!vtxModeActivationConditionPresent) {
return;
}

if (rcModeIsActive(BOXVTX)) {
if (!vtxEnabled) {
if (!vtxState.enabled) {
vtxEnable();
}
vtxEnabled = true;
}
} else {
if (vtxEnabled) {
if (vtxState.enabled) {
vtxDisable();
}
vtxEnabled = false;
}
}
}
6 changes: 6 additions & 0 deletions src/main/io/vtx.h
Expand Up @@ -34,6 +34,12 @@ typedef struct vtxState_s {

extern vtxState_t vtxState;

// VTX Control
void initVTXState(void);
bool isUsingVTXSwitch(void);
void updateVTXState(void);


// VTX API

// common methods
Expand Down

0 comments on commit 31fa9d1

Please sign in to comment.