1+ #include < Arduino.h>
2+ #include < unity.h>
3+
4+ #include " CRSF.h"
5+ #include " LoRaRadioLib.h"
6+ #include " targets.h"
7+ #include " common.h"
8+ #include < OTA.h>
9+
10+ CRSF crsf; // need an instance to provide the fields and code to be tested
11+ SX127xDriver Radio; // needed for Radio.TXdataBuffer
12+
13+
14+ /* Check that the round robin works
15+ * First call should return 0 for seq switches or 1 for hybrid
16+ * Successive calls should increment the next index until wrap
17+ * around from 7 to either 0 or 1 depending on mode.
18+ */
19+ void test_round_robin (void ) {
20+
21+ uint8_t expectedIndex=crsf.nextSwitchIndex ;
22+
23+ for (uint8_t i=0 ; i<10 ; i++) {
24+ uint8_t nsi = crsf.getNextSwitchIndex ();
25+ TEST_ASSERT_EQUAL (expectedIndex, nsi);
26+ expectedIndex++;
27+ if (expectedIndex == 8 ) {
28+ #ifdef HYBRID_SWITCHES_8
29+ expectedIndex = 1 ;
30+ #else
31+ expectedIndex = 0 ;
32+ #endif
33+ }
34+ }
35+ }
36+
37+ /* Check that a changed switch gets priority
38+ */
39+ void test_priority (void ) {
40+
41+ uint8_t nsi;
42+
43+ crsf.nextSwitchIndex =0 ; // this would be the next switch if nothing changed
44+
45+ // set all switches and sent values to be equal
46+ for (uint8_t i=0 ; i<N_SWITCHES; i++) {
47+ crsf.sentSwitches [i] = 0 ;
48+ crsf.currentSwitches [i] = 0 ;
49+ }
50+
51+ // set two switches' current value to be different
52+ crsf.currentSwitches [4 ] = 1 ;
53+ crsf.currentSwitches [6 ] = 1 ;
54+
55+ // we expect to get the lowest changed switch
56+ nsi = crsf.getNextSwitchIndex ();
57+ TEST_ASSERT_EQUAL (4 , nsi);
58+
59+ // The sending code would then change the sent value to match:
60+ crsf.sentSwitches [4 ] = 1 ;
61+
62+ // so now we expect to get 6 (the other changed switch we set above)
63+ nsi = crsf.getNextSwitchIndex ();
64+ TEST_ASSERT_EQUAL (6 , nsi);
65+
66+ // The sending code would then change the sent value to match:
67+ crsf.sentSwitches [6 ] = 1 ;
68+
69+ // Now all sent values should match the current values, and we expect
70+ // to get the last returned value +1
71+ nsi = crsf.getNextSwitchIndex ();
72+ TEST_ASSERT_EQUAL (7 , nsi);
73+
74+ }
75+
76+ /* Check the encoding of a packet for OTA tx
77+ */
78+ void test_encoding ()
79+ {
80+ uint8_t UID[6 ] = {MY_UID};
81+ uint8_t DeviceAddr = UID[5 ] & 0b111111 ;
82+ uint8_t expected;
83+
84+ // Define the input data
85+ // 4 channels of analog data
86+ crsf.ChannelDataIn [0 ] = 0x0123 ;
87+ crsf.ChannelDataIn [1 ] = 0x4567 ;
88+ crsf.ChannelDataIn [2 ] = 0x89AB ;
89+ crsf.ChannelDataIn [3 ] = 0xCDEF ;
90+
91+ // 8 switches
92+ for (int i=0 ; i<N_SWITCHES; i++) {
93+ crsf.currentSwitches [i] = i % 3 ;
94+ crsf.sentSwitches [i] = i % 3 ; // make all the sent values match
95+ }
96+
97+ // set the nextSwitchIndex so we know which switch to expect in the packet
98+ crsf.nextSwitchIndex =3 ;
99+
100+ // encode it
101+ GenerateChannelDataHybridSwitch8 (&Radio, &crsf, DeviceAddr);
102+
103+ // check it looks right
104+ // 1st byte is header & packet type
105+ uint8_t header = (DeviceAddr << 2 ) + RC_DATA_PACKET;
106+ TEST_ASSERT_EQUAL (header, Radio.TXdataBuffer [0 ]);
107+
108+ // bytes 1 through 5 are 10 bit packed analog channels
109+ for (int i=0 ; i<4 ; i++) {
110+ expected = crsf.ChannelDataIn [i] >> 3 ; // most significant 8 bits
111+ TEST_ASSERT_EQUAL (expected, Radio.TXdataBuffer [i+1 ]);
112+ }
113+
114+ // byte 5 is bits 1 and 2 of each analog channel
115+ expected = 0 ;
116+ for (int i=0 ; i<4 ; i++) {
117+ expected = (expected <<2 ) | ((crsf.ChannelDataIn [i] >> 1 ) & 0b11 );
118+ }
119+ TEST_ASSERT_EQUAL (expected, Radio.TXdataBuffer [5 ]);
120+
121+ // byte 6 is the switch encoding
122+ // expect switch 0 in bits 5 and 6, index in 2-4 and value in 0,1
123+ // top bit is undefined
124+ TEST_ASSERT_EQUAL (crsf.currentSwitches [0 ], (Radio.TXdataBuffer [6 ] & 0b01100000 )>>5 );
125+ TEST_ASSERT_EQUAL (3 , (Radio.TXdataBuffer [6 ] & 0b11100 )>>2 );
126+ TEST_ASSERT_EQUAL (crsf.currentSwitches [3 ], Radio.TXdataBuffer [6 ] & 0b11 );
127+ }
128+
129+ /* Check the decoding of a packet after rx
130+ */
131+
132+ void setup_switches ()
133+ {
134+ RUN_TEST (test_round_robin);
135+ RUN_TEST (test_priority);
136+ RUN_TEST (test_encoding);
137+ }
0 commit comments