Merge branch 'master' into abo_waypoint_tracking
[inav.git] / src / main / drivers / vtx_common.c
blob6f4cd52373f9e9b8210a5838f21ea888c20ae12c
1 /*
2 * This file is part of Cleanflight.
4 * Cleanflight is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
9 * Cleanflight is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with Cleanflight. If not, see <http://www.gnu.org/licenses/>.
18 /* Created by jflyper */
20 #include <stdbool.h>
21 #include <stdint.h>
22 #include <ctype.h>
23 #include <string.h>
25 #include "platform.h"
26 #include "build/debug.h"
28 #include "vtx_common.h"
30 static vtxDevice_t *commonVtxDevice = NULL;
32 void vtxCommonInit(void)
36 void vtxCommonSetDevice(vtxDevice_t *vtxDevice)
38 commonVtxDevice = vtxDevice;
41 vtxDevice_t *vtxCommonDevice(void)
43 return commonVtxDevice;
46 void vtxCommonProcess(vtxDevice_t *vtxDevice, timeUs_t currentTimeUs)
48 if (vtxDevice && vtxDevice->vTable->process) {
49 vtxDevice->vTable->process(vtxDevice, currentTimeUs);
53 vtxDevType_e vtxCommonGetDeviceType(vtxDevice_t *vtxDevice)
55 if (!vtxDevice || !vtxDevice->vTable->getDeviceType) {
56 return VTXDEV_UNKNOWN;
59 return vtxDevice->vTable->getDeviceType(vtxDevice);
62 bool vtxCommonDeviceIsReady(vtxDevice_t *vtxDevice)
64 if (vtxDevice && vtxDevice->vTable->isReady) {
65 return vtxDevice->vTable->isReady(vtxDevice);
67 return false;
70 // band and channel are 1 origin
71 void vtxCommonSetBandAndChannel(vtxDevice_t *vtxDevice, uint8_t band, uint8_t channel)
73 if (!vtxDevice)
74 return;
76 if ((band > vtxDevice->capability.bandCount) || (channel > vtxDevice->capability.channelCount))
77 return;
79 if (vtxDevice->vTable->setBandAndChannel) {
80 vtxDevice->vTable->setBandAndChannel(vtxDevice, band, channel);
84 // index is zero origin, zero = power off completely
85 void vtxCommonSetPowerByIndex(vtxDevice_t *vtxDevice, uint8_t index)
87 if (!vtxDevice)
88 return;
90 if (index > vtxDevice->capability.powerCount)
91 return;
93 if (vtxDevice->vTable->setPowerByIndex) {
94 vtxDevice->vTable->setPowerByIndex(vtxDevice, index);
98 // on = 1, off = 0
99 void vtxCommonSetPitMode(vtxDevice_t *vtxDevice, uint8_t onoff)
101 if (vtxDevice && vtxDevice->vTable->setPitMode) {
102 vtxDevice->vTable->setPitMode(vtxDevice, onoff);
106 bool vtxCommonGetBandAndChannel(vtxDevice_t *vtxDevice, uint8_t *pBand, uint8_t *pChannel)
108 if (vtxDevice && vtxDevice->vTable->getBandAndChannel) {
109 return vtxDevice->vTable->getBandAndChannel(vtxDevice, pBand, pChannel);
111 return false;
114 bool vtxCommonGetPowerIndex(vtxDevice_t *vtxDevice, uint8_t *pIndex)
116 if (vtxDevice && vtxDevice->vTable->getPowerIndex) {
117 return vtxDevice->vTable->getPowerIndex(vtxDevice, pIndex);
119 return false;
122 bool vtxCommonGetPitMode(vtxDevice_t *vtxDevice, uint8_t *pOnOff)
124 if (vtxDevice && vtxDevice->vTable->getPitMode) {
125 return vtxDevice->vTable->getPitMode(vtxDevice, pOnOff);
127 return false;
130 bool vtxCommonGetFrequency(const vtxDevice_t *vtxDevice, uint16_t *pFrequency)
132 if (vtxDevice && vtxDevice->vTable->getFrequency) {
133 return vtxDevice->vTable->getFrequency(vtxDevice, pFrequency);
135 return false;
138 bool vtxCommonGetDeviceCapability(vtxDevice_t *vtxDevice, vtxDeviceCapability_t *pDeviceCapability)
140 if (vtxDevice) {
141 memcpy(pDeviceCapability, &vtxDevice->capability, sizeof(vtxDeviceCapability_t));
142 return true;
144 return false;
147 bool vtxCommonGetPower(const vtxDevice_t *vtxDevice, uint8_t *pIndex, uint16_t *pPowerMw)
149 if (vtxDevice && vtxDevice->vTable->getPower) {
150 return vtxDevice->vTable->getPower(vtxDevice, pIndex, pPowerMw);
152 return false;
155 bool vtxCommonGetOsdInfo(vtxDevice_t *vtxDevice, vtxDeviceOsdInfo_t * pOsdInfo)
157 bool ret = false;
159 if (vtxDevice && vtxDevice->vTable->getOsdInfo) {
160 ret = vtxDevice->vTable->getOsdInfo(vtxDevice, pOsdInfo);
163 // Make sure we provide sane results even in case API fails
164 if (!ret) {
165 pOsdInfo->band = 0;
166 pOsdInfo->channel = 0;
167 pOsdInfo->frequency = 0;
168 pOsdInfo->powerIndex = 0;
169 pOsdInfo->powerMilliwatt = 0;
170 pOsdInfo->bandLetter = '-';
171 pOsdInfo->bandName = "-";
172 pOsdInfo->channelName = "-";
173 pOsdInfo->powerIndexLetter = '0';
176 return ret;