-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pather9x.cpp
5729 lines (5235 loc) · 120 KB
/
er9x.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* er9x.cpp
*
* Created on: 10 авг. 2019 г.
* Author: KOSTYA
*/
/*
* Author - Erez Raviv <[email protected]>
*
* Based on th9x -> http://code.google.com/p/th9x/
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
*/
#include "er9x.h"
#include <stdlib.h>
#include "language.h"
#include "pulses.h"
#include "lcd.h"
#include "menus.h"
#include "voice.h"
// Next two lines swapped as new complier/linker reverses them in memory!
const
//#ifdef REMOVE_FROM_64FRSKY
//#include "s9xsplashs.lbm"
//const prog_uchar APM s9xsplashMarker[] = {
//"Spls"
//};
//#else
#include "s9xsplash.lbm"
#include "splashmarker.h"
//#endif
#if defined(CPUM128) || defined(CPUM2561)
const uint8_t
#include "..\..\common\hand.lbm"
#endif
/*
mode1 rud ele thr ail
mode2 rud thr ele ail
mode3 ail ele thr rud
mode4 ail thr ele rud
*/
// Various debug defines
//#define SERIALVOICE 1
// #define BLIGHT_DEBUG 1
#define ROTARY 1
//uint8_t RebootReason ;
extern int16_t AltOffset ;
#ifdef V2
uint8_t Last_switch[NUM_CSW+EXTRA_CSW] ;
#else
#if defined(CPUM128) || defined(CPUM2561)
uint8_t Last_switch[NUM_CSW+EXTRA_CSW] ;
#else
uint8_t Last_switch[NUM_CSW] ;
#endif
#endif // V2
static void checkMem( void );
void checkTHR( void );
/// Pr�ft beim Einschalten ob alle Switches 'off' sind.
void checkSwitches( void );
#ifdef V2
#ifdef USE_ADJUSTERS
static void processAdjusters( void ) ;
#endif
#endif
int8_t getGvarSourceValue( uint8_t src ) ;
#ifndef SIMU
static void checkQuickSelect( void ); // Quick model select on startup
void getADC_osmp( void ) ;
#endif
#ifdef V2
V2EEGeneral g_eeGeneral;
V2ModelData g_model ;
#else
EEGeneral g_eeGeneral;
ModelData g_model ;
#endif
extern uint8_t scroll_disabled ;
const char *AlertMessage ;
uint8_t Main_running ;
uint8_t SlaveMode ;
#ifdef V2
uint8_t Vs_state[EXTRA_VOICE_SW] ;
#else
#ifdef NOVOICE_SW
//uint8_t Vs_state[NUM_CHNOUT] ;
#else
uint8_t Vs_state[NUM_CHNOUT+EXTRA_VOICE_SW] ;
#endif
#endif
uint8_t Nvs_state[NUM_VOICE_ALARMS] ;
int16_t Nvs_timer[NUM_VOICE_ALARMS] ;
uint8_t CurrentVolume ;
uint8_t ppmInAvailable = 0 ;
struct t_rotary Rotary ;
uint8_t Tevent ;
//uint16_t MenuTimer ;
uint8_t Backup_RestoreRunning ; // Used when accessing serial to Megasound
#ifndef V2
TimerMode TimerConfig[2] ;
#endif // nV2
const uint8_t bchout_ar[] = {0x1B,
0x1E,
0x27,
0x2D,
0x36,
0x39,
0x4B,
0x4E,
0x63,
0x6C,
0x72,
0x78,
0x87,
0x8D,
0x93,
0x9C,
0xB1,
0xB4,
0xC6,
0xC9,
0xD2,
0xD8,
0xE1,
0xE4};
//new audio object
audioQueue audio;
uint8_t sysFlags = 0;
uint8_t SystemOptions ;
struct t_alarmControl AlarmControl = { 100, 0, 10, 2 } ;
#ifdef V2
int16_t CsTimer[NUM_CSW+EXTRA_CSW] ;
#else
#if defined(CPUM128) || defined(CPUM2561)
int16_t CsTimer[NUM_CSW+EXTRA_CSW] ;
#else
int16_t CsTimer[NUM_CSW] ;
#endif
#endif // V2
const char Str_Alert[] = STR_ALERT ;
const char Str_Switches[] = SWITCHES_STR ;
const char Str_OFF[] = STR_OFF ;
const char Str_ON[] = STR_ON ;
#if defined(CPUM128) || defined(CPUM2561)
const char modi12x3[]= "\005" STR_STICK_NAMES ;
#else
const char modi12x3[]= {"\004" STR_STICK_NAMES} ;
#endif
const uint8_t stickScramble[]= {
0, 1, 2, 3,
0, 2, 1, 3,
3, 1, 2, 0,
3, 2, 1, 0 };
#ifdef CPUM2561
uint8_t Arduino = 0 ;
#endif
const char Str_Hyphens[] = "----" ;
uint8_t modeFixValue( uint8_t value )
{
return *(stickScramble+g_eeGeneral.stickMode*4+value)+1 ;
}
const uint8_t csTypeTable[] =
#ifdef V2
{ CS_VOFS, CS_VOFS, CS_VOFS, CS_VOFS, CS_VBOOL, CS_VBOOL, CS_VBOOL,
CS_VCOMP, CS_VCOMP, CS_VCOMP, CS_VCOMP, CS_VBOOL, CS_VBOOL, CS_TIMER, CS_VOFS, CS_TMONO, CS_TMONO
} ;
#else
#ifdef VERSION3
#if defined(CPUM128) || defined(CPUM2561)
{ CS_VOFS, CS_VOFS, CS_VOFS, CS_VOFS, CS_VBOOL, CS_VBOOL, CS_VBOOL,
CS_VCOMP, CS_VCOMP, CS_VCOMP, CS_VCOMP, CS_VBOOL, CS_VBOOL, CS_TIMER, CS_VOFS, CS_TMONO, CS_TMONO
} ;
#else
{ CS_VOFS, CS_VOFS, CS_VOFS, CS_VOFS, CS_VBOOL, CS_VBOOL, CS_VBOOL,
CS_VCOMP, CS_VCOMP, CS_VCOMP, CS_VCOMP, CS_VBOOL, CS_VBOOL, CS_TIMER, CS_VOFS
} ;
#endif
#else
{ CS_VOFS, CS_VOFS, CS_VOFS, CS_VOFS, CS_VBOOL, CS_VBOOL, CS_VBOOL,
CS_VCOMP, CS_VCOMP, CS_VCOMP, CS_VCOMP, CS_VCOMP, CS_VCOMP, CS_TIMER, CS_VOFS
} ;
#endif
#endif // V2
uint16_t get_tmr10ms()
{
uint16_t time ;
time = g_tmr10ms ;
return time ;
}
uint8_t CS_STATE( uint8_t x)
{
return *(csTypeTable+x-1) ;
}
MixData *mixaddress( uint8_t idx )
{
return &g_model.mixData[idx] ;
}
//LimitData *limitaddress( uint8_t idx )
//{
// return &g_model.limitData[idx];
//}
uint8_t throttleReversed()
{
return g_model.throttleReversed ^ g_eeGeneral.throttleReversed ;
}
void putsChnRaw(uint8_t x,uint8_t y,uint8_t idx,uint8_t att)
{
uint8_t chanLimit = NUM_XCHNRAW ;
uint8_t mix = att & MIX_SOURCE ;
if ( mix )
{
chanLimit += MAX_GVARS + 1 + 1 ;
att &= ~MIX_SOURCE ;
}
if(idx==0)
lcd_putsnAtt(x,y,Str_Hyphens,4,att);
else if(idx<=4)
{
if ( g_model.useCustomStickNames )
{
#ifdef V2
lcd_putsnAtt( x, y, ( char *)(( g_model.useCustomStickNames == 2 ) ? g_model.customStickNames : g_eeGeneral.customStickNames)+4*(idx-1), 4, att|BSS ) ;
#else
lcd_putsnAtt( x, y, ( char *)g_eeGeneral.customStickNames+4*(idx-1), 4, att|BSS ) ;
#endif
}
else
{
lcd_putsAttIdx(x,y,modi12x3,(idx-1),att) ;
}
}
else if(idx<=chanLimit)
lcd_putsAttIdx(x,y,Str_Chans_Gv,(idx-5),att);
#ifdef FRSKY
else
{
if ( mix )
{
idx += TEL_ITEM_SC1-(chanLimit-NUM_XCHNRAW) ;
}
lcd_putsAttIdx(x,y,Str_telemItems,(idx-NUM_XCHNRAW),att);
}
#endif
}
void putsChn(uint8_t x,uint8_t y,uint8_t idx1,uint8_t att)
{
#if defined(CPUM128) || defined(CPUM2561)
if ( idx1 == 0 )
{
lcd_putsnAtt(x,y,PSTR("--- "),4,att);
}
else
{
uint8_t x1 ;
x1 = x + 4*FW-2 ;
if ( idx1 < 10 )
{
x1 -= FWNUM ;
}
lcd_2_digits( x1, y, idx1, att ) ;
lcd_putsnAtt(x,y,PSTR(STR_CH),2,att);
}
#else
putsChnRaw( x, y, idx1 ? idx1+20 : idx1, att ) ;
#endif
}
#ifdef SWITCH_MAPPING
uint8_t switchMapTable[41] ;
uint8_t switchUnMapTable[HSW_MAX+1] ;
uint8_t MaxSwitchIndex ; // For ON and OFF
//uint8_t numSwitchpositions( uint8_t swtch )
//{
// uint8_t positions = ( swtch == SW_ID0 ) ? 3 : 2 ;
// uint8_t map = g_eeGeneral.switchMapping ;
// if ( swtch == SW_ElevDR )
// {
// if ( map & USE_ELE_3POS )
// {
// positions = 3 ;
// }
// }
// return positions ;
//}
void createSwitchMapping()
{
uint8_t *p = switchMapTable ;
FORCE_INDIRECT(p) ;
uint8_t map = g_eeGeneral.switchMapping ;
*p++ = 0 ;
#ifdef XSW_MOD
if ( map & USE_THR_3POS )
{
*p++ = HSW_Thr3pos0 ;
*p++ = HSW_Thr3pos1 ;
*p++ = HSW_Thr3pos2 ;
}
else
{
*p++ = HSW_ThrCt ;
}
#else
*p++ = HSW_ThrCt ;
#endif
if ( map & USE_RUD_3POS )
{
*p++ = HSW_Rud3pos0 ;
*p++ = HSW_Rud3pos1 ;
*p++ = HSW_Rud3pos2 ;
}
else
{
*p++ = HSW_RuddDR ;
}
if ( map & USE_ELE_3POS )
{
*p++ = HSW_Ele3pos0 ;
*p++ = HSW_Ele3pos1 ;
*p++ = HSW_Ele3pos2 ;
}
else
{
*p++ = HSW_ElevDR ;
}
*p++ = HSW_ID0 ;
*p++ = HSW_ID1 ;
*p++ = HSW_ID2 ;
if ( map & USE_AIL_3POS )
{
*p++ = HSW_Ail3pos0 ;
*p++ = HSW_Ail3pos1 ;
*p++ = HSW_Ail3pos2 ;
}
else
{
*p++ = HSW_AileDR ;
}
if ( map & USE_GEA_3POS )
{
*p++ = HSW_Gear3pos0 ;
*p++ = HSW_Gear3pos1 ;
*p++ = HSW_Gear3pos2 ;
}
else
{
*p++ = HSW_Gear ;
}
*p++ = HSW_Trainer ;
if ( map & USE_PB1 )
{
*p++ = HSW_Pb1 ;
}
if ( map & USE_PB2 )
{
*p++ = HSW_Pb2 ;
}
#if defined(CPUM128) || defined(CPUM2561)
for ( uint8_t i = 10 ; i <=27 ; i += 1 )
#else
for ( uint8_t i = 10 ; i <=21 ; i += 1 )
#endif
{
*p++ = i ; // Custom switches
}
*p = MAX_DRSWITCH ;
MaxSwitchIndex = p - switchMapTable ;
*++p = MAX_DRSWITCH+1 ;
for ( uint8_t i = 0 ; i <= (uint8_t)MaxSwitchIndex+1 ; i += 1 )
{
switchUnMapTable[switchMapTable[i]] = i ;
}
// uint8_t index = 1 ;
// Sw3PosList[0] = HSW_ID0 ;
// Sw3PosCount[0] = 3 ;
// Sw3PosCount[index] = 2 ;
// Sw3PosList[index] = HSW_ThrCt ;
// Sw3PosCount[++index] = 2 ;
// Sw3PosList[index] = HSW_RuddDR ;
// Sw3PosCount[++index] = 2 ;
// Sw3PosList[index] = HSW_ElevDR ;
// if ( map & USE_ELE_3POS )
// {
// Sw3PosCount[index] = 3 ;
// Sw3PosList[index] = HSW_Ele3pos0 ;
// }
// Sw3PosCount[++index] = 2 ;
// Sw3PosList[index] = HSW_AileDR ;
// Sw3PosCount[++index] = 2 ;
// Sw3PosList[index] = HSW_Gear ;
// Sw3PosCount[++index] = 2 ;
// Sw3PosList[index] = HSW_Trainer ;
if ( g_eeGeneral.pg2Input )
{
/*
DDRG &= ~0x04 ;
PORTG |= 0x04 ; // pullups
*/
}
if ( g_eeGeneral.pb7Input )
{
/*
DDRB &= ~(1<<OUT_B_LIGHT) ;
PORTB |= (1<<OUT_B_LIGHT) ; // pullups
*/
}
}
int8_t switchUnMap( int8_t x )
{
uint8_t sign = 0 ;
if ( x < 0 )
{
sign = 1 ;
x = -x ;
}
x = switchUnMapTable[x] ;
if ( sign )
{
x = -x ;
}
return x ;
}
int8_t switchMap( int8_t x )
{
uint8_t sign = 0 ;
if ( x < 0 )
{
sign = 1 ;
x = -x ;
}
x = switchMapTable[x] ;
if ( sign )
{
x = -x ;
}
return x ;
}
#endif
void putsMomentDrSwitches(uint8_t x,uint8_t y,int8_t idx1,uint8_t att)
{
if ( idx1 > TOGGLE_INDEX )
{
lcd_putcAtt(x+3*FW, y,'m',att);
idx1 -= TOGGLE_INDEX ;
}
putsDrSwitches( x-1*FW, y, idx1, att ) ;
}
void putsDrSwitches(uint8_t x,uint8_t y,int8_t idx1,uint8_t att)//, bool nc)
{
switch(idx1){
case 0: lcd_putsAtt(x+FW,y,&Str_Hyphens[1],att);return;
case MAX_DRSWITCH: lcd_putsAtt(x+FW,y,Str_ON,att);return;
case -MAX_DRSWITCH: lcd_putsAtt(x+FW,y,Str_OFF,att);return;
}
// if ( idx1 < 0 )
// {
// }
int8_t z ;
z = idx1 ;
if ( z < 0 )
{
lcd_putcAtt(x,y, '!',att);
z = -idx1 ;
}
z -= 1 ;
#ifdef XSW_MOD
if (z >= (SW_3POS_BASE-1)) {
// to get 3pos switch names from Str_Switches
z -= (SW_3POS_BASE-1-(MAX_PSWITCH+MAX_CSWITCH));
}
#else
// z *= 3 ;
if ( z > MAX_DRSWITCH )
{
z -= HSW_OFFSET ;
}
#endif
lcd_putsAttIdx(x+FW,y,Str_Switches,z,att) ;
}
void putsTmrMode(uint8_t x, uint8_t y, uint8_t attr, uint8_t type )
{ // Valid values of type are 0, 1 or 2 only
#ifndef V2
TimerMode *ptConfig ;
#else
V2TimerMode *ptConfig ;
#endif
int8_t tm ;
int8_t tmb ;
#ifndef V2
if ( type & 0x80 )
{
ptConfig = &TimerConfig[1] ;
}
else
{
ptConfig = &TimerConfig[0] ;
}
#else
if ( type & 0x80 )
{
ptConfig = &g_model.timer[1] ;
}
else
{
ptConfig = &g_model.timer[0] ;
}
#endif
tm = ptConfig->tmrModeA ;
tmb = ptConfig->tmrModeB ;
type &= 3 ;
if ( type < 2 ) // 0 or 1
{
if(tm<TMR_VAROFS)
{
lcd_putsnAtt( x, y,(STR_TRIGA_OPTS)+3*tm,3,attr);
}
else
{
tm -= TMR_VAROFS - 7 ;
lcd_putsAttIdx( x, y, Curve_Str, tm, attr ) ;
if ( tm < 9 + 7 ) // Allow for 7 offset above
{
x -= FW ;
}
lcd_putcAtt(x+3*FW, y,'%',attr);
}
}
if ( ( type == 2 ) || ( ( type == 0 ) && ( tm == 1 ) ) )
{
putsMomentDrSwitches( x, y, tmb, attr );
}
asm("") ;
}
#ifdef FRSKY
uint8_t Unit ;
uint16_t scale_telem_value( uint16_t val, uint8_t channel, uint8_t *p_att )
{
uint32_t value ;
uint16_t ratio ;
#ifdef V2
V2FrSkyChannelData *fd ;
#else
FrSkyChannelData *fd ;
#endif
uint8_t unit = 'v' ;
uint8_t places = 1 ;
fd = &g_model.frsky.channels[channel] ;
value = val ;
#ifdef V2
ratio = fd->ratio ;
if ( ratio < 100 )
{
places = 2 ;
value *= 10 ;
}
value *= ratio ;
value /= 256 ;
unit = fd->unit ;
#else
ratio = fd->opt.alarm.ratio ;
if (fd->opt.alarm.type == 2/*V*/)
{
ratio <<= 1 ;
}
value *= ratio ;
if ( fd->opt.alarm.type == 3/*A*/)
{
unit = 'A' ;
value /= 100 ;
}
else if ( ( ratio < 100 ) && ( fd->opt.alarm.type != 1 ) ) // Not raw
{
value *= 2 ;
value /= 51 ; // Same as *10 /255 but without overflow
places = 2 ;
}
else
{
value /= 255 ;
}
if ( fd->opt.alarm.type == 1 )
{
unit = ' ' ;
places = 0 ;
}
#endif
if ( p_att )
{
*p_att = places ;
}
Unit = unit ;
return value ;
}
uint8_t putsTelemValue(uint8_t x, uint8_t y, uint16_t val, uint8_t channel, uint8_t att)
{
uint16_t value ; // ??Can this be a uint16_t??
uint8_t dplaces ;
value = scale_telem_value( val, channel, &dplaces ) ;
if ( dplaces == 1 )
{
att |= PREC1 ;
}
else if ( dplaces == 2 )
{
att |= PREC2 ;
}
if ( Unit == 'v' ) //ltype == 0/*v*/) || (ltype == 2/*v*/) )
{
lcd_outdezNAtt(x, y, value, att, 5) ;
if(!(att&NO_UNIT)) lcd_putcAtt(Lcd_lastPos, y, Unit, att);
}
else
{
lcd_outdezAtt(x, y, value, att);
if ( Unit == 'A')
{
if(!(att&NO_UNIT)) lcd_putcAtt(Lcd_lastPos, y, Unit, att);
}
}
return Unit ;
}
#endif
#ifdef XSW_MOD
inline uint8_t switchPosition(uint8_t swtch)
{
uint8_t pi = (swtch - 1) ;
if (swtch >= SW_3POS_BASE) {
//assert(swtch <= SW_3POS_END);
swtch -= SW_3POS_BASE;
pi = swtch / 3;
}
return switchState(PSW_BASE + pi);
}
#endif
int16_t getValue(uint8_t i)
{
if(i<7) return calibratedStick[i];//-512..512
if(i<PPM_BASE) return 0 ;
else if(i<CHOUT_BASE)
{
int16_t x ;
x = g_ppmIns[i-PPM_BASE] ;
if(i<PPM_BASE+4)
{
x -= g_eeGeneral.trainer.calib[i-PPM_BASE] ;
}
return x*2;
}
else if(i<CHOUT_BASE+NUM_CHNOUT) return Ex_chans[i-CHOUT_BASE];
else if(i<CHOUT_BASE+NUM_CHNOUT+NUM_TELEM_ITEMS)
{
return get_telemetry_value( i-CHOUT_BASE-NUM_CHNOUT ) ;
}
return 0;
}
bool getSwitch00( int8_t swtch )
{
return getSwitch( swtch, 0, 0 ) ;
}
bool getSwitch(int8_t swtch, bool nc, uint8_t level)
{
bool ret_value ;
uint8_t cs_index ;
switch(swtch){
case 0: return nc;
case MAX_DRSWITCH: return true;
case -MAX_DRSWITCH: return false;
}
#ifdef XSW_MOD
bool dir = (swtch > 0);
uint8_t aswtch = swtch ;
if ( swtch < 0 )
aswtch = -swtch ;
if (aswtch <= MAX_PSWITCH) {
aswtch = switchState(PSW_BASE + aswtch - 1) ;
return (dir ? (aswtch != ST_UP) : (aswtch == ST_UP)) ;
}
if (aswtch >= SW_3POS_BASE) {
if (aswtch > SW_3POS_END)
return false;
aswtch -= SW_3POS_BASE;
uint8_t pi = aswtch / 3;
aswtch -= pi * 3;
ret_value = (switchState(PSW_BASE + pi) == aswtch);
return (dir ? ret_value : !ret_value);
}
//custom switch, Issue 78
//use putsChnRaw
//input -> 1..4 -> sticks, 5..8 pots
//MAX,FULL - disregard
//ppm
cs_index = aswtch - MAX_PSWITCH - 1;
#else // !XSW_MOD
#ifndef SWITCH_MAPPING
if ( swtch > MAX_DRSWITCH )
{
return false ;
}
#endif
#ifdef SWITCH_MAPPING
if ( abs(swtch) > MAX_DRSWITCH )
{
uint8_t value = hwKeyState( abs(swtch) ) ;
if ( swtch > 0 )
{
return value ;
}
else
{
return ! value ;
}
}
#endif
uint8_t dir = swtch>0;
uint8_t aswtch = swtch ;
if ( swtch < 0 )
{
aswtch = -swtch ;
}
#ifdef V2
if(aswtch<(MAX_DRSWITCH-NUM_CSW-EXTRA_CSW))
#else
#if defined(CPUM128) || defined(CPUM2561)
if(aswtch<(MAX_DRSWITCH-NUM_CSW-EXTRA_CSW))
#else
if(aswtch<(MAX_DRSWITCH-NUM_CSW))
#endif
#endif
{
aswtch = keyState((EnumKeys)(SW_BASE+aswtch-1)) ;
return !dir ? (!aswtch) : aswtch ;
}
//custom switch, Issue 78
//use putsChnRaw
//input -> 1..4 -> sticks, 5..8 pots
//MAX,FULL - disregard
//ppm
#endif // XSW_MOD
#ifdef V2
#ifndef XSW_MOD
cs_index = aswtch-(MAX_DRSWITCH-NUM_CSW-EXTRA_CSW) ;
#endif
CxSwData *cs = &g_model.customSw[cs_index];
if(!cs->func) return false;
if ( level>4 )
{
ret_value = Last_switch[cs_index] & 1 ;
return dir ? ret_value : !ret_value ;
}
int8_t a = cs->v1;
int8_t b = cs->v2;
int16_t x = 0;
int16_t y = 0;
uint8_t valid = 1 ;
// init values only if needed
uint8_t s = CS_STATE(cs->func);
if(s == CS_VOFS)
{
x = getValue(cs->v1-1);
#ifdef FRSKY
if (cs->v1 > CHOUT_BASE+NUM_CHNOUT)
{
uint8_t idx = cs->v1-CHOUT_BASE-NUM_CHNOUT-1 ;
y = convertTelemConstant( idx, cs->v2 ) ;
valid = telemItemValid( idx ) ;
}
else
#endif
y = calc100toRESX(cs->v2);
}
else if(s == CS_VCOMP)
{
x = getValue(cs->v1-1);
y = getValue(cs->v2-1);
}
switch ((uint8_t)cs->func) {
case (CS_VPOS):
ret_value = (x>y);
break;
case (CS_VNEG):
ret_value = (x<y) ;
break;
case (CS_APOS):
{
ret_value = (abs(x)>y) ;
}
break;
case (CS_ANEG):
{
ret_value = (abs(x)<y) ;
}
break;
case CS_EXEQUAL:
if ( isAgvar( cs->v1 ) )
{
x *= 10 ;
y *= 10 ;
}
ret_value = abs(x-y) < 32 ;
break;
case (CS_AND):
case (CS_OR):
case (CS_XOR):
{
bool res1 = getSwitch(a,0,level+1) ;
bool res2 = getSwitch(b,0,level+1) ;
if ( cs->func == CS_AND )
{
ret_value = res1 && res2 ;
}
else if ( cs->func == CS_OR )
{
ret_value = res1 || res2 ;
}
else // CS_XOR
{
ret_value = res1 ^ res2 ;
}
}
break;
case (CS_EQUAL):
ret_value = (x==y);
break;
case (CS_NEQUAL):
ret_value = (x!=y);
break;
case (CS_GREATER):
ret_value = (x>y);
break;
case (CS_LESS):
ret_value = (x<y);
break;
#ifndef VERSION3
case (CS_EGREATER):
ret_value = (x>=y);
break;
case (CS_ELESS):
ret_value = (x<=y);
break;
#endif
case (CS_TIME):
ret_value = CsTimer[cs_index] >= 0 ;
break;
#ifdef VERSION3
case (CS_LATCH) :
case (CS_FLIP) :
ret_value = Last_switch[cs_index] & 1 ;
break ;
#endif
default:
ret_value = false;
break;
}
if ( valid == 0 ) // Catch telemetry values not present
{
ret_value = false;
}
if ( ret_value )
{
int8_t x ;
x = cs->andsw ;
if ( x )
{
ret_value = getSwitch( x, 0, level+1) ;
}
}
#ifdef VERSION3
if ( cs->func < CS_LATCH )
{
#endif
Last_switch[cs_index] = ret_value ;
#ifdef VERSION3
}
#endif
//#ifdef XSW_MOD
// return dir ? ret_value : !ret_value ;
//#else
return swtch>0 ? ret_value : !ret_value ;
//#endif
#else // !V2
#ifndef XSW_MOD
#if defined(CPUM128) || defined(CPUM2561)
cs_index = aswtch-(MAX_DRSWITCH-NUM_CSW-EXTRA_CSW);
#else
cs_index = aswtch-(MAX_DRSWITCH-NUM_CSW);
#endif
#endif
#if defined(CPUM128) || defined(CPUM2561)
if ( cs_index >= NUM_CSW )
{
CxSwData *cs = &g_model.xcustomSw[cs_index-NUM_CSW];
if(!cs->func) return false;
if ( level>4 )
{
ret_value = Last_switch[cs_index] & 1 ;
//#ifdef XSW_MOD
// return dir ? ret_value : !ret_value ;
//#else
return swtch>0 ? ret_value : !ret_value ;
//#endif