SPRacingF3Mini - Reduce current draw when using transponder while
[betaflight.git] / src / main / io / transponder_ir.c
blobb30d0c7423df29ddcb2e5312f96c5f547f8c5346
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 #include <stdbool.h>
19 #include <stdlib.h>
20 #include <stdint.h>
21 #include <string.h>
22 #include <stdarg.h>
24 #include <platform.h>
26 #include <build_config.h>
28 #include "drivers/transponder_ir.h"
29 #include "drivers/system.h"
31 #include "drivers/usb_detection.h"
33 #include "io/transponder_ir.h"
34 #include "config/config.h"
36 static bool transponderInitialised = false;
37 static bool transponderRepeat = false;
39 // timers
40 static uint32_t nextUpdateAt = 0;
42 #define JITTER_DURATION_COUNT (sizeof(jitterDurations) / sizeof(uint8_t))
43 static uint8_t jitterDurations[] = {0,9,4,8,3,9,6,7,1,6,9,7,8,2,6};
45 void updateTransponder(void)
47 static uint32_t jitterIndex = 0;
49 if (!(transponderInitialised && transponderRepeat && isTransponderIrReady())) {
50 return;
53 uint32_t now = micros();
55 bool updateNow = (int32_t)(now - nextUpdateAt) >= 0L;
56 if (!updateNow) {
57 return;
60 // TODO use a random number genenerator for random jitter? The idea here is to avoid multiple transmitters transmitting at the same time.
61 uint32_t jitter = (1000 * jitterDurations[jitterIndex++]);
62 if (jitterIndex >= JITTER_DURATION_COUNT) {
63 jitterIndex = 0;
66 nextUpdateAt = now + 4500 + jitter;
68 #ifdef SPRACINGF3MINI
69 // reduce current draw when USB cable is plugged in by decreasing the transponder transmit rate.
70 if (usbCableIsInserted()) {
71 nextUpdateAt = now + (1000 * 1000) / 10; // 10 hz.
73 #endif
75 transponderIrTransmit();
78 void transponderInit(uint8_t* transponderData)
80 transponderInitialised = false;
81 transponderIrInit();
82 transponderIrUpdateData(transponderData);
85 void transponderEnable(void)
87 transponderInitialised = true;
90 void transponderDisable(void)
92 transponderInitialised = false;
95 void transponderStopRepeating(void)
97 transponderRepeat = false;
100 void transponderStartRepeating(void)
102 transponderRepeat = true;
105 void transponderUpdateData(uint8_t* transponderData)
107 transponderIrUpdateData(transponderData);
110 void transponderTransmitOnce(void) {
112 if (!transponderInitialised) {
113 return;
115 transponderIrTransmit();