Merge pull request #11190 from mathiasvr/pr-arraylen
[betaflight.git] / src / main / io / transponder_ir.c
blob325541764112e3f10c008949516395782f3fd747
1 /*
2 * This file is part of Cleanflight and Betaflight.
4 * Cleanflight and Betaflight are free software. You can redistribute
5 * this software and/or modify this software under the terms of the
6 * GNU General Public License as published by the Free Software
7 * Foundation, either version 3 of the License, or (at your option)
8 * any later version.
10 * Cleanflight and Betaflight are distributed in the hope that they
11 * will be useful, but WITHOUT ANY WARRANTY; without even the implied
12 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13 * See the GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this software.
18 * If not, see <http://www.gnu.org/licenses/>.
21 #include <stdbool.h>
22 #include <stdlib.h>
23 #include <stdint.h>
24 #include <string.h>
25 #include <stdarg.h>
27 #include "platform.h"
29 #ifdef USE_TRANSPONDER
30 #include "build/build_config.h"
32 #include "config/config_reset.h"
33 #include "pg/pg.h"
34 #include "pg/pg_ids.h"
36 #include "common/utils.h"
38 #include "drivers/timer.h"
39 #include "drivers/transponder_ir.h"
40 #include "drivers/system.h"
41 #include "drivers/usb_io.h"
43 #include "config/config.h"
45 #include "io/transponder_ir.h"
47 PG_REGISTER_WITH_RESET_FN(transponderConfig_t, transponderConfig, PG_TRANSPONDER_CONFIG, 0);
49 void pgResetFn_transponderConfig(transponderConfig_t *transponderConfig)
51 RESET_CONFIG_2(transponderConfig_t, transponderConfig,
52 .provider = TRANSPONDER_ILAP,
53 .reserved = 0,
54 .data = { 0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC, 0x0, 0x0, 0x0 }, // Note, this is NOT a valid transponder code, it's just for testing production hardware
55 .ioTag = IO_TAG_NONE
57 transponderConfig->ioTag = timerioTagGetByUsage(TIM_USE_TRANSPONDER, 0);
60 static bool transponderInitialised = false;
61 static bool transponderRepeat = false;
63 // timers
64 static timeUs_t nextUpdateAtUs = 0;
66 #define JITTER_DURATION_COUNT ARRAYLEN(jitterDurations)
67 static uint8_t jitterDurations[] = {0,9,4,8,3,9,6,7,1,6,9,7,8,2,6};
69 const transponderRequirement_t transponderRequirements[TRANSPONDER_PROVIDER_COUNT] = {
70 {TRANSPONDER_ILAP, TRANSPONDER_DATA_LENGTH_ILAP, TRANSPONDER_TRANSMIT_DELAY_ILAP, TRANSPONDER_TRANSMIT_JITTER_ILAP},
71 {TRANSPONDER_ARCITIMER, TRANSPONDER_DATA_LENGTH_ARCITIMER, TRANSPONDER_TRANSMIT_DELAY_ARCITIMER, TRANSPONDER_TRANSMIT_JITTER_ARCITIMER},
72 {TRANSPONDER_ERLT, TRANSPONDER_DATA_LENGTH_ERLT, TRANSPONDER_TRANSMIT_DELAY_ERLT, TRANSPONDER_TRANSMIT_JITTER_ERLT}
75 void transponderUpdate(timeUs_t currentTimeUs)
77 static uint32_t jitterIndex = 0;
79 if (!(transponderInitialised && transponderRepeat && isTransponderIrReady())) {
80 return;
83 const bool updateNow = (timeDelta_t)(currentTimeUs - nextUpdateAtUs) >= 0L;
84 if (!updateNow) {
85 return;
88 uint8_t provider = transponderConfig()->provider;
90 // TODO use a random number generator for random jitter? The idea here is to avoid multiple transmitters transmitting at the same time.
91 uint32_t jitter = (transponderRequirements[provider - 1].transmitJitter / 10 * jitterDurations[jitterIndex++]);
92 if (jitterIndex >= JITTER_DURATION_COUNT) {
93 jitterIndex = 0;
96 nextUpdateAtUs = currentTimeUs + transponderRequirements[provider - 1].transmitDelay + jitter;
98 #ifdef REDUCE_TRANSPONDER_CURRENT_DRAW_WHEN_USB_CABLE_PRESENT
99 // reduce current draw when USB cable is plugged in by decreasing the transponder transmit rate.
100 if (usbCableIsInserted()) {
101 nextUpdateAtUs = currentTimeUs + (1000 * 1000) / 10; // 10 hz.
103 #endif
105 transponderIrTransmit();
108 void transponderInit(void)
110 transponderInitialised = transponderIrInit(transponderConfig()->ioTag, transponderConfig()->provider);
111 if (!transponderInitialised) {
112 return;
115 transponderIrUpdateData(transponderConfig()->data);
118 void transponderStopRepeating(void)
120 transponderRepeat = false;
123 void transponderStartRepeating(void)
125 if (!transponderInitialised) {
126 return;
129 transponderRepeat = true;
132 void transponderUpdateData(void)
134 if (!transponderInitialised) {
135 return;
138 transponderIrUpdateData(transponderConfig()->data);
141 void transponderTransmitOnce(void)
144 if (!transponderInitialised) {
145 return;
147 transponderIrTransmit();
149 #endif