////////////////////////////////////////////////////////////////////////////////
// Name:       LoRaPingPong                                                   //
// Platform:   Arduino UNO R3                                                 //
// Created by: HARB rboek2@gmail.com June 2020 GPL copyrights                 //
// http://robotigs.nl/robots/includes/parts.php?idpart=344                    //
// Met een simpele setting in het programma wordt er 1 van beide de           //
// hoofdzender (ping) die wordt aangesloten op de seriele monitor. De andere  //
// transceiver (pong) wordt enkel op de voedingsspanning aangesloten. Door nu //
// een `T` commando te geven zal ping de huidige tijd naar pong sturen. Pong  //
// stuurt de ontvangen gegevens dan weer terug en ping laat zien hoe lang de  //
// hele cyclus heeft geduurd.                                                 //
////////////////////////////////////////////////////////////////////////////////

/*
* Getting Started example sketch for nRF24L01+ radios
* This is a very basic example of how to send data from one node to another
* Updated: Dec 2014 by TMRh20
*/

  //Define the needed header files for the precompiler, no charge if not used --
  #include <SPI.h>      //Serial Peripheral Interface requiered by software LoRa
              // http://robotigs.nl/robotigs/includes/parts_header.php?idpart=28
  #include <nRF24L01.h>
  #include <RF24.h>
  #include <printf.h>             //Needed to send to monitor radio.printDetails

  //Define PINS ----------------------------------------------------------------
  #define CE_PIN  10              //CE (Chip Enable) is an active-HIGH pin = SS?
  #define CSN_PIN  9                //CSN (Chip Select Not) is an active-LOW pin

  //Define EEPROM variables ----------------------------------------------------
  //Define DATABASE VARIABLES --------------------------------------------------
  //Define variables -----------------------------------------------------------
  int    ledOnBoardVal  =   LOW; //You choose HIGH-on or LOW-off for LED_BUILTIN
  const byte thisSlaveAddress[5] = {'R','x','A','A','A'};
  char dataReceived[10]; // this must match dataToSend in the TX
  bool newData = false;
  char tmp;
  unsigned long countOk, countNok;  //Counters for long term tests on stabillity

  //User configuration RF24 radio ----------------------------------------------
  bool radioNumber    = 1;     //Set this radio as radio number 1=Ping or 0=Pong
  bool role           = 1;  //Sending or receiving role: 1=Ping out, 0=Pong back
  byte addresses[][6] = {"1Node","2Node"};

  //Initialize OBJECTS ---------------------------------------------------------
  RF24 radio(CE_PIN, CSN_PIN);        //SPI & 10=CE 9=CSN Hardware configuration
//END OF PRECOMPILER OPTIONS ---------------------------------------------------


void setup() { //Setup runs once ***********************************************
  Serial.begin(115200);
  Serial.println("LoRaPingPong setup started");
  Serial.println("");
  pinMode(CE_PIN, OUTPUT);
  pinMode(CSN_PIN, OUTPUT);

  radio.begin();
  radio.setRetries(4,5);                                      //Delay=3, count=5
  radio.setDataRate(RF24_250KBPS);
  radio.setPALevel(RF24_PA_MIN);             //RF24_PA_MAX=default, HIGH LOW MIN

  if(radioNumber){               //Open a writing and reading pipe on each radio
    radio.openWritingPipe(addresses[1]);
    radio.openReadingPipe(1,addresses[0]);
  }else{                                 //Use opposite addresses for each radio
    radio.openWritingPipe(addresses[0]);
    radio.openReadingPipe(1,addresses[1]);
  } //End of if(radioNumber)

  printf_begin();        //Needed to print the radio.printDetails() to a monitor
  radio.printDetails();               //Set of data about the transceiver status
  radio.startListening();                  // Start the radio listening for data

  if(radioNumber){
    Serial.println("");
    Serial.println("Setup done, Ping is running");
    Serial.println("*** Enter 'R' or 'r' to change to receiver mode");
    Serial.println("");
  }else{                                    //Use opposite texts for each radio
    Serial.println("");
    Serial.println("Setup done, Pong is running");
    Serial.println("*** Enter 'T' or 't' to change to sender mode");
    Serial.println("");   
  }
} //End of setup ---------------------------------------------------------------




void loop() { //KEEP ON RUNNING THIS LOOP FOREVER  *****************************
  if (role == 1) {                                               //Ping Out Role
    pingout() ;                  //Ping sends out timestamp and waits for return
  } //End of if (role == 1)

  if (role == 0) {                                              //Pong Back Role
    pongback();              //Pong receives timestamp and returns own timestamp
  } //End of if (role == 0)

  changeRoleSerial();                         //Change Roles via Serial Commands
} //End of void loop() ----------------------- KEEP ON RUNNING THIS LOOP FOREVER




