B&W LCD receiver ID improvement (#5018)
[opentx.git] / radio / src / opentx.h
blob7fa7674fc3c92e07745d54b62b373352f020a9b3
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(VARIO) && defined(CPUARM)
54 #define CASE_VARIO_CPUARM(x) x,
55 #else
56 #define CASE_VARIO_CPUARM(x)
57 #endif
59 #if defined(LUA)
60 #define CASE_LUA(x) x,
61 #else
62 #define CASE_LUA(x)
63 #endif
65 #if defined(CPUARM) || defined(CPUM2560)
66 #define CASE_PERSISTENT_TIMERS(x) x,
67 #else
68 #define CASE_PERSISTENT_TIMERS(x)
69 #endif
71 #if defined(RTCLOCK)
72 #define CASE_RTCLOCK(x) x,
73 #else
74 #define CASE_RTCLOCK(x)
75 #endif
77 #if defined(BUZZER)
78 #define CASE_BUZZER(x) x,
79 #else
80 #define CASE_BUZZER(x)
81 #endif
83 #if defined(AUDIO)
84 #define CASE_AUDIO(x) x,
85 #else
86 #define CASE_AUDIO(x)
87 #endif
89 #if defined(VOICE)
90 #define CASE_VOICE(x) x,
91 #else
92 #define CASE_VOICE(x)
93 #endif
95 #if defined(PWM_BACKLIGHT)
96 #define CASE_PWM_BACKLIGHT(x) x,
97 #else
98 #define CASE_PWM_BACKLIGHT(x)
99 #endif
101 #if defined(TELEMETRY_FRSKY) && defined(FRSKY_HUB) && defined(GPS)
102 #define CASE_GPS(x) x,
103 #else
104 #define CASE_GPS(x)
105 #endif
107 #if defined(VARIO)
108 #define CASE_VARIO(x) x,
109 #else
110 #define CASE_VARIO(x)
111 #endif
113 #if defined(HAPTIC)
114 #define CASE_HAPTIC(x) x,
115 #else
116 #define CASE_HAPTIC(x)
117 #endif
119 #if defined(SPLASH)
120 #define CASE_SPLASH(x) x,
121 #else
122 #define CASE_SPLASH(x)
123 #endif
125 #if defined(TELEMETRY_FRSKY)
126 #define CASE_FRSKY(x) x,
127 #else
128 #define CASE_FRSKY(x)
129 #endif
131 #if defined(TELEMETRY_MAVLINK)
132 #define CASE_MAVLINK(x) x,
133 #else
134 #define CASE_MAVLINK(x)
135 #endif
137 #if defined(PXX)
138 #define CASE_PXX(x) x,
139 #else
140 #define CASE_PXX(x)
141 #endif
143 #if defined(SDCARD)
144 #define CASE_SDCARD(x) x,
145 #else
146 #define CASE_SDCARD(x)
147 #endif
149 #if defined(BLUETOOTH) && !(defined(PCBX9E) && !defined(DEBUG))
150 #define CASE_BLUETOOTH(x) x,
151 #else
152 #define CASE_BLUETOOTH(x)
153 #endif
155 #if defined(HELI)
156 #define CASE_HELI(x) x,
157 #else
158 #define CASE_HELI(x)
159 #endif
161 #if defined(TEMPLATES)
162 #define CASE_TEMPLATES(x) x,
163 #else
164 #define CASE_TEMPLATES(x)
165 #endif
167 #if defined(FLIGHT_MODES)
168 #define CASE_FLIGHT_MODES(x) x,
169 #else
170 #define CASE_FLIGHT_MODES(x)
171 #endif
173 #if defined(CURVES)
174 #define CASE_CURVES(x) x,
175 #else
176 #define CASE_CURVES(x)
177 #endif
179 #if defined(GVARS)
180 #define CASE_GVARS(x) x,
181 #else
182 #define CASE_GVARS(x)
183 #endif
185 #if defined(PCBX9DP) || defined(PCBX9E)
186 #define CASE_PCBX9E_PCBX9DP(x) x,
187 #else
188 #define CASE_PCBX9E_PCBX9DP(x)
189 #endif
191 #if defined(PCBX9E)
192 #define CASE_PCBX9E(x) x,
193 #else
194 #define CASE_PCBX9E(x)
195 #endif
197 #if defined(BLUETOOTH) && (!defined(PCBX9E) || defined(DEBUG))
198 #define CASE_BLUETOOTH(x) x,
199 #else
200 #define CASE_BLUETOOTH(x)
201 #endif
203 #if defined(PCBSKY9X) && !defined(AR9X) && !defined(REVA)
204 #define TX_CAPACITY_MEASUREMENT
205 #define CASE_CAPACITY(x) x,
206 #else
207 #define CASE_CAPACITY(x)
208 #endif
210 #if ROTARY_ENCODERS > 0
211 #define ROTARY_ENCODER_NAVIGATION
212 #endif
214 #if defined(FAI)
215 #define IS_FAI_ENABLED() true
216 #define IF_FAI_CHOICE(x)
217 #elif defined(FAI_CHOICE)
218 #define IS_FAI_ENABLED() g_eeGeneral.fai
219 #define IF_FAI_CHOICE(x) x,
220 #else
221 #define IS_FAI_ENABLED() false
222 #define IF_FAI_CHOICE(x)
223 #endif
225 #define IS_FAI_FORBIDDEN(idx) (IS_FAI_ENABLED() && idx >= MIXSRC_FIRST_TELEM)
227 #if defined(BLUETOOTH)
228 #if defined(X9E)
229 #define IS_BLUETOOTH_TRAINER() (g_model.trainerMode == TRAINER_MODE_SLAVE_BLUETOOTH)
230 #else
231 #define IS_BLUETOOTH_TRAINER() (g_model.trainerMode == TRAINER_MODE_MASTER_BLUETOOTH || g_model.trainerMode == TRAINER_MODE_SLAVE_BLUETOOTH)
232 #endif
233 #else
234 #define IS_BLUETOOTH_TRAINER() false
235 #endif
237 #if defined(CPUARM)
238 #define MASTER_VOLUME
239 #endif
241 #if !defined(CPUM64) && !defined(ACCURAT_THROTTLE_TIMER)
242 // code cost is about 16 bytes for higher throttle accuracy for timer
243 // would not be noticable anyway, because all version up to this change had only 16 steps;
244 // now it has already 32 steps; this define would increase to 128 steps
245 #if !defined(ACCURAT_THROTTLE_TIMER)
246 #define ACCURAT_THROTTLE_TIMER
247 #endif
248 #endif
250 // 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
251 #define RESX_SHIFT 10
252 #define RESX 1024
253 #define RESXu 1024u
254 #define RESXul 1024ul
255 #define RESXl 1024l
257 #include "board.h"
259 #if defined(DISK_CACHE)
260 #include "disk_cache.h"
261 #endif
263 #if defined(SIMU)
264 #include "targets/simu/simpgmspace.h"
265 #elif defined(CPUARM)
266 typedef const unsigned char pm_uchar;
267 typedef const char pm_char;
268 typedef const uint16_t pm_uint16_t;
269 typedef const uint8_t pm_uint8_t;
270 typedef const int16_t pm_int16_t;
271 typedef const int8_t pm_int8_t;
272 #define pgm_read_byte(address_short) (*(uint8_t*)(address_short))
273 #define PSTR(adr) adr
274 #define PROGMEM
275 #define pgm_read_adr(x) *(x)
276 #define cli()
277 #define sei()
278 #endif
280 #include "debug.h"
282 #if defined(PCBFLAMENCO)
283 #elif defined(PCBTARANIS) || defined(PCBHORUS)
284 #define SWSRC_THR SWSRC_SF2
285 #define SWSRC_GEA SWSRC_SG2
286 #define SWSRC_ID0 SWSRC_SA0
287 #define SWSRC_ID1 SWSRC_SA1
288 #define SWSRC_ID2 SWSRC_SA2
289 #define IS_MOMENTARY(sw) false // TODO
290 #else
291 #define SW_DSM2_BIND SW_TRN
292 #endif
294 #define NUM_PSWITCH (SWSRC_LAST_SWITCH-SWSRC_FIRST_SWITCH+1)
296 #include "myeeprom.h"
298 #if defined(CPUM64)
299 void memclear(void * ptr, uint8_t size);
300 #else
301 #define memclear(p, s) memset(p, 0, s)
302 #endif
304 void memswap(void * a, void * b, uint8_t size);
306 #if defined(PCBX9D) || defined(PCBX9DP) || defined(PCBX9E) || defined(PCBHORUS)
307 #define POT_CONFIG(x) ((g_eeGeneral.potsConfig >> (2*((x)-POT1)))&0x03)
308 #define IS_POT_MULTIPOS(x) (IS_POT(x) && POT_CONFIG(x)==POT_MULTIPOS_SWITCH)
309 #define IS_POT_WITHOUT_DETENT(x) (IS_POT(x) && POT_CONFIG(x)==POT_WITHOUT_DETENT)
310 #define IS_SLIDER_AVAILABLE(x) ((x) == SLIDER1 || (x) == SLIDER2 || (IS_SLIDER(x) && (g_eeGeneral.slidersConfig & (0x01 << ((x)-SLIDER1)))))
311 #define IS_POT_AVAILABLE(x) (IS_POT(x) && POT_CONFIG(x)!=POT_NONE)
312 #define IS_POT_SLIDER_AVAILABLE(x) (IS_POT_AVAILABLE(x) || IS_SLIDER_AVAILABLE(x))
313 #define IS_MULTIPOS_CALIBRATED(cal) (cal->count>0 && cal->count<XPOTS_MULTIPOS_COUNT)
314 #elif defined(PCBX7)
315 #define IS_POT_MULTIPOS(x) (false)
316 #define IS_POT_WITHOUT_DETENT(x) (false)
317 #define IS_POT_SLIDER_AVAILABLE(x) (true)
318 #define IS_MULTIPOS_CALIBRATED(cal) (false)
319 #elif defined(PCBFLAMENCO)
320 #define IS_POT_MULTIPOS(x) (false)
321 #define IS_POT_WITHOUT_DETENT(x) (false)
322 #define IS_POT_SLIDER_AVAILABLE(x) (true)
323 #define IS_MULTIPOS_CALIBRATED(cal) (false)
324 #else
325 #define IS_POT_MULTIPOS(x) (false)
326 #define IS_POT_WITHOUT_DETENT(x) (true)
327 #define IS_POT_SLIDER_AVAILABLE(x) (true)
328 #define IS_MULTIPOS_CALIBRATED(cal) (false)
329 #endif
331 #if defined(PWR_BUTTON_PRESS)
332 #define pwrOffPressed() pwrPressed()
333 #else
334 #define pwrOffPressed() (!pwrPressed())
335 #endif
337 #define PWR_PRESS_SHUTDOWN_DELAY 300 // 3s
339 #define GET_LOWRES_POT_POSITION(i) (getValue(MIXSRC_FIRST_POT+(i)) >> 4)
340 #define SAVE_POT_POSITION(i) g_model.potsWarnPosition[i] = GET_LOWRES_POT_POSITION(i)
342 #if ROTARY_ENCODERS > 0
343 #define IF_ROTARY_ENCODERS(x) x,
344 #else
345 #define IF_ROTARY_ENCODERS(x)
346 #endif
348 #define PPM_CENTER 1500
350 #if defined(PPM_CENTER_ADJUSTABLE)
351 #define PPM_CH_CENTER(ch) (PPM_CENTER + limitAddress(ch)->ppmCenter)
352 #else
353 #define PPM_CH_CENTER(ch) (PPM_CENTER)
354 #endif
356 #if defined(CPUARM)
357 #include "fifo.h"
358 #include "io/io_arm.h"
359 // This doesn't need protection on this processor
360 extern volatile tmr10ms_t g_tmr10ms;
361 #define get_tmr10ms() g_tmr10ms
362 #else
363 extern volatile tmr10ms_t g_tmr10ms;
364 extern inline uint16_t get_tmr10ms()
366 uint16_t time ;
367 cli();
368 time = g_tmr10ms ;
369 sei();
370 return time ;
372 #endif
374 #if defined(NAVIGATION_STICKS)
375 extern uint8_t StickScrollAllowed;
376 extern uint8_t StickScrollTimer;
377 #define STICK_SCROLL_TIMEOUT 9
378 #define STICK_SCROLL_DISABLE() StickScrollAllowed = 0
379 #else
380 #define STICK_SCROLL_DISABLE()
381 #endif
383 #if defined(CLI)
384 #include "cli.h"
385 #endif
387 #include "timers.h"
388 #include "storage/storage.h"
389 #include "pulses/pulses.h"
391 #if defined(CPUARM)
392 // Order is the same as in enum Protocols in myeeprom.h (none, ppm, xjt, dsm, crossfire, multi, r9m)
393 static const int8_t maxChannelsModules[] = { 0, 8, 8, -2, 8, 4, 8}; // relative to 8!
394 static const int8_t maxChannelsXJT[] = { 0, 8, 0, 4 }; // relative to 8!
395 #define MAX_TRAINER_CHANNELS_M8() (MAX_TRAINER_CHANNELS-8)
396 #endif
398 #if defined(MULTIMODULE)
399 #define IS_MODULE_MULTIMODULE(idx) (idx==EXTERNAL_MODULE && g_model.moduleData[EXTERNAL_MODULE].type==MODULE_TYPE_MULTIMODULE)
400 #else
401 #define IS_MODULE_MULTIMODULE(idx) (false)
402 #endif
404 #if defined(PCBTARANIS) || defined(PCBHORUS)
405 #if defined(TARANIS_INTERNAL_PPM)
406 #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))
407 #else
408 #define IS_MODULE_PPM(idx) (idx==TRAINER_MODULE || (idx==EXTERNAL_MODULE && g_model.moduleData[EXTERNAL_MODULE].type==MODULE_TYPE_PPM))
409 #endif
410 #define IS_MODULE_XJT(idx) (g_model.moduleData[idx].type==MODULE_TYPE_XJT)
411 #if defined(DSM2)
412 #define IS_MODULE_DSM2(idx) (idx==EXTERNAL_MODULE && g_model.moduleData[EXTERNAL_MODULE].type==MODULE_TYPE_DSM2)
413 #else
414 #define IS_MODULE_DSM2(idx) (false)
415 #endif
416 #if defined(CROSSFIRE)
417 #define IS_MODULE_CROSSFIRE(idx) (idx==EXTERNAL_MODULE && g_model.moduleData[EXTERNAL_MODULE].type==MODULE_TYPE_CROSSFIRE)
418 #else
419 #define IS_MODULE_CROSSFIRE(idx) (false)
420 #endif
421 #if defined(TARANIS_INTERNAL_PPM)
422 #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])
423 #else
424 #define MAX_INTERNAL_MODULE_CHANNELS() (maxChannelsXJT[1+g_model.moduleData[INTERNAL_MODULE].rfProtocol])
425 #endif
426 #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])
427 #define MAX_CHANNELS(idx) (idx==INTERNAL_MODULE ? MAX_INTERNAL_MODULE_CHANNELS() : (idx==EXTERNAL_MODULE ? MAX_EXTERNAL_MODULE_CHANNELS() : MAX_TRAINER_CHANNELS_M8()))
428 #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))
429 #elif defined(PCBSKY9X) && !defined(REVA)
430 #define IS_MODULE_PPM(idx) (idx==TRAINER_MODULE || idx==EXTRA_MODULE || (idx==EXTERNAL_MODULE && g_model.moduleData[EXTERNAL_MODULE].type==MODULE_TYPE_PPM))
431 #define IS_MODULE_XJT(idx) (idx==EXTERNAL_MODULE && g_model.moduleData[EXTERNAL_MODULE].type==MODULE_TYPE_XJT)
432 #define IS_MODULE_DSM2(idx) (idx==EXTERNAL_MODULE && g_model.moduleData[EXTERNAL_MODULE].type==MODULE_TYPE_DSM2)
433 #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])
434 #define MAX_EXTRA_MODULE_CHANNELS() (8) // Only PPM (16ch PPM)
435 #define MAX_CHANNELS(idx) (idx==EXTERNAL_MODULE ? MAX_EXTERNAL_MODULE_CHANNELS() : (idx==EXTRA_MODULE ? MAX_EXTRA_MODULE_CHANNELS() : MAX_TRAINER_CHANNELS_M8()))
436 #define NUM_CHANNELS(idx) (8+g_model.moduleData[idx].channelsCount)
437 #define IS_MODULE_CROSSFIRE(idx) (false)
438 #else
439 #define IS_MODULE_PPM(idx) (idx==TRAINER_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[EXTERNAL_MODULE].rfProtocol] : maxChannelsModules[g_model.moduleData[EXTERNAL_MODULE].type])
443 #define MAX_CHANNELS(idx) (idx==EXTERNAL_MODULE ? MAX_EXTERNAL_MODULE_CHANNELS() : MAX_TRAINER_CHANNELS_M8())
444 #define NUM_CHANNELS(idx) (8+g_model.moduleData[idx].channelsCount)
445 #endif
446 #define IS_MODULE_R9M(idx) (g_model.moduleData[idx].type==MODULE_TYPE_R9M)
447 #define IS_MODULE_PXX(idx) (IS_MODULE_XJT(idx) || IS_MODULE_R9M(idx))
450 #if defined(MULTIMODULE)
451 #define IS_MULTIMODULE_DSM(idx) (IS_MODULE_MULTIMODULE(idx) && g_model.moduleData[idx].getMultiProtocol(true) == MM_RF_PROTO_DSM2)
452 #define DEFAULT_CHANNELS(idx) (IS_MODULE_PPM(idx) ? 0 : IS_MULTIMODULE_DSM(idx) ? -1 : MAX_CHANNELS(idx))
453 #else
454 #define DEFAULT_CHANNELS(idx) (IS_MODULE_PPM(idx) ? 0 : MAX_CHANNELS(idx))
455 #endif
457 #if defined(CPUARM)
458 #define MASK_CFN_TYPE uint64_t // current max = 64 function switches
459 #define MASK_FUNC_TYPE uint32_t // current max = 32 functions
460 #elif defined(CPUM64)
461 #define MASK_CFN_TYPE uint16_t // current max = 16 function switches
462 #define MASK_FUNC_TYPE uint8_t // current max = 8 functions
463 #else
464 #define MASK_CFN_TYPE uint32_t // current max = 32 function switches
465 #define MASK_FUNC_TYPE uint8_t // current max = 8 functions
466 #endif
468 typedef struct {
469 MASK_FUNC_TYPE activeFunctions;
470 MASK_CFN_TYPE activeSwitches;
471 tmr10ms_t lastFunctionTime[MAX_SPECIAL_FUNCTIONS];
473 inline bool isFunctionActive(uint8_t func)
475 return activeFunctions & ((MASK_FUNC_TYPE)1 << func);
478 void reset()
480 memclear(this, sizeof(*this));
482 } CustomFunctionsContext;
484 #include "strhelpers.h"
485 #include "gui.h"
487 #if defined(TEMPLATES)
488 #include "templates.h"
489 #endif
491 #if !defined(SIMU)
492 #define assert(x)
493 #if !defined(CPUARM) || !defined(DEBUG)
494 #define printf printf_not_allowed
495 #endif
496 #endif
498 extern const pm_uint8_t bchout_ar[];
499 extern const pm_uint8_t modn12x3[];
501 //convert from mode 1 to mode stickMode
502 //NOTICE! => 0..3 -> 0..3
503 #define RUD_STICK 0
504 #define ELE_STICK 1
505 #define THR_STICK 2
506 #define AIL_STICK 3
507 #define CONVERT_MODE(x) (((x)<=AIL_STICK) ? pgm_read_byte(modn12x3 + 4*g_eeGeneral.stickMode + (x)) : (x) )
509 extern uint8_t channel_order(uint8_t x);
511 #define THRCHK_DEADBAND 16
513 #if defined(COLORLCD)
514 #define SPLASH_NEEDED() (false)
515 #elif defined(PCBTARANIS)
516 #define SPLASH_NEEDED() (g_eeGeneral.splashMode != 3)
517 #elif defined(CPUARM)
518 #define SPLASH_NEEDED() (g_model.moduleData[EXTERNAL_MODULE].type != MODULE_TYPE_DSM2 && !g_eeGeneral.splashMode)
519 #else
520 #define SPLASH_NEEDED() (!IS_DSM2_PROTOCOL(g_model.protocol) && !g_eeGeneral.splashMode)
521 #endif
523 #if defined(PCBHORUS)
524 #define SPLASH_TIMEOUT 0 /* we use the splash duration to load stuff from the SD */
525 #elif defined(FSPLASH)
526 #define SPLASH_TIMEOUT (g_eeGeneral.splashMode == 0 ? 60000/*infinite=10mn*/ : ((4*100) * (g_eeGeneral.splashMode & 0x03)))
527 #elif defined(PCBTARANIS) || defined(PCBFLAMENCO)
528 #define SPLASH_TIMEOUT (g_eeGeneral.splashMode==-4 ? 1500 : (g_eeGeneral.splashMode<=0 ? (400-g_eeGeneral.splashMode*200) : (400-g_eeGeneral.splashMode*100)))
529 #else
530 #define SPLASH_TIMEOUT (4*100) // 4 seconds
531 #endif
533 #if defined(ROTARY_ENCODERS)
534 #define IS_ROTARY_ENCODER_NAVIGATION_ENABLE() g_eeGeneral.reNavigation
535 extern volatile rotenc_t rotencValue[ROTARY_ENCODERS];
536 #define ROTARY_ENCODER_NAVIGATION_VALUE rotencValue[g_eeGeneral.reNavigation - 1]
537 #elif defined(ROTARY_ENCODER_NAVIGATION)
538 #define IS_ROTARY_ENCODER_NAVIGATION_ENABLE() true
539 extern volatile rotenc_t rotencValue[1];
540 #define ROTARY_ENCODER_NAVIGATION_VALUE rotencValue[0]
541 #endif
543 #if defined(CPUARM) && defined(ROTARY_ENCODER_NAVIGATION)
544 extern uint8_t rotencSpeed;
545 #define ROTENC_LOWSPEED 1
546 #define ROTENC_MIDSPEED 5
547 #define ROTENC_HIGHSPEED 50
548 #define ROTENC_DELAY_MIDSPEED 32
549 #define ROTENC_DELAY_HIGHSPEED 16
550 #endif
552 #define HEART_TIMER_10MS 1
553 #define HEART_TIMER_PULSES 2 // when multiple modules this is the first one
554 #if defined(PCBTARANIS) || defined(PCBFLAMENCO) || defined(PCBHORUS)
555 #define HEART_WDT_CHECK (HEART_TIMER_10MS + (HEART_TIMER_PULSES << 0) + (HEART_TIMER_PULSES << 1))
556 #else
557 #define HEART_WDT_CHECK (HEART_TIMER_10MS + HEART_TIMER_PULSES)
558 #endif
559 extern uint8_t heartbeat;
561 #if defined(CPUARM) && !defined(BOOT)
562 void watchdogSuspend(uint32_t timeout);
563 #define WATCHDOG_SUSPEND(x) watchdogSuspend(x)
564 #else
565 #define WATCHDOG_SUSPEND(...)
566 #endif
568 #define MAX_ALERT_TIME 60
570 struct t_inactivity
572 uint16_t counter;
573 uint8_t sum;
576 extern struct t_inactivity inactivity;
578 #define LEN_STD_CHARS 40
580 #if defined(TRANSLATIONS_CZ)
581 #define ZCHAR_MAX (LEN_STD_CHARS)
582 #else
583 #define ZCHAR_MAX (LEN_STD_CHARS + LEN_SPECIAL_CHARS)
584 #endif
586 char hex2zchar(uint8_t hex);
587 char idx2char(int8_t idx);
588 #if defined(CPUARM) || defined(SIMU)
589 int8_t char2idx(char c);
590 void str2zchar(char *dest, const char *src, int size);
591 int zchar2str(char *dest, const char *src, int size);
592 #endif
594 #include "keys.h"
595 #include "pwr.h"
597 #if defined(PCBTARANIS) || defined(PCBFLAMENCO) || defined(PCBHORUS)
598 div_t switchInfo(int switchPosition);
599 extern uint8_t potsPos[NUM_XPOTS];
600 #endif
602 #if defined(PCBHORUS)
603 uint16_t trimDown(uint16_t idx); // TODO why?
604 #else
605 uint8_t trimDown(uint8_t idx);
606 #endif
607 void readKeysAndTrims();
609 uint16_t evalChkSum();
611 #if !defined(GUI)
612 #define RAISE_ALERT(...)
613 #define ALERT(...)
614 #elif defined(VOICE)
615 #define RAISE_ALERT(title, msg, info, sound) showAlertBox(title, msg, info, sound)
616 #define ALERT(title, msg, sound) alert(title, msg, sound)
617 #else
618 #define RAISE_ALERT(title, msg, info, sound) showAlertBox(title, msg, info)
619 #define ALERT(title, msg, sound) alert(title, msg)
620 #endif
622 void alert(const pm_char * t, const pm_char * s ALERT_SOUND_ARG);
624 enum PerOutMode {
625 e_perout_mode_normal = 0,
626 e_perout_mode_inactive_flight_mode = 1,
627 e_perout_mode_notrainer = 2,
628 e_perout_mode_notrims = 4,
629 e_perout_mode_nosticks = 8,
630 e_perout_mode_noinput = e_perout_mode_notrainer+e_perout_mode_notrims+e_perout_mode_nosticks
634 #if defined(MODULE_ALWAYS_SEND_PULSES)
635 extern uint8_t startupWarningState;
637 enum StartupWarningStates {
638 STARTUP_WARNING_THROTTLE,
639 STARTUP_WARNING_SWITCHES,
640 STARTUP_WARNING_DONE,
642 #endif
645 // Fiddle to force compiler to use a pointer
646 #if defined(CPUARM) || defined(SIMU)
647 #define FORCE_INDIRECT(ptr)
648 #else
649 #define FORCE_INDIRECT(ptr) __asm__ __volatile__ ("" : "=e" (ptr) : "0" (ptr))
650 #endif
652 extern uint8_t mixerCurrentFlightMode;
653 extern uint8_t lastFlightMode;
654 extern uint8_t flightModeTransitionLast;
656 #if defined(CPUARM)
657 #define bitfield_channels_t uint32_t
658 #else
659 #define bitfield_channels_t uint16_t
660 #endif
662 #if defined(SIMU)
663 inline int availableMemory() { return 1000; }
664 #elif defined(CPUARM) && !defined(SIMU)
665 extern unsigned char *heap;
666 extern int _end;
667 extern int _estack;
668 extern int _main_stack_start;
669 extern int _heap_end;
670 #define availableMemory() ((unsigned int)((unsigned char *)&_heap_end - heap))
671 #endif
673 void evalFlightModeMixes(uint8_t mode, uint8_t tick10ms);
674 void evalMixes(uint8_t tick10ms);
675 void doMixerCalculations();
676 void scheduleNextMixerCalculation(uint8_t module, uint16_t delay);
678 #if defined(CPUARM)
679 void checkTrims();
680 #endif
681 void perMain();
682 NOINLINE void per10ms();
684 getvalue_t getValue(mixsrc_t i);
686 #if defined(CPUARM)
687 #define GETSWITCH_MIDPOS_DELAY 1
688 bool getSwitch(swsrc_t swtch, uint8_t flags=0);
689 #else
690 bool getSwitch(swsrc_t swtch);
691 #endif
693 void logicalSwitchesTimerTick();
694 void logicalSwitchesReset();
696 #if defined(CPUARM)
697 void evalLogicalSwitches(bool isCurrentPhase=true);
698 void logicalSwitchesCopyState(uint8_t src, uint8_t dst);
699 #define LS_RECURSIVE_EVALUATION_RESET()
700 #else
701 #define evalLogicalSwitches(xxx)
702 #define GETSWITCH_RECURSIVE_TYPE uint16_t
703 extern volatile GETSWITCH_RECURSIVE_TYPE s_last_switch_used;
704 extern volatile GETSWITCH_RECURSIVE_TYPE s_last_switch_value;
705 #define LS_RECURSIVE_EVALUATION_RESET() s_last_switch_used = 0
706 #endif
708 #if defined(PCBTARANIS) || defined(PCBFLAMENCO) || defined(PCBHORUS)
709 void getSwitchesPosition(bool startup);
710 #else
711 #define getSwitchesPosition(...)
712 #endif
714 extern swarnstate_t switches_states;
715 swsrc_t getMovedSwitch();
717 #if defined(CPUARM)
718 #define GET_MOVED_SOURCE_PARAMS uint8_t min
719 int8_t getMovedSource(GET_MOVED_SOURCE_PARAMS);
720 #define GET_MOVED_SOURCE(min, max) getMovedSource(min)
721 #else
722 #define GET_MOVED_SOURCE_PARAMS
723 int8_t getMovedSource();
724 #define GET_MOVED_SOURCE(min, max) getMovedSource()
725 #endif
727 #if defined(FLIGHT_MODES)
728 extern uint8_t getFlightMode();
729 #else
730 #define getFlightMode() 0
731 #endif
733 #if !defined(CPUARM)
734 uint8_t getTrimFlightMode(uint8_t phase, uint8_t idx);
735 #else
736 #define getTrimFlightMode(phase, idx) (phase)
737 #endif
739 #if defined(GVARS)
740 extern int8_t trimGvar[NUM_STICKS+NUM_AUX_TRIMS];
741 #define TRIM_REUSED(idx) trimGvar[idx] >= 0
742 #else
743 #define TRIM_REUSED(idx) 0
744 #endif
746 trim_t getRawTrimValue(uint8_t phase, uint8_t idx);
747 int getTrimValue(uint8_t phase, uint8_t idx);
749 #if defined(CPUARM)
750 bool setTrimValue(uint8_t phase, uint8_t idx, int trim);
751 #else
752 void setTrimValue(uint8_t phase, uint8_t idx, int trim);
753 #endif
755 #if defined(ROTARY_ENCODERS)
756 int16_t getRotaryEncoder(uint8_t idx);
757 void incRotaryEncoder(uint8_t idx, int8_t inc);
758 #endif
760 #if defined(PCBGRUVIN9X) || defined(PCBMEGA2560)
761 #define ROTARY_ENCODER_GRANULARITY (1)
762 #elif defined(PCBSKY9X)
763 #define ROTARY_ENCODER_GRANULARITY (2 << g_eeGeneral.rotarySteps)
764 #else
765 #define ROTARY_ENCODER_GRANULARITY (2)
766 #endif
768 #include "gvars.h"
770 extern uint16_t sessionTimer;
771 extern uint16_t s_timeCumThr;
772 extern uint16_t s_timeCum16ThrP;
774 #if defined(OVERRIDE_CHANNEL_FUNCTION)
775 #if defined(CPUARM)
776 #define OVERRIDE_CHANNEL_UNDEFINED -4096
777 #else
778 #define OVERRIDE_CHANNEL_UNDEFINED -128
779 #endif
780 extern safetych_t safetyCh[MAX_OUTPUT_CHANNELS];
781 #endif
783 extern uint8_t trimsCheckTimer;
785 #if defined(CPUARM)
786 extern uint8_t trimsDisplayTimer;
787 extern uint8_t trimsDisplayMask;
788 #endif
790 void flightReset(uint8_t check=true);
792 extern uint8_t unexpectedShutdown;
794 extern uint16_t maxMixerDuration;
796 #if !defined(CPUARM)
797 extern uint8_t g_tmr1Latency_max;
798 extern uint8_t g_tmr1Latency_min;
799 extern uint16_t lastMixerDuration;
800 #endif
802 #if defined(CPUARM)
803 #define DURATION_MS_PREC2(x) ((x)/20)
804 #else
805 #define DURATION_MS_PREC2(x) ((x)*100)/16
806 #endif
808 #if defined(THRTRACE)
809 #if defined(COLORLCD)
810 #define MAXTRACE (LCD_W-2*10)
811 #else
812 #define MAXTRACE (LCD_W - 8)
813 #endif
814 extern uint8_t s_traceBuf[MAXTRACE];
815 extern uint16_t s_traceWr;
816 extern uint8_t s_cnt_10s;
817 extern uint16_t s_cnt_samples_thr_10s;
818 extern uint16_t s_sum_samples_thr_10s;
819 #define RESET_THR_TRACE() s_traceWr = s_cnt_10s = s_cnt_samples_thr_10s = s_sum_samples_thr_10s = s_timeCum16ThrP = s_timeCumThr = 0
820 #else
821 #define RESET_THR_TRACE() s_timeCum16ThrP = s_timeCumThr = 0
822 #endif
824 #if defined(SIMU)
825 uint16_t getTmr2MHz();
826 uint16_t getTmr16KHz();
827 #elif defined(STM32)
828 static inline uint16_t getTmr2MHz() { return TIMER_2MHz_TIMER->CNT; }
829 #elif defined(PCBSKY9X)
830 static inline uint16_t getTmr2MHz() { return TC1->TC_CHANNEL[0].TC_CV; }
831 #else
832 uint16_t getTmr16KHz();
833 #endif
835 #if !defined(CPUARM)
836 uint16_t stackAvailable();
837 #endif
839 #if defined(SPLASH)
840 void doSplash();
841 #endif
843 #if MENUS_LOCK == 1
844 extern bool readonly;
845 extern bool readonlyUnlocked();
846 #define READ_ONLY() readonly
847 #define READ_ONLY_UNLOCKED() readonlyUnlocked()
848 #else
849 #define READ_ONLY() false
850 #define READ_ONLY_UNLOCKED() true
851 #endif
853 void checkLowEEPROM();
854 void checkTHR();
855 void checkSwitches();
856 void checkAlarm();
857 void checkAll();
859 #if !defined(SIMU)
860 void getADC();
861 #if defined(CPUARM)
862 #define GET_ADC_IF_MIXER_NOT_RUNNING() do { if (s_pulses_paused) getADC(); } while(0)
863 #else
864 #define GET_ADC_IF_MIXER_NOT_RUNNING() getADC()
865 #endif
866 #endif
868 #if defined(SBUS)
869 #include "sbus.h"
870 #endif
872 void backlightOn();
873 void checkBacklight();
874 void doLoopCommonActions();
876 #define BITMASK(bit) (1<<(bit))
878 #if !defined(UNUSED)
879 #define UNUSED(x) ((void)(x)) /* to avoid warnings */
880 #endif
882 /// returns the number of elements of an array
883 #define DIM(arr) (sizeof((arr))/sizeof((arr)[0]))
885 template<class t> FORCEINLINE t min(t a, t b) { return a<b?a:b; }
886 template<class t> FORCEINLINE t max(t a, t b) { return a>b?a:b; }
887 template<class t> FORCEINLINE t sgn(t a) { return a>0 ? 1 : (a < 0 ? -1 : 0); }
888 template<class t> FORCEINLINE t limit(t mi, t x, t ma) { return min(max(mi,x),ma); }
889 template<class t> void SWAP(t & a, t & b) { t tmp = b; b = a; a = tmp; }
891 uint16_t isqrt32(uint32_t n);
893 #if defined(CPUARM) && !defined(BOOT)
894 #include "tasks_arm.h"
895 extern OS_MutexID mixerMutex;
896 inline void pauseMixerCalculations()
898 CoEnterMutexSection(mixerMutex);
901 inline void resumeMixerCalculations()
903 CoLeaveMutexSection(mixerMutex);
905 #else
906 #define pauseMixerCalculations()
907 #define resumeMixerCalculations()
908 #endif
910 void generalDefault();
911 void modelDefault(uint8_t id);
913 #if defined(CPUARM)
914 void checkModelIdUnique(uint8_t index, uint8_t module);
915 #endif
917 #if defined(CPUARM)
918 uint32_t hash(const void * ptr, uint32_t size);
919 inline int divRoundClosest(const int n, const int d)
921 if (d == 0)
922 return 0;
923 else
924 return ((n < 0) ^ (d < 0)) ? ((n - d/2)/d) : ((n + d/2)/d);
927 #define calc100to256_16Bits(x) calc100to256(x)
928 #define calc100toRESX_16Bits(x) calc100toRESX(x)
930 inline int calc100to256(int x)
932 return divRoundClosest(x*256, 100);
935 inline int calc100toRESX(int x)
937 return divRoundClosest(x*RESX, 100);
940 inline int calc1000toRESX(int x)
942 return divRoundClosest(x*RESX, 1000);
945 inline int calcRESXto1000(int x)
947 return divRoundClosest(x*1000, RESX);
950 inline int calcRESXto100(int x)
952 return divRoundClosest(x*100, RESX);
955 #else
956 extern int16_t calc100to256_16Bits(int16_t x); // @@@2 open.20.fsguruh: return x*2.56
957 extern int16_t calc100to256(int8_t x); // @@@2 open.20.fsguruh: return x*2.56
958 extern int16_t calc100toRESX_16Bits(int16_t x); // @@@ open.20.fsguruh
959 extern int16_t calc100toRESX(int8_t x);
960 extern int16_t calc1000toRESX(int16_t x);
961 extern int16_t calcRESXto1000(int16_t x);
962 extern int8_t calcRESXto100(int16_t x);
963 #endif
965 #if defined(COLORLCD)
966 extern const char vers_stamp[];
967 extern const char date_stamp[];
968 extern const char time_stamp[];
969 extern const char eeprom_stamp[];
970 #else
971 extern const char vers_stamp[];
972 #endif
974 extern uint8_t g_vbat100mV;
975 #if LCD_W > 128
976 #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))
977 #else
978 #define GET_TXBATT_BARS() (limit<int8_t>(2, 20 * (g_vbat100mV - g_eeGeneral.vBatMin - 90) / (30 + g_eeGeneral.vBatMax - g_eeGeneral.vBatMin), 20))
979 #endif
980 #define IS_TXBATT_WARNING() (g_vbat100mV <= g_eeGeneral.vBatWarn)
983 #define g_blinkTmr10ms (*(uint8_t*)&g_tmr10ms)
984 extern uint8_t g_beepCnt;
985 extern uint8_t g_beepVal[5];
987 #include "trainer_input.h"
989 extern int32_t chans[MAX_OUTPUT_CHANNELS];
990 extern int16_t ex_chans[MAX_OUTPUT_CHANNELS]; // Outputs (before LIMITS) of the last perMain
991 extern int16_t channelOutputs[MAX_OUTPUT_CHANNELS];
992 extern uint16_t BandGap;
994 #if defined(CPUARM)
995 #define NUM_INPUTS (MAX_INPUTS)
996 #else
997 #define NUM_INPUTS (NUM_STICKS)
998 #endif
1000 int expo(int x, int k);
1002 #if defined(CPUARM)
1003 inline int getMaximumValue(int source)
1005 if (source < MIXSRC_FIRST_CH)
1006 return 100;
1007 else if (source <= MIXSRC_LAST_CH)
1008 return g_model.extendedLimits ? 150 : 100;
1009 else if (source >= MIXSRC_FIRST_TIMER && source <= MIXSRC_LAST_TIMER)
1010 return (23*60)+59;
1011 else
1012 return 30000;
1014 #endif
1016 // Curves
1017 enum BaseCurves {
1018 CURVE_NONE,
1019 CURVE_X_GT0,
1020 CURVE_X_LT0,
1021 CURVE_ABS_X,
1022 CURVE_F_GT0,
1023 CURVE_F_LT0,
1024 CURVE_ABS_F,
1025 CURVE_BASE
1027 int8_t * curveAddress(uint8_t idx);
1028 struct point_t
1030 coord_t x;
1031 coord_t y;
1033 point_t getPoint(uint8_t i);
1034 #if !defined(CURVES)
1035 #define LOAD_MODEL_CURVES()
1036 #define applyCurve(x, idx) (x)
1037 #elif defined(CPUARM)
1038 typedef CurveData CurveInfo;
1039 void loadCurves();
1040 #define LOAD_MODEL_CURVES() loadCurves()
1041 int intpol(int x, uint8_t idx);
1042 int applyCurve(int x, CurveRef & curve);
1043 int applyCustomCurve(int x, uint8_t idx);
1044 int applyCurrentCurve(int x);
1045 int8_t getCurveX(int noPoints, int point);
1046 void resetCustomCurveX(int8_t * points, int noPoints);
1047 bool moveCurve(uint8_t index, int8_t shift); // TODO bool?
1048 #else
1049 struct CurveInfo {
1050 int8_t * crv;
1051 uint8_t points:7;
1052 uint8_t custom:1;
1054 CurveInfo curveInfo(uint8_t idx);
1055 int intpol(int x, uint8_t idx);
1056 int applyCurve(int x, int8_t idx);
1057 #define LOAD_MODEL_CURVES()
1058 #define applyCustomCurve(x, idx) intpol(x, idx)
1059 int applyCurrentCurve(int x);
1060 bool moveCurve(uint8_t index, int8_t shift, int8_t custom=0);
1061 #endif
1063 #if defined(CPUARM)
1064 #define APPLY_EXPOS_EXTRA_PARAMS_INC , uint8_t ovwrIdx=0, int16_t ovwrValue=0
1065 #define APPLY_EXPOS_EXTRA_PARAMS , uint8_t ovwrIdx, int16_t ovwrValue
1066 #else
1067 #define APPLY_EXPOS_EXTRA_PARAMS_INC
1068 #define APPLY_EXPOS_EXTRA_PARAMS
1069 #endif
1071 #if defined(CPUARM)
1072 void clearInputs();
1073 void defaultInputs();
1074 #endif
1076 void applyExpos(int16_t * anas, uint8_t mode APPLY_EXPOS_EXTRA_PARAMS_INC);
1077 int16_t applyLimits(uint8_t channel, int32_t value);
1079 void evalInputs(uint8_t mode);
1080 uint16_t anaIn(uint8_t chan);
1082 extern int16_t calibratedAnalogs[NUM_CALIBRATED_ANALOGS];
1084 #define FLASH_DURATION 20 /*200ms*/
1086 extern uint8_t beepAgain;
1087 extern uint16_t lightOffCounter;
1088 extern uint8_t flashCounter;
1089 extern uint8_t mixWarning;
1091 FlightModeData * flightModeAddress(uint8_t idx);
1092 ExpoData * expoAddress(uint8_t idx);
1093 MixData * mixAddress(uint8_t idx);
1094 LimitData * limitAddress(uint8_t idx);
1095 LogicalSwitchData * lswAddress(uint8_t idx);
1097 // static variables used in evalFlightModeMixes - moved here so they don't interfere with the stack
1098 // It's also easier to initialize them here.
1099 #if defined(CPUARM)
1100 extern int8_t virtualInputsTrims[NUM_INPUTS];
1101 #else
1102 extern int16_t rawAnas[NUM_INPUTS];
1103 #endif
1105 extern int16_t anas [NUM_INPUTS];
1106 extern int16_t trims[NUM_STICKS+NUM_AUX_TRIMS];
1107 extern BeepANACenter bpanaCenter;
1109 extern uint8_t s_mixer_first_run_done;
1111 void applyDefaultTemplate();
1113 void incSubtrim(uint8_t idx, int16_t inc);
1114 void instantTrim();
1115 void evalTrims();
1116 void copyTrimsToOffset(uint8_t ch);
1117 void copySticksToOffset(uint8_t ch);
1118 void moveTrimsToOffsets();
1120 #if defined(CPUARM)
1121 #define ACTIVE_PHASES_TYPE uint16_t
1122 #define DELAY_POS_SHIFT 0
1123 #define DELAY_POS_MARGIN 3
1124 #define delayval_t int16_t
1125 PACK(typedef struct {
1126 uint16_t delay;
1127 int16_t now; // timer trigger source -> off, abs, stk, stk%, sw/!sw, !m_sw/!m_sw
1128 int16_t prev;
1129 uint8_t activeMix;
1130 uint8_t activeExpo;
1131 }) SwOn;
1132 #else
1133 #define ACTIVE_PHASES_TYPE uint8_t
1134 #define DELAY_POS_SHIFT 10
1135 #define DELAY_POS_MARGIN 0
1136 #define delayval_t int8_t
1137 PACK(typedef struct {
1138 uint16_t delay:10;
1139 int16_t now:2; // timer trigger source -> off, abs, stk, stk%, sw/!sw, !m_sw/!m_sw
1140 int16_t prev:2;
1141 int16_t activeMix:1;
1142 int16_t activeExpo:1;
1143 }) SwOn;
1144 #endif
1146 extern SwOn swOn[MAX_MIXERS];
1147 extern int24_t act[MAX_MIXERS];
1149 #if defined(BOLD_FONT)
1150 inline bool isExpoActive(uint8_t expo)
1152 return swOn[expo].activeExpo;
1155 inline bool isMixActive(uint8_t mix)
1157 return swOn[mix].activeMix;
1159 #else
1160 #define isExpoActive(x) false
1161 #define isMixActive(x) false
1162 #endif
1164 enum LogicalSwitchFamilies {
1165 LS_FAMILY_OFS,
1166 LS_FAMILY_BOOL,
1167 LS_FAMILY_COMP,
1168 LS_FAMILY_DIFF,
1169 LS_FAMILY_TIMER,
1170 LS_FAMILY_STICKY,
1171 LS_FAMILY_RANGE,
1172 LS_FAMILY_EDGE
1175 uint8_t lswFamily(uint8_t func);
1176 int16_t lswTimerValue(delayval_t val);
1178 enum FunctionsActive {
1179 FUNCTION_TRAINER,
1180 FUNCTION_INSTANT_TRIM = FUNCTION_TRAINER+4,
1181 FUNCTION_VARIO,
1182 FUNCTION_BACKLIGHT,
1183 #if defined(SDCARD)
1184 FUNCTION_LOGS,
1185 #endif
1186 #if defined(CPUARM)
1187 FUNCTION_BACKGND_MUSIC,
1188 FUNCTION_BACKGND_MUSIC_PAUSE,
1189 #endif
1192 #define VARIO_FREQUENCY_ZERO 700/*Hz*/
1193 #define VARIO_FREQUENCY_RANGE 1000/*Hz*/
1194 #define VARIO_REPEAT_ZERO 500/*ms*/
1195 #define VARIO_REPEAT_MAX 80/*ms*/
1197 #if defined(CPUARM)
1198 extern CustomFunctionsContext modelFunctionsContext;
1199 extern CustomFunctionsContext globalFunctionsContext;
1200 inline bool isFunctionActive(uint8_t func)
1202 return globalFunctionsContext.isFunctionActive(func) || modelFunctionsContext.isFunctionActive(func);
1204 void evalFunctions(const CustomFunctionData * functions, CustomFunctionsContext & functionsContext);
1205 inline void customFunctionsReset()
1207 globalFunctionsContext.reset();
1208 modelFunctionsContext.reset();
1210 #else
1211 extern CustomFunctionsContext modelFunctionsContext;
1212 #define isFunctionActive(func) modelFunctionsContext.isFunctionActive(func)
1213 void evalFunctions();
1214 #define customFunctionsReset() modelFunctionsContext.reset()
1215 #endif
1217 #include "telemetry/telemetry.h"
1219 #if defined(CPUARM)
1220 uint8_t crc8(const uint8_t * ptr, uint32_t len);
1221 uint16_t crc16(const uint8_t * ptr, uint32_t len);
1222 #endif
1224 #define PLAY_REPEAT(x) (x) /* Range 0 to 15 */
1225 #define PLAY_NOW 0x10
1226 #define PLAY_BACKGROUND 0x20
1227 #define PLAY_INCREMENT(x) ((uint8_t)(((uint8_t)x) << 6)) /* -1, 0, 1, 2 */
1229 enum AUDIO_SOUNDS {
1230 AUDIO_HELLO,
1231 #if defined(CPUARM)
1232 AU_BYE,
1233 #endif
1234 #if defined(VOICE)
1235 AU_THROTTLE_ALERT,
1236 AU_SWITCH_ALERT,
1237 AU_BAD_RADIODATA,
1238 #endif
1239 AU_TX_BATTERY_LOW,
1240 AU_INACTIVITY,
1241 #if defined(CPUARM)
1242 AU_RSSI_ORANGE,
1243 AU_RSSI_RED,
1244 AU_SWR_RED,
1245 AU_TELEMETRY_LOST,
1246 AU_TELEMETRY_BACK,
1247 AU_TRAINER_LOST,
1248 AU_TRAINER_BACK,
1249 AU_SENSOR_LOST,
1250 AU_SERVO_KO,
1251 AU_RX_OVERLOAD,
1252 AU_MODEL_STILL_POWERED,
1253 #endif
1254 #if defined(PCBSKY9X)
1255 AU_TX_MAH_HIGH,
1256 AU_TX_TEMP_HIGH,
1257 #endif
1258 AU_ERROR,
1259 AU_WARNING1,
1260 AU_WARNING2,
1261 AU_WARNING3,
1262 AU_TRIM_MIDDLE,
1263 #if defined(CPUARM)
1264 AU_TRIM_MIN,
1265 AU_TRIM_MAX,
1266 #endif
1267 #if defined(CPUARM)
1268 AU_STICK1_MIDDLE,
1269 AU_STICK2_MIDDLE,
1270 AU_STICK3_MIDDLE,
1271 AU_STICK4_MIDDLE,
1272 #endif
1273 #if defined(PCBTARANIS) || defined(PCBFLAMENCO) || defined(PCBHORUS)
1274 AU_POT1_MIDDLE,
1275 AU_POT2_MIDDLE,
1276 AU_SLIDER1_MIDDLE,
1277 AU_SLIDER2_MIDDLE,
1278 #if defined(PCBX9E)
1279 AU_SLIDER3_MIDDLE,
1280 AU_SLIDER4_MIDDLE,
1281 #endif
1282 #elif defined(CPUARM)
1283 AU_POT1_MIDDLE,
1284 AU_POT2_MIDDLE,
1285 AU_POT3_MIDDLE,
1286 #else
1287 AU_POT_MIDDLE,
1288 #endif
1289 AU_MIX_WARNING_1,
1290 AU_MIX_WARNING_2,
1291 AU_MIX_WARNING_3,
1292 #if defined(CPUARM)
1293 AU_TIMER1_ELAPSED,
1294 AU_TIMER2_ELAPSED,
1295 AU_TIMER3_ELAPSED,
1296 #endif
1298 AU_SPECIAL_SOUND_FIRST,
1299 AU_SPECIAL_SOUND_BEEP1 = AU_SPECIAL_SOUND_FIRST,
1300 AU_SPECIAL_SOUND_BEEP2,
1301 AU_SPECIAL_SOUND_BEEP3,
1302 AU_SPECIAL_SOUND_WARN1,
1303 AU_SPECIAL_SOUND_WARN2,
1304 AU_SPECIAL_SOUND_CHEEP,
1305 AU_SPECIAL_SOUND_RATATA,
1306 AU_SPECIAL_SOUND_TICK,
1307 AU_SPECIAL_SOUND_SIREN,
1308 AU_SPECIAL_SOUND_RING,
1309 AU_SPECIAL_SOUND_SCIFI,
1310 AU_SPECIAL_SOUND_ROBOT,
1311 AU_SPECIAL_SOUND_CHIRP,
1312 AU_SPECIAL_SOUND_TADA,
1313 AU_SPECIAL_SOUND_CRICKET,
1314 AU_SPECIAL_SOUND_ALARMC,
1315 AU_SPECIAL_SOUND_LAST,
1317 AU_NONE=0xff
1320 #if defined(AUDIO)
1321 #if defined(CPUARM)
1322 #include "audio_arm.h"
1323 #else
1324 #include "audio_avr.h"
1325 #endif
1326 #endif
1328 #include "buzzer.h"
1330 #if defined(PCBSTD) && defined(VOICE)
1331 #include "targets/9x/voice.h"
1332 #endif
1334 #if defined(PCBGRUVIN9X) && defined(VOICE)
1335 #include "targets/gruvin9x/voice.h"
1336 #endif
1338 #if defined(PCBMEGA2560) && defined(VOICE)
1339 #include "targets/mega2560/voice.h"
1340 #endif
1342 #include "translations.h"
1343 #include "fonts.h"
1345 #if defined(HAPTIC)
1346 #include "haptic.h"
1347 #endif
1349 #if defined(SDCARD)
1350 #include "sdcard.h"
1351 #endif
1353 #if defined(RTCLOCK)
1354 #include "rtc.h"
1355 #endif
1357 #if defined(REVX)
1358 void setMFP();
1359 void clearMFP();
1360 #endif
1362 #if defined(CPUARM)
1363 extern uint8_t requiredSpeakerVolume;
1364 #endif
1366 #if defined(CPUARM)
1367 enum MainRequest {
1368 REQUEST_SCREENSHOT,
1369 REQUEST_FLIGHT_RESET,
1372 extern uint8_t mainRequestFlags;
1373 #endif
1375 void checkBattery();
1376 void opentxClose(uint8_t shutdown=true);
1377 void opentxInit();
1378 void opentxResume();
1380 #if defined(PCBHORUS) || defined(PCBX7)
1381 #define LED_ERROR_BEGIN() ledRed()
1382 #define LED_ERROR_END() ledBlue()
1383 #else
1384 #define LED_ERROR_BEGIN()
1385 #define LED_ERROR_END()
1386 #endif
1388 // Re-useable byte array to save having multiple buffers
1389 #if LCD_W <= 212
1390 #define SD_SCREEN_FILE_LENGTH 32
1391 #else
1392 #define SD_SCREEN_FILE_LENGTH 64
1393 #endif
1394 union ReusableBuffer
1396 // 275 bytes
1397 struct
1399 #if !defined(CPUARM)
1400 char listnames[NUM_BODY_LINES][LEN_MODEL_NAME];
1401 #endif
1402 #if defined(EEPROM_RLC) && LCD_W < 212
1403 uint16_t eepromfree;
1404 #endif
1405 #if defined(SDCARD)
1406 char menu_bss[POPUP_MENU_MAX_LINES][MENU_LINE_LENGTH];
1407 char mainname[45]; // because reused for SD backup / restore, max backup filename 44 chars: "/MODELS/MODEL0134353-2014-06-19-04-51-27.bin"
1408 #else
1409 char mainname[LEN_MODEL_NAME];
1410 #endif
1411 } modelsel;
1413 struct {
1414 char msg[64];
1415 } msgbuf;
1417 // 103 bytes
1418 struct
1420 int16_t midVals[NUM_STICKS+NUM_POTS+NUM_SLIDERS+NUM_MOUSE_ANALOGS];
1421 int16_t loVals[NUM_STICKS+NUM_POTS+NUM_SLIDERS+NUM_MOUSE_ANALOGS];
1422 int16_t hiVals[NUM_STICKS+NUM_POTS+NUM_SLIDERS+NUM_MOUSE_ANALOGS];
1423 uint8_t state;
1424 #if defined(PCBTARANIS) || defined(PCBFLAMENCO) || defined(PCBHORUS)
1425 struct {
1426 uint8_t stepsCount;
1427 int16_t steps[XPOTS_MULTIPOS_COUNT];
1428 uint8_t lastCount;
1429 int16_t lastPosition;
1430 } xpotsCalib[NUM_XPOTS];
1431 #endif
1432 } calib;
1434 #if defined(SDCARD)
1435 // 274 bytes
1436 struct
1438 char lines[NUM_BODY_LINES][SD_SCREEN_FILE_LENGTH+1+1]; // the last char is used to store the flags (directory) of the line
1439 uint32_t available;
1440 uint16_t offset;
1441 uint16_t count;
1442 char originalName[SD_SCREEN_FILE_LENGTH+1];
1443 } sdmanager;
1444 #endif
1446 #if defined(STM32)
1447 struct
1449 char id[27];
1450 } version;
1451 #endif
1452 struct
1454 uint8_t stickMode;
1455 } generalSettings;
1458 extern union ReusableBuffer reusableBuffer;
1460 void checkFlashOnBeep();
1462 #if defined(CPUARM)
1463 uint8_t zlen(const char *str, uint8_t size);
1464 bool zexist(const char *str, uint8_t size);
1465 unsigned int effectiveLen(const char * str, unsigned int size);
1466 char * strcat_zchar(char *dest, const char *name, uint8_t size, const char *defaultName=NULL, uint8_t defaultNameSize=0, uint8_t defaultIdx=0);
1467 #define strcat_phasename(dest, idx) strcat_zchar(dest, g_model.flightModeData[idx].name, LEN_FLIGHT_MODE_NAME, STR_FP, PSIZE(TR_FP), idx+1)
1468 #if defined(EEPROM)
1469 #define strcat_modelname(dest, idx) strcat_zchar(dest, modelHeaders[idx].name, LEN_MODEL_NAME, STR_MODEL, PSIZE(TR_MODEL), idx+1)
1470 #define strcat_currentmodelname(dest) strcat_modelname(dest, g_eeGeneral.currModel)
1471 #else
1472 #define strcat_currentmodelname(dest) strcat_zchar(dest, g_model.header.name, LEN_MODEL_NAME)
1473 #endif
1474 #define ZLEN(s) zlen(s, sizeof(s))
1475 #define ZEXIST(s) zexist(s, sizeof(s))
1476 #endif
1478 // Stick tolerance varies between transmitters, Higher is better
1479 #if defined (PCB9XR) || defined (PCB9XR128)
1480 #define STICK_TOLERANCE 16
1481 #else
1482 #define STICK_TOLERANCE 64
1483 #endif
1485 #if defined(FRSKY_HUB) && defined(GAUGES)
1486 enum BarThresholdIdx {
1487 THLD_ALT,
1488 THLD_RPM,
1489 THLD_FUEL,
1490 THLD_T1,
1491 THLD_T2,
1492 THLD_SPEED,
1493 THLD_DIST,
1494 THLD_GPSALT,
1495 THLD_CELL,
1496 THLD_CELLS_SUM,
1497 THLD_VFAS,
1498 THLD_CURRENT,
1499 THLD_CONSUMPTION,
1500 THLD_MAX,
1503 #if defined(CPUARM)
1504 #define FILL_THRESHOLD(idx, val) barsThresholds[idx] = (val)
1505 #else
1506 #define FILL_THRESHOLD(idx, val) barsThresholds[idx] = 128 + (val)
1507 #endif
1509 extern bar_threshold_t barsThresholds[THLD_MAX];
1510 #else
1511 #define FILL_THRESHOLD(idx, val)
1512 #endif
1514 #if defined(TELEMETRY_FRSKY)
1515 ls_telemetry_value_t minTelemValue(source_t channel);
1516 ls_telemetry_value_t maxTelemValue(source_t channel);
1517 #else
1518 #define minTelemValue(channel) 255
1519 #define maxTelemValue(channel) 255
1520 #endif
1522 #if defined(CPUARM)
1523 getvalue_t convert16bitsTelemValue(source_t channel, ls_telemetry_value_t value);
1524 ls_telemetry_value_t max8bitsTelemValue(source_t channel);
1525 #endif
1527 getvalue_t convert8bitsTelemValue(source_t channel, ls_telemetry_value_t value);
1528 getvalue_t convertLswTelemValue(LogicalSwitchData * cs);
1530 #if defined(CPUARM)
1531 #define convertTelemValue(channel, value) convert16bitsTelemValue(channel, value)
1532 #define convertBarTelemValue(channel, value) convert8bitsTelemValue(channel, value)
1533 #define maxBarTelemValue(channel) max8bitsTelemValue(channel)
1534 #else
1535 #define convertTelemValue(channel, value) convert8bitsTelemValue(channel, value)
1536 #define convertBarTelemValue(channel, value) convert8bitsTelemValue(channel, value)
1537 #define maxBarTelemValue(channel) maxTelemValue(channel)
1538 #endif
1540 #if defined(TELEMETRY_FRSKY) || defined(CPUARM)
1541 lcdint_t applyChannelRatio(source_t channel, lcdint_t val);
1542 #define ANA_CHANNEL_UNIT(channel) g_model.frsky.channels[channel].type
1543 #endif
1545 inline int div_and_round(int num, int den)
1547 if (den == 0) {
1548 return 0;
1550 else if (num >= 0) {
1551 num += den / 2;
1553 else {
1554 num -= den / 2;
1556 return num / den;
1559 #if defined(TELEMETRY_FRSKY)
1560 #if !defined(CPUARM)
1561 NOINLINE uint8_t getRssiAlarmValue(uint8_t alarm);
1562 #endif
1564 extern const pm_uint8_t bchunit_ar[];
1566 #if defined(CPUARM)
1567 #define FRSKY_MULTIPLIER_MAX 5
1568 #else
1569 #define FRSKY_MULTIPLIER_MAX 3
1570 #endif
1572 enum TelemetryViews {
1573 TELEMETRY_CUSTOM_SCREEN_1,
1574 TELEMETRY_CUSTOM_SCREEN_2,
1575 #if defined(CPUARM)
1576 TELEMETRY_CUSTOM_SCREEN_3,
1577 TELEMETRY_CUSTOM_SCREEN_4,
1578 TELEMETRY_VIEW_MAX = TELEMETRY_CUSTOM_SCREEN_4
1579 #else
1580 TELEMETRY_VOLTAGES_SCREEN,
1581 TELEMETRY_AFTER_FLIGHT_SCREEN,
1582 TELEMETRY_VIEW_MAX = TELEMETRY_AFTER_FLIGHT_SCREEN
1583 #endif
1586 extern uint8_t s_frsky_view;
1588 #endif
1590 #define EARTH_RADIUSKM ((uint32_t)6371)
1591 #define EARTH_RADIUS ((uint32_t)111194) // meters * pi / 180°
1593 void getGpsPilotPosition();
1594 void getGpsDistance();
1595 void varioWakeup();
1597 #if defined(AUDIO) && defined(BUZZER)
1598 #define IS_SOUND_OFF() (g_eeGeneral.buzzerMode==e_mode_quiet && g_eeGeneral.beepMode==e_mode_quiet)
1599 #else
1600 #define IS_SOUND_OFF() (g_eeGeneral.beepMode == e_mode_quiet)
1601 #endif
1603 #if defined(CPUARM)
1604 #define IS_IMPERIAL_ENABLE() (g_eeGeneral.imperial)
1605 #elif defined(IMPERIAL_UNITS)
1606 #define IS_IMPERIAL_ENABLE() (1)
1607 #else
1608 #define IS_IMPERIAL_ENABLE() (0)
1609 #endif
1611 #if defined(CPUARM)
1612 #elif defined(TELEMETRY_FRSKY)
1613 FORCEINLINE void convertUnit(getvalue_t & val, uint8_t & unit)
1615 if (IS_IMPERIAL_ENABLE()) {
1616 if (unit == UNIT_TEMPERATURE) {
1617 val += 18;
1618 val *= 115;
1619 val >>= 6;
1621 if (unit == UNIT_DIST) {
1622 // m to ft *105/32
1623 val = val * 3 + (val >> 2) + (val >> 5);
1625 if (unit == UNIT_FEET) {
1626 unit = UNIT_DIST;
1628 if (unit == UNIT_KTS) {
1629 // kts to mph
1630 unit = UNIT_SPEED;
1631 val = (val * 23) / 20;
1634 else {
1635 if (unit == UNIT_KTS) {
1636 // kts to km/h
1637 unit = UNIT_SPEED;
1638 val = (val * 50) / 27;
1642 if (unit == UNIT_HDG) {
1643 unit = UNIT_TEMPERATURE;
1646 #else
1647 #define convertUnit(...)
1648 #endif
1650 #if !defined(CPUARM)
1651 #define IS_USR_PROTO_FRSKY_HUB() (g_model.frsky.usrProto == USR_PROTO_FRSKY)
1652 #define IS_USR_PROTO_WS_HOW_HIGH() (g_model.frsky.usrProto == USR_PROTO_WS_HOW_HIGH)
1653 #endif
1655 #if defined(TELEMETRY_FRSKY) && defined(FRSKY_HUB) && defined(GPS)
1656 #define IS_GPS_AVAILABLE() IS_USR_PROTO_FRSKY_HUB()
1657 #else
1658 #define IS_GPS_AVAILABLE() (0)
1659 #endif
1661 #if defined(PCBTARANIS)
1662 extern const pm_uchar logo_taranis[];
1663 #endif
1665 #if defined(USB_MASS_STORAGE)
1666 void usbPluggedIn();
1667 #endif
1669 #include "lua/lua_api.h"
1671 #if defined(SDCARD)
1672 enum ClipboardType {
1673 CLIPBOARD_TYPE_NONE,
1674 CLIPBOARD_TYPE_CUSTOM_SWITCH,
1675 CLIPBOARD_TYPE_CUSTOM_FUNCTION,
1676 CLIPBOARD_TYPE_SD_FILE,
1679 #if defined(SIMU)
1680 #define CLIPBOARD_PATH_LEN 1024
1681 #else
1682 #define CLIPBOARD_PATH_LEN 32
1683 #endif
1685 struct Clipboard {
1686 ClipboardType type;
1687 union {
1688 LogicalSwitchData csw;
1689 CustomFunctionData cfn;
1690 struct {
1691 char directory[CLIPBOARD_PATH_LEN];
1692 char filename[CLIPBOARD_PATH_LEN];
1693 } sd;
1694 } data;
1697 extern Clipboard clipboard;
1698 #endif
1700 #if !defined(SIMU)
1701 extern uint16_t s_anaFilt[NUM_ANALOGS];
1702 #endif
1704 #if defined(JITTER_MEASURE)
1705 extern JitterMeter<uint16_t> rawJitter[NUM_ANALOGS];
1706 extern JitterMeter<uint16_t> avgJitter[NUM_ANALOGS];
1707 #if defined(PCBHORUS)
1708 #define JITTER_MEASURE_ACTIVE() (menuHandlers[menuLevel] == menuStatsAnalogs)
1709 #elif defined(PCBTARANIS)
1710 #define JITTER_MEASURE_ACTIVE() (menuHandlers[menuLevel] == menuRadioDiagAnalogs)
1711 #elif defined(CLI)
1712 #define JITTER_MEASURE_ACTIVE() (1)
1713 #else
1714 #define JITTER_MEASURE_ACTIVE() (0)
1715 #endif
1716 #endif
1718 #if defined(INTERNAL_GPS)
1719 #include "gps.h"
1720 #endif
1722 #if defined(BLUETOOTH)
1723 #include "bluetooth.h"
1724 #endif
1726 #endif // _OPENTX_H_