
//******************************************************************************
// ISUS NV VARIABLES OCCUPY (0x0065 - 0x00C8) IN THE 256 BYTE DS3234 SRAM                    
//******************************************************************************

//ISUS UCHAR value and memory address in DS3234 SRAM (0x0065 - 0x006F)
#define ISUS_TCOMPMODE_PTR 		0x0065 	
#define ISUS_TCOMPMODE_VAL		1         		// Br temperature compensation mode
#define ISUS_SALFITMODE_PTR 	0x0066	
#define ISUS_SALFITMODE_VAL		0         		// fitting algorithim, fix/fit salinity, mode  0=fix, 1=fit
#define ISUS_WARMSHUT_PTR		0x0067	
#define ISUS_WARMSHUT_VAL		1				// boolean of shutter state during warmup
	
//ISUS USHORT value and memory address in DS3234 SRAM (0x0070 - 0x008F)
#define ISUS_INTSCANS_PTR		0x0070
#define ISUS_INTSCANS_VAL		3         		// number of intensity scans
#define ISUS_DCSCANS_PTR		0x0072
#define ISUS_DCSCANS_VAL		1         		// number of dark current scans
#define ISUS_PRESCANS_PTR		0x0074
#define ISUS_PRESCANS_VAL		2         		// number of spectromter 4ms pre scans (to clear PDA)
#define ISUS_PIXELBEG_PTR		0x0076
#define ISUS_PIXELBEG_VAL		17        		// start  fitting pixel
#define ISUS_PIXELEND_PTR		0x0078
#define ISUS_PIXELEND_VAL		87        		// ending fitting pixel
#define ISUS_FITCONCS_PTR		0x007A		
#define ISUS_FITCONCS_VAL		2         		// number of concentrations to fit
#define ISUS_BSLNMODL_PTR		0x007C
#define ISUS_BSLNMODL_VAL		1         		// baseline model to use in curve fitting
#define ISUS_REFLIMIT_PTR		0x007E
#define ISUS_REFLIMIT_VAL		5000       		// min number of spectrometer A/D counts that establishs when the DU Lamp is on
#define ISUS_REFSAMPS_PTR		0x0080
#define ISUS_REFSAMPS_VAL		15				// number of reference detector readings
#define ISUS_DATPIXELBEG_PTR	0x0082
#define ISUS_DATPIXELBEG_VAL	17       		// start  reported spectra pixel (APF)
#define ISUS_DATPIXELEND_PTR	0x0084
#define ISUS_DATPIXELEND_VAL	87        		// ending reported spectra pixel (APF)
#define ISUS_LAMPWARM_PTR		0x0086
#define ISUS_LAMPWARM_VAL		3				// number secs to warmup lamp after power on
#define ISUS_CLEANUV_PTR		0x0088
#define ISUS_CLEANUV_VAL		30        		// number of seconds for UV "cleaning" cycle
    
//ISUS INT value and memory address in DS3234 SRAM (0x0070 - 0x008F)
#define ISUS_LASTERROR_PTR		0x008A
#define ISUS_LASTERROR_VAL		0				// Clear any value
	
//ISUS ULONG value and memory address in DS3234 SRAM (0x0090 - 0x00C8)
#define ISUS_ERRORCTR_PTR		0x0090
#define ISUS_ERRORCTR_VAL		0         		// counter indicating number of ISUS errors
#define ISUS_LASTERRTIME_PTR	0x0094
#define ISUS_LASTERRTIME_VAL	0         		// time of the last error event
#define ISUS_SAMPLECTR_PTR		0x0098
#define ISUS_SAMPLECTR_VAL		0         		// counter indicating total number of ISUS samples
#define ISUS_LASTSAMPTIME_PTR	0x009C
#define ISUS_LASTSAMPTIME_VAL	0         		// time of the last ISUS sample
#define ISUS_PERIOD_PTR			0x00A0
#define ISUS_PERIOD_VAL			1000      		// number of milliseconds to integrate spectrometer
#define ISUS_SAMPSPERDC_PTR		0x00A4
#define ISUS_SAMPSPERDC_VAL		25        		// number of samples per dark current reading
#define ISUS_SAMPSPERUV_PTR		0x00A8
#define ISUS_SAMPSPERUV_VAL		24        		// number of samples per UV "cleaning" cycle
	
//ISUS FLOAT value and memory address in DS3234 SRAM 
#define ISUS_NO3SPAN_PTR		0x00AC
#define ISUS_NO3SPAN_VAL		45.0	
#define ISUS_NO3OFFSET_PTR		0x00B0
#define ISUS_NO3OFFSET_VAL		5.0


//******************************************************************************
// ISUS Global Variables						      
//******************************************************************************

#define ZCOEFS              5       // Zeiss Spectrometer Wavelength Coefs
#define TCOEFS              5       // Bromide Temperature Compensation Coefs
#define FITVALUES   		10      // Returned Values from lsqfit()

// UCHARs Stored in DS3234 NVRAM:
uchar  isus_tcomp_mode, isus_salfit_mode, isus_warm_shut;

// UCHARs NOT Stored in DS3234 NVRAM:
uchar  isus_reset_flag;
uchar  isus_spec_pwr, isus_lamp_pwr, isus_ref_pwr;

// USHORTs Stored in DS3234 NVRAM: 
ushort isus_scan_num, isus_dark_num, isus_pre_scans;
ushort isus_pixel_beg, isus_pixel_end, isus_fit_concs;
ushort isus_bsl_model, isus_ref_limit, isus_ref_samples;
ushort isus_warm_lamp, isus_clean_lamp;
ushort isus_datpixel_beg, isus_datpixel_end;

// USHORTs NOT Stored in DS3234 NVRAM: 
ushort isus_fit_pixels;
ushort isus_lamp_evt;				// value comes from DS1683

// INTs Stored in DS3234 NVRAM
int    isus_last_error;

// INTs NOT Stored in DS3234 NVRAM
int    Zcoefs, Ecoefs, Zeros;

// ULONGs Stored in DS3234 NVRAM			  
ulong  isus_error_ctr, isus_error_time; 	
ulong  isus_sample_ctr, isus_sample_time;     
ulong  isus_spec_period, isus_scans_per_dc, isus_samps_per_clean;

// ULONGs NOT Stored in DS3234 NVRAM	
ulong  isus_lamp_elp;				// value comes from DS1683

// FLOATs Stored in DS3234 NVRAM	
float  no3_span, no3_offset;

ushort *SpecData, *DarkData, *RefData;

static TUPort *SpecPort;
static TUPort *RefPort; 

char   CalDate[25];
double SwCalTemp;
double ZeissCoefs[ZCOEFS];
double TcompCoefs[TCOEFS];
double FitData[FITVALUES];
double ZeroData[260];
double LambdaData[260];
double AbsData[260];
double AbsFitData[260];
double ResidualsData[260];
double **EcoefData, **EcoefFile;

