2020#include " iface_nrf24l01.h"
2121
2222#define CFLIE_BIND_COUNT 60
23+ // #define CFLIE_USE_CRTP_RPYT
2324
2425// =============================================================================
2526// CRTP (Crazy RealTime Protocol) Implementation
@@ -220,8 +221,8 @@ static uint8_t packet_ack()
220221
221222static void set_rate_channel (uint8_t rate, uint8_t channel)
222223{
223- NRF24L01_WriteReg (NRF24L01_05_RF_CH, channel); // Defined by model id
224- NRF24L01_SetBitrate (rate); // Defined by model id
224+ NRF24L01_WriteReg (NRF24L01_05_RF_CH, channel);
225+ NRF24L01_SetBitrate (rate);
225226}
226227
227228static void send_search_packet ()
@@ -232,23 +233,26 @@ static void send_search_packet()
232233 NRF24L01_WriteReg (NRF24L01_07_STATUS, (BV (NRF24L01_07_TX_DS) | BV (NRF24L01_07_MAX_RT)));
233234 NRF24L01_FlushTx ();
234235
235- if (rf_ch_num++ > 125 )
236- {
237- rf_ch_num = 0 ;
238- switch (data_rate)
239- {
240- case NRF24L01_BR_250K:
241- data_rate = NRF24L01_BR_1M;
242- break ;
243- case NRF24L01_BR_1M:
244- data_rate = NRF24L01_BR_2M;
245- break ;
246- case NRF24L01_BR_2M:
247- data_rate = NRF24L01_BR_250K;
248- break ;
249- }
250- }
251- set_rate_channel (data_rate, rf_ch_num);
236+ if (sub_protocol == CFLIE_AUTO)
237+ {
238+ if (rf_ch_num++ > 125 )
239+ {
240+ rf_ch_num = 0 ;
241+ switch (data_rate)
242+ {
243+ case NRF24L01_BR_250K:
244+ data_rate = NRF24L01_BR_1M;
245+ break ;
246+ case NRF24L01_BR_1M:
247+ data_rate = NRF24L01_BR_2M;
248+ break ;
249+ case NRF24L01_BR_2M:
250+ data_rate = NRF24L01_BR_250K;
251+ break ;
252+ }
253+ }
254+ }
255+ set_rate_channel (data_rate, rf_ch_num);
252256
253257 NRF24L01_WritePayload (buf, sizeof (buf));
254258
@@ -355,7 +359,7 @@ static void send_crtp_rpyt_packet()
355359 send_packet ();
356360}
357361
358- /* static void send_crtp_cppm_emu_packet()
362+ static void send_crtp_cppm_emu_packet ()
359363{
360364 struct CommanderPacketCppmEmu {
361365 struct {
@@ -376,7 +380,7 @@ static void send_crtp_rpyt_packet()
376380
377381 // Make sure the number of aux channels in use is capped to MAX_CPPM_AUX_CHANNELS
378382 // uint8_t numAuxChannels = Model.num_channels - 4;
379- uint8_t numAuxChannels = 2; // TODO: Figure this out correctly
383+ uint8_t numAuxChannels = 4 ;
380384 if (numAuxChannels > MAX_CPPM_AUX_CHANNELS)
381385 {
382386 numAuxChannels = MAX_CPPM_AUX_CHANNELS;
@@ -385,16 +389,16 @@ static void send_crtp_rpyt_packet()
385389 cpkt.hdr .numAuxChannels = numAuxChannels;
386390
387391 // Remap AETR to AERT (RPYT)
388- cpkt.channelRoll = convert_channel_16b_limit(AILERON,1000, 2000);
392+ cpkt.channelRoll = convert_channel_16b_limit (AILERON,2000 , 1000 );
389393 cpkt.channelPitch = convert_channel_16b_limit (ELEVATOR,1000 ,2000 );
390394 // Note: T & R Swapped:
391395 cpkt.channelYaw = convert_channel_16b_limit (RUDDER, 1000 , 2000 );
392396 cpkt.channelThrust = convert_channel_16b_limit (THROTTLE, 1000 , 2000 );
393397
394398 // Rescale the rest of the aux channels - RC channel 4 and up
395- for (uint8_t i = 4 ; i < 14 ; i++)
399+ for (uint8_t i = 0 ; i < 10 ; i++)
396400 {
397- cpkt.channelAux[i] = convert_channel_16b_limit(i, 1000, 2000 );
401+ cpkt.channelAux [i] = convert_channel_16b_limit (i+ 4 , 2000 , 1000 );
398402 }
399403
400404 // Total size of the commander packet is a 1-byte header, 4 2-byte channels and
@@ -409,25 +413,19 @@ static void send_crtp_rpyt_packet()
409413 memcpy (&packet[2 ], (char *)&cpkt, commanderPacketSize); // Why not use sizeof(cpkt) here??
410414 tx_payload_len = 2 + commanderPacketSize; // CRTP header, commander type, and packet
411415 send_packet ();
412- }*/
416+ }
413417
414418static void send_cmd_packet ()
415419{
416- // TODO: Fix this so we can actually configure the packet type
417- // switch(Model.proto_opts[PROTOOPTS_CRTP_MODE])
418- // {
419- // case CRTP_MODE_CPPM:
420- // send_crtp_cppm_emu_packet();
421- // break;
422- // case CRTP_MODE_RPYT:
423- // send_crtp_rpyt_packet();
424- // break;
425- // default:
426- // send_crtp_rpyt_packet();
427- // }
428-
429- // send_crtp_cppm_emu_packet(); // oh maAAAn
430- send_crtp_rpyt_packet ();
420+ #if defined(CFLIE_USE_CRTP_RPYT)
421+ {
422+ send_crtp_rpyt_packet ();
423+ }
424+ #else
425+ {
426+ send_crtp_cppm_emu_packet ();
427+ }
428+ #endif
431429}
432430
433431// State machine for setting up CRTP logging
@@ -794,52 +792,36 @@ static uint8_t CFLIE_initialize_rx_tx_addr()
794792 rx_tx_addr[3 ] =
795793 rx_tx_addr[4 ] = 0xE7 ; // CFlie uses fixed address
796794
797- // if (Model.fixed_id) {
798- // rf_ch_num = Model.fixed_id % 100;
799- // switch (Model.fixed_id / 100) {
800- // case 0:
801- // data_rate = NRF24L01_BR_250K;
802- // break;
803- // case 1:
804- // data_rate = NRF24L01_BR_1M;
805- // break;
806- // case 2:
807- // data_rate = NRF24L01_BR_2M;
808- // break;
809- // default:
810- // break;
811- // }
812-
813- // if (Model.proto_opts[PROTOOPTS_TELEMETRY] == TELEM_ON_CRTPLOG) {
814- // return CFLIE_INIT_CRTP_LOG;
815- // } else {
816- // return CFLIE_INIT_DATA;
817- // }
818- // } else {
819- // data_rate = NRF24L01_BR_250K;
820- // rf_ch_num = 10;
821- // return CFLIE_INIT_SEARCH;
822- // }
823-
824- // Default 1
825- data_rate = NRF24L01_BR_1M;
826- rf_ch_num = 10 ;
827-
828- // Default 2
829- // data_rate = NRF24L01_BR_2M;
830- // rf_ch_num = 110;
795+ switch (sub_protocol) {
796+ case CFLIE_2Mbps:
797+ data_rate = NRF24L01_BR_2M;
798+ rf_ch_num = option;
799+ break ;
800+ case CFLIE_1Mbps:
801+ data_rate = NRF24L01_BR_1M;
802+ rf_ch_num = option;
803+ break ;
804+ case CFLIE_250kbps:
805+ data_rate = NRF24L01_BR_250K;
806+ rf_ch_num = option;
807+ break ;
808+ default :
809+ data_rate = NRF24L01_BR_2M;
810+ rf_ch_num = 80 ;
811+ }
812+
831813 return CFLIE_INIT_SEARCH;
832814}
833815
834816void CFLIE_init (void )
835817{
836818 BIND_IN_PROGRESS; // autobind protocol
837819
838- phase = CFLIE_initialize_rx_tx_addr ();
839- crtp_log_setup_state = CFLIE_CRTP_LOG_SETUP_STATE_INIT;
840- packet_count=0 ;
820+ phase = CFLIE_initialize_rx_tx_addr ();
821+ crtp_log_setup_state = CFLIE_CRTP_LOG_SETUP_STATE_INIT;
822+ packet_count=0 ;
841823
842- CFLIE_RF_init ();
824+ CFLIE_RF_init ();
843825}
844826
845- #endif
827+ #endif
0 commit comments