/*
 * apf.h
 *  Created on: 8 May 2024    Porting CF2 ISUS and APF code
 *      Author: thom maughan
 *
 */

#ifndef APF_H_
#define APF_H_

#include <time.h>

//******************************************************************************
// ISUS Global Variables
//******************************************************************************

#define ZCOEFS              5       // Zeiss Spectrometer Wavelength Coefs
#define TCOEFS              5       // Bromide Temperature Compensation Coefs
#define FITVALUES           10      // Returned Values from lsqfit()

#define FIX                 0

/* Local Defines */
#define ACKBUFSIZE      32
#define CMDBUFSIZE      64
#define DATBUFSIZE      2048



// Memory Mapped Latches
//ushort WrRegVal;
//ushort WhichRxd;

// UCHARs Stored in DS3234 NVRAM:
//uchar  sys_slog_delay, sys_deploy_mode, sys_sensor_type;
//uchar  sys_data_mode, sys_time_type, sys_watchdog_enable;
//uchar  sys_message_enable, sys_pump_enable;

// UCHARs NOT Stored in DS3234 NVRAM:
//uchar  sys_sensor_mode;                 // CF2 Logger Sensor Select, 0=Default 1=ISUS 2=DuraFET
//uchar  sys_por_flag;
//uchar  sys_pump_pwr;

// USHORTs Stored in DS3234 NVRAM:
//ushort sys_delay_hour, sys_sync_hour, sys_sync_min;
//ushort sys_df_limit, sys_sf_limit, sys_ef_limit;
//ushort sys_last_doy, sys_serial_num, sys_apf_tmout;

// INTs Stored in DS3234 NVRAM
//int  sys_tzoffset, sys_last_error;

// ULONGs Stored in DS3234 NVRAM
//ulong  sys_error_ctr, sys_error_time;
//ulong  sys_reset_ctr, sys_reset_time;
//ulong  sys_por_ctr, sys_file_ctr;
//ulong  sys_sleep_period, sys_pump_period;

// All data is stored in FRAM and stored as 32bit integers (CF2 had uchars and shorts)

struct apfRtcTime
{
    time_t DS3234_secs;     // time in secs from Dallas DS3234 RTC (batt backed)
    time_t CF2_secs;        // time in secs from CF2 TPU RTC (NOT batt backed)
    time_t alarm_time;      // time in secs to set the wakeup alarm time to, gets passed to PWRSuspendUntil()
    time_t wake_time;       // time in secs indicating when PWRSuspendUntil() returned
    time_t current_time;    // time in secs indicating the current time
    time_t data_time;       // time in secs indicating when data sample was taken
    float  data_time_fsecs; // time in fractional decimal secs indicating when data sample was taken (if time_type == FRACSECS)
};



// there may be a need to break apfGlobals struct into two, one for EEPROM, one for FRAM (volatile)

struct apfGlobals
{
    int            errNum;                   //was errno, now errNum
    unsigned int   isus_spec_pwr;
    unsigned int   isus_lamp_pwr;
    unsigned int   isus_ref_pwr;
    unsigned int   isus_ref_limit;
    unsigned int   isus_error_ctr;
    unsigned int   isus_last_error;     // Added 14 May
    unsigned int   dura_atod_pwr;
    unsigned int   sys_sensor_mode;   //ISUS_SENSOR_MODE or DURA_SENSOR_MODE
    unsigned int   sys_por_ctr;
    unsigned int   sys_reset_ctr;
    unsigned int   sys_reset_time;
    unsigned int   isus_error_time;
    unsigned int   dura_error_time;
    unsigned int   isus_sample_time;
    unsigned int   dura_sample_time;
    unsigned int   gotSample;      // flag for indicating sample taken
    unsigned int   acquire_time;   // time to acquire sample
    unsigned int   dura_error_ctr;
    unsigned int   dura_last_error;     // Added 14 May
    unsigned int   sys_sf_limit;        // Added 14 May
    unsigned int   sys_ef_limit;        // Added 14 May

    unsigned int   isus_reset_flag;
    unsigned int   dura_reset_flag;
    unsigned int   sys_apf_tmout;
    unsigned int   isus_lamp_elp;
    unsigned int   isus_lamp_evt;
    unsigned int   isus_fit_concs;

    unsigned int   isus_sample_ctr;
    unsigned int   dura_sample_ctr;

    unsigned int   isus_spec_period;
    unsigned int   isus_pre_scans;
    unsigned int   isus_scan_num;
    unsigned int   isus_dark_num;

    unsigned int   isus_bsl_model;

    unsigned int   lambda_beg;
    unsigned int   lambda_end;
    unsigned int   LambdaData;
    unsigned int   isus_pixel_beg;
    unsigned int   isus_pixel_end;
    unsigned int   isus_fit_pixels;
    unsigned int   isus_tcomp_mode;
    unsigned int   isus_salfit_mode;
    unsigned int   dura_atod_readings;
    unsigned int   dura_atod_delay;
    unsigned int   dura_samps_per_cal;


    //uchar
    unsigned char  sys_slog_delay;
    unsigned char  sys_deploy_mode;
    unsigned char  sys_data_mode;
    unsigned char  sys_time_type;
    unsigned char  sys_watchdog_enable;
    unsigned char  sys_message_enable;
    unsigned char  sys_pump_enable;
    unsigned char  sys_sensor_type;

    unsigned int   dura_led_enable;
    unsigned int   isus_datpixel_beg;
    unsigned int   isus_datpixel_end;

