/*
 * cronin.h
 *
 *  Created on: Feb 13, 2024
 *      Author: tm
 */

#ifndef MMS_SPEC_H_
#define MMS_SPEC_H_

#define MMS_BAUD    115200

void mms_dev_test(int testNum);
void mms_test_menu(void);
void mms_isusLampTest(void);
int test_takeIsusSample_loop(void);
int test_takeIsusSample_timing(void);

void mms_uart_init(unsigned long mms_baud);
void mms_open(void);
void mms_close(void);
unsigned char mms_getc(void);
void mms_putc(unsigned char c);
int mms_rxBytesAvail(void);
uint32_t mms_rx_byte_timeout(uint32_t timeout_ms);
void mms_flushTx(bool bDiscard);
void mms_flushRx(void);

void mms_tx_str(unsigned char *strBuf);
void mms_tx_buf(unsigned char *strBuf ,unsigned int bufLen);
int mms_tx_buf_size(void);
int mms_rx_buf_size(void);
int mms_rx_str(unsigned char *rxPtr, unsigned char termChar, uint32_t timeout_msec);

uint32_t mms_get_cts(void);
int mms_wait_cts(int state, uint32_t timeout);
int mms_check_mmsDig_version(void);

int mms_lampPwrOn( void );
int mms_lampPwrOnUsingSpec(uint16_t *Data, uint8_t ShowRef );
int mms_lampPwrOff( void );
int mms_lamp12vPwrOn( void );
int mms_lamp12vPwrOff( void );
int mms_lampDeuteriumPwrOn( void );
int mms_lampDeuteriumPwrOff( void );
int mms_lampTungstenPwrOff( void );
int mms_lampTungstenPwrOn( void );

void mms_pwr_rs232_xcvr(uint32_t state);
uint32_t mms_get_cts(void);
int log_write_errorMsg(char *ebuf, int errorCode, uint32_t enableFlg);
int mms_specPwrOn(void);
int mms_specPwrOff(void);

int mms_scanSpectra(uint16_t scan_num, uint16_t *Data, unsigned char ScanType, int printFlg, int writeFlg );
int mms_singleScanSpectra(uint32_t scan_period, uint16_t *Data );
int mms_tuGetsTmout(char *buf, uint16_t buflen, uint16_t seconds);
int mms_tuPuts(char *str);

int mms_enable_rs232_xcvr(unsigned int state);

int mms_rts(unsigned int state);

int mms_lampPwrOn( void );
int mms_lampPwrOff( void );
int SingleScanSpectra(unsigned long scan_period, unsigned short *Data );

int mms_takeIsusSample(float *atime, uint32_t CTD_Time, float CTD_Temp, float CTD_Salinity, float CTD_Depth, int txFlg);

int mms_takeIsusPseudoSample(char *dbuf, float *atime, uint32_t CTD_Time, float CTD_Temp, float CTD_Salinity, float CTD_Depth);

float mms_calc_NO3(void);


int RefPwrOff(void);
int RefPwrOn(void);
void LampCloseShutter(void);
void LampOpenShutter(void);

#define DARKCURRENT_SCAN    0
#define INTENSITY_SCAN      1

#define SPEC_OK             0
#define SPEC_WR_TIMEOUT     (-1)
#define SPEC_RD_TIMEOUT     (-2)

#define SPEC_CTS_TIMEOUT    500   //500msec #define SPEC_CTS_TIMEOUT    500000              // 500ms TIMEOUT FOR CTS ASSERTED LOW  (CF2 elapsed timer func uses us)
#define SPEC_INT_TIMEOUT    15000  //15sec  #define SPEC_INT_TIMEOUT    15000000            // 15secs TIMEOUT FOR CTS RELEASED HIGH (CF2 elapsed timer func uses us)
#define SPEC_DAT_TIMEOUT    500    //500msec             // 500ms TIMEOUT FOR CTS ASSERTED LOW  (CF2 TPU UART func uses ms)

#define SPEC_CTS_WAIT_500MS   SPEC_CTS_TIMEOUT
#define SPEC_INT_WAIT_15000MS SPEC_INT_TIMEOUT
#define SPEC_DAT_WAIT_500MS   SPEC_DAT_TIMEOUT



/* SET LAMP IGNITION TIMEOUT, in microsecs */
//STD
#define STD_LAMP_IGNITE_TIMEOUT 15000000L   //CF2 usec timebase
#define STD_LAMP_IGNITE_WAIT_15000MS 15000L      //15sec
//APF
#define APF_LAMP_IGNITE_TIMEOUT 10000000L       //CF2 usec timebase
#define APF_LAMP_IGNITE_WAIT_10000MS 10000L      // 10 secs

/* Heraeus DTM 6/1 Deuterium Lamp - function prototypes */

extern float DataMean, DataStdDev;
extern float sysTemperature, sysHumidity;


#define MMS_BUFFER_SIZE         128
struct mmsDriver
{
             int state;
    unsigned int old_tick10ms;      // for timeout
    unsigned int new_tick10ms;
    unsigned int type;          // which type of mms ?  TBD on def
    //float        salt;
    //float        tempC;
    //float        pressure;
    //float        depth;
    unsigned int bufIndx;
    unsigned char buf[MMS_BUFFER_SIZE];      // defd in system.h
};

//External Prototypes
//void    DelayMilliSecs( ushort msecs );
void    ShortVectorMeanStdDev(unsigned short end, unsigned short data[], float *mean, float *stdev);






#ifdef APFTODO
//#if BOARD_MSC == 1

//#include    <msc_config.h>

//External Vars
extern uchar isus_spec_pwr;
extern uchar sys_message_enable;
extern ulong isus_sample_time;
extern ulong isus_spec_period;
extern float DataMean, DataStdDev;
extern float sysTemperature, sysHumidity;

//External Prototypes
//void    DelayMilliSecs( ushort msecs );
int     LOGERRORMSG(char *emsg, int ecode, uchar outp);
void    ShortVectorMeanStdDev(ushort end, ushort data[], float *mean, float *stdev);

#endif



/* Zeiss MMS-UV Spectrometer w/Cronin 16bit electronics - function prototypes */

/* Spectrometer high level function prototypes */

/* Spectrometer low  level function prototypes */

#ifdef APFTODO
int SpecPwrOn( TUPort **tup );
int SpecPwrOff( TUPort *tup );
int ScanSpectra( TUPort *tup, FILE *fptr, ushort scan_num, ushort *Data, uchar ScanType );
int SingleScanSpectra( TUPort *tup, ulong scan_period, ushort *Data );

int TUPuts(TUPort *tup, char *str);
int TUGetsTmout(TUPort *tup, char *buf, ushort buflen, ushort seconds);
#endif


#endif /* MMS_SPEC_H_ */
