#define  VERSION             3          // 2 = B or 3 = C version    
#define  DEBUG
         
#ifdef   DEBUG 
   #define dprint(x)         Serial.print(x)
   #define dprintHex(x,HEX)  Serial.print(x,HEX)
   #define dprintln(x)       Serial.println(x)
   #define D(x)              x 
 #else  
   #define dprint(x)     
   #define dprintln(x)   
   #define dprintHex(x,HEX)   
   #define D(x)       
#endif   

// pins definition and I/O commands common
  #define LedPinMask        0X80
  #define LedPinOn          PORTB &= ~LedPinMask 
  #define LedPinOff         PORTB !=  LedPinMask
  #define OffPinMask        0x80
  #define RfOn              PORTD &= ~OffPinMask
  #define RfOff             PORTD |=  OffPinMask
  #define SPIOUT            0X2C 
  #define SetupPortB        LedPinMask | SPIOUT     // LedPin and SPI output bits
  #define SetupPortC        0                       // all pin inputs
  #define SetupPortD        OffPinMask              // all pin inputs except OFF 
  #define IRQ               0x04

// pins definition and I/O commands for version C 
#if VERSION == 3
  #define Button1In        (PINC & 0x01)
  #define Button2In        (PINC & 0x02)
  #define Button3In        (PINC & 0x04)
  #define Button4In        (PINC & 0x08)
  #define KeyIntRegister    2                       // enable key
  #define KeyIntMaskC       0xF                     // interrupt button MAsk
  #define KeyIntMaskB       0                       // interrupt button MAsk  
#endif


// pins definition and I/O commands for version B
#if VERSION == 2
  #define Button1In        (PINC & 0x08)
  #define Button2In        (PINB & 0x02)
  #define Button3In        (PINC & 0x04)
  #define Button4In        (PINB & 0x01)
  #define KeyIntRegister    3                      // enable key
  #define KeyIntMaskC       0x0C                   // interrupt button MAsk
  #define KeyIntMaskB       3                      // interrupt button MAsk
#endif


// command group - ID
#define IdAck             1       // acknowledge 
#define IdMasterS         0x10    // secure master command
#define IdRemoteS         0x20    // secure remote controller 
#define IdGateS           0x21    // secure gate commands

// read write commands
#define CmdReadEeprom     0x31    // read from EEPROM control structure
#define CmdWriteEeprom    0x32    // write to  EEPROM control structure
#define CmdReadRam        0x33    // read from RAM control structure
#define CmdWriteRam       0x34    // write to  RAM control structure

// gate commands 
#define CmdGateOposite    0x41    // if opened close, if closed open
#define CmdGateOpen       0x42        
#define CmdGateClose      0x43        
#define CmdWingOposite    0x44        
#define CmdWingOpen       0x45              
#define CmdWingClose      0x46        

// reomte commands
#define CmdRemoteKey      0x51    // remote key command    
#define CmdRemoteFunction 0x52    // remote function command    

// other definitions
#define Encrypt           0
#define Decrypt           1
#define FunctionMax       6       // maximum function number available
#define CountersTx        4       // number of TX counters
#define CountersRx        3       // number of RX counters

#define GateAddr          0xFF

typedef struct remote {
  uint32_t  serialn;
  uint32_t  counter;  
  uint8_t   command;            // command  KEY or FUNCTION
  uint8_t   battery;            // resolution 10mV, offset -2V, range 2 to 4.55V
  int8_t    temperature;        // resolution 1 degree Celsius
  uint8_t   button;             // button pushed  
  uint8_t   rez[4];
} remotet;


typedef struct base {
  uint8_t     key[16];          // encryption key
  uint32_t    counterTx[CountersTx];  // transmit counter
  uint32_t    serialn;          // serial number  
  uint16_t    counterWindow;    // size of counter window
  uint8_t     type;             // type of this device - remote
  uint8_t     function[30];     // type of this device - remote
} baset;  
