#ifndef _RFM64W_H_
#define _RFM64W_H_
//----------------------------------------------------------------------------
///
/// \file   rfm64w.h
///
/// \brief  See implementation for further implementation
///
/// \date   20170906
/// \author Maximilian Seesslen <mes@seesslen.net>
///
//----------------------------------------------------------------------------


//---Includes-----------------------------------------------------------------

#include <lepto/debug.h>
#include <biwak/input.hpp>
#include <biwak/spi.hpp>
#include <biwak/output.hpp>

#if defined(MCU_PLATFORM_STM32)
   #include <lepto/ring.h>
   #include <lepto/mockQt.hpp>
#else
   #include <QQueue>
#endif


//---Defines------------------------------------------------------------------


// Active low
#define NSS_ACTIVE         false
#define NSS_INACTIVE       true
#define RFM_CONFIG_SIZE    0x1F
#define RFM_READ_FLAG      ( 1<<6 )
#define RFM_WRITE_FLAG     ( 0<<6 )


//----------------------------------------------------------------------------

class CLed;
enum ERegister: unsigned int;

enum ERfmState: unsigned int
{
   eRfmStateNone,
   eRfmStateReceiving,
   eRfmStateTransmiting,
   eRfmStateStandby,
};


class CRfm64w //: public CInput
{
   private:
      bool configValid;
      int receivePacketCount=0;
      int sendPacketCount=0;
      lrtimer_t startTimer=0; //LRTIMER_INVAL;
      int lostPacketCount=0;
      int errorPacketCount=0;
      CSpi &m_spi;
      CInput m_irq;
      COutput nssConfig;
      COutput nssData;
      COutput outputReset;
      CLed *trafficLed;
      ERfmState state;
      char *data;

   public:

      uint8_t config[RFM_CONFIG_SIZE];

      CRfm64w(CSpi &_spi,
              EGpioPeripheral eGpioPeripheralInterrupt, EGpioPin eGpioPinInterrupt,
              const SExtiConfig &extiConfig,
              EGpioPeripheral eGpioPeripheralNssConfig, EGpioPin eGpioPinNssConfig,
              EGpioPeripheral eGpioPeripheralNssData,   EGpioPin eGpioPinNssData,
              EGpioPeripheral eGpioPeripheralReset,   EGpioPin eGpioPinReset,
              CLed *_trafficLed=NULL);
      void setCsConfig();
      void setCsData();
      void setCsNone();

      int setTxMode();
      int setRxMode();
      int setStbyMode();

      void readConfig();
      void readConfig( int address);
      void writeConfig( int address, int value);
      void writeConfig();
      int setConfig( int address, int bitPos, int bitSize, int value);
      int getConfig( int address, int bitPos, int bitSize);
      int getConfig( ERegister eRegister );
      int setConfig( ERegister eRegister, int value );
      void setWriteConfig( ERegister eRegister, int value );
      void dumpConfig( );

      int initConfig();

      int sendPackage(int data);

      void interrupt(bool value);

      int getReceivePacketCount()
      {
         return(receivePacketCount);
      };
      int getSendPacketCount()
      {
         return(sendPacketCount);
      };
      lrtimer_t getStartTimer()
      {
         return(startTimer);
      }
      int getLostPacketCount()
      {
         return(lostPacketCount);
      }
      int getErrorPacketCount()
      {
         return(errorPacketCount);
      }
      ERfmState getState()
      {
         return(state);
      }
      char *getData()
      {
         return(data);
      }
};


//---fin----------------------------------------------------------------------
#endif // ? ! _RFM64W_H_
