////////////////////////////////////////////////////////////////////////////////
// Name:       Node01.ino                                                     //
//             01 = Allereerst wegpoetsen van Ping.                           //
// http://robotigs.nl/robots/includes/parts.php?idpart=344                    //
// Een aangepaste versie van Pong                                             //
// Created by: HARB rboek2@gmail.com July 2020 GPL copyrights                 //
// Platform:   Arduino UNO R3                                                 //
////////////////////////////////////////////////////////////////////////////////
// As outputs the following modules are mounted:                              //
// - Standard Arduino Onboard LED (PWM)                                       //
//           http://robotigs.nl/robotigs/includes/parts_header.php?idpart=185 //
//                                                                            //
// As inputs the following modules are mounted:                               //
// - Temp DS18B20                                                             //
//           http://robotigs.nl/robotigs/includes/parts_header.php?idpart=180 //
//                                                                            //
// For communications and statistics are mounted:                             //
// - Standard Serial Monitor output                                           //
//            http://robotigs.nl/robotigs/includes/parts_header.php?idpart=43 //
// - LoRa NRF24L01+ unit                                                      //
//           http://robotigs.nl/robotigs/includes/parts_header.php?idpart=344 //
////////////////////////////////////////////////////////////////////////////////


  //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    = 0;     //Set this radio as radio number 1=Ping or 0=Pong
  bool role           = 0;  //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  *****************************
    pongback();              //Pong receives timestamp and returns own timestamp
} //End of void loop() ----------------------- KEEP ON RUNNING THIS LOOP FOREVER



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 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                                    //
// 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