JuReceiver/system_general/rc_handler.h

134 lines
3.5 KiB
C
Raw Normal View History

2023-09-10 15:33:34 +02:00
#ifndef _RC_HANDLER_H
#define _RC_HANDLER_H
#define maxLastRcPackage 2500
#include <SPI.h>
2023-09-17 13:57:28 +02:00
#include <jmutex.h>
#include <jqueue.h>
2023-09-10 15:33:34 +02:00
#include "../structsAndDefines.h"
#include "RF24.h"
#include "nRF24L01.h"
mutex_t radioAccess;
RF24 radio(0, 4);
const uint64_t myPipe = 0xF0F0F0F000 | myId;
byte jRCbuff[32];
unsigned long jLastRCData = 0;
byte jLastStatus = 0;
void initRC() {
mutex_init(&radioAccess);
mutex_enter_blocking(&radioAccess);
if (!radio.begin()) {
Serial.println("radio hardware is not responding!!");
while (1) {
}
}
radio.enableDynamicPayloads();
2023-09-17 13:57:28 +02:00
radio.setPALevel(RF24_PA_HIGH);
radio.setDataRate(RF24_1MBPS);
2023-09-10 15:33:34 +02:00
radio.setRetries(5, 15);
radio.setChannel(myChannel);
2023-09-17 13:57:28 +02:00
radio.enableDynamicPayloads();
radio.disableAckPayload();
radio.setAutoAck(false);
2023-09-10 15:33:34 +02:00
radio.openReadingPipe(0, myPipe);
radio.startListening();
mutex_exit(&radioAccess);
}
bool jSendNRF(const void* buf, uint8_t length) {
mutex_enter_blocking(&radioAccess);
radio.stopListening();
bool succ = radio.write(buf, length);
radio.startListening();
mutex_exit(&radioAccess);
return succ;
}
#include "../rcserial.h"
#include "../system_sensors/compass.h"
#include "../system_sensors/gnss.h"
#include "../system_sensors/mpu.h"
#include "multicast.h"
#include "rc_data.h"
2023-09-17 13:57:28 +02:00
#if debugReceive
2023-09-10 15:33:34 +02:00
const String b16 = "0123456789abcdef";
String SensorDataBuffer = "";
#endif
2023-09-17 13:57:28 +02:00
bool receiveRC() {
2023-09-10 15:33:34 +02:00
mutex_enter_blocking(&radioAccess);
if (radio.available()) {
2023-09-17 13:57:28 +02:00
#if debugReceive
2023-09-10 15:33:34 +02:00
Serial.println("Radio available!");
#endif
uint8_t payloadSize = radio.getDynamicPayloadSize();
if (payloadSize >= 1) {
jLastRCData = millis();
radio.read(&jRCbuff, payloadSize);
mutex_exit(&radioAccess);
2023-09-17 13:57:28 +02:00
#if debugReceive
2023-09-10 15:33:34 +02:00
SensorDataBuffer = "data: ";
for (size_t i = 0; i < payloadSize; i++) {
SensorDataBuffer += b16[(jRCbuff[i] >> 4) & 0xf];
SensorDataBuffer += b16[jRCbuff[i] & 0xf];
}
Serial.println(SensorDataBuffer);
#endif
switch (jRCbuff[0]) {
case jType_RC: {
RCdataradioTask();
} break;
case jType_RC_SERIAL: {
_rc_serial_add((char*)(jRCbuff + 2), jRCbuff[1]);
} break;
case jType_S_MPU: {
MPUradioTask();
} break;
case jType_S_GNSS: {
GNSSradioTask();
} break;
case jType_S_COMP: {
COMPradioTask();
} break;
}
2023-09-17 13:57:28 +02:00
return true;
} else {
2023-09-10 15:33:34 +02:00
mutex_exit(&radioAccess);
2023-09-17 13:57:28 +02:00
return false;
}
} else {
2023-09-10 15:33:34 +02:00
mutex_exit(&radioAccess);
2023-09-17 13:57:28 +02:00
return false;
}
2023-09-10 15:33:34 +02:00
}
void handleRC() {
2023-09-17 13:57:28 +02:00
while (receiveRC())
;
2023-09-10 15:33:34 +02:00
if (millis() - jLastRCData > maxLastRcPackage) { // no connection
if (jLastStatus == 1) {
if (_j_on_disconnect != nullptr) _j_on_disconnect(true);
jLastStatus = 3;
2023-09-17 13:57:28 +02:00
radio.setAutoAck(false);
2023-09-10 15:33:34 +02:00
}
2023-09-17 13:57:28 +02:00
RCMulticastTask();
2023-09-10 15:33:34 +02:00
} else { // connection
if (jLastStatus != 1) {
if (_j_on_connect != nullptr) _j_on_connect();
radio.openWritingPipe(myPipe);
2023-09-17 13:57:28 +02:00
radio.setAutoAck(true);
2023-09-10 15:33:34 +02:00
radio.startListening();
jLastStatus = 1;
}
2023-09-17 13:57:28 +02:00
_handle_serial_send();
2023-09-10 15:33:34 +02:00
}
// todo: multicast, send serial
}
#endif