#ifndef MSCMAIN_INC
#define MSCMAIN_INC		1

#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	<cronin.h>
#include	<fibrlite.h>
#include	<dura_adc.h>


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


/* 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
ushort	SerGetsTmout(char *buf, ushort buflen, ushort 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( ushort secs );
void 	DelayMilliSecs( ushort 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);

// Math Routines  
int 	CalcAbs(double *ZeroVals, ushort *IntenVals, double *AbsVals, double DCMean, ushort 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, ushort *pixel_beg, ushort *pixel_end);
double 	CellNumToLambda(int cell, double *ZeissCoefs);
void 	FloatVectorMinMax(ushort end, float data[], float *min, float *max);
void 	FloatVectorMeanStdDev(ushort end, float data[], float *mean, float *stdev);
void 	ShortVectorMinMax(ushort end, ushort data[], ushort *min, ushort *max);
void 	ShortVectorMeanStdDev(ushort end, ushort 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[]);
ushort 	fCrc16Bit(const unsigned char *msg);


/* 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
