Traxxas TQ 1st gen: try 5
[DIY-Multiprotocol-TX-Module.git] / Multiprotocol / SHENQI_nrf24l01.ino
blobff4f7f18c3c6d9c7157e89383423fa968abcb4f8
1 /*
2  This project is free software: you can redistribute it and/or modify
3  it under the terms of the GNU General Public License as published by
4  the Free Software Foundation, either version 3 of the License, or
5  (at your option) any later version.
7  Multiprotocol is distributed in the hope that it will be useful,
8  but WITHOUT ANY WARRANTY; without even the implied warranty of
9  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10  GNU General Public License for more details.
12  You should have received a copy of the GNU General Public License
13  along with Multiprotocol.  If not, see <http://www.gnu.org/licenses/>.
14  */
16 #if defined(SHENQI_NRF24L01_INO)
18 #include "iface_nrf24l01.h"
20 const uint8_t PROGMEM SHENQI_Freq[] = {
21                         50,50,20,60,30,40,
22                         10,30,40,20,60,10,
23                         50,20,50,40,10,60,
24                         30,30,60,10,40,50,
25                         20,10,60,20,50,30,
26                         40,40,30,50,20,60,
27                         10,10,20,30,40,50,
28                         60,60,50,40,30,20,
29                         10,60,10,50,30,40,
30                         20,10,40,30,60,20 };
32 void SHENQI_RF_init()
34     NRF24L01_Initialize();
36         LT8900_Config(4, 8, _BV(LT8900_CRC_ON)|_BV(LT8900_PACKET_LENGTH_EN), 0xAA);
37         LT8900_SetChannel(2);
38         LT8900_SetAddress((uint8_t *)"\x9A\x9A\x9A\x9A",4);
39         LT8900_SetTxRxMode(RX_EN);
42 void SHENQI_send_packet()
44         packet[0]=0x00;
45         if(packet_count==0)
46         {
47                 uint8_t bind_addr[4];
48                 bind_addr[0]=rx_tx_addr[0];
49                 bind_addr[1]=rx_tx_addr[1];
50                 bind_addr[2]=0x9A;
51                 bind_addr[3]=0x9A;
52                 LT8900_SetAddress(bind_addr,4);
53                 LT8900_SetChannel(2);
54                 packet[1]=rx_tx_addr[2];
55                 packet[2]=rx_tx_addr[3];
56                 packet_period=2508;
57         }
58         else
59         {
60                 #ifdef MULTI_SYNC
61                         if(packet_count==1)
62                                 telemetry_set_input_sync(3000+2508+6*1750);
63                 #endif
64                 LT8900_SetAddress(rx_tx_addr,4);
65                 packet[1]=255-convert_channel_8b(RUDDER);
66                 packet[2]=255-convert_channel_16b_limit(THROTTLE,0x60,0xA0);
67                 uint8_t freq=pgm_read_byte_near(&SHENQI_Freq[hopping_frequency_no])+(rx_tx_addr[2]&0x0F);
68                 LT8900_SetChannel(freq);
69                 hopping_frequency_no++;
70                 if(hopping_frequency_no==60)
71                         hopping_frequency_no=0;
72                 packet_period=1750;
73         }
74         // Send packet + 1 retransmit - not sure why but needed (not present on original TX...)
75         LT8900_WritePayload(packet,3);
76         while(NRF24L01_packet_ack()!=PKT_ACKED);
77         LT8900_WritePayload(packet,3);
78         
79         packet_count++;
80         if(packet_count==7)
81         {
82                 packet_count=0;
83                 packet_period=3000;
84         }
85         // Set power
86         NRF24L01_SetPower();
89 uint16_t SHENQI_callback()
91         if(IS_BIND_DONE)
92         {
93                 SHENQI_send_packet();
94         }
95         else
96         {
97                 if( NRF24L01_ReadReg(NRF24L01_07_STATUS) & _BV(NRF24L01_07_RX_DR))
98                 {
99                         if(LT8900_ReadPayload(packet, 3))
100                         {
101                                 BIND_DONE;
102                                 rx_tx_addr[0]=packet[1];
103                                 rx_tx_addr[1]=packet[2];
104                                 LT8900_SetTxRxMode(TX_EN);
105                                 packet_period=14000;
106                         }
107                         NRF24L01_FlushRx();
108                 }
109         }
110     return packet_period;
113 void SHENQI_init()
115         BIND_IN_PROGRESS;       // autobind protocol
116         SHENQI_RF_init();
117         hopping_frequency_no = 0;
118         packet_count=0;
119         packet_period=500;
122 #endif