PadController/nrf.h
2022-08-21 14:00:32 +02:00

123 lines
2.7 KiB
C

#include <SPI.h>
#include "nRF24L01.h"
#include "RF24.h"
//pevieas 1.3.2
#define NUM_ANALOG_channels 12
#define NUM_BTN_channels 32
byte NRFBTNcache[NUM_BTN_channels];
int16_t NRFAnalogSet[NUM_ANALOG_channels];
int32_t NRFAnalogincrement[NUM_ANALOG_channels];
#define multicastChannel 124
#define multicastAddress 0xF0F0F0F0FF
byte NRFisConnected = 0;
uint64_t myPipe = 0 ;
byte myChannel = 0;
unsigned long lastMulticast = 0;
//3 (CE) 4
//4 (CSN) 5
//5 (SCK) 18
//6 (MOSI) 23
//7 (MISO) 19
// CE CSN
RF24 radio(4,5);
#include "nrfCom.h"
void iniNRF(){
radio.begin();
radio.enableDynamicPayloads();
radio.setRetries(5,15);
radio.setAutoAck(false);
radio.setChannel(multicastChannel);
radio.openReadingPipe(0,multicastAddress);
radio.startListening();
for (byte i = 0; i < NUM_BTN_channels; i++) {
NRFBTNcache[i] = 0;
}
for (byte i = 0; i < NUM_ANALOG_channels; i++) {
NRFAnalogSet[i] = 0;
NRFAnalogincrement[i] = 0;
}
}
void NRFpress(byte btn){
if(NRFBTNcache[btn]<255)NRFBTNcache[btn]++;
}
void NRFrelese(byte btn){
if(NRFBTNcache[btn]>0)NRFBTNcache[btn]--;
}
void NRFsetAxis(byte axe, int16_t value){
NRFAnalogSet[axe] = value;
}
void NRFincreseAxis(byte axe, int16_t value){
int32_t now = NRFAnalogincrement[axe];
now+=(int32_t) value;
if(now>65536)now = 65536;
if(now<-65536)now = -65536;
NRFAnalogincrement[axe] = (int32_t) now;
}
void NRFsend(){
NRFreceive();
ndata.btns = 0;
for (byte i = 0; i < NUM_BTN_channels; i++) {
if(NRFBTNcache[i]>0){
ndata.btns |= 1<<i;
}
}
for (byte i = 0; i < NUM_ANALOG_channels; i++) {
int32_t now = (int32_t)NRFAnalogSet[i];
now+=(int32_t) NRFAnalogincrement[i];
if(now>32767)now = 32767;
if(now<-32767)now = -32767;
ndata.analog[i] = (int16_t) now;
NRFAnalogSet[i] = 0;
NRFAnalogincrement[i] = 0;
}
leds[21] = NRFisConnected == 1 ? NRFColorConnected : NRFColorNotConnected;
if(NRFisConnected==1)sendRCdata(&ndata,sizeof(ndata));
}
byte notsendcount = 0;
const uint16_t emptyBuffer = 0xffff;
void NRFnotsend(){
NRFreceive();
if(NRFisConnected==1&&((notsendcount++)%40)==0)sendRCdata(&emptyBuffer, 2, true);
}
void NRFclick(){
if(NRFisConnected != 1){
if(myPipe != 0&&millis()-lastMulticast < 2000){
radio.stopListening();
radio.setAutoAck(true);
radio.setChannel(myChannel);
radio.openWritingPipe(myPipe);
radio.openReadingPipe(0,myPipe);
radio.startListening();
NRFisConnected = 1;
}
}else{
radio.stopListening();
radio.setAutoAck(false);
radio.setChannel(multicastChannel);
radio.openReadingPipe(0,multicastAddress);
radio.startListening();
NRFisConnected = 2;
}
}
void NRFshutdown(){
}