/**********************************************************************************************//**
  @file       pixl_intfc.h
  @date       $Date$
  @revision   $Revision$

  @brief      Pixl soil sensor interface
**************************************************************************************************/

#ifndef _PIXL_INTFC_H
#define _PIXL_INTFC_H

/***************************************************************************************************
 *                                            INCLUDES
 ***************************************************************************************************/
#include <stdint.h>
#include "ComTypeDefs.h"


/***************************************************************************************************
 *                                            MACROS
 ***************************************************************************************************/

#define PIXL_MAX_NUM_SENSORS		4
#define PIXLN_MAX_NUM_SENSORS       1

#define SNSR_SN_LENGTH       10



#define FRAME_START_DELIMITER      0x7e

#define DETAIL_DATA_PCKD_LEN	3
#define DETAIL_DATAN_PCKD_LEN    4


#define REF_CNT_IDX		0
#define MEAS_CNT_IDX	1

#define PIXL_FRAME_HEADER_LEN    16		// sizeof(PixlHeader_t)
//#define PIXL_FRAME_HEADER_LEN    2		// sizeof(PixlHeader_t)

#define PIXL_SLOT_MASK		0x03
#define PIXL_FRAME_MASK		0xf0
#define PIXLN_SNSR_MASK     0x0f


// Max frame type size, not including header & CRC
#define PIXL_MAX_FRAME_SIZE		 30 //20
#define PIXLN_MAX_FRAME_SIZE    22 //12

/***************************************************************************************************
 *                                            PIXL Structs
 ***************************************************************************************************/
// Message formats

#ifndef __XC__
#pragma pack(push,1)
#endif

enum pixl_types
{
	PIXL_DetailedSensor 		= 0x00, // was 0x01 before
    PIXLN_DetailedSensor        = 0x01,
	PIXL_CompressedSensor 		= 0x02
};

typedef struct PixlFWVer
{
	uint8_t		maj:4;
	uint8_t		min:4;
	uint8_t		rev;
} sPixlFWVer;

typedef struct DAFEver
{
    uint8_t     maj;
    uint8_t     min;
} sDAFEver;

typedef struct DAFEtype
{
    uint8_t     type:4;
    uint8_t     slot:4;
} sDAFEtype;

typedef struct PixlHeader
{
	uint8_t			length;			// data field length
	sPixlFWVer		fwver;
    uint16_t        hwver;
    uint8_t         frame_slot;
    uint8_t			sn[10];		// 10 ascii chars of sensor s/n
} PixlHeader_t;

typedef struct DAFEHeader
{
    uint8_t         length;         // data field length
    sDAFEver     fwver;     // maj/minor 8b values
    sDAFEver      hwver;    // maj/minor 8b values
    uint8_t      type_slot; // 4b values packed into 8b
    uint8_t       sn[SNSR_SN_LENGTH];       // 10 ascii digits of sensor s/n
} DAFEHeader_t;

typedef struct PckdDetailedDataPoint
{
	uint8_t		data[DETAIL_DATA_PCKD_LEN];		// 2x 24b values
} PckdDetailedDataPoint_t;

typedef struct PckdDetailedDataPointN
{
    uint8_t     data[DETAIL_DATAN_PCKD_LEN];     // 3x 32b values
} PckdDetailedDataPointN_t;


typedef struct DetailedSensorData
{
	uint16_t				temper;			// Temperature (units of 0.125 degC)
	PckdDetailedDataPoint_t	f10mhz[2];		// 0: ref, 1: meas
	PckdDetailedDataPoint_t	f50mhz_up[2];
	PckdDetailedDataPoint_t	f50mhz_low[2];

} DetailedSensorData_t;

typedef struct DetailedSensorDataN
{
    uint32_t                data[3];
} DetailedSensorDataN_t;

typedef struct DetailedSensorDataWHdr
{
	PixlHeader_t			hdr;
	DetailedSensorData_t	data;
} DetailedSensorDataWHdr_t;

typedef struct DetailedSensorDataNWHdr
{
    DAFEHeader_t            hdr;
    DetailedSensorDataN_t    data;
} DetailedSensorDataWHdrN_t;

#ifndef __XC__
#pragma pack(pop)
#endif

#endif // _PIXL_INTFC_H
