External module on X10 now tested OK
[opentx.git] / radio / src / opentx.h
blob38c209df747e71b3ddd7048b41825c77b868cc1d
1 /*
2 * Copyright (C) OpenTX
4 * Based on code named
5 * th9x - http://code.google.com/p/th9x
6 * er9x - http://code.google.com/p/er9x
7 * gruvin9x - http://code.google.com/p/gruvin9x
9 * License GPLv2: http://www.gnu.org/licenses/gpl-2.0.html
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License version 2 as
13 * published by the Free Software Foundation.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
21 #ifndef _OPENTX_H_
22 #define _OPENTX_H_
24 #include <inttypes.h>
25 #include <string.h>
26 #include <stddef.h>
27 #include <stdlib.h>
28 #include "definitions.h"
29 #include "opentx_types.h"
31 #if defined(SIMU)
32 #define SWITCH_SIMU(a, b) (a)
33 #else
34 #define SWITCH_SIMU(a, b) (b)
35 #endif
37 #if defined(PCBSKY9X)
38 #define IS_PCBSKY9X true
39 #define CASE_PCBSKY9X(x) x,
40 #else
41 #define IS_PCBSKY9X false
42 #define CASE_PCBSKY9X(x)
43 #endif
45 #if defined(CPUARM)
46 #define CASE_CPUARM(x) x,
47 #define IF_CPUARM(x) x
48 #else
49 #define CASE_CPUARM(x)
50 #define IF_CPUARM(x)
51 #endif
53 #if defined(STM32)
54 #define CASE_STM32(x) x,
55 #else
56 #define CASE_STM32(x)
57 #endif
59 #if defined(VARIO) && defined(CPUARM)
60 #define CASE_VARIO_CPUARM(x) x,
61 #else
62 #define CASE_VARIO_CPUARM(x)
63 #endif
65 #if defined(LUA)
66 #define CASE_LUA(x) x,
67 #else
68 #define CASE_LUA(x)
69 #endif
71 #if defined(CPUARM) || defined(CPUM2560)
72 #define CASE_PERSISTENT_TIMERS(x) x,
73 #else
74 #define CASE_PERSISTENT_TIMERS(x)
75 #endif
77 #if defined(RTCLOCK)
78 #define CASE_RTCLOCK(x) x,
79 #else
80 #define CASE_RTCLOCK(x)
81 #endif
83 #if defined(BUZZER)
84 #define CASE_BUZZER(x) x,
85 #else
86 #define CASE_BUZZER(x)
87 #endif
89 #if defined(AUDIO)
90 #define CASE_AUDIO(x) x,
91 #else
92 #define CASE_AUDIO(x)
93 #endif
95 #if defined(VOICE)
96 #define CASE_VOICE(x) x,
97 #else
98 #define CASE_VOICE(x)
99 #endif
101 #if defined(PWM_BACKLIGHT)
102 #define CASE_PWM_BACKLIGHT(x) x,
103 #else
104 #define CASE_PWM_BACKLIGHT(x)
105 #endif
107 #if defined(TELEMETRY_FRSKY) && defined(FRSKY_HUB) && defined(GPS)
108 #define CASE_GPS(x) x,
109 #else
110 #define CASE_GPS(x)
111 #endif
113 #if defined(VARIO)
114 #define CASE_VARIO(x) x,
115 #else
116 #define CASE_VARIO(x)
117 #endif
119 #if defined(HAPTIC)
120 #define CASE_HAPTIC(x) x,
121 #else
122 #define CASE_HAPTIC(x)
123 #endif
125 #if defined(SPLASH)
126 #define CASE_SPLASH(x) x,
127 #else
128 #define CASE_SPLASH(x)
129 #endif
131 #if defined(TELEMETRY_FRSKY)
132 #define CASE_FRSKY(x) x,
133 #else
134 #define CASE_FRSKY(x)
135 #endif
137 #if defined(TELEMETRY_MAVLINK)
138 #define CASE_MAVLINK(x) x,
139 #else
140 #define CASE_MAVLINK(x)
141 #endif
143 #if defined(PXX)
144 #define CASE_PXX(x) x,
145 #else
146 #define CASE_PXX(x)
147 #endif
149 #if defined(SDCARD)
150 #define CASE_SDCARD(x) x,
151 #else
152 #define CASE_SDCARD(x)
153 #endif
155 #if defined(BLUETOOTH) && !(defined(PCBX9E) && !defined(USEHORUSBT))
156 #define CASE_BLUETOOTH(x) x,
157 #else
158 #define CASE_BLUETOOTH(x)
159 #endif
161 #if defined(HELI)
162 #define CASE_HELI(x) x,
163 #else
164 #define CASE_HELI(x)
165 #endif
167 #if defined(TEMPLATES)
168 #define CASE_TEMPLATES(x) x,
169 #else
170 #define CASE_TEMPLATES(x)
171 #endif
173 #if defined(FLIGHT_MODES)
174 #define CASE_FLIGHT_MODES(x) x,
175 #else
176 #define CASE_FLIGHT_MODES(x)
177 #endif
179 #if defined(CURVES)
180 #define CASE_CURVES(x) x,
181 #else
182 #define CASE_CURVES(x)
183 #endif
185 #if defined(GVARS)
186 #define CASE_GVARS(x) x,
187 #else
188 #define CASE_GVARS(x)
189 #endif
191 #if defined(PCBX9DP) || defined(PCBX9E)
192 #define CASE_PCBX9E_PCBX9DP(x) x,
193 #else
194 #define CASE_PCBX9E_PCBX9DP(x)
195 #endif
197 #if defined(PCBX9E)
198 #define CASE_PCBX9E(x) x,
199 #else
200 #define CASE_PCBX9E(x)
201 #endif
203 #if defined(BLUETOOTH) && !(defined(PCBX9E) && !defined(USEHORUSBT))
204 #define CASE_BLUETOOTH(x) x,
205 #else
206 #define CASE_BLUETOOTH(x)
207 #endif
209 #if defined(PCBSKY9X) && !defined(AR9X) && !defined(REVA)
210 #define TX_CAPACITY_MEASUREMENT
211 #define CASE_CAPACITY(x) x,
212 #else
213 #define CASE_CAPACITY(x)
214 #endif
216 #if ROTARY_ENCODERS > 0
217 #define ROTARY_ENCODER_NAVIGATION
218 #endif
220 #if defined(FAI)
221 #define IS_FAI_ENABLED() true
222 #define IF_FAI_CHOICE(x)
223 #elif defined(FAI_CHOICE)
224 #define IS_FAI_ENABLED() g_eeGeneral.fai
225 #define IF_FAI_CHOICE(x) x,
226 #else
227 #define IS_FAI_ENABLED() false
228 #define IF_FAI_CHOICE(x)
229 #endif
231 #define IS_FAI_FORBIDDEN(idx) (IS_FAI_ENABLED() && idx >= MIXSRC_FIRST_TELEM)
233 #if defined(BLUETOOTH)
234 #if defined(X9E) && !defined(USEHORUSBT)
235 #define IS_BLUETOOTH_TRAINER() (g_model.trainerMode == TRAINER_MODE_SLAVE_BLUETOOTH)
236 #define IS_SLAVE_TRAINER() (g_model.trainerMode == TRAINER_MODE_SLAVE)
237 #else
238 #define IS_BLUETOOTH_TRAINER() (g_model.trainerMode == TRAINER_MODE_MASTER_BLUETOOTH || g_model.trainerMode == TRAINER_MODE_SLAVE_BLUETOOTH)
239 #define IS_SLAVE_TRAINER() (g_model.trainerMode == TRAINER_MODE_SLAVE || g_model.trainerMode == TRAINER_MODE_SLAVE_BLUETOOTH)
240 #endif
241 #else
242 #define IS_BLUETOOTH_TRAINER() false
243 #define IS_SLAVE_TRAINER() (g_model.trainerMode == TRAINER_MODE_SLAVE)
244 #endif
246 #if defined(CPUARM)
247 #define MASTER_VOLUME
248 #endif
250 #if !defined(CPUM64) && !defined(ACCURAT_THROTTLE_TIMER)
251 // code cost is about 16 bytes for higher throttle accuracy for timer
252 // would not be noticable anyway, because all version up to this change had only 16 steps;
253 // now it has already 32 steps; this define would increase to 128 steps
254 #if !defined(ACCURAT_THROTTLE_TIMER)
255 #define ACCURAT_THROTTLE_TIMER
256 #endif
257 #endif
259 // RESX range is used for internal calculation; The menu says -100.0 to 100.0; internally it is -1024 to 1024 to allow some optimizations
260 #define RESX_SHIFT 10
261 #define RESX 1024
262 #define RESXu 1024u
263 #define RESXul 1024ul
264 #define RESXl 1024l
266 #include "board.h"
268 #if defined(DISK_CACHE)
269 #include "disk_cache.h"
270 #endif
272 #if defined(SIMU)
273 #include "targets/simu/simpgmspace.h"
274 #elif defined(CPUARM)
275 typedef const unsigned char pm_uchar;
276 typedef const char pm_char;
277 typedef const uint16_t pm_uint16_t;
278 typedef const uint8_t pm_uint8_t;
279 typedef const int16_t pm_int16_t;
280 typedef const int8_t pm_int8_t;
281 #define pgm_read_byte(address_short) (*(uint8_t*)(address_short))
282 #define PSTR(adr) adr
283 #define PROGMEM
284 #define pgm_read_adr(x) *(x)
285 #define cli()
286 #define sei()
287 #endif
289 #include "debug.h"
291 #if defined(PCBFLAMENCO)
292 #elif defined(PCBTARANIS) || defined(PCBHORUS)
293 #define SWSRC_THR SWSRC_SF2
294 #define SWSRC_GEA SWSRC_SG2
295 #define SWSRC_ID0 SWSRC_SA0
296 #define SWSRC_ID1 SWSRC_SA1
297 #define SWSRC_ID2 SWSRC_SA2
298 #define IS_MOMENTARY(sw) false // TODO
299 #else
300 #define SW_DSM2_BIND SW_TRN
301 #endif
303 #define NUM_PSWITCH (SWSRC_LAST_SWITCH-SWSRC_FIRST_SWITCH+1)
305 #include "myeeprom.h"
307 #if defined(CPUM64)
308 void memclear(void * ptr, uint8_t size);
309 #else
310 #define memclear(p, s) memset(p, 0, s)
311 #endif
313 void memswap(void * a, void * b, uint8_t size);
315 #if defined(PCBX9D) || defined(PCBX9DP) || defined(PCBX9E) || defined(PCBHORUS)
316 #define POT_CONFIG(x) ((g_eeGeneral.potsConfig >> (2*((x)-POT1)))&0x03)
317 #define IS_POT_MULTIPOS(x) (IS_POT(x) && POT_CONFIG(x)==POT_MULTIPOS_SWITCH)
318 #define IS_POT_WITHOUT_DETENT(x) (IS_POT(x) && POT_CONFIG(x)==POT_WITHOUT_DETENT)
319 #define IS_SLIDER_AVAILABLE(x) ((x) == SLIDER1 || (x) == SLIDER2 || (IS_SLIDER(x) && (g_eeGeneral.slidersConfig & (0x01 << ((x)-SLIDER1)))))
320 #define IS_POT_AVAILABLE(x) (IS_POT(x) && POT_CONFIG(x)!=POT_NONE)
321 #define IS_POT_SLIDER_AVAILABLE(x) (IS_POT_AVAILABLE(x) || IS_SLIDER_AVAILABLE(x))
322 #define IS_MULTIPOS_CALIBRATED(cal) (cal->count>0 && cal->count<XPOTS_MULTIPOS_COUNT)
323 #elif defined(PCBX7)
324 #define IS_POT_MULTIPOS(x) (false)
325 #define IS_POT_WITHOUT_DETENT(x) (false)
326 #define IS_POT_SLIDER_AVAILABLE(x) (true)
327 #define IS_MULTIPOS_CALIBRATED(cal) (false)
328 #elif defined(PCBFLAMENCO)
329 #define IS_POT_MULTIPOS(x) (false)
330 #define IS_POT_WITHOUT_DETENT(x) (false)
331 #define IS_POT_SLIDER_AVAILABLE(x) (true)
332 #define IS_MULTIPOS_CALIBRATED(cal) (false)
333 #else
334 #define IS_POT_MULTIPOS(x) (false)
335 #define IS_POT_WITHOUT_DETENT(x) (true)
336 #define IS_POT_SLIDER_AVAILABLE(x) (true)
337 #define IS_MULTIPOS_CALIBRATED(cal) (false)
338 #endif
340 #if defined(PWR_BUTTON_PRESS)
341 #define pwrOffPressed() pwrPressed()
342 #else
343 #define pwrOffPressed() (!pwrPressed())
344 #endif
346 #define PWR_PRESS_SHUTDOWN_DELAY 300 // 3s
348 #define GET_LOWRES_POT_POSITION(i) (getValue(MIXSRC_FIRST_POT+(i)) >> 4)
349 #define SAVE_POT_POSITION(i) g_model.potsWarnPosition[i] = GET_LOWRES_POT_POSITION(i)
351 #if ROTARY_ENCODERS > 0
352 #define IF_ROTARY_ENCODERS(x) x,
353 #else
354 #define IF_ROTARY_ENCODERS(x)
355 #endif
357 #define PPM_CENTER 1500
359 #if defined(PPM_CENTER_ADJUSTABLE)
360 #define PPM_CH_CENTER(ch) (PPM_CENTER + limitAddress(ch)->ppmCenter)
361 #else
362 #define PPM_CH_CENTER(ch) (PPM_CENTER)
363 #endif
365 #if defined(CPUARM)
366 #include "fifo.h"
367 #include "io/io_arm.h"
368 // This doesn't need protection on this processor
369 extern volatile tmr10ms_t g_tmr10ms;
370 #define get_tmr10ms() g_tmr10ms
371 #else
372 extern volatile tmr10ms_t g_tmr10ms;
373 extern inline uint16_t get_tmr10ms()
375 uint16_t time ;
376 cli();
377 time = g_tmr10ms ;
378 sei();
379 return time ;
381 #endif
383 #if defined(NAVIGATION_STICKS)
384 extern uint8_t StickScrollAllowed;
385 extern uint8_t StickScrollTimer;
386 #define STICK_SCROLL_TIMEOUT 9
387 #define STICK_SCROLL_DISABLE() StickScrollAllowed = 0
388 #else
389 #define STICK_SCROLL_DISABLE()
390 #endif
392 #if defined(CLI)
393 #include "cli.h"
394 #endif
396 #include "timers.h"
397 #include "storage/storage.h"
398 #include "pulses/pulses.h"
400 #if defined(CPUARM)
401 // Order is the same as in enum Protocols in myeeprom.h (none, ppm, xjt, dsm, crossfire, multi, r9m)
402 static const int8_t maxChannelsModules[] = { 0, 8, 8, -2, 8, 4, 8}; // relative to 8!
403 static const int8_t maxChannelsXJT[] = { 0, 8, 0, 4 }; // relative to 8!
404 #define MAX_TRAINER_CHANNELS_M8() (MAX_TRAINER_CHANNELS-8)
405 #endif
407 #if defined(MULTIMODULE)
408 #define IS_MODULE_MULTIMODULE(idx) (idx==EXTERNAL_MODULE && g_model.moduleData[EXTERNAL_MODULE].type==MODULE_TYPE_MULTIMODULE)
409 #else
410 #define IS_MODULE_MULTIMODULE(idx) (false)
411 #endif
413 #if defined(PCBTARANIS) || defined(PCBHORUS)
414 #if defined(TARANIS_INTERNAL_PPM)
415 #define IS_MODULE_PPM(idx) (idx==TRAINER_MODULE || (idx==INTERNAL_MODULE && g_model.moduleData[INTERNAL_MODULE].type==MODULE_TYPE_PPM)|| (idx==EXTERNAL_MODULE && g_model.moduleData[EXTERNAL_MODULE].type==MODULE_TYPE_PPM))
416 #else
417 #define IS_MODULE_PPM(idx) (idx==TRAINER_MODULE || (idx==EXTERNAL_MODULE && g_model.moduleData[EXTERNAL_MODULE].type==MODULE_TYPE_PPM))
418 #endif
419 #define IS_MODULE_XJT(idx) (g_model.moduleData[idx].type==MODULE_TYPE_XJT)
420 #if defined(DSM2)
421 #define IS_MODULE_DSM2(idx) (idx==EXTERNAL_MODULE && g_model.moduleData[EXTERNAL_MODULE].type==MODULE_TYPE_DSM2)
422 #else
423 #define IS_MODULE_DSM2(idx) (false)
424 #endif
425 #if defined(CROSSFIRE)
426 #define IS_MODULE_CROSSFIRE(idx) (idx==EXTERNAL_MODULE && g_model.moduleData[EXTERNAL_MODULE].type==MODULE_TYPE_CROSSFIRE)
427 #else
428 #define IS_MODULE_CROSSFIRE(idx) (false)
429 #endif
430 #if defined(TARANIS_INTERNAL_PPM)
431 #define MAX_INTERNAL_MODULE_CHANNELS() ((g_model.moduleData[INTERNAL_MODULE].type == MODULE_TYPE_XJT) ? maxChannelsXJT[1+g_model.moduleData[0].rfProtocol] : maxChannelsModules[g_model.moduleData[INTERNAL_MODULE].type])
432 #else
433 #define MAX_INTERNAL_MODULE_CHANNELS() (maxChannelsXJT[1+g_model.moduleData[INTERNAL_MODULE].rfProtocol])
434 #endif
435 #define MAX_EXTERNAL_MODULE_CHANNELS() ((g_model.moduleData[EXTERNAL_MODULE].type == MODULE_TYPE_XJT) ? maxChannelsXJT[1+g_model.moduleData[1].rfProtocol] : maxChannelsModules[g_model.moduleData[EXTERNAL_MODULE].type])
436 #define MAX_CHANNELS(idx) (idx==INTERNAL_MODULE ? MAX_INTERNAL_MODULE_CHANNELS() : (idx==EXTERNAL_MODULE ? MAX_EXTERNAL_MODULE_CHANNELS() : MAX_TRAINER_CHANNELS_M8()))
437 #define NUM_CHANNELS(idx) ((IS_MODULE_CROSSFIRE(idx) || (IS_MODULE_MULTIMODULE(idx) && (g_model.moduleData[idx].getMultiProtocol(true) != MM_RF_PROTO_DSM2))) ? CROSSFIRE_CHANNELS_COUNT : (8+g_model.moduleData[idx].channelsCount))
438 #elif defined(PCBSKY9X) && !defined(REVA)
439 #define IS_MODULE_PPM(idx) (idx==TRAINER_MODULE || idx==EXTRA_MODULE || (idx==EXTERNAL_MODULE && g_model.moduleData[EXTERNAL_MODULE].type==MODULE_TYPE_PPM))
440 #define IS_MODULE_XJT(idx) (idx==EXTERNAL_MODULE && g_model.moduleData[EXTERNAL_MODULE].type==MODULE_TYPE_XJT)
441 #define IS_MODULE_DSM2(idx) (idx==EXTERNAL_MODULE && g_model.moduleData[EXTERNAL_MODULE].type==MODULE_TYPE_DSM2)
442 #define MAX_EXTERNAL_MODULE_CHANNELS() ((g_model.moduleData[EXTERNAL_MODULE].type == MODULE_TYPE_XJT) ? maxChannelsXJT[1+g_model.moduleData[0].rfProtocol] : maxChannelsModules[g_model.moduleData[EXTERNAL_MODULE].type])
443 #define MAX_EXTRA_MODULE_CHANNELS() (8) // Only PPM (16ch PPM)
444 #define MAX_CHANNELS(idx) (idx==EXTERNAL_MODULE ? MAX_EXTERNAL_MODULE_CHANNELS() : (idx==EXTRA_MODULE ? MAX_EXTRA_MODULE_CHANNELS() : MAX_TRAINER_CHANNELS_M8()))
445 #define NUM_CHANNELS(idx) (8+g_model.moduleData[idx].channelsCount)
446 #define IS_MODULE_CROSSFIRE(idx) (false)
447 #else
448 #define IS_MODULE_PPM(idx) (idx==TRAINER_MODULE || (idx==EXTERNAL_MODULE && g_model.moduleData[EXTERNAL_MODULE].type==MODULE_TYPE_PPM))
449 #define IS_MODULE_XJT(idx) (idx==EXTERNAL_MODULE && g_model.moduleData[EXTERNAL_MODULE].type==MODULE_TYPE_XJT)
450 #define IS_MODULE_DSM2(idx) (idx==EXTERNAL_MODULE && g_model.moduleData[EXTERNAL_MODULE].type==MODULE_TYPE_DSM2)
451 #define MAX_EXTERNAL_MODULE_CHANNELS() ((g_model.moduleData[EXTERNAL_MODULE].type == MODULE_TYPE_XJT) ? maxChannelsXJT[1+g_model.moduleData[EXTERNAL_MODULE].rfProtocol] : maxChannelsModules[g_model.moduleData[EXTERNAL_MODULE].type])
452 #define MAX_CHANNELS(idx) (idx==EXTERNAL_MODULE ? MAX_EXTERNAL_MODULE_CHANNELS() : MAX_TRAINER_CHANNELS_M8())
453 #define NUM_CHANNELS(idx) (8+g_model.moduleData[idx].channelsCount)
454 #endif
455 #define IS_MODULE_R9M(idx) (g_model.moduleData[idx].type==MODULE_TYPE_R9M)
456 #define IS_MODULE_PXX(idx) (IS_MODULE_XJT(idx) || IS_MODULE_R9M(idx))
459 #if defined(MULTIMODULE)
460 #define IS_MULTIMODULE_DSM(idx) (IS_MODULE_MULTIMODULE(idx) && g_model.moduleData[idx].getMultiProtocol(true) == MM_RF_PROTO_DSM2)
461 #define DEFAULT_CHANNELS(idx) (IS_MODULE_PPM(idx) ? 0 : IS_MULTIMODULE_DSM(idx) ? -1 : MAX_CHANNELS(idx))
462 #else
463 #define DEFAULT_CHANNELS(idx) (IS_MODULE_PPM(idx) ? 0 : MAX_CHANNELS(idx))
464 #endif
466 #if defined(CPUARM)
467 #define MASK_CFN_TYPE uint64_t // current max = 64 function switches
468 #define MASK_FUNC_TYPE uint32_t // current max = 32 functions
469 #elif defined(CPUM64)
470 #define MASK_CFN_TYPE uint16_t // current max = 16 function switches
471 #define MASK_FUNC_TYPE uint8_t // current max = 8 functions
472 #else
473 #define MASK_CFN_TYPE uint32_t // current max = 32 function switches
474 #define MASK_FUNC_TYPE uint8_t // current max = 8 functions
475 #endif
477 typedef struct {
478 MASK_FUNC_TYPE activeFunctions;
479 MASK_CFN_TYPE activeSwitches;
480 tmr10ms_t lastFunctionTime[MAX_SPECIAL_FUNCTIONS];
482 inline bool isFunctionActive(uint8_t func)
484 return activeFunctions & ((MASK_FUNC_TYPE)1 << func);
487 void reset()
489 memclear(this, sizeof(*this));
491 } CustomFunctionsContext;
493 #include "strhelpers.h"
494 #include "gui.h"
496 #if defined(TEMPLATES)
497 #include "templates.h"
498 #endif
500 #if !defined(SIMU)
501 #define assert(x)
502 #if !defined(CPUARM) || !defined(DEBUG)
503 #define printf printf_not_allowed
504 #endif
505 #endif
507 extern const pm_uint8_t bchout_ar[];
508 extern const pm_uint8_t modn12x3[];
510 //convert from mode 1 to mode stickMode
511 //NOTICE! => 0..3 -> 0..3
512 #define RUD_STICK 0
513 #define ELE_STICK 1
514 #define THR_STICK 2
515 #define AIL_STICK 3
516 #define CONVERT_MODE(x) (((x)<=AIL_STICK) ? pgm_read_byte(modn12x3 + 4*g_eeGeneral.stickMode + (x)) : (x) )
518 extern uint8_t channel_order(uint8_t x);
520 #define THRCHK_DEADBAND 16
522 #if defined(COLORLCD)
523 #define SPLASH_NEEDED() (false)
524 #elif defined(PCBTARANIS)
525 #define SPLASH_NEEDED() (g_eeGeneral.splashMode != 3)
526 #elif defined(CPUARM)
527 #define SPLASH_NEEDED() (g_model.moduleData[EXTERNAL_MODULE].type != MODULE_TYPE_DSM2 && !g_eeGeneral.splashMode)
528 #else
529 #define SPLASH_NEEDED() (!IS_DSM2_PROTOCOL(g_model.protocol) && !g_eeGeneral.splashMode)
530 #endif
532 #if defined(PCBHORUS)
533 #define SPLASH_TIMEOUT 0 /* we use the splash duration to load stuff from the SD */
534 #elif defined(FSPLASH)
535 #define SPLASH_TIMEOUT (g_eeGeneral.splashMode == 0 ? 60000/*infinite=10mn*/ : ((4*100) * (g_eeGeneral.splashMode & 0x03)))
536 #elif defined(PCBTARANIS) || defined(PCBFLAMENCO)
537 #define SPLASH_TIMEOUT (g_eeGeneral.splashMode==-4 ? 1500 : (g_eeGeneral.splashMode<=0 ? (400-g_eeGeneral.splashMode*200) : (400-g_eeGeneral.splashMode*100)))
538 #else
539 #define SPLASH_TIMEOUT (4*100) // 4 seconds
540 #endif
542 #if defined(ROTARY_ENCODERS)
543 #define IS_ROTARY_ENCODER_NAVIGATION_ENABLE() g_eeGeneral.reNavigation
544 extern volatile rotenc_t rotencValue[ROTARY_ENCODERS];
545 #define ROTARY_ENCODER_NAVIGATION_VALUE rotencValue[g_eeGeneral.reNavigation - 1]
546 #elif defined(ROTARY_ENCODER_NAVIGATION)
547 #define IS_ROTARY_ENCODER_NAVIGATION_ENABLE() true
548 extern volatile rotenc_t rotencValue[1];
549 #define ROTARY_ENCODER_NAVIGATION_VALUE rotencValue[0]
550 #endif
552 #if defined(CPUARM) && defined(ROTARY_ENCODER_NAVIGATION)
553 extern uint8_t rotencSpeed;
554 #define ROTENC_LOWSPEED 1
555 #define ROTENC_MIDSPEED 5
556 #define ROTENC_HIGHSPEED 50
557 #define ROTENC_DELAY_MIDSPEED 32
558 #define ROTENC_DELAY_HIGHSPEED 16
559 #endif
561 #define HEART_TIMER_10MS 1
562 #define HEART_TIMER_PULSES 2 // when multiple modules this is the first one
563 #if defined(PCBTARANIS) || defined(PCBFLAMENCO) || defined(PCBHORUS)
564 #define HEART_WDT_CHECK (HEART_TIMER_10MS + (HEART_TIMER_PULSES << 0) + (HEART_TIMER_PULSES << 1))
565 #else
566 #define HEART_WDT_CHECK (HEART_TIMER_10MS + HEART_TIMER_PULSES)
567 #endif
568 extern uint8_t heartbeat;
570 #if defined(CPUARM) && !defined(BOOT)
571 void watchdogSuspend(uint32_t timeout);
572 #define WATCHDOG_SUSPEND(x) watchdogSuspend(x)
573 #else
574 #define WATCHDOG_SUSPEND(...)
575 #endif
577 #define MAX_ALERT_TIME 60
579 struct t_inactivity
581 uint16_t counter;
582 uint8_t sum;
585 extern struct t_inactivity inactivity;
587 #define LEN_STD_CHARS 40
589 #if defined(TRANSLATIONS_CZ)
590 #define ZCHAR_MAX (LEN_STD_CHARS)
591 #else
592 #define ZCHAR_MAX (LEN_STD_CHARS + LEN_SPECIAL_CHARS)
593 #endif
595 char hex2zchar(uint8_t hex);
596 char idx2char(int8_t idx);
597 #if defined(CPUARM) || defined(SIMU)
598 int8_t char2idx(char c);
599 void str2zchar(char *dest, const char *src, int size);
600 int zchar2str(char *dest, const char *src, int size);
601 #endif
603 #include "keys.h"
604 #include "pwr.h"
606 #if defined(PCBTARANIS) || defined(PCBFLAMENCO) || defined(PCBHORUS)
607 div_t switchInfo(int switchPosition);
608 extern uint8_t potsPos[NUM_XPOTS];
609 #endif
611 #if defined(PCBHORUS)
612 uint16_t trimDown(uint16_t idx); // TODO why?
613 #else
614 uint8_t trimDown(uint8_t idx);
615 #endif
616 void readKeysAndTrims();
618 uint16_t evalChkSum();
620 #if !defined(GUI)
621 #define RAISE_ALERT(...)
622 #define ALERT(...)
623 #elif defined(VOICE)
624 #define RAISE_ALERT(title, msg, info, sound) showAlertBox(title, msg, info, sound)
625 #define ALERT(title, msg, sound) alert(title, msg, sound)
626 #else
627 #define RAISE_ALERT(title, msg, info, sound) showAlertBox(title, msg, info)
628 #define ALERT(title, msg, sound) alert(title, msg)
629 #endif
631 void alert(const pm_char * t, const pm_char * s ALERT_SOUND_ARG);
633 enum PerOutMode {
634 e_perout_mode_normal = 0,
635 e_perout_mode_inactive_flight_mode = 1,
636 e_perout_mode_notrainer = 2,
637 e_perout_mode_notrims = 4,
638 e_perout_mode_nosticks = 8,
639 e_perout_mode_noinput = e_perout_mode_notrainer+e_perout_mode_notrims+e_perout_mode_nosticks
643 #if defined(MODULE_ALWAYS_SEND_PULSES)
644 extern uint8_t startupWarningState;
646 enum StartupWarningStates {
647 STARTUP_WARNING_THROTTLE,
648 STARTUP_WARNING_SWITCHES,
649 STARTUP_WARNING_DONE,
651 #endif
654 // Fiddle to force compiler to use a pointer
655 #if defined(CPUARM) || defined(SIMU)
656 #define FORCE_INDIRECT(ptr)
657 #else
658 #define FORCE_INDIRECT(ptr) __asm__ __volatile__ ("" : "=e" (ptr) : "0" (ptr))
659 #endif
661 extern uint8_t mixerCurrentFlightMode;
662 extern uint8_t lastFlightMode;
663 extern uint8_t flightModeTransitionLast;
665 #if defined(CPUARM)
666 #define bitfield_channels_t uint32_t
667 #else
668 #define bitfield_channels_t uint16_t
669 #endif
671 #if defined(SIMU)
672 inline int availableMemory() { return 1000; }
673 #elif defined(CPUARM) && !defined(SIMU)
674 extern unsigned char *heap;
675 extern int _end;
676 extern int _estack;
677 extern int _main_stack_start;
678 extern int _heap_end;
679 #define availableMemory() ((unsigned int)((unsigned char *)&_heap_end - heap))
680 #endif
682 void evalFlightModeMixes(uint8_t mode, uint8_t tick10ms);
683 void evalMixes(uint8_t tick10ms);
684 void doMixerCalculations();
685 void scheduleNextMixerCalculation(uint8_t module, uint16_t delay);
687 #if defined(CPUARM)
688 void checkTrims();
689 #endif
690 void perMain();
691 NOINLINE void per10ms();
693 getvalue_t getValue(mixsrc_t i);
695 #if defined(CPUARM)
696 #define GETSWITCH_MIDPOS_DELAY 1
697 bool getSwitch(swsrc_t swtch, uint8_t flags=0);
698 #else
699 bool getSwitch(swsrc_t swtch);
700 #endif
702 void logicalSwitchesTimerTick();
703 void logicalSwitchesReset();
705 #if defined(CPUARM)
706 void evalLogicalSwitches(bool isCurrentPhase=true);
707 void logicalSwitchesCopyState(uint8_t src, uint8_t dst);
708 #define LS_RECURSIVE_EVALUATION_RESET()
709 #else
710 #define evalLogicalSwitches(xxx)
711 #define GETSWITCH_RECURSIVE_TYPE uint16_t
712 extern volatile GETSWITCH_RECURSIVE_TYPE s_last_switch_used;
713 extern volatile GETSWITCH_RECURSIVE_TYPE s_last_switch_value;
714 #define LS_RECURSIVE_EVALUATION_RESET() s_last_switch_used = 0
715 #endif
717 #if defined(PCBTARANIS) || defined(PCBFLAMENCO) || defined(PCBHORUS)
718 void getSwitchesPosition(bool startup);
719 #else
720 #define getSwitchesPosition(...)
721 #endif
723 extern swarnstate_t switches_states;
724 swsrc_t getMovedSwitch();
726 #if defined(CPUARM)
727 #define GET_MOVED_SOURCE_PARAMS uint8_t min
728 int8_t getMovedSource(GET_MOVED_SOURCE_PARAMS);
729 #define GET_MOVED_SOURCE(min, max) getMovedSource(min)
730 #else
731 #define GET_MOVED_SOURCE_PARAMS
732 int8_t getMovedSource();
733 #define GET_MOVED_SOURCE(min, max) getMovedSource()
734 #endif
736 #if defined(FLIGHT_MODES)
737 extern uint8_t getFlightMode();
738 #else
739 #define getFlightMode() 0
740 #endif
742 #if !defined(CPUARM)
743 uint8_t getTrimFlightMode(uint8_t phase, uint8_t idx);
744 #else
745 #define getTrimFlightMode(phase, idx) (phase)
746 #endif
748 #if defined(GVARS)
749 extern int8_t trimGvar[NUM_STICKS+NUM_AUX_TRIMS];
750 #define TRIM_REUSED(idx) trimGvar[idx] >= 0
751 #else
752 #define TRIM_REUSED(idx) 0
753 #endif
755 trim_t getRawTrimValue(uint8_t phase, uint8_t idx);
756 int getTrimValue(uint8_t phase, uint8_t idx);
758 #if defined(CPUARM)
759 bool setTrimValue(uint8_t phase, uint8_t idx, int trim);
760 #else
761 void setTrimValue(uint8_t phase, uint8_t idx, int trim);
762 #endif
764 #if defined(ROTARY_ENCODERS)
765 int16_t getRotaryEncoder(uint8_t idx);
766 void incRotaryEncoder(uint8_t idx, int8_t inc);
767 #endif
769 #if defined(PCBGRUVIN9X) || defined(PCBMEGA2560)
770 #define ROTARY_ENCODER_GRANULARITY (1)
771 #elif defined(PCBSKY9X)
772 #define ROTARY_ENCODER_GRANULARITY (2 << g_eeGeneral.rotarySteps)
773 #else
774 #define ROTARY_ENCODER_GRANULARITY (2)
775 #endif
777 #include "gvars.h"
779 extern uint16_t sessionTimer;
780 extern uint16_t s_timeCumThr;
781 extern uint16_t s_timeCum16ThrP;
783 #if defined(OVERRIDE_CHANNEL_FUNCTION)
784 #if defined(CPUARM)
785 #define OVERRIDE_CHANNEL_UNDEFINED -4096
786 #else
787 #define OVERRIDE_CHANNEL_UNDEFINED -128
788 #endif
789 extern safetych_t safetyCh[MAX_OUTPUT_CHANNELS];
790 #endif
792 extern uint8_t trimsCheckTimer;
794 #if defined(CPUARM)
795 extern uint8_t trimsDisplayTimer;
796 extern uint8_t trimsDisplayMask;
797 #endif
799 void flightReset(uint8_t check=true);
801 extern uint8_t unexpectedShutdown;
803 extern uint16_t maxMixerDuration;
805 #if !defined(CPUARM)
806 extern uint8_t g_tmr1Latency_max;
807 extern uint8_t g_tmr1Latency_min;
808 extern uint16_t lastMixerDuration;
809 #endif
811 #if defined(CPUARM)
812 #define DURATION_MS_PREC2(x) ((x)/20)
813 #else
814 #define DURATION_MS_PREC2(x) ((x)*100)/16
815 #endif
817 #if defined(THRTRACE)
818 #if defined(COLORLCD)
819 #define MAXTRACE (LCD_W-2*10)
820 #else
821 #define MAXTRACE (LCD_W - 8)
822 #endif
823 extern uint8_t s_traceBuf[MAXTRACE];
824 extern uint16_t s_traceWr;
825 extern uint8_t s_cnt_10s;
826 extern uint16_t s_cnt_samples_thr_10s;
827 extern uint16_t s_sum_samples_thr_10s;
828 #define RESET_THR_TRACE() s_traceWr = s_cnt_10s = s_cnt_samples_thr_10s = s_sum_samples_thr_10s = s_timeCum16ThrP = s_timeCumThr = 0
829 #else
830 #define RESET_THR_TRACE() s_timeCum16ThrP = s_timeCumThr = 0
831 #endif
833 #if defined(SIMU)
834 uint16_t getTmr2MHz();
835 uint16_t getTmr16KHz();
836 #elif defined(STM32)
837 static inline uint16_t getTmr2MHz() { return TIMER_2MHz_TIMER->CNT; }
838 #elif defined(PCBSKY9X)
839 static inline uint16_t getTmr2MHz() { return TC1->TC_CHANNEL[0].TC_CV; }
840 #else
841 uint16_t getTmr16KHz();
842 #endif
844 #if !defined(CPUARM)
845 uint16_t stackAvailable();
846 #endif
848 #if defined(SPLASH)
849 void doSplash();
850 #endif
852 #if MENUS_LOCK == 1
853 extern bool readonly;
854 extern bool readonlyUnlocked();
855 #define READ_ONLY() readonly
856 #define READ_ONLY_UNLOCKED() readonlyUnlocked()
857 #else
858 #define READ_ONLY() false
859 #define READ_ONLY_UNLOCKED() true
860 #endif
862 void checkLowEEPROM();
863 void checkTHR();
864 void checkSwitches();
865 void checkAlarm();
866 void checkAll();
868 #if !defined(SIMU)
869 void getADC();
870 #if defined(CPUARM)
871 #define GET_ADC_IF_MIXER_NOT_RUNNING() do { if (s_pulses_paused) getADC(); } while(0)
872 #else
873 #define GET_ADC_IF_MIXER_NOT_RUNNING() getADC()
874 #endif
875 #endif
877 #if defined(SBUS)
878 #include "sbus.h"
879 #endif
881 void backlightOn();
882 void checkBacklight();
883 void doLoopCommonActions();
885 #define BITMASK(bit) (1<<(bit))
887 #if !defined(UNUSED)
888 #define UNUSED(x) ((void)(x)) /* to avoid warnings */
889 #endif
891 /// returns the number of elements of an array
892 #define DIM(arr) (sizeof((arr))/sizeof((arr)[0]))
894 template<class t> FORCEINLINE t min(t a, t b) { return a<b?a:b; }
895 template<class t> FORCEINLINE t max(t a, t b) { return a>b?a:b; }
896 template<class t> FORCEINLINE t sgn(t a) { return a>0 ? 1 : (a < 0 ? -1 : 0); }
897 template<class t> FORCEINLINE t limit(t mi, t x, t ma) { return min(max(mi,x),ma); }
898 template<class t> void SWAP(t & a, t & b) { t tmp = b; b = a; a = tmp; }
900 uint16_t isqrt32(uint32_t n);
902 #if defined(CPUARM) && !defined(BOOT)
903 #include "tasks_arm.h"
904 extern OS_MutexID mixerMutex;
905 inline void pauseMixerCalculations()
907 CoEnterMutexSection(mixerMutex);
910 inline void resumeMixerCalculations()
912 CoLeaveMutexSection(mixerMutex);
914 #else
915 #define pauseMixerCalculations()
916 #define resumeMixerCalculations()
917 #endif
919 void generalDefault();
920 void modelDefault(uint8_t id);
922 #if defined(CPUARM)
923 void checkModelIdUnique(uint8_t index, uint8_t module);
924 #endif
926 #if defined(CPUARM)
927 uint32_t hash(const void * ptr, uint32_t size);
928 inline int divRoundClosest(const int n, const int d)
930 if (d == 0)
931 return 0;
932 else
933 return ((n < 0) ^ (d < 0)) ? ((n - d/2)/d) : ((n + d/2)/d);
936 #define calc100to256_16Bits(x) calc100to256(x)
937 #define calc100toRESX_16Bits(x) calc100toRESX(x)
939 inline int calc100to256(int x)
941 return divRoundClosest(x*256, 100);
944 inline int calc100toRESX(int x)
946 return divRoundClosest(x*RESX, 100);
949 inline int calc1000toRESX(int x)
951 return divRoundClosest(x*RESX, 1000);
954 inline int calcRESXto1000(int x)
956 return divRoundClosest(x*1000, RESX);
959 inline int calcRESXto100(int x)
961 return divRoundClosest(x*100, RESX);
964 #else
965 extern int16_t calc100to256_16Bits(int16_t x); // @@@2 open.20.fsguruh: return x*2.56
966 extern int16_t calc100to256(int8_t x); // @@@2 open.20.fsguruh: return x*2.56
967 extern int16_t calc100toRESX_16Bits(int16_t x); // @@@ open.20.fsguruh
968 extern int16_t calc100toRESX(int8_t x);
969 extern int16_t calc1000toRESX(int16_t x);
970 extern int16_t calcRESXto1000(int16_t x);
971 extern int8_t calcRESXto100(int16_t x);
972 #endif
974 #if defined(COLORLCD)
975 extern const char vers_stamp[];
976 extern const char date_stamp[];
977 extern const char time_stamp[];
978 extern const char eeprom_stamp[];
979 #else
980 extern const char vers_stamp[];
981 #endif
983 extern uint8_t g_vbat100mV;
984 #if LCD_W > 128
985 #define GET_TXBATT_BARS() (limit<int8_t>(0, div_and_round(10 * (g_vbat100mV - g_eeGeneral.vBatMin - 90), 30 + g_eeGeneral.vBatMax - g_eeGeneral.vBatMin), 10))
986 #else
987 #define GET_TXBATT_BARS() (limit<int8_t>(2, 20 * (g_vbat100mV - g_eeGeneral.vBatMin - 90) / (30 + g_eeGeneral.vBatMax - g_eeGeneral.vBatMin), 20))
988 #endif
989 #define IS_TXBATT_WARNING() (g_vbat100mV <= g_eeGeneral.vBatWarn)
992 #define g_blinkTmr10ms (*(uint8_t*)&g_tmr10ms)
993 extern uint8_t g_beepCnt;
994 extern uint8_t g_beepVal[5];
996 #include "trainer_input.h"
998 extern int32_t chans[MAX_OUTPUT_CHANNELS];
999 extern int16_t ex_chans[MAX_OUTPUT_CHANNELS]; // Outputs (before LIMITS) of the last perMain
1000 extern int16_t channelOutputs[MAX_OUTPUT_CHANNELS];
1001 extern uint16_t BandGap;
1003 #if defined(CPUARM)
1004 #define NUM_INPUTS (MAX_INPUTS)
1005 #else
1006 #define NUM_INPUTS (NUM_STICKS)
1007 #endif
1009 int expo(int x, int k);
1011 #if defined(CPUARM)
1012 inline int getMaximumValue(int source)
1014 if (source < MIXSRC_FIRST_CH)
1015 return 100;
1016 else if (source <= MIXSRC_LAST_CH)
1017 return g_model.extendedLimits ? 150 : 100;
1018 else if (source >= MIXSRC_FIRST_TIMER && source <= MIXSRC_LAST_TIMER)
1019 return (23*60)+59;
1020 else
1021 return 30000;
1023 #endif
1025 // Curves
1026 enum BaseCurves {
1027 CURVE_NONE,
1028 CURVE_X_GT0,
1029 CURVE_X_LT0,
1030 CURVE_ABS_X,
1031 CURVE_F_GT0,
1032 CURVE_F_LT0,
1033 CURVE_ABS_F,
1034 CURVE_BASE
1036 int8_t * curveAddress(uint8_t idx);
1037 struct point_t
1039 coord_t x;
1040 coord_t y;
1042 point_t getPoint(uint8_t i);
1043 #if !defined(CURVES)
1044 #define LOAD_MODEL_CURVES()
1045 #define applyCurve(x, idx) (x)
1046 #elif defined(CPUARM)
1047 typedef CurveData CurveInfo;
1048 void loadCurves();
1049 #define LOAD_MODEL_CURVES() loadCurves()
1050 int intpol(int x, uint8_t idx);
1051 int applyCurve(int x, CurveRef & curve);
1052 int applyCustomCurve(int x, uint8_t idx);
1053 int applyCurrentCurve(int x);
1054 int8_t getCurveX(int noPoints, int point);
1055 void resetCustomCurveX(int8_t * points, int noPoints);
1056 bool moveCurve(uint8_t index, int8_t shift); // TODO bool?
1057 #else
1058 struct CurveInfo {
1059 int8_t * crv;
1060 uint8_t points:7;
1061 uint8_t custom:1;
1063 CurveInfo curveInfo(uint8_t idx);
1064 int intpol(int x, uint8_t idx);
1065 int applyCurve(int x, int8_t idx);
1066 #define LOAD_MODEL_CURVES()
1067 #define applyCustomCurve(x, idx) intpol(x, idx)
1068 int applyCurrentCurve(int x);
1069 bool moveCurve(uint8_t index, int8_t shift, int8_t custom=0);
1070 #endif
1072 #if defined(CPUARM)
1073 #define APPLY_EXPOS_EXTRA_PARAMS_INC , uint8_t ovwrIdx=0, int16_t ovwrValue=0
1074 #define APPLY_EXPOS_EXTRA_PARAMS , uint8_t ovwrIdx, int16_t ovwrValue
1075 #else
1076 #define APPLY_EXPOS_EXTRA_PARAMS_INC
1077 #define APPLY_EXPOS_EXTRA_PARAMS
1078 #endif
1080 #if defined(CPUARM)
1081 void clearInputs();
1082 void defaultInputs();
1083 #endif
1085 void applyExpos(int16_t * anas, uint8_t mode APPLY_EXPOS_EXTRA_PARAMS_INC);
1086 int16_t applyLimits(uint8_t channel, int32_t value);
1088 void evalInputs(uint8_t mode);
1089 uint16_t anaIn(uint8_t chan);
1091 extern int16_t calibratedAnalogs[NUM_CALIBRATED_ANALOGS];
1093 #define FLASH_DURATION 20 /*200ms*/
1095 extern uint8_t beepAgain;
1096 extern uint16_t lightOffCounter;
1097 extern uint8_t flashCounter;
1098 extern uint8_t mixWarning;
1100 FlightModeData * flightModeAddress(uint8_t idx);
1101 ExpoData * expoAddress(uint8_t idx);
1102 MixData * mixAddress(uint8_t idx);
1103 LimitData * limitAddress(uint8_t idx);
1104 LogicalSwitchData * lswAddress(uint8_t idx);
1106 // static variables used in evalFlightModeMixes - moved here so they don't interfere with the stack
1107 // It's also easier to initialize them here.
1108 #if defined(CPUARM)
1109 extern int8_t virtualInputsTrims[NUM_INPUTS];
1110 #else
1111 extern int16_t rawAnas[NUM_INPUTS];
1112 #endif
1114 extern int16_t anas [NUM_INPUTS];
1115 extern int16_t trims[NUM_STICKS+NUM_AUX_TRIMS];
1116 extern BeepANACenter bpanaCenter;
1118 extern uint8_t s_mixer_first_run_done;
1120 void applyDefaultTemplate();
1122 void incSubtrim(uint8_t idx, int16_t inc);
1123 void instantTrim();
1124 void evalTrims();
1125 void copyTrimsToOffset(uint8_t ch);
1126 void copySticksToOffset(uint8_t ch);
1127 void moveTrimsToOffsets();
1129 #if defined(CPUARM)
1130 #define ACTIVE_PHASES_TYPE uint16_t
1131 #define DELAY_POS_SHIFT 0
1132 #define DELAY_POS_MARGIN 3
1133 #define delayval_t int16_t
1134 PACK(typedef struct {
1135 uint16_t delay;
1136 int16_t now; // timer trigger source -> off, abs, stk, stk%, sw/!sw, !m_sw/!m_sw
1137 int16_t prev;
1138 uint8_t activeMix;
1139 uint8_t activeExpo;
1140 }) SwOn;
1141 #else
1142 #define ACTIVE_PHASES_TYPE uint8_t
1143 #define DELAY_POS_SHIFT 10
1144 #define DELAY_POS_MARGIN 0
1145 #define delayval_t int8_t
1146 PACK(typedef struct {
1147 uint16_t delay:10;
1148 int16_t now:2; // timer trigger source -> off, abs, stk, stk%, sw/!sw, !m_sw/!m_sw
1149 int16_t prev:2;
1150 int16_t activeMix:1;
1151 int16_t activeExpo:1;
1152 }) SwOn;
1153 #endif
1155 extern SwOn swOn[MAX_MIXERS];
1156 extern int24_t act[MAX_MIXERS];
1158 #if defined(BOLD_FONT)
1159 inline bool isExpoActive(uint8_t expo)
1161 return swOn[expo].activeExpo;
1164 inline bool isMixActive(uint8_t mix)
1166 return swOn[mix].activeMix;
1168 #else
1169 #define isExpoActive(x) false
1170 #define isMixActive(x) false
1171 #endif
1173 enum LogicalSwitchFamilies {
1174 LS_FAMILY_OFS,
1175 LS_FAMILY_BOOL,
1176 LS_FAMILY_COMP,
1177 LS_FAMILY_DIFF,
1178 LS_FAMILY_TIMER,
1179 LS_FAMILY_STICKY,
1180 LS_FAMILY_RANGE,
1181 LS_FAMILY_EDGE
1184 uint8_t lswFamily(uint8_t func);
1185 int16_t lswTimerValue(delayval_t val);
1187 enum FunctionsActive {
1188 FUNCTION_TRAINER,
1189 FUNCTION_INSTANT_TRIM = FUNCTION_TRAINER+4,
1190 FUNCTION_VARIO,
1191 FUNCTION_BACKLIGHT,
1192 #if defined(SDCARD)
1193 FUNCTION_LOGS,
1194 #endif
1195 #if defined(CPUARM)
1196 FUNCTION_BACKGND_MUSIC,
1197 FUNCTION_BACKGND_MUSIC_PAUSE,
1198 #endif
1201 #define VARIO_FREQUENCY_ZERO 700/*Hz*/
1202 #define VARIO_FREQUENCY_RANGE 1000/*Hz*/
1203 #define VARIO_REPEAT_ZERO 500/*ms*/
1204 #define VARIO_REPEAT_MAX 80/*ms*/
1206 #if defined(CPUARM)
1207 extern CustomFunctionsContext modelFunctionsContext;
1208 extern CustomFunctionsContext globalFunctionsContext;
1209 inline bool isFunctionActive(uint8_t func)
1211 return globalFunctionsContext.isFunctionActive(func) || modelFunctionsContext.isFunctionActive(func);
1213 void evalFunctions(const CustomFunctionData * functions, CustomFunctionsContext & functionsContext);
1214 inline void customFunctionsReset()
1216 globalFunctionsContext.reset();
1217 modelFunctionsContext.reset();
1219 #else
1220 extern CustomFunctionsContext modelFunctionsContext;
1221 #define isFunctionActive(func) modelFunctionsContext.isFunctionActive(func)
1222 void evalFunctions();
1223 #define customFunctionsReset() modelFunctionsContext.reset()
1224 #endif
1226 #include "telemetry/telemetry.h"
1228 #if defined(CPUARM)
1229 uint8_t crc8(const uint8_t * ptr, uint32_t len);
1230 uint16_t crc16(const uint8_t * ptr, uint32_t len);
1231 #endif
1233 #define PLAY_REPEAT(x) (x) /* Range 0 to 15 */
1234 #define PLAY_NOW 0x10
1235 #define PLAY_BACKGROUND 0x20
1236 #define PLAY_INCREMENT(x) ((uint8_t)(((uint8_t)x) << 6)) /* -1, 0, 1, 2 */
1238 enum AUDIO_SOUNDS {
1239 AUDIO_HELLO,
1240 #if defined(CPUARM)
1241 AU_BYE,
1242 #endif
1243 #if defined(VOICE)
1244 AU_THROTTLE_ALERT,
1245 AU_SWITCH_ALERT,
1246 AU_BAD_RADIODATA,
1247 #endif
1248 AU_TX_BATTERY_LOW,
1249 AU_INACTIVITY,
1250 #if defined(CPUARM)
1251 AU_RSSI_ORANGE,
1252 AU_RSSI_RED,
1253 AU_SWR_RED,
1254 AU_TELEMETRY_LOST,
1255 AU_TELEMETRY_BACK,
1256 AU_TRAINER_LOST,
1257 AU_TRAINER_BACK,
1258 AU_SENSOR_LOST,
1259 AU_SERVO_KO,
1260 AU_RX_OVERLOAD,
1261 AU_MODEL_STILL_POWERED,
1262 #endif
1263 #if defined(PCBSKY9X)
1264 AU_TX_MAH_HIGH,
1265 AU_TX_TEMP_HIGH,
1266 #endif
1267 AU_ERROR,
1268 AU_WARNING1,
1269 AU_WARNING2,
1270 AU_WARNING3,
1271 AU_TRIM_MIDDLE,
1272 #if defined(CPUARM)
1273 AU_TRIM_MIN,
1274 AU_TRIM_MAX,
1275 #endif
1276 #if defined(CPUARM)
1277 AU_STICK1_MIDDLE,
1278 AU_STICK2_MIDDLE,
1279 AU_STICK3_MIDDLE,
1280 AU_STICK4_MIDDLE,
1281 #endif
1282 #if defined(PCBTARANIS) || defined(PCBFLAMENCO) || defined(PCBHORUS)
1283 AU_POT1_MIDDLE,
1284 AU_POT2_MIDDLE,
1285 AU_SLIDER1_MIDDLE,
1286 AU_SLIDER2_MIDDLE,
1287 #if defined(PCBX9E)
1288 AU_SLIDER3_MIDDLE,
1289 AU_SLIDER4_MIDDLE,
1290 #endif
1291 #elif defined(CPUARM)
1292 AU_POT1_MIDDLE,
1293 AU_POT2_MIDDLE,
1294 AU_POT3_MIDDLE,
1295 #else
1296 AU_POT_MIDDLE,
1297 #endif
1298 AU_MIX_WARNING_1,
1299 AU_MIX_WARNING_2,
1300 AU_MIX_WARNING_3,
1301 #if defined(CPUARM)
1302 AU_TIMER1_ELAPSED,
1303 AU_TIMER2_ELAPSED,
1304 AU_TIMER3_ELAPSED,
1305 #endif
1307 AU_SPECIAL_SOUND_FIRST,
1308 AU_SPECIAL_SOUND_BEEP1 = AU_SPECIAL_SOUND_FIRST,
1309 AU_SPECIAL_SOUND_BEEP2,
1310 AU_SPECIAL_SOUND_BEEP3,
1311 AU_SPECIAL_SOUND_WARN1,
1312 AU_SPECIAL_SOUND_WARN2,
1313 AU_SPECIAL_SOUND_CHEEP,
1314 AU_SPECIAL_SOUND_RATATA,
1315 AU_SPECIAL_SOUND_TICK,
1316 AU_SPECIAL_SOUND_SIREN,
1317 AU_SPECIAL_SOUND_RING,
1318 AU_SPECIAL_SOUND_SCIFI,
1319 AU_SPECIAL_SOUND_ROBOT,
1320 AU_SPECIAL_SOUND_CHIRP,
1321 AU_SPECIAL_SOUND_TADA,
1322 AU_SPECIAL_SOUND_CRICKET,
1323 AU_SPECIAL_SOUND_ALARMC,
1324 AU_SPECIAL_SOUND_LAST,
1326 AU_NONE=0xff
1329 #if defined(AUDIO)
1330 #if defined(CPUARM)
1331 #include "audio_arm.h"
1332 #else
1333 #include "audio_avr.h"
1334 #endif
1335 #endif
1337 #include "buzzer.h"
1339 #if defined(PCBSTD) && defined(VOICE)
1340 #include "targets/9x/voice.h"
1341 #endif
1343 #if defined(PCBGRUVIN9X) && defined(VOICE)
1344 #include "targets/gruvin9x/voice.h"
1345 #endif
1347 #if defined(PCBMEGA2560) && defined(VOICE)
1348 #include "targets/mega2560/voice.h"
1349 #endif
1351 #include "translations.h"
1352 #include "fonts.h"
1354 #if defined(HAPTIC)
1355 #include "haptic.h"
1356 #endif
1358 #if defined(SDCARD)
1359 #include "sdcard.h"
1360 #endif
1362 #if defined(RTCLOCK)
1363 #include "rtc.h"
1364 #endif
1366 #if defined(REVX)
1367 void setMFP();
1368 void clearMFP();
1369 #endif
1371 #if defined(CPUARM)
1372 extern uint8_t requiredSpeakerVolume;
1373 #endif
1375 #if defined(CPUARM)
1376 enum MainRequest {
1377 REQUEST_SCREENSHOT,
1378 REQUEST_FLIGHT_RESET,
1381 extern uint8_t mainRequestFlags;
1382 #endif
1384 void checkBattery();
1385 void opentxClose(uint8_t shutdown=true);
1386 void opentxInit();
1387 void opentxResume();
1389 #if defined(PCBHORUS) || defined(PCBX7)
1390 #define LED_ERROR_BEGIN() ledRed()
1391 #define LED_ERROR_END() ledBlue()
1392 #else
1393 #define LED_ERROR_BEGIN()
1394 #define LED_ERROR_END()
1395 #endif
1397 // Re-useable byte array to save having multiple buffers
1398 #if LCD_W <= 212
1399 #define SD_SCREEN_FILE_LENGTH 32
1400 #else
1401 #define SD_SCREEN_FILE_LENGTH 64
1402 #endif
1403 union ReusableBuffer
1405 // 275 bytes
1406 struct
1408 #if !defined(CPUARM)
1409 char listnames[NUM_BODY_LINES][LEN_MODEL_NAME];
1410 #endif
1411 #if defined(EEPROM_RLC) && LCD_W < 212
1412 uint16_t eepromfree;
1413 #endif
1414 #if defined(SDCARD)
1415 char menu_bss[POPUP_MENU_MAX_LINES][MENU_LINE_LENGTH];
1416 char mainname[45]; // because reused for SD backup / restore, max backup filename 44 chars: "/MODELS/MODEL0134353-2014-06-19-04-51-27.bin"
1417 #else
1418 char mainname[LEN_MODEL_NAME];
1419 #endif
1420 } modelsel;
1422 struct {
1423 char msg[64];
1424 } msgbuf; // used in modelsel and modelsetup (only in a warning message)
1426 // 103 bytes
1427 struct
1429 int16_t midVals[NUM_STICKS+NUM_POTS+NUM_SLIDERS+NUM_MOUSE_ANALOGS];
1430 int16_t loVals[NUM_STICKS+NUM_POTS+NUM_SLIDERS+NUM_MOUSE_ANALOGS];
1431 int16_t hiVals[NUM_STICKS+NUM_POTS+NUM_SLIDERS+NUM_MOUSE_ANALOGS];
1432 uint8_t state;
1433 #if defined(PCBTARANIS) || defined(PCBFLAMENCO) || defined(PCBHORUS)
1434 struct {
1435 uint8_t stepsCount;
1436 int16_t steps[XPOTS_MULTIPOS_COUNT];
1437 uint8_t lastCount;
1438 int16_t lastPosition;
1439 } xpotsCalib[NUM_XPOTS];
1440 #endif
1441 } calib;
1443 #if defined(SDCARD)
1444 // 274 bytes
1445 struct
1447 char lines[NUM_BODY_LINES][SD_SCREEN_FILE_LENGTH+1+1]; // the last char is used to store the flags (directory) of the line
1448 uint32_t available;
1449 uint16_t offset;
1450 uint16_t count;
1451 char originalName[SD_SCREEN_FILE_LENGTH+1];
1452 } sdmanager;
1453 #endif
1455 #if defined(STM32)
1456 struct
1458 char id[27];
1459 } version;
1460 #endif
1461 struct
1463 uint8_t stickMode;
1464 } generalSettings;
1467 extern union ReusableBuffer reusableBuffer;
1469 void checkFlashOnBeep();
1471 #if defined(CPUARM)
1472 uint8_t zlen(const char *str, uint8_t size);
1473 bool zexist(const char *str, uint8_t size);
1474 unsigned int effectiveLen(const char * str, unsigned int size);
1475 char * strcat_zchar(char *dest, const char *name, uint8_t size, const char *defaultName=NULL, uint8_t defaultNameSize=0, uint8_t defaultIdx=0);
1476 #define strcat_phasename(dest, idx) strcat_zchar(dest, g_model.flightModeData[idx].name, LEN_FLIGHT_MODE_NAME, STR_FP, PSIZE(TR_FP), idx+1)
1477 #if defined(EEPROM)
1478 #define strcat_modelname(dest, idx) strcat_zchar(dest, modelHeaders[idx].name, LEN_MODEL_NAME, STR_MODEL, PSIZE(TR_MODEL), idx+1)
1479 #define strcat_currentmodelname(dest) strcat_modelname(dest, g_eeGeneral.currModel)
1480 #else
1481 #define strcat_currentmodelname(dest) strcat_zchar(dest, g_model.header.name, LEN_MODEL_NAME)
1482 #endif
1483 #define ZLEN(s) zlen(s, sizeof(s))
1484 #define ZEXIST(s) zexist(s, sizeof(s))
1485 #endif
1487 // Stick tolerance varies between transmitters, Higher is better
1488 #if defined (PCB9XR) || defined (PCB9XR128)
1489 #define STICK_TOLERANCE 16
1490 #else
1491 #define STICK_TOLERANCE 64
1492 #endif
1494 #if defined(FRSKY_HUB) && defined(GAUGES)
1495 enum BarThresholdIdx {
1496 THLD_ALT,
1497 THLD_RPM,
1498 THLD_FUEL,
1499 THLD_T1,
1500 THLD_T2,
1501 THLD_SPEED,
1502 THLD_DIST,
1503 THLD_GPSALT,
1504 THLD_CELL,
1505 THLD_CELLS_SUM,
1506 THLD_VFAS,
1507 THLD_CURRENT,
1508 THLD_CONSUMPTION,
1509 THLD_MAX,
1512 #if defined(CPUARM)
1513 #define FILL_THRESHOLD(idx, val) barsThresholds[idx] = (val)
1514 #else
1515 #define FILL_THRESHOLD(idx, val) barsThresholds[idx] = 128 + (val)
1516 #endif
1518 extern bar_threshold_t barsThresholds[THLD_MAX];
1519 #else
1520 #define FILL_THRESHOLD(idx, val)
1521 #endif
1523 #if defined(TELEMETRY_FRSKY)
1524 ls_telemetry_value_t minTelemValue(source_t channel);
1525 ls_telemetry_value_t maxTelemValue(source_t channel);
1526 #else
1527 #define minTelemValue(channel) 255
1528 #define maxTelemValue(channel) 255
1529 #endif
1531 #if defined(CPUARM)
1532 getvalue_t convert16bitsTelemValue(source_t channel, ls_telemetry_value_t value);
1533 ls_telemetry_value_t max8bitsTelemValue(source_t channel);
1534 #endif
1536 getvalue_t convert8bitsTelemValue(source_t channel, ls_telemetry_value_t value);
1537 getvalue_t convertLswTelemValue(LogicalSwitchData * cs);
1539 #if defined(CPUARM)
1540 #define convertTelemValue(channel, value) convert16bitsTelemValue(channel, value)
1541 #define convertBarTelemValue(channel, value) convert8bitsTelemValue(channel, value)
1542 #define maxBarTelemValue(channel) max8bitsTelemValue(channel)
1543 #else
1544 #define convertTelemValue(channel, value) convert8bitsTelemValue(channel, value)
1545 #define convertBarTelemValue(channel, value) convert8bitsTelemValue(channel, value)
1546 #define maxBarTelemValue(channel) maxTelemValue(channel)
1547 #endif
1549 #if defined(TELEMETRY_FRSKY) || defined(CPUARM)
1550 lcdint_t applyChannelRatio(source_t channel, lcdint_t val);
1551 #define ANA_CHANNEL_UNIT(channel) g_model.frsky.channels[channel].type
1552 #endif
1554 inline int div_and_round(int num, int den)
1556 if (den == 0) {
1557 return 0;
1559 else if (num >= 0) {
1560 num += den / 2;
1562 else {
1563 num -= den / 2;
1565 return num / den;
1568 #if defined(TELEMETRY_FRSKY)
1569 #if !defined(CPUARM)
1570 NOINLINE uint8_t getRssiAlarmValue(uint8_t alarm);
1571 #endif
1573 extern const pm_uint8_t bchunit_ar[];
1575 #if defined(CPUARM)
1576 #define FRSKY_MULTIPLIER_MAX 5
1577 #else
1578 #define FRSKY_MULTIPLIER_MAX 3
1579 #endif
1581 enum TelemetryViews {
1582 TELEMETRY_CUSTOM_SCREEN_1,
1583 TELEMETRY_CUSTOM_SCREEN_2,
1584 #if defined(CPUARM)
1585 TELEMETRY_CUSTOM_SCREEN_3,
1586 TELEMETRY_CUSTOM_SCREEN_4,
1587 TELEMETRY_VIEW_MAX = TELEMETRY_CUSTOM_SCREEN_4
1588 #else
1589 TELEMETRY_VOLTAGES_SCREEN,
1590 TELEMETRY_AFTER_FLIGHT_SCREEN,
1591 TELEMETRY_VIEW_MAX = TELEMETRY_AFTER_FLIGHT_SCREEN
1592 #endif
1595 extern uint8_t s_frsky_view;
1597 #endif
1599 #define EARTH_RADIUSKM ((uint32_t)6371)
1600 #define EARTH_RADIUS ((uint32_t)111194) // meters * pi / 180°
1602 void getGpsPilotPosition();
1603 void getGpsDistance();
1604 void varioWakeup();
1606 #if defined(AUDIO) && defined(BUZZER)
1607 #define IS_SOUND_OFF() (g_eeGeneral.buzzerMode==e_mode_quiet && g_eeGeneral.beepMode==e_mode_quiet)
1608 #else
1609 #define IS_SOUND_OFF() (g_eeGeneral.beepMode == e_mode_quiet)
1610 #endif
1612 #if defined(CPUARM)
1613 #define IS_IMPERIAL_ENABLE() (g_eeGeneral.imperial)
1614 #elif defined(IMPERIAL_UNITS)
1615 #define IS_IMPERIAL_ENABLE() (1)
1616 #else
1617 #define IS_IMPERIAL_ENABLE() (0)
1618 #endif
1620 #if defined(CPUARM)
1621 #elif defined(TELEMETRY_FRSKY)
1622 FORCEINLINE void convertUnit(getvalue_t & val, uint8_t & unit)
1624 if (IS_IMPERIAL_ENABLE()) {
1625 if (unit == UNIT_TEMPERATURE) {
1626 val += 18;
1627 val *= 115;
1628 val >>= 6;
1630 if (unit == UNIT_DIST) {
1631 // m to ft *105/32
1632 val = val * 3 + (val >> 2) + (val >> 5);
1634 if (unit == UNIT_FEET) {
1635 unit = UNIT_DIST;
1637 if (unit == UNIT_KTS) {
1638 // kts to mph
1639 unit = UNIT_SPEED;
1640 val = (val * 23) / 20;
1643 else {
1644 if (unit == UNIT_KTS) {
1645 // kts to km/h
1646 unit = UNIT_SPEED;
1647 val = (val * 50) / 27;
1651 if (unit == UNIT_HDG) {
1652 unit = UNIT_TEMPERATURE;
1655 #else
1656 #define convertUnit(...)
1657 #endif
1659 #if !defined(CPUARM)
1660 #define IS_USR_PROTO_FRSKY_HUB() (g_model.frsky.usrProto == USR_PROTO_FRSKY)
1661 #define IS_USR_PROTO_WS_HOW_HIGH() (g_model.frsky.usrProto == USR_PROTO_WS_HOW_HIGH)
1662 #endif
1664 #if defined(TELEMETRY_FRSKY) && defined(FRSKY_HUB) && defined(GPS)
1665 #define IS_GPS_AVAILABLE() IS_USR_PROTO_FRSKY_HUB()
1666 #else
1667 #define IS_GPS_AVAILABLE() (0)
1668 #endif
1670 #if defined(PCBTARANIS)
1671 extern const pm_uchar logo_taranis[];
1672 #endif
1674 #if defined(USB_MASS_STORAGE)
1675 void usbPluggedIn();
1676 #endif
1678 #include "lua/lua_api.h"
1680 #if defined(SDCARD)
1681 enum ClipboardType {
1682 CLIPBOARD_TYPE_NONE,
1683 CLIPBOARD_TYPE_CUSTOM_SWITCH,
1684 CLIPBOARD_TYPE_CUSTOM_FUNCTION,
1685 CLIPBOARD_TYPE_SD_FILE,
1688 #if defined(SIMU)
1689 #define CLIPBOARD_PATH_LEN 1024
1690 #else
1691 #define CLIPBOARD_PATH_LEN 32
1692 #endif
1694 struct Clipboard {
1695 ClipboardType type;
1696 union {
1697 LogicalSwitchData csw;
1698 CustomFunctionData cfn;
1699 struct {
1700 char directory[CLIPBOARD_PATH_LEN];
1701 char filename[CLIPBOARD_PATH_LEN];
1702 } sd;
1703 } data;
1706 extern Clipboard clipboard;
1707 #endif
1709 #if !defined(SIMU)
1710 extern uint16_t s_anaFilt[NUM_ANALOGS];
1711 #endif
1713 #if defined(JITTER_MEASURE)
1714 extern JitterMeter<uint16_t> rawJitter[NUM_ANALOGS];
1715 extern JitterMeter<uint16_t> avgJitter[NUM_ANALOGS];
1716 #if defined(PCBHORUS)
1717 #define JITTER_MEASURE_ACTIVE() (menuHandlers[menuLevel] == menuStatsAnalogs)
1718 #elif defined(PCBTARANIS)
1719 #define JITTER_MEASURE_ACTIVE() (menuHandlers[menuLevel] == menuRadioDiagAnalogs)
1720 #elif defined(CLI)
1721 #define JITTER_MEASURE_ACTIVE() (1)
1722 #else
1723 #define JITTER_MEASURE_ACTIVE() (0)
1724 #endif
1725 #endif
1727 #if defined(INTERNAL_GPS)
1728 #include "gps.h"
1729 #endif
1731 #if defined(BLUETOOTH)
1732 #include "bluetooth.h"
1733 #endif
1735 #endif // _OPENTX_H_