Bump workflow action (#13355)
[betaflight.git] / src / main / io / transponder_ir.c
blob1d62dd9e0eafa15bd74bfff641af36f20a526826
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 #ifdef TRANSPONDER_PIN
58 transponderConfig->ioTag = IO_TAG(TRANSPONDER_PIN);
59 #else
60 transponderConfig->ioTag = IO_TAG_NONE;
61 #endif
64 static bool transponderInitialised = false;
65 static bool transponderRepeat = false;
67 // timers
68 static timeUs_t nextUpdateAtUs = 0;
70 #define JITTER_DURATION_COUNT ARRAYLEN(jitterDurations)
71 static uint8_t jitterDurations[] = {0,9,4,8,3,9,6,7,1,6,9,7,8,2,6};
73 const transponderRequirement_t transponderRequirements[TRANSPONDER_PROVIDER_COUNT] = {
74 {TRANSPONDER_ILAP, TRANSPONDER_DATA_LENGTH_ILAP, TRANSPONDER_TRANSMIT_DELAY_ILAP, TRANSPONDER_TRANSMIT_JITTER_ILAP},
75 {TRANSPONDER_ARCITIMER, TRANSPONDER_DATA_LENGTH_ARCITIMER, TRANSPONDER_TRANSMIT_DELAY_ARCITIMER, TRANSPONDER_TRANSMIT_JITTER_ARCITIMER},
76 {TRANSPONDER_ERLT, TRANSPONDER_DATA_LENGTH_ERLT, TRANSPONDER_TRANSMIT_DELAY_ERLT, TRANSPONDER_TRANSMIT_JITTER_ERLT}
79 void transponderUpdate(timeUs_t currentTimeUs)
81 static uint32_t jitterIndex = 0;
83 if (!(transponderInitialised && transponderRepeat && isTransponderIrReady())) {
84 return;
87 const bool updateNow = (timeDelta_t)(currentTimeUs - nextUpdateAtUs) >= 0L;
88 if (!updateNow) {
89 return;
92 uint8_t provider = transponderConfig()->provider;
94 // TODO use a random number generator for random jitter? The idea here is to avoid multiple transmitters transmitting at the same time.
95 uint32_t jitter = (transponderRequirements[provider - 1].transmitJitter / 10 * jitterDurations[jitterIndex++]);
96 if (jitterIndex >= JITTER_DURATION_COUNT) {
97 jitterIndex = 0;
100 nextUpdateAtUs = currentTimeUs + transponderRequirements[provider - 1].transmitDelay + jitter;
102 #ifdef REDUCE_TRANSPONDER_CURRENT_DRAW_WHEN_USB_CABLE_PRESENT
103 // reduce current draw when USB cable is plugged in by decreasing the transponder transmit rate.
104 if (usbCableIsInserted()) {
105 nextUpdateAtUs = currentTimeUs + (1000 * 1000) / 10; // 10 hz.
107 #endif
109 transponderIrTransmit();
112 void transponderInit(void)
114 transponderInitialised = transponderIrInit(transponderConfig()->ioTag, transponderConfig()->provider);
115 if (!transponderInitialised) {
116 return;
119 transponderIrUpdateData(transponderConfig()->data);
122 void transponderStopRepeating(void)
124 transponderRepeat = false;
127 void transponderStartRepeating(void)
129 if (!transponderInitialised) {
130 return;
133 transponderRepeat = true;
136 void transponderUpdateData(void)
138 if (!transponderInitialised) {
139 return;
142 transponderIrUpdateData(transponderConfig()->data);
145 void transponderTransmitOnce(void)
148 if (!transponderInitialised) {
149 return;
151 transponderIrTransmit();
153 #endif