init
This commit is contained in:
@@ -0,0 +1,62 @@
|
||||
#ifndef RadioController_h
|
||||
#define RadioController_h
|
||||
|
||||
#include <Arduino.h>
|
||||
// nRF24L01+
|
||||
#include <SPI.h>
|
||||
#include <nRF24L01.h>
|
||||
#include <RF24.h>
|
||||
|
||||
class RadioController {
|
||||
private:
|
||||
RF24 radio; // CE, CSN
|
||||
const uint64_t writePipe = 0xE8E8F0F0E1LL;
|
||||
const uint64_t readPipe = 0xE8E8F0F0E2LL;
|
||||
public:
|
||||
void setup(int cepin, int cspin);
|
||||
void send(String msg);
|
||||
String receive();
|
||||
};
|
||||
|
||||
void RadioController::setup(int cepin, int cspin) {
|
||||
radio = RF24(cepin, cspin);
|
||||
|
||||
if (!radio.begin()) {
|
||||
Serial.println("Error initializing radio");
|
||||
} else {
|
||||
Serial.println("Succesfully initializing radio");
|
||||
radio.openWritingPipe(writePipe);
|
||||
radio.openReadingPipe(1, readPipe);
|
||||
radio.setPALevel(RF24_PA_MAX);
|
||||
radio.disableDynamicPayloads();
|
||||
radio.setAutoAck(false);
|
||||
radio.setRetries(5, 15);
|
||||
}
|
||||
}
|
||||
|
||||
void RadioController::send(String msg) {
|
||||
radio.stopListening();
|
||||
const char *text = msg.c_str();
|
||||
bool transmissionSuccess = radio.write(text, strlen(text));
|
||||
|
||||
if (transmissionSuccess) {
|
||||
// Serial.println("Transmission successful");
|
||||
} else {
|
||||
Serial.println("Transmission failed");
|
||||
}
|
||||
}
|
||||
|
||||
String RadioController::receive() {
|
||||
radio.startListening();
|
||||
String msg = "";
|
||||
|
||||
if (radio.available()) {
|
||||
char text[32] = "";
|
||||
radio.read(&text, sizeof(text));
|
||||
msg = String(text);
|
||||
}
|
||||
|
||||
return msg;
|
||||
}
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user