working version 1
This commit is contained in:
+19
-30
@@ -1,3 +1,5 @@
|
||||
#include "HardwareSerial.h"
|
||||
#include "Print.h"
|
||||
#ifndef OledDisplay_h
|
||||
#define OledDisplay_h
|
||||
|
||||
@@ -12,22 +14,18 @@
|
||||
class OledDisplay {
|
||||
private:
|
||||
Adafruit_SSD1306 display{SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET};
|
||||
int currentLine = 0;
|
||||
|
||||
public:
|
||||
void setup();
|
||||
void write(String msg);
|
||||
void clear();
|
||||
void droneMessage(String msg);
|
||||
void writeLine(char* msg, int line);
|
||||
};
|
||||
|
||||
void OledDisplay::setup() {
|
||||
// display module
|
||||
display.begin(SSD1306_SWITCHCAPVCC, 0x3C); // I2C address = 0x3C
|
||||
display.setTextSize(1);
|
||||
display.setTextColor(WHITE);
|
||||
display.setCursor(0, 0);
|
||||
display.clearDisplay();
|
||||
display.setTextColor(WHITE, BLACK);
|
||||
clear();
|
||||
display.print("Loading");
|
||||
display.display();
|
||||
for (int i = 0; i < 3; i++) {
|
||||
@@ -35,38 +33,29 @@ void OledDisplay::setup() {
|
||||
display.display();
|
||||
delay(500);
|
||||
}
|
||||
display.clearDisplay();
|
||||
clear();
|
||||
delay(10);
|
||||
write("Display is ready");
|
||||
}
|
||||
|
||||
void OledDisplay::write(String msg) {
|
||||
if (currentLine >= 60) {
|
||||
currentLine = 0;
|
||||
}
|
||||
display.setCursor(0, currentLine);
|
||||
display.print(msg);
|
||||
display.print("Display is ready");
|
||||
display.display();
|
||||
currentLine+=10;
|
||||
}
|
||||
|
||||
void OledDisplay::clear() {
|
||||
display.clearDisplay();
|
||||
display.setCursor(0, 0);
|
||||
currentLine = 0;
|
||||
}
|
||||
|
||||
void OledDisplay::droneMessage(String msg) {
|
||||
clear();
|
||||
const char delimiter[] = "|";
|
||||
|
||||
char* token = strtok(msg.c_str(), delimiter);
|
||||
|
||||
while (token != NULL) {
|
||||
Serial.println(token);
|
||||
write(String(token));
|
||||
token=strtok(NULL, delimiter);
|
||||
void OledDisplay::writeLine(char* msg, int line) {
|
||||
int x = 0;
|
||||
int y = line * 10;
|
||||
if (line >= 5) {
|
||||
x = 64;
|
||||
y -= 5 * 10;
|
||||
}
|
||||
display.setCursor(x, y);
|
||||
display.print(" "); // Print spaces to clear the line
|
||||
display.setCursor(x, y);
|
||||
display.print(msg);
|
||||
display.display();
|
||||
}
|
||||
|
||||
|
||||
#endif
|
||||
|
||||
+48
-9
@@ -1,3 +1,4 @@
|
||||
#include "HardwareSerial.h"
|
||||
#ifndef RadioController_h
|
||||
#define RadioController_h
|
||||
|
||||
@@ -10,32 +11,70 @@
|
||||
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();
|
||||
const char* sendAck(char* msg, int& tx);
|
||||
};
|
||||
|
||||
void RadioController::setup(int cepin, int cspin) {
|
||||
uint8_t address[][6] = { "1Node", "2Node" };
|
||||
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);
|
||||
// radio.openWritingPipe(writePipe);
|
||||
// radio.openReadingPipe(1, readPipe);
|
||||
// radio.setPALevel(RF24_PA_MAX);
|
||||
// radio.disableDynamicPayloads();
|
||||
// radio.setAutoAck(false);
|
||||
// radio.setDataRate( RF24_250KBPS );
|
||||
// radio.enableAckPayload();
|
||||
// radio.setRetries(15, 15); // delay, count
|
||||
|
||||
// Set the PA Level low to try preventing power supply related problems
|
||||
// because these examples are likely run with nodes in close proximity to
|
||||
// each other.
|
||||
radio.setPALevel(RF24_PA_LOW); // RF24_PA_MAX is default.
|
||||
|
||||
// to use ACK payloads, we need to enable dynamic payload lengths (for all nodes)
|
||||
radio.enableDynamicPayloads(); // ACK payloads are dynamically sized
|
||||
|
||||
// Acknowledgement packets have no payloads by default. We need to enable
|
||||
// this feature for all nodes (TX & RX) to use ACK payloads.
|
||||
radio.enableAckPayload();
|
||||
|
||||
// set the TX address of the RX node into the TX pipe
|
||||
radio.openWritingPipe(address[0]); // always uses pipe 0
|
||||
|
||||
// set the RX address of the TX node into a RX pipe
|
||||
radio.openReadingPipe(1, address[1]); // using pipe 1
|
||||
|
||||
radio.stopListening(); // put radio in TX mode
|
||||
}
|
||||
}
|
||||
const char* RadioController::sendAck(char* textToSend, int& tx) {
|
||||
bool transmissionSuccess = radio.write(textToSend, strlen(textToSend));
|
||||
tx = 0;
|
||||
if (transmissionSuccess) {
|
||||
if (radio.available()) {
|
||||
static char receivedText[8] = ""; // Declare a static char array to hold the received text
|
||||
radio.read(receivedText, sizeof(receivedText)); // Read the received text into the char array
|
||||
tx = 1;
|
||||
return receivedText; // Return the char array
|
||||
} else {
|
||||
Serial.println("Acknowledge but no data");
|
||||
}
|
||||
} else {
|
||||
Serial.println("Transmission failed");
|
||||
}
|
||||
return ""; // Return an empty char array if no data was received
|
||||
}
|
||||
|
||||
void RadioController::send(String msg) {
|
||||
radio.stopListening();
|
||||
const char *text = msg.c_str();
|
||||
bool transmissionSuccess = radio.write(text, strlen(text));
|
||||
|
||||
|
||||
+38
-20
@@ -39,7 +39,7 @@ int buttons[] = { up_button, down_button, left_button,
|
||||
int val = 0;
|
||||
|
||||
unsigned long previousTime = 0; // Variable to store the previous time
|
||||
unsigned long interval = 3000;
|
||||
unsigned long interval = 1000;
|
||||
|
||||
void setup() {
|
||||
Serial.begin(115200);
|
||||
@@ -55,49 +55,67 @@ void setup() {
|
||||
display.setup();
|
||||
|
||||
Serial.println("Setup finished");
|
||||
delay(2000);
|
||||
display.clear();
|
||||
}
|
||||
|
||||
|
||||
void loop() {
|
||||
char msgToSend[32] = "";
|
||||
|
||||
// val = map(analogRead(PIN_POTI), 0, 676, 30, 110);
|
||||
// Serial.println(val);
|
||||
|
||||
// String msg = radio.receive();
|
||||
// Serial.println(msg);
|
||||
|
||||
// display.droneMessage("PT:0.00|RL:0.00|MP_LF:1000");
|
||||
// display.droneMessage(msg);
|
||||
|
||||
delay(50);
|
||||
|
||||
String msgToSend = "";
|
||||
|
||||
unsigned long currentTime = millis(); // Get the current time
|
||||
|
||||
if (currentTime - previousTime >= interval) {
|
||||
// Perform your action here
|
||||
// This code will execute every 3 seconds
|
||||
msgToSend += "Alive|";
|
||||
strcat(msgToSend, "Alive|");
|
||||
|
||||
previousTime = currentTime; // Update the previous time
|
||||
}
|
||||
|
||||
|
||||
if (digitalRead(left_button) == LOW) {
|
||||
msgToSend += "Stop|";
|
||||
strcat(msgToSend, "Stop|");
|
||||
} else if (digitalRead(up_button) == LOW) {
|
||||
msgToSend += "Up|";
|
||||
strcat(msgToSend, "Up|");
|
||||
} else if (digitalRead(down_button) == LOW) {
|
||||
msgToSend += "Down|";
|
||||
strcat(msgToSend, "Down|");
|
||||
} else if (digitalRead(right_button) == LOW) {
|
||||
strcat(msgToSend, "MOTOR|");
|
||||
}
|
||||
|
||||
if (strlen(msgToSend) != 0) {
|
||||
display.writeLine(msgToSend, 5);
|
||||
// display.showDroneMessage();
|
||||
}
|
||||
|
||||
// Serial.print("Send:");
|
||||
// Serial.println(msgToSend);
|
||||
int tx = 0;
|
||||
const char* recText = radio.sendAck(msgToSend, tx);
|
||||
// Serial.println(recText);
|
||||
|
||||
radio.send(msgToSend);
|
||||
const char* transmitMsg = "RIP";
|
||||
if (tx == 1) {
|
||||
transmitMsg = "TX";
|
||||
}
|
||||
display.writeLine(transmitMsg, 6);
|
||||
|
||||
//display_inputs();
|
||||
delay(10);
|
||||
if (strncmp(recText, "Alive", 5) == 0) {
|
||||
display.writeLine(recText, 0);
|
||||
}else if (strncmp(recText, "BL", 2) == 0) {
|
||||
display.writeLine(recText, 1);
|
||||
}else if (strncmp(recText, "BR", 2) == 0) {
|
||||
display.writeLine(recText, 2);
|
||||
}else if (strncmp(recText, "FL", 2) == 0) {
|
||||
display.writeLine(recText, 3);
|
||||
}else if (strncmp(recText, "FR", 2) == 0) {
|
||||
display.writeLine(recText, 4);
|
||||
}
|
||||
|
||||
// display_inputs();
|
||||
delay(50);
|
||||
}
|
||||
|
||||
void display_inputs() {
|
||||
|
||||
Reference in New Issue
Block a user