/**********************************************************************************************//**
  @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,
    DAFE_DetailedSensor        = 0x01
};


//************* DAFE Structs *******

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

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

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 PckdDetailedDataPointDAFE {
    uint8_t     data[DETAIL_DATAN_PCKD_LEN];     // 3x 32b values
} PckdDetailedDataPointDAFE_t;

typedef struct DetailedDAFEData
{
    uint32_t                    temper;     // Temperature
    uint32_t                    adc_chan0;  // ADC_chan0
    uint32_t                    adc_chan1;  // ADC_chan1
} DetailedDAFEData_t;

typedef struct DetailedDAFEDataWHdr {
    DAFEHeader_t                hdr;
    DetailedDAFEData_t          data;
} DetailedDAFEDataWHdr_t;


//************* PIXL Structs *******

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

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

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

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

typedef struct DetailedPIXLDataWHdr
{
    PIXLHeader_t                hdr;
    DetailedPIXLData_t          data;
} DetailedPIXLDataWHdr_t;


//************* FLOW Structs *******

typedef struct FLOWHeader
{
    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
} FLOWHeader_t;

typedef struct DetailedFLOWData
{
    uint32_t            batt_and_temp_and_motors; // 12-bits batt, 12-bits temp, 8 bits motors
    uint32_t            counter;                // counter
} DetailedFLOWData_t;

typedef struct DetailedFLOWDataWHdr {
    DAFEHeader_t        hdr;
    DetailedFLOWData_t  data;
} DetailedFLOWDataWHdr_t;

//************* AQUA Structs *******

typedef struct AQUAHeader {
    uint8_t             address;            // modbus address
    uint8_t             cmd;                // mb command
    uint8_t             length;              // number of bytes of data - crc16 not included
    uint8_t             frame_slot;          // Set this for decode at dalmatian
} AQUAHeader_t;

typedef struct DetailedAQUAData
{
    uint16_t           data[6];
    uint16_t           crc16;
} DetailedAQUAData_t;

typedef struct DetailedAQUADataWHdr
{
    AQUAHeader_t        hdr;
    DetailedAQUAData_t  data;
} DetailedAQUADataWHdr_t;

#ifndef __XC__
#pragma pack(pop)
#endif

#endif // _PIXL_INTFC_H
