#ifndef xRadioLib_H
#define xRadioLib_H
#include <RFM69_ATC.h>  //(https://github.com/TomWS1/RFM69_ATC/tree/ListenModeExtensions
//#include <RFM69.h>    //get it here: https://www.github.com/lowpowerlab/rfm69
#include <WirelessHEX69.h> //get it here: https://github.com/LowPowerLab/WirelessProgramming/tree/master/WirelessHEX69
#include <SPIFlash.h>

#define NO_SLEEP_FOR_U 1
#define _ENCRYPTKEY "sampleEncryptKey" //exactly the same 16 characters/bytes on all nodes!
#ifdef __AVR_ATmega1284P__
    #define FLASH_SS      23 // and FLASH SS on D23
  #else
    #define FLASH_SS      8 // and FLASH SS on D8
#endif

class xRadioLib {
public:
  xRadioLib();
  ~xRadioLib();
  
  void init(uint8_t node=49, uint8_t network=100, uint8_t freq=RF69_915MHZ);
  void initWithDefaults();

  void setRadioInHubMode();
  
  void setHighPower(bool power);
  uint8_t setRadioInListenMode(byte *msg);
  uint8_t sleepUntilPacket(bool goToSleepDirect=false);

  
  void wakeUpNodeWithCmd( uint8_t targetNode,byte *cmd, byte len );
  
  uint8_t receivedBytesFromListenningMode();
  
  //pass address of var, returns who sent the latest cmd
  uint8_t getLatestCommand(byte *cmd, int *rssi);

  bool sendToHub(char *sendBuffer, byte len);
  bool sendCmdToNode(int node, char *sendBuffer, byte len);
  
  void off();
  void blink(int time);

  byte _buffer[64];//max size of 1 transmit? On Arduino, char is int8_t but byte is uint8_t.
  uint8_t _bufferSize=64;
  uint8_t _latestNodeId; //stores who sent us the latest cmd
  int _latestSenderRSSI;
  RFM69_ATC _radio;
private:
  boolean isFlashPresent = false;
  bool _hubMode = false;      //is the radio used in the hub / Gateway mode?
  uint8_t _nodeId;             //network ID used for this unit
  uint8_t _networkId;         //the network ID we are on
  uint8_t _gatewayId;           //the node ID we're sending to
  uint8_t _frequency= RF69_915MHZ; //others: RF69_868MHZ, RF69_915MHZ
  bool _is_Rfm69Hw=1;              //uncomment only for RFM69HW! Leave out if you have RFM69W!
  uint32_t _packetCount = 0;
  bool _promiscuousMode = false; //set to 'true' to sniff all packets on the same network
  byte _nbRetriesRadioSend = 30;
};

#endif