    //ushorts
    unsigned short sys_delay_hour;
    unsigned short sys_sync_hour;
    unsigned short sys_sync_min;
    unsigned short sys_df_limit;
    //unsigned short sys_sf_limit;
    //unsigned short sys_ef_limit;
    unsigned short sys_last_doy;
    unsigned short sys_serial_num;
    unsigned short sys_tmout;
    unsigned short sys_tzoffset;
    unsigned short sys_last_error;
    unsigned short sys_error_ctr;
    unsigned short sys_error_time;
    //unsigned short sys_reset_ctr;
    //unsigned short sys_reset_time;
    //unsigned short sys_por_ctr;
    unsigned short sys_file_ctr;
    unsigned short sys_sleep_period;
    unsigned short sys_pump_period;

    float DataMean;
    float DataStdDev;
    float sysTemperature;
    float sysHumidity;



#ifdef NOCODE
    int32_t    errno;                   //was errno
    uint32_t   isus_spec_pwr;
    uint32_t   isus_lamp_pwr;
    uint32_t   isus_ref_pwr;
    uint32_t   dura_atod_pwr;
    uint32_t   sys_sensor_mode;   //ISUS_SENSOR_MODE or DURA_SENSOR_MODE
    uint32_t   sys_por_ctr;
    uint32_t   sys_reset_ctr;
    uint32_t   sys_reset_time;
    uint32_t   isus_error_time;
    uint32_t   dura_error_time;
    uint32_t   isus_sample_time;
    uint32_t   dura_sample_time;
    uint32_t   gotSample;      // flag for indicating sample taken
    uint32_t   acquire_time;   // time to acquire sample
    uint32_t   dura_error_ctr;


    uint32_t   isus_tcomp_mode;
    uint32_t   isus_salfit_mode;
    uint32_t   dura_led_enable;
    uint32_t   isus_fit_concs;
    uint32_t   sys_apf_tmout;
    uint32_t   isus_sample_ctr;
    uint32_t   dura_sample_ctr;
    uint32_t   lambda_beg;
    uint32_t   lambda_end;
    uint32_t   LambdaData;
    uint32_t   isus_pixel_beg;
    uint32_t   isus_pixel_end;
    uint32_t   isus_fit_pixels;
    uint32_t   lambda_beg;
    uint32_t   lambda_end;


    //uchar
    uint8_t  sys_slog_delay;
    uint8_t  sys_deploy_mode;
    uint8_t  sys_data_mode;
    uint8_t  sys_time_type;
    uint8_t  sys_watchdog_enable;
    uint8_t  sys_message_enable;
    uint8_t  sys_pump_enable;
    uint8_t  sys_sensor_type;

    //ushorts
    uint16_t sys_delay_hour;
    uint16_t sys_sync_hour;
    uint16_t sys_sync_min;
    uint16_t sys_df_limit;
    uint16_t sys_sf_limit;
    uint16_t sys_ef_limit;
    uint16_t sys_last_doy;
    uint16_t sys_serial_num;
    uint16_t sys_tmout;
    uint16_t sys_tzoffset;
    uint16_t sys_last_error;
    uint16_t sys_error_ctr;
    uint16_t sys_error_time;
    //uint16_t sys_reset_ctr;
    //uint16_t sys_reset_time;
    //uint16_t sys_por_ctr;
    uint16_t sys_file_ctr;
    uint16_t sys_sleep_period;
    uint16_t sys_pump_period;
#endif

};


//msc_config.h
#define FNULL                   ((FILE *)NULL)

//#define TRUE                    1
//#define FALSE                   0

#define ENABLE                  1
#define DISABLE                 0

#define HIGH                    1
#define LOW                     0

#define OK                      0
#define ERROR                   (-1)
#define TIMEOUT                 (-2)

#define CTRL_A                  0x01
#define CTRL_C                  0x03
#define CTRL_D                  0x04
#define CAPS_B                  0x42
#define CAPS_R                  0x52
#define CAPS_U                  0x55
#define CAPS_S                  0x53
#define CAPS_Q                  0x51
#define ACK                     0x06
#define NAK                     0x15




// from MSC_ERR_ISUS.H
#define SPEC_PWR_ON_FAILURE     -201
#define LAMP_PWR_ON_FAILURE     -202
#define REF_PWR_ON_FAILURE      -203
#define NRC_FIT_FAILURE         -204
#define SPEC_TPU_INIT_ERROR     -205
#define SPEC_CTS_SCAN_ERROR     -206
#define SPEC_CTS_DATA_ERROR     -207
#define SPEC_DAT_READ_ERROR     -208


// from MSC_ERR_SYS.H
#define SLEEP_TIME_MISSED           -101
#define MYSTERY_WAKEUP              -102
#define ALARM_TIME_TIMEOUT          -103
#define POWER_FAIL_SUSPEND          -104
#define POWER_FAIL_IRQ2             -105
#define ALARM_TIME_STALE            -106
#define COMMAND_MODE_EMPTY          -107
#define ERROR_FILE_TOO_BIG          -108
#define OFFLOAD_FILE_TOO_BIG        -109
#define COMMAND_ACK_FAILURE         -110
#define RTC_DELTA_EXCEEDED          -111
#define FILE_OPEN_FAILURE           -112
#define FLASH_CARD_FULL             -113
#define MAX_SLEEP_TIME_MISSED       -114
#define MALLOC_FAILURE              -115
#define INIT_NVRAM_WITH_DEFAULTS    -116


#endif /* APF_H_ */