void pingout(void) { //Ping sends out timestamp and waits for return ***********
  radio.stopListening();                   //First stop listening so we can talk
  Serial.println("*** Now sending");            //Show the start of the sequence

  unsigned long start_time = micros();               //Read the time and send it
  if (!radio.write( &start_time, sizeof(unsigned long) )){
    Serial.println("    Error radio.write failed");                      //Error
    radio.startListening();                 //Continue listening till next round
  } //End of if (!radio.write so

  radio.startListening();              //Continue listening for answer from Pong
  unsigned long started_waiting_at = micros();        //Start timer microseconds
  boolean timeoutbool = false;         //Indicate if radio is receiving anything

    while (!radio.available()){                 //As long as nothing is received
      if (micros() - started_waiting_at > 200000 ){     //If waited longer 200ms
          timeoutbool = true;             //Then indicate timeout and exit while
          break;                            //Exit waiting in case of a time-out
      } //End of if (micros() - started_waiting_at > 200000 )
    } //End of while (!radio.available())

    if (timeoutbool){                                     //Describe the results
        countNok++;                                           //One more failure
        Serial.println("    Failed, response timed out.");
    }else{
        unsigned long got_time;
        radio.read( &got_time, sizeof(unsigned long) );//Read response from Pong
        unsigned long end_time = micros();
        countOk++;                           //One more succesfull communication
        Serial.print("    Sent ");
        Serial.print(start_time);
        Serial.print(", Got response ");
        Serial.print(got_time);
        Serial.print(", Round-trip delay ");
        Serial.print(end_time-start_time);     //Compare and send debugging spew
        Serial.println(" microseconds");
    }


  Serial.print("    Succesvol:");
  Serial.print(countOk);
  Serial.print("  Mislukkingen:");
  Serial.print(countNok);
  Serial.println("  Wacht 4 seconden");
  Serial.println("");
  delay(4000);     //Try again x seconds later
} //Exit pingout ---------------------------------------------------------------
//345678911234567892123456789312345678941234567895123456789612345678971234567898





void pongback(void) { //Pong receives timestamp and returns own timestamp ******
  unsigned long got_time;                  //Variable for the received timestamp
  if (radio.available()){ 
    while (radio.available()) {                                   // While there is data ready
      radio.read( &got_time, sizeof(unsigned long) );             // Get the payload
    }
    //if (got_time > 0){
      radio.stopListening();                                        // First, stop listening so we can talk   
      radio.write( &got_time, sizeof(unsigned long) );              // Send the final one back.      
      radio.startListening();                                       // Now, resume listening so we catch the next packets.
      countOk++;                           //One more succesfull communication
      Serial.print(countOk);
      Serial.print(" antwoord verzonden ");
      Serial.println(got_time);  
    //}
  } else {
     //Serial.println("nothing available");
  }//End of if (radio.available())
} //Exit pongback --------------------------------------------------------------





void changeRoleSerial(void) { //Change Roles via Serial Commands ***************
  if (Serial.available()){
    char c = toupper(Serial.read());
    if ( c == 'T' && role == 0 ){
      Serial.println("Ping rol start. Enter 'R' of 'r' om terug te schakelen");
      role = 1;                        //Become the primary transmitter ping out
    }else if ( c == 'R' && role == 1 ){
      Serial.println("Pong rol start. Enter 'T' of 't' om terug te schakelen");
      role = 0;                         //Become the primary receiver, pong back
      radio.startListening();       //En laten we dan gelijk maar gaan luisteren
    } //End of if ( c == 'T' && role == 0 )
  } //End of if (Serial.available())
} //Exit changeRoleSerial ------------------------------------------------------







void getData() {
  newData = false;
    if ( radio.available() ) {
        radio.read( &dataReceived, sizeof(dataReceived) );
        newData = true;
    }
    if (newData == true) {
        Serial.print("Data received ");
        Serial.print(dataReceived);
        newData = false;
    } else {
        Serial.print("Nott received ");
    }
}

void showData() {
    if (newData == true) {
        Serial.print("Data received ");
        Serial.println(dataReceived);
        newData = false;
    }
}

void showChipConnected() {
  if (radio.isChipConnected()){
        Serial.println("CHP Connected");
  }else{
        Serial.println("Not connected");
  }
}


void toggle_ledOnBoard(void){ //Toggles the LED_BUILTIN on-board LED on or off *
  ledOnBoardVal = !ledOnBoardVal;                                 //Toggle value
  digitalWrite(LED_BUILTIN, ledOnBoardVal);     //Set Arduino boards onboard LED
} //Exit toggle_ledOnBoard -----------------------------------------------------




void disable_jtag(void) { //Disable jtag to free port C, enabled by default ****
#if defined(JTD)                           //Not all AVR controller include jtag
  MCUCR |= ( 1 << JTD );                                //Write twice to disable
  MCUCR |= ( 1 << JTD );                                       //So stutter once
#endif                                            //End of conditional compiling
} //Exit jtag_disable ----------------------------------------------------------





