Files
remote/Nrf.h
T
mangavalk ee57e22b26 wip wireless
payload max 32byte problem
2019-05-26 22:29:00 +02:00

66 lines
1.5 KiB
C

#include <SPI.h>
#include "RF24.h"
#include "TransferDataStructure.h"
// Nrf24 settings
byte addresses[][6] = {"1Node", "2Node"};
RF24 radio(9, 10); // CE, CSN
void NrfInit()
{
radio.begin();
// Set the PA Level low to prevent power supply related issues since this is a
// getting_started sketch, and the likelihood of close proximity of the devices. RF24_PA_MAX is default.
radio.setPALevel(RF24_PA_LOW);
radio.openWritingPipe(addresses[0]);
radio.openReadingPipe(1, addresses[1]);
// Possibly init data object here
radio.startListening();
}
dataStruct2 NrfSendData(dataStruct2 data, bool debug = false)
{
radio.stopListening();
// First, stop listening so we can talk.
if (debug)
Serial.println(F("Now sending"));
if (!radio.write( &data, sizeof(data) )) {
if (debug)
Serial.println(F("failed"));
return data;
}
radio.startListening();
// Now, continue listening
unsigned long started_waiting_at = micros();
// Set up a timeout period, get the current microseconds
boolean timeout = false;
// Set up a variable to indicate if a response was received or not
while ( ! radio.available() ) {
if (micros() - started_waiting_at > 200000 ) {
// If waited longer than 200ms, indicate timeout and exit while loop
timeout = true;
break;
}
}
if ( timeout ) {
if (debug)
Serial.println(F("Failed, response timed out."));
return data;
} else {
// Grab the response, compare, and send to debugging spew
radio.read( &data, sizeof(data) );
return data;
}
}