Skip to content

Commit

Permalink
VTX abstraction (experimental)
Browse files Browse the repository at this point in the history
  • Loading branch information
ezshinoda committed Jan 14, 2017
1 parent 2d6b718 commit cdd0cd4
Show file tree
Hide file tree
Showing 9 changed files with 573 additions and 24 deletions.
4 changes: 4 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -591,6 +591,8 @@ HIGHEND_SRC = \
drivers/serial_escserial.c \
drivers/serial_softserial.c \
drivers/sonar_hcsr04.c \
drivers/vtx_var.c \
drivers/vtx_common.c \
flight/navigation.c \
flight/gps_conversion.c \
io/dashboard.c \
Expand Down Expand Up @@ -704,6 +706,8 @@ SPEED_OPTIMISED_SRC := $(SPEED_OPTIMISED_SRC) \

SIZE_OPTIMISED_SRC := $(SIZE_OPTIMISED_SRC) \
drivers/serial_escserial.c \
drivers/vtx_var.c \
drivers/vtx_common.c \
io/cli.c \
io/serial_4way.c \
io/serial_4way_avrootloader.c \
Expand Down
140 changes: 140 additions & 0 deletions src/main/drivers/vtx_common.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
/*
* This file is part of Cleanflight.
*
* Cleanflight is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Cleanflight is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Cleanflight. If not, see <http://www.gnu.org/licenses/>.
*/

/* Created by jflyper */

#include <stdbool.h>
#include <stdint.h>
#include <ctype.h>
#include <string.h>

#include "platform.h"
#include "build/debug.h"

#if defined(VTX_COMMON)

#include "vtx_common.h"
#include "vtx_var.h"

vtxDevice_t *vtxDevice = NULL;

void vtxCommonInit(void)
{
}

// Whatever registered last will win

void vtxCommonRegisterDevice(vtxDevice_t *pDevice)
{
vtxDevice = pDevice;
}

vtxDevType_e vtxCommonGetDeviceType(void)
{
if (!vtxDevice)
return VTXDEV_UNKNOWN;

return vtxDevice->devtype;
}

// band and chan are 1 origin
void vtxCommonSetBandChan(uint8_t band, uint8_t chan)
{
if (!vtxDevice)
return;

if (vtxDevice->vTable->setBandChan)
vtxDevice->vTable->setBandChan(band, chan);
}

// index is zero origin, zero = power off completely
void vtxCommonSetPowerByIndex(uint8_t index)
{
if (!vtxDevice)
return;

if (vtxDevice->vTable->setPowerByIndex)
vtxDevice->vTable->setPowerByIndex(index);
}

// on = 1, off = 0
void vtxCommonSetPitmode(uint8_t onoff)
{
if (!vtxDevice)
return;

if (vtxDevice->vTable->setPitmode)
vtxDevice->vTable->setPitmode(onoff);
}

bool vtxCommonGetBandChan(uint8_t *pBand, uint8_t *pChan)
{
if (!vtxDevice)
return false;

if (vtxDevice->vTable->getBandChan)
return vtxDevice->vTable->getBandChan(pBand, pChan);
else
return false;
}

bool vtxCommonGetPowerIndex(uint8_t *pIndex)
{
if (!vtxDevice)
return false;

if (vtxDevice->vTable->getPowerIndex)
return vtxDevice->vTable->getPowerIndex(pIndex);
else
return false;
}

bool vtxCommonGetPitmode(uint8_t *pOnoff)
{
if (!vtxDevice)
return false;

if (vtxDevice->vTable->getPitmode)
return vtxDevice->vTable->getPitmode(pOnoff);
else
return false;
}

// Utilities

bool vtx58_Freq2Bandchan(uint16_t freq, uint8_t *pBand, uint8_t *pChan)
{
uint8_t band;
uint8_t chan;

for (band = 0 ; band < 5 ; band++) {
for (chan = 0 ; chan < 8 ; chan++) {
if (vtx58FreqTable[band][chan] == freq) {
*pBand = band + 1;
*pChan = chan + 1;
return true;
}
}
}

*pBand = 0;
*pChan = 0;

return false;
}

#endif
82 changes: 82 additions & 0 deletions src/main/drivers/vtx_common.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
/*
* This file is part of Cleanflight.
*
* Cleanflight is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Cleanflight is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Cleanflight. If not, see <http://www.gnu.org/licenses/>.
*/

