@@ -20,21 +20,21 @@ Multiprotocol is distributed in the hope that it will be useful,
2020
2121// #define FORCE_SGF22_ORIGINAL_ID
2222
23- #define SGF22_PACKET_PERIOD 11950 // 10240
24- #define SGF22_BIND_RF_CHANNEL 78
25- #define SGF22_PAYLOAD_SIZE 12
26- #define SGF22_BIND_COUNT 50
27- #define SGF22_RF_NUM_CHANNELS 4
28- #define SGF22_F22S_BIND_RF_CHANNEL 10
29- #define SGF22_J20_BIND_RF_CHANNEL 28
23+ #define SGF22_PACKET_PERIOD 11950 // 10240
24+ #define SGF22_BIND_RF_CHANNEL 78
25+ #define SGF22_PAYLOAD_SIZE 12
26+ #define SGF22_BIND_COUNT 50
27+ #define SGF22_RF_NUM_CHANNELS 4
28+ #define SGF22_F22S_BIND_RF_CHANNEL 10
29+ #define SGF22_J20_BIND_RF_CHANNEL 28
3030
3131// packet[8]
32- #define SGF22_FLAG_3D 0x00
33- #define SGF22_FLAG_LIGHT 0x04
34- #define SGF22_FLAG_ROLL 0x08
35- #define SGF22_FLAG_VIDEO 0x10
36- #define SGF22_FLAG_6G 0x40
37- #define SGF22_FLAG_VERTICAL 0xC0
32+ #define SGF22_FLAG_3D 0x00
33+ #define SGF22_FLAG_LIGHT 0x04
34+ #define SGF22_FLAG_ROLL 0x08
35+ #define SGF22_FLAG_VIDEO 0x10
36+ #define SGF22_FLAG_6G 0x40
37+ #define SGF22_FLAG_VERTICAL 0xC0
3838
3939#define SGF22_J20_FLAG_HORIZONTAL 0x80
4040// #define SGF22_J20_FLAG_SPEED 0x01 // Up/Down trim, not implemented
@@ -44,9 +44,19 @@ Multiprotocol is distributed in the hope that it will be useful,
4444
4545
4646// packet[9]
47- #define SGF22_FLAG_TRIMRESET 0x04
48- #define SGF22_FLAG_PHOTO 0x40 // #define SGF22_J20_FLAG_INVERT 0x40
49- #define SGF22_J20_FLAG_FIXHEIGHT 0x80
47+ #define SGF22_FLAG_TRIMRESET 0x04
48+ #define SGF22_FLAG_PHOTO 0x40 // #define SGF22_J20_FLAG_INVERT 0x40
49+ #define SGF22_J20_FLAG_FIXHEIGHT 0x80
50+
51+ #define SGF22_WRITE_TIME 1000
52+ // #define SGF22_HUB_TELEMETRY
53+
54+ enum {
55+ SGF22_DATA1,
56+ SGF22_DATA2,
57+ SGF22_DATA3,
58+ SGF22_RX,
59+ };
5060
5161static void __attribute__ ((unused)) SGF22_send_packet()
5262{
@@ -148,34 +158,80 @@ static void __attribute__((unused)) SGF22_RF_init()
148158{
149159 XN297_Configure (XN297_CRCEN, XN297_SCRAMBLED, XN297_1M);
150160 XN297_SetTXAddr ((uint8_t *)" \xC7\x95\x3C\xBB\xA5 " , 5 );
161+ #ifdef SGF22_HUB_TELEMETRY
162+ XN297_SetRXAddr ((uint8_t *)" \xC7\x95\x3C\xBB\xA5 " , SGF22_PAYLOAD_SIZE);
163+ #endif
164+
151165 const uint8_t bind_chan[] = {SGF22_BIND_RF_CHANNEL, SGF22_F22S_BIND_RF_CHANNEL, SGF22_J20_BIND_RF_CHANNEL};
152166 XN297_RFChannel (bind_chan[sub_protocol]); // Set bind channel
153167}
154168
155169uint16_t SGF22_callback ()
156170{
157- if (phase == 0 )
171+ #ifdef SGF22_HUB_TELEMETRY
172+ bool rx = false ;
173+ #endif
174+
175+ switch (phase)
158176 {
159- phase++;
160- #ifdef MULTI_SYNC
161- telemetry_set_input_sync (SGF22_PACKET_PERIOD);
162- #endif
163- SGF22_send_packet ();
164- if (IS_BIND_IN_PROGRESS)
165- {
166- if (--bind_counter==0 )
167- BIND_DONE;
168- }
169- }
170- else
171- {// send 3 times in total the same packet
172- XN297_ReSendPayload ();
173- phase++;
174- if (phase > 2 )
175- {
176- phase = 0 ;
177- return SGF22_PACKET_PERIOD - 2 *1550 ;
178- }
177+ case SGF22_DATA1:
178+ #ifdef MULTI_SYNC
179+ telemetry_set_input_sync (SGF22_PACKET_PERIOD);
180+ #endif
181+ #ifdef SGF22_HUB_TELEMETRY
182+ rx = XN297_IsRX ();
183+ XN297_SetTxRxMode (TXRX_OFF);
184+ #endif
185+ SGF22_send_packet ();
186+ if (IS_BIND_IN_PROGRESS)
187+ {
188+ if (--bind_counter==0 )
189+ BIND_DONE;
190+ }
191+ #ifdef SGF22_HUB_TELEMETRY
192+ if (rx)
193+ {
194+ uint8_t p_len = XN297_ReadEnhancedPayload (packet_in, SGF22_PAYLOAD_SIZE);
195+ if (p_len == 3 && packet_in[0 ] == rx_tx_addr[2 ] && packet_in[1 ] == rx_tx_addr[3 ])
196+ {// packets only received when battery is low: 00 0B 01
197+ telemetry_link = 1 ;
198+ v_lipo1 = packet_in[2 ] ? 0 : 255 ;
199+ }
200+ #if 0
201+ debug("L %d ",p_len);
202+ debug("RX");
203+ for(uint8_t i=0; i<SGF22_PAYLOAD_SIZE; i++)
204+ debug(" %02X",packet_in[i]);
205+ debugln("");
206+ #endif
207+ }
208+ #endif
209+ phase++;
210+ break ;
211+ case SGF22_DATA2:
212+ case SGF22_DATA3:
213+ // send 3 times in total the same packet
214+ XN297_ReSendPayload ();
215+ phase++;
216+ break ;
217+ default : // SGF22_RX
218+ #ifdef SGF22_HUB_TELEMETRY
219+ /* { // Wait for packet to be sent before switching to receive mode
220+ uint16_t start=(uint16_t)micros(), count=0;
221+ while ((uint16_t)((uint16_t)micros()-(uint16_t)start) < 500)
222+ {
223+ if(XN297_IsPacketSent())
224+ break;
225+ count++;
226+ }
227+ debugln("%d",count);
228+ }*/
229+ // Switch to RX
230+ XN297_SetTxRxMode (TXRX_OFF);
231+ XN297_SetTxRxMode (RX_EN);
232+ #endif
233+ phase = SGF22_DATA1;
234+ return SGF22_PACKET_PERIOD - 3 *1550 ;
179235 }
180236 return 1550 ;
181237}
@@ -187,7 +243,10 @@ void SGF22_init()
187243 SGF22_RF_init ();
188244 bind_counter=SGF22_BIND_COUNT;
189245 packet_sent = packet_count = 0x26 ; // TX2:26 TX3:26
190- phase = 0 ;
246+ phase = SGF22_DATA1;
247+ #ifdef SGF22_HUB_TELEMETRY
248+ RX_RSSI = 100 ; // Dummy value
249+ #endif
191250}
192251
193252#endif
0 commit comments