#ifndef NRF24L01_H
#define NRF24L01_H

#define PACKET_BUFFER_LENGTH 22

/* AVR I/O pin definionts */
#define NRF_CE_DDR   DDRD
#define NRF_CE_PORT  PORTD
#define NRF_CE_PIN   PD7

#define NRF_CSN_DDR   DDRD
#define NRF_CSN_PORT  PORTD
#define NRF_CSN_PIN   PD6

#define NRF_IRQ_DDR     DDRD
#define NRF_IRQ_PORT    PORTD
#define NRF_IRQ_PIN     PD5
#define NRF_IRQ_PORTIN  PIND


void Initialize_NRF24L01(void);
void Set_NRF24L01_Pins(void);
void Configure_Transmission(uint8_t moduleId);
uint8_t Read_NRF_Status_Register(void);
uint8_t Read_NRF_Register(uint8_t address, uint8_t * registerContents);
void Write_NRF_Register(uint8_t address, uint8_t registerContents);
void Send_Activate_Command(void);

void NRF24L01_Send_Message(uint8_t *buffer, uint8_t length);
void Write_Message_To_TX_FIFO(uint8_t length, uint8_t * buffer);
void Set_TX_Address(uint8_t * txAddress, uint8_t length);
void Set_RX_P0_Address(uint8_t * rxAddress, uint8_t length);

typedef enum
{
	PACKET_TYPE_SALT = 0,
	PACKET_TYPE_REPORT = 1,
	PACKET_TYPE_GET_VALUES = 2,
	PACKET_TYPE_VALUES = 3,
} PACKET_TYPE;

typedef enum
{
	VALUE_TYPE_TIME = 0,
	VALUE_TYPE_TEMPERATURE = 1,
	VALUE_TYPE_PRESSURE = 2,
	VALUE_TYPE_HUMIDITY = 3,
} VALUE_TYPES;

typedef struct __attribute__((packed)) BITFIELD_PACKET_COUNT_ELEMENT
{
	uint8_t packetType : 5;
	uint8_t elementCount : 3;
} BITFIELD_PACKET_COUNT_ELEMENT;

typedef struct __attribute__((packed)) PACKET
{
	uint64_t salt; /* 1 byte device id, 7 bytes remainder */
	union {
		struct {
			BITFIELD_PACKET_COUNT_ELEMENT packetIdentifier;
			uint8_t valueTypeTemperature;
			int16_t temperature;
			uint8_t valueTypePressure;
			uint32_t pressure;
			uint8_t valueTypeHumidity;
			uint16_t humidity;
			uint8_t unused[10];
		} values;
		uint8_t buffer[PACKET_BUFFER_LENGTH];
	}payload;
	uint16_t crc;
} PACKET;


#endif
