#include <NRF24.h>
#include <SPI.h>
#pragma pack(1);
typedef struct
{
float roll;
float pitch;
float yaw;
uint16_t thrust;
} CommanderCrtpValues;
#pragma pack()
#define CRTP_HEADER(port, channel) (((port & 0x0F) << 4) | (channel & 0x0F))
#define CRTP_HEADER_PORT(h) ((h >> 4) & 0xf)
#define CRTP_HEADER_CHANNEL(h) (h & 0x3)
#define PARAM_TOC_CH 0
#define PARAM_READ_CH 1
#define PARAM_WRITE_CH 2
#define LOG_TOC_CH 0
#define LOG_CONTROL_CH 1
#define LOG_LOG_CH 2
#define LOG_MAX_OPS 64
#define LOG_MAX_BLOCKS 8
typedef enum {
CRTP_PORT_CONSOLE = 0x00,
CRTP_PORT_PARAM = 0x02,
CRTP_PORT_COMMANDER = 0x03,
CRTP_PORT_LOG = 0x05,
CRTP_PORT_LINK = 0x0F,
} CRTPPort;
#define CMD_GET_ITEM 0
#define CMD_GET_INFO 1
uint8_t address[] = { 0xe7, 0xe7, 0xe7, 0xe7, 0xe7 };
void setup()
{
Serial.begin(115200);
while (!Serial)
;
Serial.println("NRF24 init failed");
Serial.println("setChannel failed");
Serial.println("setRF failed");
Serial.println("setPipeAddress failed");
nrf24.
setConfiguration(NRF24_MASK_RX_DR | NRF24_MASK_TX_DS | NRF24_MASK_MAX_RT | NRF24_EN_CRC | NRF24_CRCO);
Serial.println("setRetry failed");
Serial.println("initialised");
}
void dump(char* prompt, uint8_t* data, uint8_t len)
{
Serial.print(prompt);
Serial.print(": ");
for (int i = 0; i < len; i++)
{
Serial.print(data[i], HEX);
Serial.print(" ");
}
Serial.println("");
}
void sendAckPayload(uint8_t* data, uint8_t len)
{
}
void sendAck()
{
sendAckPayload(0, 0);
}
void loop()
{
uint8_t buf[100];
uint8_t buflen = sizeof(buf);
static uint32_t last_second = 0;
if (nrf24.
recv(buf, &buflen))
{
if (CRTP_HEADER_PORT(buf[0]) == CRTP_PORT_LINK)
{
sendAck();
}
else if (CRTP_HEADER_PORT(buf[0]) == CRTP_PORT_COMMANDER)
{
CommanderCrtpValues *p = (CommanderCrtpValues*)(buf+1);
Serial.println(p->roll);
Serial.println(p->pitch);
Serial.println(p->yaw);
Serial.println(p->thrust);
sendAck();
}
else if (CRTP_HEADER_PORT(buf[0]) == CRTP_PORT_PARAM)
{
if (buf[1] == CMD_GET_INFO)
{
uint8_t reply[] = { CRTP_HEADER(CRTP_PORT_PARAM, PARAM_TOC_CH), CMD_GET_INFO, 1, 0, 0, 2, 0};
sendAckPayload(reply, sizeof(reply));
}
else if (buf[1] == CMD_GET_ITEM)
{
uint8_t reply[] = { CRTP_HEADER(CRTP_PORT_PARAM, PARAM_TOC_CH), CMD_GET_ITEM, 0, 1, 'x', 0, 'y', 0};
sendAckPayload(reply, sizeof(reply));
}
else
{
Serial.print("unknown param type ");
Serial.println(buf[1]);
}
}
else if (buf[0] == CRTP_HEADER_PORT(buf[0]) == CRTP_PORT_PARAM)
{
if (buf[1] == CMD_GET_INFO)
{
uint8_t reply[] = { CRTP_HEADER(CRTP_PORT_LOG, LOG_TOC_CH), CMD_GET_INFO, 1, 0, 0, 2, 1, 8, 64};
sendAckPayload(reply, sizeof(reply));
}
else if (buf[1] == CMD_GET_ITEM)
{
uint8_t reply[] = { CRTP_HEADER(CRTP_PORT_LOG, LOG_TOC_CH), CMD_GET_ITEM, 0, 7, 'p', 'm', 0, 'v', 'b', 'a', 't', 0};
sendAckPayload(reply, sizeof(reply));
}
}
else
{
}
}
uint32_t this_second = millis() / 1000;
if (this_second != last_second)
{
last_second = this_second;
}
}