/*
This project is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Multiprotocol 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.
You should have received a copy of the GNU General Public License
along with Multiprotocol. If not, see .
*/
#if defined(MOULDKG_NRF24L01_INO)
#include "iface_xn297.h"
//#define FORCE_MOULDKG_ORIGINAL_ID
#define MOULDKG_PACKET_PERIOD 5000
#define MOULDKG_BIND_PACKET_PERIOD 12000
#define MOULDKG_TX_BIND_CHANNEL 11
#define MOULDKG_RX_BIND_CHANNEL 76
#define MOULDKG_PAYLOAD_SIZE_DIGIT 5
#define MOULDKG_PAYLOAD_SIZE_ANALOG 10
#define MOULDKG_BIND_PAYLOAD_SIZE 7
#define MOULDKG_BIND_COUNT 300
#define MOULDKG_RF_NUM_CHANNELS 4
enum {
MOULDKG_BINDTX=0,
MOULDKG_BINDRX,
MOULDKG_DATA,
};
static void __attribute__((unused)) MOULDKG_send_packet()
{
uint8_t len = MOULDKG_BIND_PAYLOAD_SIZE;
memcpy(&packet[1],rx_tx_addr,3);
if(IS_BIND_IN_PROGRESS)
{
packet[0] = 0xC0;
memset(&packet[4], 0x00, 3);
}
else
{
XN297_RFChannel(hopping_frequency[(packet_count>>1)&0x03]);
if(sub_protocol == MOULDKG_ANALOG)
{
packet[0] = 0x36;
uint8_t ch[]={ 1,0,2,3 };
for(uint8_t i=0;i<4;i++)
packet[i+4] = convert_channel_8b(ch[i]);
len = MOULDKG_PAYLOAD_SIZE_ANALOG;
}
else
{//DIGIT
len = MOULDKG_PAYLOAD_SIZE_DIGIT;
uint8_t val=0;
if(packet_count&1)
{
packet[0] = 0x31;
//Button B
if(Channel_data[CH2]>CHANNEL_MAX_COMMAND) val |= 0x40;
else if(Channel_data[CH2]CHANNEL_MAX_COMMAND) val |= 0x10;
else if(Channel_data[CH3]CHANNEL_MAX_COMMAND) val |= 0x01;
else if(Channel_data[CH1]CHANNEL_MAX_COMMAND) val |= 0x04;
else if(Channel_data[CH4] RF channels 0F,1C,39,3C
XN297_ReadPayload(packet_in, MOULDKG_BIND_PAYLOAD_SIZE);
for(uint8_t i=0; i < MOULDKG_BIND_PAYLOAD_SIZE; i++)
debug("%02X ", packet_in[i]);
debugln();
//Not sure if I should test packet_in[0]
if(memcmp(&packet_in[1],&packet[1],3)==0)
{//TX ID match
//Use RX ID as address
XN297_SetTXAddr(&packet_in[4], 3);
//Calculate frequencies based on RX ID
hopping_frequency[0] = 0x0C + (packet_in[4] & 0x0F);
hopping_frequency[1] = 0x1C + (packet_in[4] >> 4);
hopping_frequency[2] = 0x2C + (packet_in[5] & 0x0F);
hopping_frequency[3] = 0x3C + (packet_in[5] >> 4);
//Switch to normal mode
BIND_DONE;
XN297_SetTxRxMode(TXRX_OFF);
phase = MOULDKG_DATA;
break;
}
}
XN297_RFChannel(MOULDKG_TX_BIND_CHANNEL); // Set TX bind channel
XN297_SetTxRxMode(TXRX_OFF);
MOULDKG_send_packet();
phase++;
return 600;
case MOULDKG_BINDRX:
//Wait for the packet transmission to finish
while(XN297_IsPacketSent()==false);
//Switch to RX
XN297_SetTxRxMode(TXRX_OFF);
XN297_RFChannel(MOULDKG_RX_BIND_CHANNEL); // Set RX bind channel
XN297_SetTxRxMode(RX_EN);
phase = MOULDKG_BINDTX;
return MOULDKG_BIND_PACKET_PERIOD-600;
case MOULDKG_DATA:
#ifdef MULTI_SYNC
telemetry_set_input_sync(MOULDKG_PACKET_PERIOD);
#endif
MOULDKG_send_packet();
break;
}
return MOULDKG_PACKET_PERIOD;
}
void MOULDKG_init()
{
BIND_IN_PROGRESS; // autobind protocol
MOULDKG_initialize_txid();
MOULDKG_RF_init();
bind_counter = MOULDKG_BIND_COUNT;
phase = MOULDKG_BINDTX;
packet_count = 0;
}
#endif
// Analog
// Bind TX: C=11 S=Y A= 4B 44 48 P(7)= C0 46 01 00 00 00 00
// Bind RX: 5A 46 01 00 63 82 4E
// Norm: C=15 S=Y A= 63 82 4E P(10)= 36 46 01 00 80 80 80 80 00 00