/* Created by jflyper */

typedef enum {
VTXDEV_UNKNOWN = 0,
VTXDEV_SMARTAUDIO = 3,
VTXDEV_TRAMP = 4,
} vtxDevType_e;

struct vtxVTable_s;

typedef struct vtxDevice_s {
const struct vtxVTable_s *vTable;

vtxDevType_e devtype; // 3.1 only; eventually goes away

uint8_t numBand;
uint8_t numChan;
uint8_t numPower;

uint16_t *freqTable; // Array of [numBand][numChan]
char **bandNames; // char *bandNames[numBand]
char **chanNames; // char *chanNames[numChan]
char **powerNames; // char *powerNames[numPower]

uint8_t curBand;
uint8_t curChan;
uint8_t curPowerIndex;
uint8_t curPitState; // 0 = non-PIT, 1 = PIT

} vtxDevice_t;

// {set,get}BandChan: band and chan are 1 origin
// {set,get}PowerByIndex: 0 = Power OFF, 1 = device dependent
// {set,get}Pitmode: 0 = OFF, 1 = ON

typedef struct vtxVTable_s {
vtxDevType_e (*getDeviceType)(void);
bool (*isReady)(void);

void (*setBandChan)(uint8_t band, uint8_t chan);
void (*setPowerByIndex)(uint8_t level);
void (*setPitmode)(uint8_t onoff);

bool (*getBandChan)(uint8_t *pBand, uint8_t *pChan);
bool (*getPowerIndex)(uint8_t *pIndex);
bool (*getPitmode)(uint8_t *pOnoff);
} vtxVTable_t;

// 3.1.0
// PIT mode is defined as LOWEST POSSIBLE RF POWER.
// - It can be a dedicated mode, or lowest RF power possible.
// - It is *NOT* RF on/off control ?

void vtxCommonInit(void);
void vtxCommonRegisterDevice(vtxDevice_t *pDevice);

uint8_t vtxCommonGetDeviceType(void);
void vtxCommonSetBandChan(uint8_t band, uint8_t chan);
void vtxCommonSetPowerByIndex(uint8_t level);
void vtxCommonSetPitmode(uint8_t onoff);
bool vtxCommonGetBandChan(uint8_t *pBand, uint8_t *pChan);
bool vtxCommonGetPowerIndex(uint8_t *pIndex);
bool vtxCommonGetPitmode(uint8_t *pOnoff);

bool vtx58_Freq2Bandchan(uint16_t freq, uint8_t *pBand, uint8_t *pChan);
45 changes: 45 additions & 0 deletions src/main/drivers/vtx_var.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* This file is part of Cleanflight.
*
* Cleanflight is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Cleanflight is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Cleanflight. If not, see <http://www.gnu.org/licenses/>.
*/

/* Created by jflyper */

#include <stdint.h>
#include "vtx_var.h"

const uint16_t vtx58FreqTable[5][8] =
{
{ 5865, 5845, 5825, 5805, 5785, 5765, 5745, 5725 }, // Boscam A
{ 5733, 5752, 5771, 5790, 5809, 5828, 5847, 5866 }, // Boscam B
{ 5705, 5685, 5665, 5645, 5885, 5905, 5925, 5945 }, // Boscam E
{ 5740, 5760, 5780, 5800, 5820, 5840, 5860, 5880 }, // FatShark
{ 5658, 5695, 5732, 5769, 5806, 5843, 5880, 5917 }, // RaceBand
};

const char * const vtx58BandNames[] = {
"--------",
"BOSCAM A",
"BOSCAM B",
"BOSCAM E",
"FATSHARK",
"RACEBAND",
};

const char vtx58BandLetter[] = "-ABEFR";

const char * const vtx58ChanNames[] = {
"-", "1", "2", "3", "4", "5", "6", "7", "8",
};
27 changes: 27 additions & 0 deletions src/main/drivers/vtx_var.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* This file is part of Cleanflight.
*
* Cleanflight is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Cleanflight is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Cleanflight. If not, see <http://www.gnu.org/licenses/>.
*/

/* Created by jflyper */

#pragma once

#include <stdint.h>

extern const uint16_t vtx58FreqTable[5][8];
extern const char * const vtx58BandNames[];
extern const char * const vtx58ChanNames[];
extern const char vtx58BandLetter[];
Loading

0 comments on commit cdd0cd4

Please sign in to comment.