/*
 * apf_init.h
 *
 *  Created on: Feb 26, 2024
 *      Author: tm, from lc cf2 apf_main.h
 */

#ifndef APF_INIT_H_
#define APF_INIT_H_

void init_apf_global_vars(void);


#ifdef NOCODE
#include    <msc_config.h>

#include    <io_regs.h>
#include    <io_spi.h>
#include    <io_i2c.h>
#include    <ds1683.h>
#include    <ds2433.h>
#include    <ds2482.h>
#include    <ds3234.h>
#include    <pcal6416.h>
#include    <hih6130.h>
#include    <ina219.h>
#include    <tca6424.h>
#include    "mms_spec.h"
#include    <fibrlite.h>
#include    <dura_adc.h>


#include    <nr.h>
#include    <nrutil.h>
#include    <lsqfit.h>
#include    <cf2_zmod.h>
#include    <userio.h>
#endif




#ifdef NOCODE

/* MSC Main Functions */
int     TakeDuraSample(char *dbuf, float *atime, ulong CTD_Time, float CTD_Temp, float CTD_Salinity, float CTD_Depth);
int     TakeIsusSample(char *dbuf, float *atime, ulong CTD_Time, float CTD_Temp, float CTD_Salinity, float CTD_Depth);
int     TakeIsusPseudoSample(char *dbuf, float *atime, ulong CTD_Time, float CTD_Temp, float CTD_Salinity, float CTD_Depth);


/* MSC Init Functions */
void    InitMSC( void );
void    InitVars( void );
void    GetVarsFromNVRAM( void );
void    SetDefaultsIntoNVRAM( void );
void    GetSysVarsFromNVRAM( void );
void    SetSysDefaultsIntoNVRAM( void );
void    GetIsusVarsFromNVRAM( void );
void    SetIsusDefaultsIntoNVRAM( void );
void    GetDuraVarsFromNVRAM( void );
void    SetDuraDefaultsIntoNVRAM( void );
int     IOPwrUp( void );
int     IOPwrDown( void );
void    InitWatchDogKeepAlive( void );
void    EnableWatchDog( void );
void    DisableWatchDog( void );
void    SysShutDown( void );


/* MSC Util Functions */

// File Routines
FILE    *OpenApfLogFile(uchar select);
int     LogErrorMsg(char *emsg, int ecode, uchar outp);
long    CalcRecsPending(uchar select);
int     SendRecords(uchar select, fpos_t *fpsrc);
int     CheckFileSize(uchar select);
int     LoadIsusECoefs(double *Lambda, double **Ecoefs, double *Zeros, double *Tcoefs, double *CalTemp, char *CalDate);
int     LoadIsusZCoefs(double *ZeissCoefs);
int     LoadDuraFetIIITCoefs(double *Coefs);
int     pdx_access(const *path, int amode);

// Serial I/O Routines
unsigned short  SerGetsTmout(char *buf, unsigned short buflen, unsigned short seconds);
int     SerPuts(char *str);
int     safegets(char *buffer, int n);
char    *strupr (char *string);
int     MatchACK(char *buf);

// Time Routines
int     SyncNextSample( void );
void    CheckRtcDelta( void );
int     DayOfYear(int yy, int mm, int dd);
int     DayOfWeek(int yy, int mm, int dd);
void    DelaySecs( unsigned short secs );
void    DelayMilliSecs( unsigned short msecs );
short   Sleep(ulong us, bool absolute);

// Output Routines
void    OutputIsusHeader(FILE *fptr);
void    OutputDuraHeader(FILE *fptr);
void    DisplaySystemInfo(void);
void    DisplayTimes(void);
void    DisplayAlarmTime( void );
void    DisplayChipSelectSettings(void);
void    DisplayFormattedTime(time_t ptime);
void    DisplayLambdaInfo(FILE *fptr);
void    DisplayByteVal(char *msgstr, uchar ByteVal);

#endif

// Math Routines
int     CalcAbs(double *ZeroVals, unsigned short *IntenVals, double *AbsVals, double DCMean, unsigned short fit_pixels);
void    XformSwEcoefs(double **Ecoefs, double **XformedEcoefs, double *Lambda, double *Tcoefs, double CalTemp, double SamTemp);
void    InitEcoefMatrix(double **SrcEcoefs);
void    CopyEcoefs(double **SrcEcoefs, double **DstEcoefs);
void    CopySwEcoefs(double **SrcEcoefs, double **DstEcoefs);
void    InitLambdaArray(double *Lambda, double *ZeissCoefs);
int     MatchPixelsToLambda(double *Lambda, int lambda_beg, int lambda_end, unsigned short *pixel_beg, unsigned short *pixel_end);
double  CellNumToLambda(int cell, double *ZeissCoefs);
void    FloatVectorMinMax(unsigned short end, float data[], float *min, float *max);
void    FloatVectorMeanStdDev(unsigned short end, float data[], float *mean, float *stdev);
void    ShortVectorMinMax(unsigned short end, unsigned short data[], unsigned short *min, unsigned short *max);
void    ShortVectorMeanStdDev(unsigned short end, unsigned short data[], float *mean, float *stdev);
float   CtoF(float degC);
double  poly(double x, int degree, double p[]);
double  rtot(double r, double a[]);
double  ttor(double t, double a[]);
unsigned short  fCrc16Bit(const unsigned char *msg);



#ifdef NOCODE
/* APF Menu Functions */

void    RootMenu(void);

void    SetupMenu(void);
void    IsusSetup(void);
void    IsusSpecSetup(void);
void    IsusLampSetup(void);
void    IsusFitSetup(void);
void    DuraSetup(void);
void    LoggerSetup(void);
void    RtcSetup(void);

void    DataMenu(void);
void    IsusDataMenu(void);
void    IsusAcquireData(FILE *fptr);
void    DuraDataMenu(void);
void    DuraAcquireData(FILE *fptr);
void    DuraAcquireAtodValues(FILE *fptr);

void    TestMenu(void);
void    IsusTestMenu(void);
void    IsusLampTest(void);
void    IsusRefChTest(void);
void    DuraTestMenu(void);

void    InfoMenu(void);

void    FileMenu(void);
void    DownloadFile(void);
void    UploadFile(void);
void    DisplayFile(void);
void    RenameFile(void);
void    EraseFile(void);
void    EraseFiles(void);
void    ScanDir( void );

void    FNameError(void);
void    RangeError(long val1, long val2);
int     SysPass( void );

#endif




#endif /* APF_INIT_H_ */
