forked from iNavFlight/inav
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vtx_smartaudio.c
786 lines (629 loc) · 19 KB
/
vtx_smartaudio.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
/*
* 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 <string.h>
#include "platform.h"
#if defined(VTX_SMARTAUDIO) && defined(VTX_CONTROL)
#include "build/build_config.h"
#include "build/debug.h"
#include "cms/cms.h"
#include "cms/cms_types.h"
#include "cms/cms_menu_vtx_smartaudio.h"
#include "common/axis.h"
#include "common/printf.h"
#include "common/utils.h"
#include "config/parameter_group.h"
#include "config/parameter_group_ids.h"
#include "drivers/system.h"
#include "drivers/serial.h"
#include "drivers/time.h"
#include "drivers/vtx_common.h"
#include "fc/rc_controls.h"
#include "fc/runtime_config.h"
#include "flight/pid.h"
#include "config/config_master.h"
#include "io/serial.h"
#include "io/vtx_smartaudio.h"
#include "io/vtx_string.h"
//#define SMARTAUDIO_DEBUG_MONITOR
static serialPort_t *smartAudioSerialPort = NULL;
#if defined(USE_CMS) || defined(VTX_COMMON)
static const char * const saPowerNames[] = {
"---", "25 ", "200", "500", "800",
};
#endif
#ifdef VTX_COMMON
static const vtxVTable_t saVTable; // Forward
static vtxDevice_t vtxSmartAudio = {
.vTable = &saVTable,
.capability = {
.bandCount = 5,
.channelCount = 8,
.powerCount = 4,
},
.bandNames = (char **)vtx58BandNames,
.channelNames = (char **)vtx58ChannelNames,
.powerNames = (char **)saPowerNames,
};
#endif // VTX_COMMON
// SmartAudio command and response codes
enum {
SA_CMD_NONE = 0x00,
SA_CMD_GET_SETTINGS = 0x01,
SA_CMD_SET_POWER,
SA_CMD_SET_CHAN,
SA_CMD_SET_FREQ,
SA_CMD_SET_MODE,
SA_CMD_GET_SETTINGS_V2 = 0x09 // Response only
} smartAudioCommand_e;
// This is not a good design; can't distinguish command from response this way.
#define SACMD(cmd) (((cmd) << 1) | 1)
#define SA_IS_PITMODE(n) ((n) & SA_MODE_GET_PITMODE)
#define SA_IS_PIRMODE(n) (((n) & SA_MODE_GET_PITMODE) && ((n) & SA_MODE_GET_IN_RANGE_PITMODE))
#define SA_IS_PORMODE(n) (((n) & SA_MODE_GET_PITMODE) && ((n) & SA_MODE_GET_OUT_RANGE_PITMODE))
// Statistical counters, for user side trouble shooting.
smartAudioStat_t saStat = {
.pktsent = 0,
.pktrcvd = 0,
.badpre = 0,
.badlen = 0,
.crc = 0,
.ooopresp = 0,
.badcode = 0,
};
saPowerTable_t saPowerTable[] = {
{ 25, 7, 0 },
{ 200, 16, 1 },
{ 500, 25, 2 },
{ 800, 40, 3 },
};
// Last received device ('hard') states
smartAudioDevice_t saDevice = {
.version = 0,
.channel = -1,
.power = -1,
.mode = 0,
.freq = 0,
.orfreq = 0,
};
static smartAudioDevice_t saDevicePrev = {
.version = 0,
};
// XXX Possible compliance problem here. Need LOCK/UNLOCK menu?
static uint8_t saLockMode = SA_MODE_SET_UNLOCK; // saCms variable?
// XXX Should be configurable by user?
bool saDeferred = true; // saCms variable?
// Receive frame reassembly buffer
#define SA_MAX_RCVLEN 11
static uint8_t sa_rbuf[SA_MAX_RCVLEN+4]; // XXX delete 4 byte guard
//
// CRC8 computations
//
#define POLYGEN 0xd5
static uint8_t CRC8(const uint8_t *data, const int8_t len)
{
uint8_t crc = 0; /* start with 0 so first byte can be 'xored' in */
uint8_t currByte;
for (int i = 0 ; i < len ; i++) {
currByte = data[i];
crc ^= currByte; /* XOR-in the next input byte */
for (int i = 0; i < 8; i++) {
if ((crc & 0x80) != 0) {
crc = (uint8_t)((crc << 1) ^ POLYGEN);
} else {
crc <<= 1;
}
}
}
return crc;
}
static void saPrintSettings(void)
{
#ifdef SMARTAUDIO_DPRINTF
dprintf(("Current status: version: %d\r\n", saDevice.version));
dprintf((" mode(0x%x): fmode=%s", saDevice.mode, (saDevice.mode & 1) ? "freq" : "chan"));
dprintf((" pit=%s ", (saDevice.mode & 2) ? "on " : "off"));
dprintf((" inb=%s", (saDevice.mode & 4) ? "on " : "off"));
dprintf((" outb=%s", (saDevice.mode & 8) ? "on " : "off"));
dprintf((" lock=%s", (saDevice.mode & 16) ? "unlocked" : "locked"));
dprintf((" deferred=%s\r\n", (saDevice.mode & 32) ? "on" : "off"));
dprintf((" channel: %d ", saDevice.channel));
dprintf(("freq: %d ", saDevice.freq));
dprintf(("power: %d ", saDevice.power));
dprintf(("pitfreq: %d ", saDevice.orfreq));
dprintf(("\r\n"));
#endif
}
int saDacToPowerIndex(int dac)
{
int idx;
for (idx = 3 ; idx >= 0 ; idx--) {
if (saPowerTable[idx].valueV1 <= dac)
return(idx);
}
return(0);
}
//
// Autobauding
//
#define SMARTBAUD_MIN 4800
#define SMARTBAUD_MAX 4950
uint16_t sa_smartbaud = SMARTBAUD_MIN;
static int sa_adjdir = 1; // -1=going down, 1=going up
static int sa_baudstep = 50;
#define SMARTAUDIO_CMD_TIMEOUT 120
static void saAutobaud(void)
{
if (saStat.pktsent < 10)
// Not enough samples collected
return;
#if 0
dprintf(("autobaud: %d rcvd %d/%d (%d)\r\n",
sa_smartbaud, saStat.pktrcvd, saStat.pktsent, ((saStat.pktrcvd * 100) / saStat.pktsent)));
#endif
if (((saStat.pktrcvd * 100) / saStat.pktsent) >= 70) {
// This is okay
saStat.pktsent = 0; // Should be more moderate?
saStat.pktrcvd = 0;
return;
}
dprintf(("autobaud: adjusting\r\n"));
if ((sa_adjdir == 1) && (sa_smartbaud == SMARTBAUD_MAX)) {
sa_adjdir = -1;
dprintf(("autobaud: now going down\r\n"));
} else if ((sa_adjdir == -1 && sa_smartbaud == SMARTBAUD_MIN)) {
sa_adjdir = 1;
dprintf(("autobaud: now going up\r\n"));
}
sa_smartbaud += sa_baudstep * sa_adjdir;
dprintf(("autobaud: %d\r\n", sa_smartbaud));
smartAudioSerialPort->vTable->serialSetBaudRate(smartAudioSerialPort, sa_smartbaud);
saStat.pktsent = 0;
saStat.pktrcvd = 0;
}
// Transport level variables
static uint32_t sa_lastTransmission = 0;
static uint8_t sa_outstanding = SA_CMD_NONE; // Outstanding command
static uint8_t sa_osbuf[32]; // Outstanding comamnd frame for retransmission
static int sa_oslen; // And associate length
static void saProcessResponse(uint8_t *buf, int len)
{
uint8_t resp = buf[0];
if (resp == sa_outstanding) {
sa_outstanding = SA_CMD_NONE;
} else if ((resp == SA_CMD_GET_SETTINGS_V2) && (sa_outstanding == SA_CMD_GET_SETTINGS)) {
sa_outstanding = SA_CMD_NONE;
} else {
saStat.ooopresp++;
dprintf(("processResponse: outstanding %d got %d\r\n", sa_outstanding, resp));
}
switch(resp) {
case SA_CMD_GET_SETTINGS_V2: // Version 2 Get Settings
case SA_CMD_GET_SETTINGS: // Version 1 Get Settings
if (len < 7)
break;
saDevice.version = (buf[0] == SA_CMD_GET_SETTINGS) ? 1 : 2;
saDevice.channel = buf[2];
saDevice.power = buf[3];
saDevice.mode = buf[4];
saDevice.freq = (buf[5] << 8)|buf[6];
#ifdef SMARTAUDIO_DEBUG_MONITOR
debug[0] = saDevice.version * 100 + saDevice.mode;
debug[1] = saDevice.channel;
debug[2] = saDevice.freq;
debug[3] = saDevice.power;
#endif
break;
case SA_CMD_SET_POWER: // Set Power
break;
case SA_CMD_SET_CHAN: // Set Channel
break;
case SA_CMD_SET_FREQ: // Set Frequency
if (len < 5)
break;
uint16_t freq = (buf[2] << 8)|buf[3];
if (freq & SA_FREQ_GETPIT) {
saDevice.orfreq = freq & SA_FREQ_MASK;
dprintf(("saProcessResponse: GETPIT freq %d\r\n", saDevice.orfreq));
} else if (freq & SA_FREQ_SETPIT) {
saDevice.orfreq = freq & SA_FREQ_MASK;
dprintf(("saProcessResponse: SETPIT freq %d\r\n", saDevice.orfreq));
} else {
saDevice.freq = freq;
dprintf(("saProcessResponse: SETFREQ freq %d\r\n", freq));
}
break;
case SA_CMD_SET_MODE: // Set Mode
dprintf(("saProcessResponse: SET_MODE 0x%x\r\n", buf[2]));
break;
default:
saStat.badcode++;
return;
}
// Debug
if (memcmp(&saDevice, &saDevicePrev, sizeof(smartAudioDevice_t)))
saPrintSettings();
saDevicePrev = saDevice;
#ifdef VTX_COMMON
// Todo: Update states in saVtxDevice?
#endif
#ifdef USE_CMS
// Export current device status for CMS
saCmsUpdate();
saUpdateStatusString();
#endif
}
//
// Datalink
//
static void saReceiveFramer(uint8_t c)
{
static enum saFramerState_e {
S_WAITPRE1, // Waiting for preamble 1 (0xAA)
S_WAITPRE2, // Waiting for preamble 2 (0x55)
S_WAITRESP, // Waiting for response code
S_WAITLEN, // Waiting for length
S_DATA, // Receiving data
S_WAITCRC, // Waiting for CRC
} state = S_WAITPRE1;
static int len;
static int dlen;
switch(state) {
case S_WAITPRE1:
if (c == 0xAA) {
state = S_WAITPRE2;
} else {
state = S_WAITPRE1; // Don't need this (no change)
}
break;
case S_WAITPRE2:
if (c == 0x55) {
state = S_WAITRESP;
} else {
saStat.badpre++;
state = S_WAITPRE1;
}
break;
case S_WAITRESP:
sa_rbuf[0] = c;
state = S_WAITLEN;
break;
case S_WAITLEN:
sa_rbuf[1] = c;
len = c;
if (len > SA_MAX_RCVLEN - 2) {
saStat.badlen++;
state = S_WAITPRE1;
} else if (len == 0) {
state = S_WAITCRC;
} else {
dlen = 0;
state = S_DATA;
}
break;
case S_DATA:
// XXX Should check buffer overflow (-> saerr_overflow)
sa_rbuf[2 + dlen] = c;
if (++dlen == len) {
state = S_WAITCRC;
}
break;
case S_WAITCRC:
if (CRC8(sa_rbuf, 2 + len) == c) {
// Got a response
saProcessResponse(sa_rbuf, len + 2);
saStat.pktrcvd++;
} else if (sa_rbuf[0] & 1) {
// Command echo
// XXX There is an exceptional case (V2 response)
// XXX Should check crc in the command format?
} else {
saStat.crc++;
}
state = S_WAITPRE1;
break;
}
}
static void saSendFrame(uint8_t *buf, int len)
{
int i;
serialWrite(smartAudioSerialPort, 0x00); // Generate 1st start bit
for (i = 0 ; i < len ; i++)
serialWrite(smartAudioSerialPort, buf[i]);
serialWrite(smartAudioSerialPort, 0x00); // XXX Probably don't need this
sa_lastTransmission = millis();
saStat.pktsent++;
}
/*
* Retransmission and command queuing
*
* The transport level support includes retransmission on response timeout
* and command queueing.
*
* Resend buffer:
* The smartaudio returns response for valid command frames in no less
* than 60msec, which we can't wait. So there's a need for a resend buffer.
*
* Command queueing:
* The driver autonomously sends GetSettings command for auto-bauding,
* asynchronous to user initiated commands; commands issued while another
* command is outstanding must be queued for later processing.
* The queueing also handles the case in which multiple commands are
* required to implement a user level command.
*/
// Retransmission
static void saResendCmd(void)
{
saSendFrame(sa_osbuf, sa_oslen);
}
static void saSendCmd(uint8_t *buf, int len)
{
int i;
for (i = 0 ; i < len ; i++)
sa_osbuf[i] = buf[i];
sa_oslen = len;
sa_outstanding = (buf[2] >> 1);
saSendFrame(sa_osbuf, sa_oslen);
}
// Command queue management
typedef struct saCmdQueue_s {
uint8_t *buf;
int len;
} saCmdQueue_t;
#define SA_QSIZE 4 // 1 heartbeat (GetSettings) + 2 commands + 1 slack
static saCmdQueue_t sa_queue[SA_QSIZE];
static uint8_t sa_qhead = 0;
static uint8_t sa_qtail = 0;
#ifdef DPRINTF_SMARTAUDIO
static int saQueueLength(void)
{
if (sa_qhead >= sa_qtail) {
return sa_qhead - sa_qtail;
} else {
return SA_QSIZE + sa_qhead - sa_qtail;
}
}
#endif
static bool saQueueEmpty(void)
{
return sa_qhead == sa_qtail;
}
static bool saQueueFull(void)
{
return ((sa_qhead + 1) % SA_QSIZE) == sa_qtail;
}
static void saQueueCmd(uint8_t *buf, int len)
{
if (saQueueFull())
return;
sa_queue[sa_qhead].buf = buf;
sa_queue[sa_qhead].len = len;
sa_qhead = (sa_qhead + 1) % SA_QSIZE;
}
static void saSendQueue(void)
{
if (saQueueEmpty())
return;
saSendCmd(sa_queue[sa_qtail].buf, sa_queue[sa_qtail].len);
sa_qtail = (sa_qtail + 1) % SA_QSIZE;
}
// Individual commands
static void saGetSettings(void)
{
static uint8_t bufGetSettings[5] = {0xAA, 0x55, SACMD(SA_CMD_GET_SETTINGS), 0x00, 0x9F};
saQueueCmd(bufGetSettings, 5);
}
void saSetFreq(uint16_t freq)
{
static uint8_t buf[7] = { 0xAA, 0x55, SACMD(SA_CMD_SET_FREQ), 2 };
if (freq & SA_FREQ_GETPIT) {
dprintf(("smartAudioSetFreq: GETPIT\r\n"));
} else if (freq & SA_FREQ_SETPIT) {
dprintf(("smartAudioSetFreq: SETPIT %d\r\n", freq & SA_FREQ_MASK));
} else {
dprintf(("smartAudioSetFreq: SET %d\r\n", freq));
}
buf[4] = (freq >> 8) & 0xff;
buf[5] = freq & 0xff;
buf[6] = CRC8(buf, 6);
saQueueCmd(buf, 7);
}
#if 0
static void saSetPitFreq(uint16_t freq)
{
saSetFreq(freq | SA_FREQ_SETPIT);
}
static void saGetPitFreq(void)
{
saSetFreq(SA_FREQ_GETPIT);
}
#endif
void saSetBandAndChannel(uint8_t band, uint8_t channel)
{
static uint8_t buf[6] = { 0xAA, 0x55, SACMD(SA_CMD_SET_CHAN), 1 };
buf[4] = band * 8 + channel;
buf[5] = CRC8(buf, 5);
saQueueCmd(buf, 6);
}
void saSetMode(int mode)
{
static uint8_t buf[6] = { 0xAA, 0x55, SACMD(SA_CMD_SET_MODE), 1 };
buf[4] = (mode & 0x3f)|saLockMode;
buf[5] = CRC8(buf, 5);
saQueueCmd(buf, 6);
}
void saSetPowerByIndex(uint8_t index)
{
static uint8_t buf[6] = { 0xAA, 0x55, SACMD(SA_CMD_SET_POWER), 1 };
dprintf(("saSetPowerByIndex: index %d\r\n", index));
if (saDevice.version == 0) {
// Unknown or yet unknown version.
return;
}
if (index > 3)
return;
buf[4] = (saDevice.version == 1) ? saPowerTable[index].valueV1 : saPowerTable[index].valueV2;
buf[5] = CRC8(buf, 5);
saQueueCmd(buf, 6);
}
bool vtxSmartAudioInit(void)
{
#ifdef SMARTAUDIO_DPRINTF
// Setup debugSerialPort
debugSerialPort = openSerialPort(DPRINTF_SERIAL_PORT, FUNCTION_NONE, NULL, NULL, 115200, MODE_RXTX, 0);
if (debugSerialPort) {
setPrintfSerialPort(debugSerialPort);
dprintf(("smartAudioInit: OK\r\n"));
}
#endif
serialPortConfig_t *portConfig = findSerialPortConfig(FUNCTION_VTX_SMARTAUDIO);
if (portConfig) {
smartAudioSerialPort = openSerialPort(portConfig->identifier, FUNCTION_VTX_SMARTAUDIO, NULL, NULL, 4800, MODE_RXTX, SERIAL_BIDIR|SERIAL_BIDIR_PP);
}
if (!smartAudioSerialPort) {
return false;
}
vtxCommonRegisterDevice(&vtxSmartAudio);
return true;
}
void vtxSAProcess(uint32_t now)
{
static bool initialSent = false;
if (smartAudioSerialPort == NULL)
return;
while (serialRxBytesWaiting(smartAudioSerialPort) > 0) {
uint8_t c = serialRead(smartAudioSerialPort);
saReceiveFramer((uint16_t)c);
}
// Re-evaluate baudrate after each frame reception
saAutobaud();
if (!initialSent) {
saGetSettings();
saSetFreq(SA_FREQ_GETPIT);
saSendQueue();
initialSent = true;
return;
}
if ((sa_outstanding != SA_CMD_NONE)
&& (now - sa_lastTransmission > SMARTAUDIO_CMD_TIMEOUT)) {
// Last command timed out
// dprintf(("process: resending 0x%x\r\n", sa_outstanding));
// XXX Todo: Resend termination and possible offline transition
saResendCmd();
} else if (!saQueueEmpty()) {
// Command pending. Send it.
// dprintf(("process: sending queue\r\n"));
saSendQueue();
} else if (now - sa_lastTransmission >= 1000) {
// Heart beat for autobauding
//dprintf(("process: sending heartbeat\r\n"));
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;
vtxCommonGetBandAndChannel(&monBand, &monChan);
vtxCommonGetPowerIndex(&monPower);
debug[0] = monBand;
debug[1] = monChan;
debug[2] = monPower;
}
#endif
}
#ifdef VTX_COMMON
// Interface to common VTX API
vtxDevType_e vtxSAGetDeviceType(void)
{
return VTXDEV_SMARTAUDIO;
}
bool vtxSAIsReady(void)
{
return !(saDevice.version == 0);
}
void vtxSASetBandAndChannel(uint8_t band, uint8_t channel)
{
if (band && channel)
saSetBandAndChannel(band - 1, channel - 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;
}
bool vtxSAGetBandAndChannel(uint8_t *pBand, uint8_t *pChannel)
{
if (!vtxSAIsReady())
return false;
*pBand = (saDevice.channel / 8) + 1;
*pChannel = (saDevice.channel % 8) + 1;
return true;
}
bool vtxSAGetPowerIndex(uint8_t *pIndex)
{
if (!vtxSAIsReady())
return false;
*pIndex = ((saDevice.version == 1) ? saDacToPowerIndex(saDevice.power) : saDevice.power) + 1;
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 const vtxVTable_t saVTable = {
.process = vtxSAProcess,
.getDeviceType = vtxSAGetDeviceType,
.isReady = vtxSAIsReady,
.setBandAndChannel = vtxSASetBandAndChannel,
.setPowerByIndex = vtxSASetPowerByIndex,
.setPitMode = vtxSASetPitMode,
.getBandAndChannel = vtxSAGetBandAndChannel,
.getPowerIndex = vtxSAGetPowerIndex,
.getPitMode = vtxSAGetPitMode,
};
#endif // VTX_COMMON
#endif // VTX_SMARTAUDIO