////////////////////////////////////////////////////////////////////////////////
// PIN ALLOCATIONS TABLE ARDUINO UNO                                          //
// Board -Atmel- PIN - IDE - Function          - Connection               ALT //
//                                                                            //
// CONNECTIONS RAILS TOP LEFT: DIGITAL PWM<~> ******************************* //
// SCL   -  28 - PC5 -19/A5- ADC5/SCL/PCINT13  -                           NC //
// SDA   -  27 - PC4 -18/A4- ADC4/SDA/PCINT12  -                           NC //
// AREF  -  21 - REF -     - AREF              -                              //
// GND   -  22 - GND -     - GND               -                              //
// 13    -  19 - PB5 -  13 - SCK/PCINT5        - SCK LoRa oranje BUILT_IN SPI //
// 12    -  18 - PB4 -  12 - MISO/PCINT4       - MISO LoRa geel           SPI //
// ~11   -  17 - PB3 -  11 - MOSI/OC2A/PCINT3  - MOSI Lora groen      PWM SPI //
// ~10   -  16 - PB2 -  10 - SS/OC1B/PCINT2    - CE LoRa wit          PWM SPI //
// ~9    -  15 - PB1 -   9 - OC1A/PCINT1       - CSN LoRa paars           PWM //
// 8     -  14 - PB0 -   8 - PCINT0/CLK0/ICP1  -                          DIO //
//                                                                            //
// CONNECTIONS RAILS TOP RIGHT: DIGITAL PWM<~> ****************************** //
// 7     -  13 - PD7 -   7 - PCINT23/AIN1      -                          DIO //
// ~6    -  12 - PD6 -   6 - PCINT22/OCA0/AIN0 -                          PWM //
// ~5    -  11 - PD5 -   5 - PCINT21/OC0B/T1   -                          PWM //
// 4     -   6 - PD4 -   4 - PCINT20/XCK/T0    -                          INT //
// ~3    -   5 - PD3 -   3 - PCINT19/OC2B/INT1 -                          PWM //
// ~2    -   4 - PD2 -   2 - PCINT18/INT0      -                          INT //
// TX->1 -   3 - PD1 -   1 - PCINT17/TXD       - Serial monitor           TXD //
// RX<-0 -   2 - PD0 -   0 - PCINT16/RCD       - Serial Monitor           RCD //
//                                                                            //
// CONNECTIONS RAILS BOTTOM LEFT: POWER ************************************* //
// 5V    -   7 - VCC -     - VCC               -                          VCC //
// RES   -   1 - RES -     - PCINT14/RESET     -                          RES //
// 3.3V  -     -     -     -                   -                              //
// 5V    -     -     -     -                   -                              //
// GND   -     -     -     -                   -                              //
// GND   -     -     -     -                   -                              //
// Vin   -     -     -     -                   -                              //
//                                                                            //
// CONNECTIONS RAILS BOTTOM RIGHT: ANALOG IN ******************************** //
// A0    -  23 - PC0 -A0/14- ADC0/PCINT8       -                          ADC //
// A1    -  24 - PC1 -A1/15- ADC1/PCINT9       -                          ADC //
// A2    -  25 - PC2 -A2/16- ADC2/PCINT10      -                          ADC //
// A3    -  26 - PC3 -A3/17- ADC3/PCINT12      -                          ADC //
// A4    -  27 - PC4 -A4/18- ADC4/SDA/PCINT12  -                          TWI //
// A5    -  28 - PC5 -A5/19- ADC5/SCL/PCINT13  -                          TWI //
//                                                                            //
////////////////////////////////////////////////////////////////////////////////




////////////////////////////////////////////////////////////////////////////////
// EEPROM MEMORY MAP:                                                         //
// Start End  Number Description                                              //
// 0000  0000    1        Never use this memory location to be AVR compatible //
////////////////////////////////////////////////////////////////////////////////



////////////////////////////////////////////////////////////////////////////////
// FUSES (can always be altered by using the STK500)                          //
// On-Chip Debug Enabled: off                            (OCDEN=0)            //
// JTAG Interface Enabled: off                           (JTAGEN=0)           //
// Preserve EEPROM mem through the Chip Erase cycle: On  (EESAVE = 0)         //
// Boot Flash section = 2048 words, Boot startaddr=$3800 (BOOTSZ=00)          //
// Boot Reset vector Enabled, default address=$0000      (BOOTSTR=0)          //
// CKOPT fuse (operation dependent of CKSEL fuses        (CKOPT=0)            //
// Brown-out detection level at VCC=2,7V;                (BODLEVEL=0)         //
// Ext. Cr/Res High Freq.; Start-up time: 16K CK + 64 ms (CKSEL=1111 SUT=11)  //
//                                                                            //
// LOCKBITS (are dangerous to change, since they cannot be reset)             //
// Mode 1: No memory lock features enabled                                    //0
// Application Protect Mode 1: No lock on SPM and LPM in Application Section  //
// Boot Loader Protect Mode 1: No lock on SPM and LPM in Boot Loader Section  //
////////////////////////////////////////////////////////////////////////////////
//345678911234567892123456789312345678941234567895123456789612345678971234567898