/**
  ******************************************************************************
  * @file    RSTi_ThermArray.h
  * @author  C. Otten
  * @version V0.0.0
  * @date    03/25/2011
  * @brief   RSTi Thermistor Array
  ******************************************************************************
  *
  * COPYRIGHT 2011 Liquid Robotics, Inc.
  **/

#ifndef _RDSTI_TA
#define _RDSTI_TA

#include "ff.h"

void RSTi_Thermarray(void* pParam);

#define NUM_SDCARDS                   2         // Number of SD Card personality modules in system.
#define NUM_TA_NODES                  6         // Number of thermistor nodes in string.
#define MAX_RSTI_THERMARRAY_SAMPLES   10        // Number of string samples in report.
/////////////////////// USE 5 for now until the new Float C&C firmware is released. Then set to 10.

#define SIZEOF_RSTI_TMP_STR           MEM_PART_SIZE_256
#define INDEX_RSTI_TMP_STR            MEM_PART_256

typedef struct _RSTI_THERMARRAY_DATA_LOG_FORMAT_
{                                               // Sensor data only, all nodes.
  int16_t   degreesx100[NUM_TA_NODES];
}RSTI_THERMARRAY_DATA_LOG_FORMAT, *PRSTI_THERMARRAY_DATA_LOG_FORMAT;

typedef struct _RSTI_THERMARRAY_DATA_REPORT_BLOCK_
{                                               // Single sensor sample, including overhead info.
  TYPE_90_91_DATA_HEADER          headerData;   // Lat., lomg., time.
  RSTI_THERMARRAY_DATA_LOG_FORMAT data_sample;  // Temperature data for all nodes.
}RSTI_THERMARRAY_DATA_REPORT_BLOCK, *PRSTI_THERMARRAY_DATA_REPORT_BLOCK;

typedef struct _RSTI_THERMARRAY_DATA_REPORT_
{                                               // Complete type 0x91 packet.
  TYPE_91_REPORT_HEADER             headerReport;
  RSTI_THERMARRAY_DATA_REPORT_BLOCK dataBlock[MAX_RSTI_THERMARRAY_SAMPLES];
}RSTI_THERMARRAY_DATA_REPORT, *PRSTI_THERMARRAY_DATA_REPORT;

typedef enum
{
  FOPTYPE_NOP = 0,
  FOPTYPE_FMOUNT,
  FOPTYPE_FCHDIR,
  FOPTYPE_FMKDIR,
  FOPTYPE_FOPEN,
  FOPTYPE_FCLOSE,
  FOPTYPE_FLSEEK,
  FOPTYPE_FWRITE,
} FOPTYPE;

// Generic data type for use with RSTi Thermistor Array firmware.
typedef struct
{                                               // Information associated with an individual
                                                //  thermistor node.
  BOOL          active;                         // Node serial number is found and active when TRUE.
  uint16_t      serialnum;                      // Unique node serial number.
  BOOL          valid;                          // Data recorded is valid when TRUE.
  uint16_t      temp;                           // Most recent read temerature.
} TA_Node_s;

#define SDCSTAT_nPRESENT      (1 << 0)          // Used as masks for '.all'.
#define SDCSTAT_nVALIDDIR     (1 << 1)
#define SDCSTAT_nCREATED      (1 << 2)
#define SDCSTAT_nOPEN         (1 << 3)
#define SDCSTAT_nSEEK         (1 << 4)
#define SDCSTAT_nWRITE        (1 << 5)
#define SDCSTAT_nCLOSE        (1 << 6)
#define SDCSTAT_THEREST       (1 << 7)

typedef struct
{
                                                  // LSB
  BOOL                        notpresent:1;       // When ZERO, indicates that a SD Card has been
                                                  //  detected (has been mounted).
  BOOL                        notvaliddir:1;      // When ZERO, indicates that we have a valid path to
                                                  //  the data directory.
  BOOL                        notcreated:1;       // When ZERO, indicates that there's a file created.
  BOOL                        notopen:1;          // When ZERO, indicates that there's a file that's open.
  BOOL                        notseek:1;          // When ZERO, indicates that there's a file seek issue.
  BOOL                        notwrite:1;         // When ZERO, indicates that there's a file write issue.
  BOOL                        notclose:1;         // When ZERO, indicates that there's a file close issue.
  BOOL                        therest:1;          // Should be set to zero. Non-zero will prevent
                                                  //  SD Card functionality.
                                                  // MSB
} sdc_status_t;

typedef   union sdcard
{
  uint8_t                     all;                // All zero means SD Card is OK to use.
  sdc_status_t                s;
} sdcard_u;

typedef struct
{
  sdcard_u                    status;             // SD Card related status.
  FATFS                       *pfatfs;            // Pointer to its FAT file system structure.
  FIL                         *pfobject;          // Pointer to its file object.
  uint32_t                    file_number;        // Numeric value used as file name.
  FOPTYPE                     file_op_type;       // File's last operation.
  FRESULT                     file_op_result;     // File's last operation result.
  uint32_t                    recsave_good_cnt;   // Track number of successful record saves since power up.
  uint32_t                    recsave_fail_cnt;   // Track number of failed record saves since power up.
  char                        fpath[30];          // File name associated with this task.
} RSTI_SDCARD_TRACKING, *PRSTI_SDCARD_TRACKING;

typedef struct _RSTI_THERMARRAY_BUFFER_
{
  uint16_t                    baseAddr;           // EEPROM base address for task specific data.
  uint8_t                     pon_mask;           // Mask containing bit position for PON control.
  uint8_t                     pok_mask;           // Mask containing bit position for POK input.
  char                        bufferTx[256];
  char                        bufferRx[1024];
  BOOL                        sampling_active;    // When TRUE, sample data. When FALSE, turn off power
                                                  //  to thermistor chain.
  uint8_t                     collection_sample_interval;
                                                  // Programable constant: How often a sample gets
                                                  //  added to the collection. This is based on the
                                                  //  regular base sampling rate of the nodes.
  uint8_t                     collection_size;    // Programable constant: Number of samples before
                                                  //  a collection is complete. Once complete, the
                                                  //  collection will be sent to shore.
  int8_t                      sample_count;       // Count of samples placed in the collection so far.
  int8_t                      sample_time;        // Count down to when a sample is to be placed in a
                                                  //  collection.
  RSTI_SDCARD_TRACKING        sdcard[NUM_SDCARDS];  // Allow for multiple SD Cards.
  char                        *rsti_tmp_str;      // Common use string dedicated to task located in
                                                  //  memory partition. Using this is faster than
                                                  //  allocating it for each use, but does tie up that
                                                  //  memory partition. At least it is thread safe.
  uint32_t                    timestamp;          // Collateral data belonging with node data below.
  uint32_t                    latitude;
  uint32_t                    longitude;
  TA_Node_s                   node[NUM_TA_NODES]; // Last node sample obtained.
  RSTI_THERMARRAY_DATA_REPORT reportData;         // Report structure containing, among others, a complete
                                                  //  collection set to be sent to shore.
}RSTI_THERMARRAY_BUFFER, *PRSTI_THERMARRAY_BUFFER;

typedef struct _RSTI_THERMARRAY_TASK_
{
  uint8_t taskID;
  uint8_t portNum;
  PRSTI_THERMARRAY_BUFFER  pBuf;
}RSTI_THERMARRAY_TASK, *PRSTI_THERMARRAY_TASK;

#endif // _RDSTI_TA
