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

#ifndef MMS_SPEC_H_
#define MMS_SPEC_H_


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);
unsigned char mms_rx_byte_timeout(uint32_t timeout_ms);
void mms_tx_flush(bool bDiscard);
void mms_rx_flush(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_buf_write(unsigned char inChar);
int mms_stateMachine(int mmsState);




uint32_t mms_get_cts(void);





int mms_lampPwrOn( void );
int mms_lampPwrOnUsingSpec(uint16_t *Data, uint8_t ShowRef );
int mms_lampPwrOff( 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(FILE *fptr, uint16_t scan_num, uint16_t *Data, unsigned char ScanType );
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_write_rtc_en_lo(unsigned int state);
int mms_set_rts(unsigned int state);

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


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    500000              // 500ms TIMEOUT FOR CTS ASSERTED LOW  (CF2 elapsed timer func uses us)
#define SPEC_INT_TIMEOUT    15000000            // 15secs TIMEOUT FOR CTS RELEASED HIGH (CF2 elapsed timer func uses us)
#define SPEC_DAT_TIMEOUT    500                 // 500ms TIMEOUT FOR CTS ASSERTED LOW  (CF2 TPU UART func uses ms)


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 );
int     LogErrorMsg(char *emsg, int ecode, uint8_t outp);
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


#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    500000              // 500ms TIMEOUT FOR CTS ASSERTED LOW  (CF2 elapsed timer func uses us)
#define SPEC_INT_TIMEOUT    15000000            // 15secs TIMEOUT FOR CTS RELEASED HIGH (CF2 elapsed timer func uses us)
#define SPEC_DAT_TIMEOUT    500                 // 500ms TIMEOUT FOR CTS ASSERTED LOW  (CF2 TPU UART func uses ms)

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