193 lines
3.8 KiB
C
193 lines
3.8 KiB
C
#ifndef _juPadReceiver_h_
|
|
#define _juPadReceiver_h_
|
|
|
|
#define maxLast 2500
|
|
|
|
typedef struct{
|
|
byte type = 0xff;
|
|
uint64_t pipe;
|
|
byte channel;
|
|
}nrfMulticast;
|
|
|
|
#ifndef myId
|
|
#error "myId is not defined"
|
|
#endif
|
|
#ifndef myChannel
|
|
#error "myChannel is not defined"
|
|
#endif
|
|
|
|
#define multicastChannel 124
|
|
#define multicastAddress 0xF0F0F0F0FF
|
|
|
|
#define jChannel_0 13
|
|
#define jChannel_1 12
|
|
#define jChannel_2 14
|
|
#define jChannel_3 27
|
|
#define jChannel_4 26
|
|
#define jChannel_5 25
|
|
#define jChannel_6 33
|
|
#define jChannel_7 32
|
|
#define jChannel_8 15
|
|
|
|
#define jServo 1
|
|
#define jhBridge 2
|
|
#define jESC 3
|
|
#define jDigital 4
|
|
|
|
#include <Wire.h>
|
|
#include <SPI.h>
|
|
#include "nRF24L01.h"
|
|
#include "RF24.h"
|
|
#include <Preferences.h>
|
|
Preferences preferences;
|
|
|
|
void jSendNRF(const void* buf, uint8_t length);
|
|
|
|
byte jRCbuff[32];
|
|
|
|
#include "MPU.h"
|
|
#include "RCdata.h"
|
|
#include "GNSS.h"
|
|
#include "COMP.h"
|
|
#include "BUTTONDesc.h"
|
|
|
|
//3 (CE) 0 //4 (CSN) 4 //5 (SCK) 18 //6 (MOSI) 23 //7 (MISO) 19
|
|
// CE CSN
|
|
RF24 radio(0,4);
|
|
|
|
const uint64_t myPipe = 0xF0F0F0F000|myId ;
|
|
|
|
//0 -> never, 1 -> connected, 2 -> lost/wait
|
|
byte jLastStatus = 0;
|
|
unsigned long jLast = 0;
|
|
|
|
float avgRound = 10;
|
|
unsigned long lastRound = 0;
|
|
unsigned long delta = 10;
|
|
|
|
void jSetup(){
|
|
Serial.begin(115200);
|
|
Serial.print("Starting up ... ");
|
|
preferences.begin("data", false);
|
|
if(!radio.begin()){
|
|
Serial.println("radio hardware is not responding!!");
|
|
while (1) {}
|
|
}
|
|
radio.enableDynamicPayloads();
|
|
radio.setRetries(5,15);
|
|
radio.setChannel(myChannel);
|
|
radio.openReadingPipe(0,myPipe);
|
|
radio.startListening();
|
|
Wire.begin();
|
|
initMPU();
|
|
initGNSS();
|
|
initCOMP();
|
|
initRCdata();
|
|
Serial.print("wait ... ");
|
|
delay(1000);
|
|
Serial.println("done");
|
|
#ifdef debug
|
|
Serial.println("> debuging");
|
|
#endif
|
|
lastRound = millis();
|
|
}
|
|
|
|
void jNoConn(){
|
|
if(jLastStatus==1){
|
|
onDisconnect(false);
|
|
jLastStatus=3;
|
|
}
|
|
if(random(25)==1){
|
|
#ifdef debug
|
|
Serial.println("Multicast!");
|
|
#endif
|
|
radio.stopListening();
|
|
radio.setChannel(multicastChannel);
|
|
radio.openWritingPipe(multicastAddress);
|
|
nrfMulticast data;
|
|
data.pipe = myPipe;
|
|
data.channel = myChannel;
|
|
radio.write(&data, sizeof(data), true);
|
|
radio.setChannel(myChannel);
|
|
radio.startListening();
|
|
}
|
|
}
|
|
void jConn(){
|
|
if(jLastStatus!=1){
|
|
onConnect();
|
|
radio.openWritingPipe(myPipe);
|
|
radio.startListening();
|
|
jLastStatus=1;
|
|
}
|
|
}
|
|
|
|
|
|
#ifdef debug
|
|
const String b16 = "0123456789abcdef";
|
|
|
|
String SensorDataBuffer="";
|
|
#endif
|
|
|
|
void receive(){
|
|
if(radio.available()){
|
|
#ifdef debug
|
|
Serial.println("Radio available!");
|
|
#endif
|
|
uint8_t payloadSize = radio.getDynamicPayloadSize();
|
|
if(payloadSize>=1){
|
|
jLast = millis();
|
|
radio.read( &jRCbuff, payloadSize );
|
|
#ifdef debug
|
|
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_S_MPU:{
|
|
MPUradioTask();
|
|
}break;
|
|
case jType_S_GNSS:{
|
|
GNSSradioTask();
|
|
}break;
|
|
case jType_S_COMP:{
|
|
COMPradioTask();
|
|
}break;
|
|
case jType_BUTTON_Desc:{
|
|
BUTTON_DescradioTask();
|
|
}break;
|
|
case jType_ANALOG_Desc:{
|
|
ANALOG_DescradioTask();
|
|
}break;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
void jLoop(){
|
|
unsigned long now = millis();
|
|
delta = now - lastRound;
|
|
lastRound = now;
|
|
avgRound = avgRound*0.9 + delta*0.1;
|
|
|
|
MPULoop();
|
|
GNSSLoop();
|
|
COMPLoop();
|
|
RCdataLoop();
|
|
receive();
|
|
if(millis()-jLast>maxLast)jNoConn();
|
|
else jConn();
|
|
}
|
|
|
|
void jSendNRF(const void* buf, uint8_t length){
|
|
radio.stopListening();
|
|
radio.write( buf, length );
|
|
radio.startListening();
|
|
}
|
|
|
|
#endif
|