From cdd0cd4528024b20bfd29c9ab614f4e550b49225 Mon Sep 17 00:00:00 2001 From: jflyper Date: Sun, 15 Jan 2017 01:27:02 +0900 Subject: [PATCH 1/6] VTX abstraction (experimental) --- Makefile | 4 + src/main/drivers/vtx_common.c | 140 ++++++++++++++++++++++++++++++++ src/main/drivers/vtx_common.h | 82 +++++++++++++++++++ src/main/drivers/vtx_var.c | 45 +++++++++++ src/main/drivers/vtx_var.h | 27 +++++++ src/main/io/vtx_singularity.c | 110 +++++++++++++++++++++++++ src/main/io/vtx_singularity.h | 41 ++++++++++ src/main/io/vtx_smartaudio.c | 147 ++++++++++++++++++++++++++++------ src/main/target/common.h | 1 + 9 files changed, 573 insertions(+), 24 deletions(-) create mode 100644 src/main/drivers/vtx_common.c create mode 100644 src/main/drivers/vtx_common.h create mode 100644 src/main/drivers/vtx_var.c create mode 100644 src/main/drivers/vtx_var.h create mode 100644 src/main/io/vtx_singularity.c create mode 100644 src/main/io/vtx_singularity.h diff --git a/Makefile b/Makefile index 1c8fa50377..af64ce9457 100644 --- a/Makefile +++ b/Makefile @@ -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 \ @@ -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 \ diff --git a/src/main/drivers/vtx_common.c b/src/main/drivers/vtx_common.c new file mode 100644 index 0000000000..1f924ed691 --- /dev/null +++ b/src/main/drivers/vtx_common.c @@ -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 . + */ + +/* Created by jflyper */ + +#include +#include +#include +#include + +#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 diff --git a/src/main/drivers/vtx_common.h b/src/main/drivers/vtx_common.h new file mode 100644 index 0000000000..17aa292d08 --- /dev/null +++ b/src/main/drivers/vtx_common.h @@ -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 . + */ + +/* 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); diff --git a/src/main/drivers/vtx_var.c b/src/main/drivers/vtx_var.c new file mode 100644 index 0000000000..908d4284a6 --- /dev/null +++ b/src/main/drivers/vtx_var.c @@ -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 . + */ + +/* Created by jflyper */ + +#include +#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", +}; diff --git a/src/main/drivers/vtx_var.h b/src/main/drivers/vtx_var.h new file mode 100644 index 0000000000..eac1b84a3d --- /dev/null +++ b/src/main/drivers/vtx_var.h @@ -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 . + */ + +/* Created by jflyper */ + +#pragma once + +#include + +extern const uint16_t vtx58FreqTable[5][8]; +extern const char * const vtx58BandNames[]; +extern const char * const vtx58ChanNames[]; +extern const char vtx58BandLetter[]; diff --git a/src/main/io/vtx_singularity.c b/src/main/io/vtx_singularity.c new file mode 100644 index 0000000000..1831a6c816 --- /dev/null +++ b/src/main/io/vtx_singularity.c @@ -0,0 +1,110 @@ +/* + * 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 . + */ + + +// Get target build configuration +#include "platform.h" + +#ifdef VTX_SINGULARITY + +// Own interfaces +#include "io/vtx_singularity.h" +#include "io/osd.h" + +//External dependencies +#include "config/config_master.h" +#include "config/config_eeprom.h" +#include "drivers/vtx_rtc6705.h" +#include "fc/runtime_config.h" +#include "io/beeper.h" + + +static uint8_t locked = 0; + +void vtxSingularityInit(void) +{ + rtc6705Init(); + if (masterConfig.vtx_mode == 0) { + rtc6705SetChannel(masterConfig.vtx_band, masterConfig.vtx_channel); + } else if (masterConfig.vtx_mode == 1) { + rtc6705SetFreq(masterConfig.vtx_mhz); + } +} + +static void setChannelSaveAndNotify(uint8_t *bandOrChannel, uint8_t step, int32_t min, int32_t max) +{ + if (ARMING_FLAG(ARMED)) { + locked = 1; + } + + if (masterConfig.vtx_mode == 0 && !locked) { + uint8_t temp = (*bandOrChannel) + step; + temp = constrain(temp, min, max); + *bandOrChannel = temp; + + rtc6705SetChannel(masterConfig.vtx_band, masterConfig.vtx_channel); + writeEEPROM(); + readEEPROM(); + beeperConfirmationBeeps(temp); + } +} + +void vtxSingularityIncrementBand(void) +{ + setChannelSaveAndNotify(&(masterConfig.vtx_band), 1, RTC6705_BAND_MIN, RTC6705_BAND_MAX); +} + +void vtxSingularityDecrementBand(void) +{ + setChannelSaveAndNotify(&(masterConfig.vtx_band), -1, RTC6705_BAND_MIN, RTC6705_BAND_MAX); +} + +void vtxSingularityIncrementChannel(void) +{ + setChannelSaveAndNotify(&(masterConfig.vtx_channel), 1, RTC6705_CHANNEL_MIN, RTC6705_CHANNEL_MAX); +} + +void vtxSingularityDecrementChannel(void) +{ + setChannelSaveAndNotify(&(masterConfig.vtx_channel), -1, RTC6705_CHANNEL_MIN, RTC6705_CHANNEL_MAX); +} + +void vtxSingularityUpdateActivatedChannel(void) +{ + if (ARMING_FLAG(ARMED)) { + locked = 1; + } + + if (masterConfig.vtx_mode == 2 && !locked) { + static uint8_t lastIndex = -1; + uint8_t index; + + for (index = 0; index < MAX_CHANNEL_ACTIVATION_CONDITION_COUNT; index++) { + vtxChannelActivationCondition_t *vtxChannelActivationCondition = &masterConfig.vtxChannelActivationConditions[index]; + + if (isRangeActive(vtxChannelActivationCondition->auxChannelIndex, &vtxChannelActivationCondition->range) + && index != lastIndex) { + lastIndex = index; + rtc6705SetChannel(vtxChannelActivationCondition->band, vtxChannelActivationCondition->channel); + break; + } + } + } +} + +#endif + diff --git a/src/main/io/vtx_singularity.h b/src/main/io/vtx_singularity.h new file mode 100644 index 0000000000..a823aacb82 --- /dev/null +++ b/src/main/io/vtx_singularity.h @@ -0,0 +1,41 @@ +/* + * 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 . + */ + +#pragma once + +#include "fc/rc_controls.h" + +#define VTX_BAND_MIN 1 +#define VTX_BAND_MAX 5 +#define VTX_CHANNEL_MIN 1 +#define VTX_CHANNEL_MAX 8 +#define MAX_CHANNEL_ACTIVATION_CONDITION_COUNT 10 + +typedef struct vtxChannelActivationCondition_s { + uint8_t auxChannelIndex; + uint8_t band; + uint8_t channel; + channelRange_t range; +} vtxChannelActivationCondition_t; + +void vtxSingularityInit(void); +void vtxSingularityIncrementBand(void); +void vtxSingularityDecrementBand(void); +void vtxSingularityIncrementChannel(void); +void vtxSingularityDecrementChannel(void); +void vtxSingularityUpdateActivatedChannel(void); + diff --git a/src/main/io/vtx_smartaudio.c b/src/main/io/vtx_smartaudio.c index 2fbe947435..d5d6abfb8b 100644 --- a/src/main/io/vtx_smartaudio.c +++ b/src/main/io/vtx_smartaudio.c @@ -33,6 +33,8 @@ #include "common/utils.h" #include "drivers/system.h" #include "drivers/serial.h" +#include "drivers/vtx_var.h" +#include "drivers/vtx_common.h" #include "io/serial.h" #include "io/vtx_smartaudio.h" @@ -69,6 +71,25 @@ static void saUpdateStatusString(void); // Forward static serialPort_t *smartAudioSerialPort = NULL; +#if defined(CMS) || defined(VTX_COMMON) +static const char * const saPowerNames[] = { + "---", "25 ", "200", "500", "800", +}; +#endif + +#ifdef VTX_COMMON +static vtxVTable_t saVTable; // Forward +static vtxDevice_t vtxSmartAudio = { + .vTable = &saVTable, + .numBand = 5, + .numChan = 8, + .numPower = 4, + .bandNames = (char **)vtx58BandNames, + .chanNames = (char **)vtx58ChanNames, + .powerNames = (char **)saPowerNames, +}; +#endif + // SmartAudio command and response codes enum { SA_CMD_NONE = 0x00, @@ -378,6 +399,10 @@ static void saProcessResponse(uint8_t *buf, int len) saPrintSettings(); saDevicePrev = saDevice; +#ifdef VTX_COMMON + // Todo: Update states in saVtxDevice? +#endif + #ifdef CMS // Export current device status for CMS saCmsUpdate(); @@ -672,6 +697,9 @@ bool smartAudioInit() return false; } + vtxSmartAudio.vTable = &saVTable; + vtxCommonRegisterDevice(&vtxSmartAudio); + return true; } @@ -716,6 +744,98 @@ void smartAudioProcess(uint32_t now) } } +#ifdef VTX_COMMON +// Interface to common VTX API + +vtxDevType_e vtxSAGetDeviceType(void) +{ + return VTXDEV_SMARTAUDIO; +} + +bool vtxSAIsReady(void) +{ + return !(saDevice.version == 0); +} + +void vtxSASetBandChan(uint8_t band, uint8_t chan) +{ + if (band && chan) + saSetBandChan(band - 1, chan - 1); +} + +void vtxSASetPowerByIndex(uint8_t index) +{ + if (index == 0) { + // SmartAudio doesn't support power off. + return; + } + + saSetPowerByIndex(index - 1); +} + +void vtxSASetPitmode(uint8_t onoff) +{ + if (!(vtxSAIsReady() && (saDevice.version == 2))) + return; + + if (onoff) { + // SmartAudio can not turn pit mode on by software. + return; + } + + uint8_t newmode = SA_MODE_CLR_PITMODE; + + if (saDevice.mode & SA_MODE_GET_IN_RANGE_PITMODE) + newmode |= SA_MODE_SET_IN_RANGE_PITMODE; + + if (saDevice.mode & SA_MODE_GET_OUT_RANGE_PITMODE) + newmode |= SA_MODE_SET_OUT_RANGE_PITMODE; + + saSetMode(newmode); + + return true; +} + +bool vtxSAGetBandChan(uint8_t *pBand, uint8_t *pChan) +{ + if (!vtxSAIsReady()) + return false; + + *pBand = (saDevice.chan / 8) + 1; + *pChan = (saDevice.chan % 8) + 1; + return true; +} + +bool vtxSAGetPowerIndex(uint8_t *pIndex) +{ + if (!vtxSAIsReady()) + return false; + + *pIndex = (saDevice.version == 1) ? saDacToPowerIndex(saDevice.power) : saDevice.power; + return true; +} + +bool vtxSAGetPitmode(uint8_t *pOnoff) +{ + if (!(vtxSAIsReady() && (saDevice.version == 2))) + return false; + + *pOnoff = (saDevice.mode & SA_MODE_GET_PITMODE) ? 1 : 0; + return true; +} + +static vtxVTable_t saVTable = { + .getDeviceType = vtxSAGetDeviceType, + .isReady = vtxSAIsReady, + .setBandChan = vtxSASetBandChan, + .setPowerByIndex = vtxSASetPowerByIndex, + .setPitmode = vtxSASetPitmode, + .getBandChan = vtxSAGetBandChan, + .getPowerIndex = vtxSAGetPowerIndex, + .getPitmode = vtxSAGetPitmode, +}; +#endif // VTX_COMMON + #ifdef CMS // Interface to CMS @@ -994,32 +1114,11 @@ static CMS_Menu saCmsMenuStats = { .entries = saCmsMenuStatsEntries }; -static const char * const saCmsBandNames[] = { - "--------", - "BOSCAM A", - "BOSCAM B", - "BOSCAM E", - "FATSHARK", - "RACEBAND", -}; +static OSD_TAB_t saCmsEntBand = { &saCmsBand, 5, vtx58BandNames, NULL }; -static OSD_TAB_t saCmsEntBand = { &saCmsBand, 5, &saCmsBandNames[0], NULL }; +static OSD_TAB_t saCmsEntChan = { &saCmsChan, 8, vtx58ChanNames, NULL }; -static const char * const saCmsChanNames[] = { - "-", "1", "2", "3", "4", "5", "6", "7", "8", -}; - -static OSD_TAB_t saCmsEntChan = { &saCmsChan, 8, &saCmsChanNames[0], NULL }; - -static const char * const saCmsPowerNames[] = { - "---", - "25 ", - "200", - "500", - "800", -}; - -static OSD_TAB_t saCmsEntPower = { &saCmsPower, 4, saCmsPowerNames}; +static OSD_TAB_t saCmsEntPower = { &saCmsPower, 4, saPowerNames}; static OSD_UINT16_t saCmsEntFreqRef = { &saCmsFreqRef, 5600, 5900, 0 }; diff --git a/src/main/target/common.h b/src/main/target/common.h index f71963ee9b..13a4220343 100644 --- a/src/main/target/common.h +++ b/src/main/target/common.h @@ -103,6 +103,7 @@ #define TELEMETRY_IBUS #define USE_RX_MSP #define USE_SERIALRX_JETIEXBUS +#define VTX_COMMON #define VTX_CONTROL #define VTX_SMARTAUDIO #define USE_SENSOR_NAMES From 12e265c00630854343804bbcc32b93d841b7a588 Mon Sep 17 00:00:00 2001 From: jflyper Date: Sun, 15 Jan 2017 01:54:26 +0900 Subject: [PATCH 2/6] vtx58_Freq2Bandchan to use vtxDevice_t parameters --- src/main/drivers/vtx_common.c | 8 ++++---- src/main/drivers/vtx_common.h | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/main/drivers/vtx_common.c b/src/main/drivers/vtx_common.c index 1f924ed691..763918c9af 100644 --- a/src/main/drivers/vtx_common.c +++ b/src/main/drivers/vtx_common.c @@ -116,14 +116,14 @@ bool vtxCommonGetPitmode(uint8_t *pOnoff) // Utilities -bool vtx58_Freq2Bandchan(uint16_t freq, uint8_t *pBand, uint8_t *pChan) +bool vtx58_Freq2Bandchan(vtxDevice_t *pVtxDev, 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) { + for (band = 0 ; band < pVtxDev->numBand ; band++) { + for (chan = 0 ; chan < pVtxDev->numChan ; chan++) { + if (pVtxDev->freqTable[band * pVtxDev->numChan + chan] == freq) { *pBand = band + 1; *pChan = chan + 1; return true; diff --git a/src/main/drivers/vtx_common.h b/src/main/drivers/vtx_common.h index 17aa292d08..685f02bb5f 100644 --- a/src/main/drivers/vtx_common.h +++ b/src/main/drivers/vtx_common.h @@ -79,4 +79,4 @@ 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); +bool vtx58_Freq2Bandchan(vtxDevice_t *pVtxDev, uint16_t freq, uint8_t *pBand, uint8_t *pChan); From b9430f82b74d88d99cac069a2744946d4a50e128 Mon Sep 17 00:00:00 2001 From: jflyper Date: Sun, 15 Jan 2017 02:14:39 +0900 Subject: [PATCH 3/6] Call vtxCommonProcess instead of device dependent periodic service function --- src/main/drivers/vtx_common.c | 9 +++++++++ src/main/drivers/vtx_common.h | 4 ++++ src/main/fc/fc_tasks.c | 6 +++--- src/main/io/vtx_smartaudio.c | 3 ++- 4 files changed, 18 insertions(+), 4 deletions(-) diff --git a/src/main/drivers/vtx_common.c b/src/main/drivers/vtx_common.c index 763918c9af..2fa683a5b0 100644 --- a/src/main/drivers/vtx_common.c +++ b/src/main/drivers/vtx_common.c @@ -43,6 +43,15 @@ void vtxCommonRegisterDevice(vtxDevice_t *pDevice) vtxDevice = pDevice; } +void vtxCommonProcess(uint32_t currentTimeUs) +{ + if (!vtxDevice) + return; + + if (vtxDevice->vTable->process) + vtxDevice->vTable->process(currentTimeUs); +} + vtxDevType_e vtxCommonGetDeviceType(void) { if (!vtxDevice) diff --git a/src/main/drivers/vtx_common.h b/src/main/drivers/vtx_common.h index 685f02bb5f..5c45bbc3e4 100644 --- a/src/main/drivers/vtx_common.h +++ b/src/main/drivers/vtx_common.h @@ -51,6 +51,7 @@ typedef struct vtxDevice_s { // {set,get}Pitmode: 0 = OFF, 1 = ON typedef struct vtxVTable_s { + void (*process)(uint32_t currentTimeUs); vtxDevType_e (*getDeviceType)(void); bool (*isReady)(void); @@ -71,6 +72,8 @@ typedef struct vtxVTable_s { void vtxCommonInit(void); void vtxCommonRegisterDevice(vtxDevice_t *pDevice); +// VTable functions +void vtxCommonProcess(uint32_t currentTimeUs); uint8_t vtxCommonGetDeviceType(void); void vtxCommonSetBandChan(uint8_t band, uint8_t chan); void vtxCommonSetPowerByIndex(uint8_t level); @@ -79,4 +82,5 @@ bool vtxCommonGetBandChan(uint8_t *pBand, uint8_t *pChan); bool vtxCommonGetPowerIndex(uint8_t *pIndex); bool vtxCommonGetPitmode(uint8_t *pOnoff); +// Utilities bool vtx58_Freq2Bandchan(vtxDevice_t *pVtxDev, uint16_t freq, uint8_t *pBand, uint8_t *pChan); diff --git a/src/main/fc/fc_tasks.c b/src/main/fc/fc_tasks.c index 6442870268..a13b5114d0 100644 --- a/src/main/fc/fc_tasks.c +++ b/src/main/fc/fc_tasks.c @@ -32,6 +32,7 @@ #include "drivers/compass.h" #include "drivers/serial.h" #include "drivers/stack_check.h" +#include "drivers/vtx_common.h" #include "fc/config.h" #include "fc/fc_msp.h" @@ -52,7 +53,6 @@ #include "io/osd.h" #include "io/serial.h" #include "io/transponder_ir.h" -#include "io/vtx_smartaudio.h" #include "msp/msp_serial.h" @@ -212,8 +212,8 @@ void taskVtxControl(uint32_t currentTime) if (ARMING_FLAG(ARMED)) return; -#ifdef VTX_SMARTAUDIO - smartAudioProcess(currentTime); +#ifdef VTX_COMMON + vtxCommonProcess(currentTime); #endif } #endif diff --git a/src/main/io/vtx_smartaudio.c b/src/main/io/vtx_smartaudio.c index d5d6abfb8b..b9ebad2bf5 100644 --- a/src/main/io/vtx_smartaudio.c +++ b/src/main/io/vtx_smartaudio.c @@ -703,7 +703,7 @@ bool smartAudioInit() return true; } -void smartAudioProcess(uint32_t now) +void vtxSAProcess(uint32_t now) { static bool initialSent = false; @@ -825,6 +825,7 @@ bool vtxSAGetPitmode(uint8_t *pOnoff) } static vtxVTable_t saVTable = { + .process = vtxSAProcess, .getDeviceType = vtxSAGetDeviceType, .isReady = vtxSAIsReady, .setBandChan = vtxSASetBandChan, From b1e1c7ef7bfb14442360b319ac7b6c2928dc47fe Mon Sep 17 00:00:00 2001 From: jflyper Date: Sun, 15 Jan 2017 16:06:38 +0900 Subject: [PATCH 4/6] Some changes to "When" changes take effect. --- src/main/io/vtx_smartaudio.c | 42 ++++++++++++++++++++++++++++++------ 1 file changed, 35 insertions(+), 7 deletions(-) diff --git a/src/main/io/vtx_smartaudio.c b/src/main/io/vtx_smartaudio.c index b9ebad2bf5..e756c38090 100644 --- a/src/main/io/vtx_smartaudio.c +++ b/src/main/io/vtx_smartaudio.c @@ -742,6 +742,25 @@ void vtxSAProcess(uint32_t now) saGetSettings(); saSendQueue(); } + +#ifdef SMARTAUDIO_TEST_VTX_COMMON + // Testing VTX_COMMON API + { + static uint32_t lastMonitorUs = 0; + if (cmp32(now, lastMonitorUs) < 5 * 1000 * 1000) + return; + + static uint8_t monBand; + static uint8_t monChan; + static uint8_t monPower; + + vtxCommonGetBandChan(&monBand, &monChan); + vtxCommonGetPowerIndex(&monPower); + debug[0] = monBand; + debug[1] = monChan; + debug[2] = monPower; + } +#endif } #ifdef VTX_COMMON @@ -811,7 +830,7 @@ bool vtxSAGetPowerIndex(uint8_t *pIndex) if (!vtxSAIsReady()) return false; - *pIndex = (saDevice.version == 1) ? saDacToPowerIndex(saDevice.power) : saDevice.power; + *pIndex = ((saDevice.version == 1) ? saDacToPowerIndex(saDevice.power) : saDevice.power) + 1; return true; } @@ -938,7 +957,7 @@ if (saDevice.mode & SA_MODE_GET_OUT_RANGE_PITMODE) else saCmsPitFMode = 0; - saCmsStatusString[0] = "-FP"[(saDevice.mode & SA_MODE_GET_PITMODE) ? SACMS_OPMODEL_RACE : SACMS_OPMODEL_FREE]; + saCmsStatusString[0] = "-FR"[saCmsOpmodel]; saCmsStatusString[2] = "ABEFR"[saDevice.chan / 8]; saCmsStatusString[3] = '1' + (saDevice.chan % 8); @@ -978,15 +997,13 @@ static long saCmsConfigBandByGvar(displayPort_t *pDisp, const void *self) return 0; } -dprintf(("saCmsConfigBand: band req %d ", saCmsBand)); - if (saCmsBand == 0) { // Bouce back, no going back to undef state saCmsBand = 1; return 0; } - if (!(saCmsOpmodel == SACMS_OPMODEL_FREE && saDeferred)) + if ((saCmsOpmodel == SACMS_OPMODEL_FREE) && !saDeferred) saSetBandChan(saCmsBand - 1, saCmsChan - 1); saCmsFreqRef = saFreqTable[saCmsBand - 1][saCmsChan - 1]; @@ -1011,7 +1028,7 @@ static long saCmsConfigChanByGvar(displayPort_t *pDisp, const void *self) return 0; } - if (!(saCmsOpmodel == SACMS_OPMODEL_FREE && saDeferred)) + if ((saCmsOpmodel == SACMS_OPMODEL_FREE) && !saDeferred) saSetBandChan(saCmsBand - 1, saCmsChan - 1); saCmsFreqRef = saFreqTable[saCmsBand - 1][saCmsChan - 1]; @@ -1036,7 +1053,8 @@ static long saCmsConfigPowerByGvar(displayPort_t *pDisp, const void *self) return 0; } - saSetPowerByIndex(saCmsPower - 1); + if (saCmsOpmodel == SACMS_OPMODEL_FREE) + saSetPowerByIndex(saCmsPower - 1); return 0; } @@ -1172,11 +1190,21 @@ static long saCmsCommence(displayPort_t *pDisp, const void *self) UNUSED(self); if (saCmsOpmodel == SACMS_OPMODEL_RACE) { + // Race model + // Setup band, freq and power. + + saSetBandChan(saCmsBand - 1, saCmsChan - 1); + saSetPowerByIndex(saCmsPower - 1); + + // If in pit mode, cancel it. + if (saCmsPitFMode == 0) saSetMode(SA_MODE_CLR_PITMODE|SA_MODE_SET_IN_RANGE_PITMODE); else saSetMode(SA_MODE_CLR_PITMODE|SA_MODE_SET_OUT_RANGE_PITMODE); } else { + // Freestyle model + // Setup band and freq / user freq if (saCmsFselMode == 0) saSetBandChan(saCmsBand - 1, saCmsChan - 1); else From f56513810163490fc86c888ad67ce4eb78607916 Mon Sep 17 00:00:00 2001 From: jflyper Date: Mon, 16 Jan 2017 22:59:33 +0900 Subject: [PATCH 5/6] Touch-ups --- Makefile | 4 +- src/main/drivers/vtx_common.c | 25 ------------ src/main/drivers/vtx_common.h | 3 -- src/main/drivers/vtx_rtc6705.c | 2 + src/main/drivers/vtx_var.c | 45 ---------------------- src/main/drivers/vtx_var.h | 27 ------------- src/main/fc/fc_tasks.c | 2 + src/main/io/vtx_smartaudio.c | 7 ++-- src/main/io/{vtx_common.c => vtx_string.c} | 2 +- src/main/io/{vtx_common.h => vtx_string.h} | 2 +- src/main/io/vtx_tramp.c | 2 +- src/main/target/common_post.h | 3 ++ 12 files changed, 14 insertions(+), 110 deletions(-) delete mode 100644 src/main/drivers/vtx_var.c delete mode 100644 src/main/drivers/vtx_var.h rename src/main/io/{vtx_common.c => vtx_string.c} (98%) rename src/main/io/{vtx_common.h => vtx_string.h} (92%) diff --git a/Makefile b/Makefile index fc5be64c68..129b4e0c94 100644 --- a/Makefile +++ b/Makefile @@ -591,7 +591,6 @@ 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 \ @@ -614,7 +613,7 @@ HIGHEND_SRC = \ telemetry/mavlink.c \ telemetry/ibus.c \ sensors/esc_sensor.c \ - io/vtx_common.c \ + io/vtx_string.c \ io/vtx_smartaudio.c \ io/vtx_tramp.c @@ -708,7 +707,6 @@ 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 \ diff --git a/src/main/drivers/vtx_common.c b/src/main/drivers/vtx_common.c index 2fa683a5b0..7e617b03ac 100644 --- a/src/main/drivers/vtx_common.c +++ b/src/main/drivers/vtx_common.c @@ -28,7 +28,6 @@ #if defined(VTX_COMMON) #include "vtx_common.h" -#include "vtx_var.h" vtxDevice_t *vtxDevice = NULL; @@ -122,28 +121,4 @@ bool vtxCommonGetPitmode(uint8_t *pOnoff) else return false; } - -// Utilities - -bool vtx58_Freq2Bandchan(vtxDevice_t *pVtxDev, uint16_t freq, uint8_t *pBand, uint8_t *pChan) -{ - uint8_t band; - uint8_t chan; - - for (band = 0 ; band < pVtxDev->numBand ; band++) { - for (chan = 0 ; chan < pVtxDev->numChan ; chan++) { - if (pVtxDev->freqTable[band * pVtxDev->numChan + chan] == freq) { - *pBand = band + 1; - *pChan = chan + 1; - return true; - } - } - } - - *pBand = 0; - *pChan = 0; - - return false; -} - #endif diff --git a/src/main/drivers/vtx_common.h b/src/main/drivers/vtx_common.h index 5c45bbc3e4..c7c521c666 100644 --- a/src/main/drivers/vtx_common.h +++ b/src/main/drivers/vtx_common.h @@ -81,6 +81,3 @@ void vtxCommonSetPitmode(uint8_t onoff); bool vtxCommonGetBandChan(uint8_t *pBand, uint8_t *pChan); bool vtxCommonGetPowerIndex(uint8_t *pIndex); bool vtxCommonGetPitmode(uint8_t *pOnoff); - -// Utilities -bool vtx58_Freq2Bandchan(vtxDevice_t *pVtxDev, uint16_t freq, uint8_t *pBand, uint8_t *pChan); diff --git a/src/main/drivers/vtx_rtc6705.c b/src/main/drivers/vtx_rtc6705.c index 38915a8750..ea9403b87e 100644 --- a/src/main/drivers/vtx_rtc6705.c +++ b/src/main/drivers/vtx_rtc6705.c @@ -87,7 +87,9 @@ #define DISABLE_RTC6705 GPIO_SetBits(RTC6705_CS_GPIO, RTC6705_CS_PIN) #define ENABLE_RTC6705 GPIO_ResetBits(RTC6705_CS_GPIO, RTC6705_CS_PIN) +#if defined(SPRACINGF3NEO) static IO_t vtxPowerPin = IO_NONE; +#endif #define ENABLE_VTX_POWER IOLo(vtxPowerPin) #define DISABLE_VTX_POWER IOHi(vtxPowerPin) diff --git a/src/main/drivers/vtx_var.c b/src/main/drivers/vtx_var.c deleted file mode 100644 index 908d4284a6..0000000000 --- a/src/main/drivers/vtx_var.c +++ /dev/null @@ -1,45 +0,0 @@ -/* - * 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 . - */ - -/* Created by jflyper */ - -#include -#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", -}; diff --git a/src/main/drivers/vtx_var.h b/src/main/drivers/vtx_var.h deleted file mode 100644 index eac1b84a3d..0000000000 --- a/src/main/drivers/vtx_var.h +++ /dev/null @@ -1,27 +0,0 @@ -/* - * 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 . - */ - -/* Created by jflyper */ - -#pragma once - -#include - -extern const uint16_t vtx58FreqTable[5][8]; -extern const char * const vtx58BandNames[]; -extern const char * const vtx58ChanNames[]; -extern const char vtx58BandLetter[]; diff --git a/src/main/fc/fc_tasks.c b/src/main/fc/fc_tasks.c index 2210ca66aa..2a4d25c5f1 100644 --- a/src/main/fc/fc_tasks.c +++ b/src/main/fc/fc_tasks.c @@ -53,6 +53,7 @@ #include "io/osd.h" #include "io/serial.h" #include "io/transponder_ir.h" +#include "io/vtx_tramp.h" // Will be gone #include "msp/msp_serial.h" @@ -215,6 +216,7 @@ void taskVtxControl(uint32_t currentTime) #ifdef VTX_COMMON vtxCommonProcess(currentTime); #endif +// Call to trampProcess() will be gone #ifdef VTX_TRAMP trampProcess(currentTime); #endif diff --git a/src/main/io/vtx_smartaudio.c b/src/main/io/vtx_smartaudio.c index fa198b0e59..2c465462bc 100644 --- a/src/main/io/vtx_smartaudio.c +++ b/src/main/io/vtx_smartaudio.c @@ -33,11 +33,10 @@ #include "common/utils.h" #include "drivers/system.h" #include "drivers/serial.h" -#include "drivers/vtx_var.h" #include "drivers/vtx_common.h" #include "io/serial.h" #include "io/vtx_smartaudio.h" -#include "io/vtx_common.h" +#include "io/vtx_string.h" #include "fc/rc_controls.h" #include "fc/runtime_config.h" @@ -86,7 +85,7 @@ static vtxDevice_t vtxSmartAudio = { .numChan = 8, .numPower = 4, .bandNames = (char **)vtx58BandNames, - .chanNames = (char **)vtx58ChanNames, + .chanNames = (char **)vtx58ChannelNames, .powerNames = (char **)saPowerNames, }; #endif @@ -1124,7 +1123,7 @@ static CMS_Menu saCmsMenuStats = { static OSD_TAB_t saCmsEntBand = { &saCmsBand, 5, vtx58BandNames, NULL }; -static OSD_TAB_t saCmsEntChan = { &saCmsChan, 8, vtx58ChanNames, NULL }; +static OSD_TAB_t saCmsEntChan = { &saCmsChan, 8, vtx58ChannelNames, NULL }; static const char * const saCmsPowerNames[] = { "---", diff --git a/src/main/io/vtx_common.c b/src/main/io/vtx_string.c similarity index 98% rename from src/main/io/vtx_common.c rename to src/main/io/vtx_string.c index 5f89be8dd3..318c82b2e2 100644 --- a/src/main/io/vtx_common.c +++ b/src/main/io/vtx_string.c @@ -25,7 +25,7 @@ #include "platform.h" #include "build/debug.h" -#if defined(VTX_CONTROL) +#if defined(VTX_COMMON) const uint16_t vtx58FreqTable[5][8] = { diff --git a/src/main/io/vtx_common.h b/src/main/io/vtx_string.h similarity index 92% rename from src/main/io/vtx_common.h rename to src/main/io/vtx_string.h index cd593048b6..bbe8f4b0da 100644 --- a/src/main/io/vtx_common.h +++ b/src/main/io/vtx_string.h @@ -2,7 +2,7 @@ #include -#if defined(VTX_CONTROL) +#if defined(VTX_COMMON) extern const uint16_t vtx58FreqTable[5][8]; extern const char * const vtx58BandNames[]; diff --git a/src/main/io/vtx_tramp.c b/src/main/io/vtx_tramp.c index 4e4716836a..343a215f45 100644 --- a/src/main/io/vtx_tramp.c +++ b/src/main/io/vtx_tramp.c @@ -35,7 +35,7 @@ #include "drivers/serial.h" #include "drivers/system.h" #include "io/vtx_tramp.h" -#include "io/vtx_common.h" +#include "io/vtx_string.h" static serialPort_t *trampSerialPort = NULL; diff --git a/src/main/target/common_post.h b/src/main/target/common_post.h index d881516c14..7a089ebff7 100644 --- a/src/main/target/common_post.h +++ b/src/main/target/common_post.h @@ -22,4 +22,7 @@ // Targets with built-in vtx do not need external vtx #if defined(VTX) || defined(USE_RTC6705) # undef VTX_CONTROL +# undef VTX_COMMON +# undef VTX_SMARTAUDIO +# undef VTX_TRAMP #endif From 2e0e4562bee1e5f8229143c0a7a9ff83a803dd00 Mon Sep 17 00:00:00 2001 From: jflyper Date: Tue, 17 Jan 2017 00:14:09 +0900 Subject: [PATCH 6/6] Fix power table lookup issue --- src/main/io/vtx_smartaudio.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/io/vtx_smartaudio.c b/src/main/io/vtx_smartaudio.c index 2c465462bc..29811bbfcc 100644 --- a/src/main/io/vtx_smartaudio.c +++ b/src/main/io/vtx_smartaudio.c @@ -248,11 +248,11 @@ static int saDacToPowerIndex(int dac) { int idx; - for (idx = 0 ; idx < 4 ; idx++) { + for (idx = 3 ; idx >= 0 ; idx--) { if (saPowerTable[idx].valueV1 <= dac) return(idx); } - return(3); + return(0); } //