/*
 * apf_util.h
 *
 *  Created on: May 14, 2024
 *      Author: tm
 */

#ifndef APF_UTIL_H_
#define APF_UTIL_H_

#include "fatfs/source/ff.h"

#include "stdint.h"
#include "stdbool.h"

#define DATBUFSIZE      2048        // from apf_util
//#define TCOEFS             5        // Bromide Temperature Compensation Coefs  (defd in apf.h)

extern struct apfBootInitGlobals apf;
extern struct apfVolatileGlobals apf_fram;
extern struct apfCfgGlobals      apf_eeprom;
extern struct apfIsusArrays      apfArray;

struct apfVolatileGlobals const * get_const_apf_fram(void);
struct apfCfgGlobals const * get_const_apf_eeprom(void);
struct apfIsusArrays const* get_const_apfArray(void);

struct apfVolatileGlobals* get_apf_fram(void);
struct apfCfgGlobals* get_apf_eeprom(void);
struct apfIsusArrays* get_apfArray(void);

extern long Erecs;

// apf_util.h
//FILE *OpenApfLogFile( unsigned int select );     // was uint8_t select
int prepApfLogFile( unsigned int select );
// Returns ERROR or OK
int logErrorMsg(char const * const emsg, int ecode, uint8_t outp);
int LogErrorSizeHander(void);

int read_print_ecoefs_Cal_file(void);
int read_print_pH_Cal_file(void);

int UART_GetStr_timeout(unsigned char *rxPtr, uint32_t bufLen, uint32_t timeout_msec);

void print_isus_header(void);
void print_dura_header(void);

//void apf_send_last_nitrate(void);  //moved to serial_cmd_fxn.c
//void apf_send_last_pH(void);
//int get_storage_stat(uint32_t *storageSize, uint32_t *storageRemaining);   //moved to microsd.c
long CalcRecsPending(unsigned int select);
int SendRecords(unsigned int select, FSIZE_t *fOffset);
int CheckFileSize(unsigned int select);
int ReinitializeErrorLogPtrs(void);

//int LoadIsusECoefs(double *Lambda, double **Ecoefs, double *Zeros, double *Tcoefs, double *CalTemp, char *CalDate);
int LoadIsusZCoefs(double *ZeissCoefs);
int LoadDuraFetIIITCoefs(double *Coefs);

//int print_write_isusdura_header(uint32_t printFlg, uint32_t writeFlg, uint32_t smode, FILE *fptr);
int print_write_isusdura_header(uint32_t printFlg, uint32_t writeFlg, uint32_t smode, FIL *fptr);
void print_dura_header(void);
void print_isus_header(void);

//unsigned short SerGetsTmout(char *buf, unsigned short buflen, unsigned short seconds);
int UART_GetStr_timeout(unsigned char *rxPtr, uint32_t bufLen, uint32_t timeout_msec);
int SerPuts(char *str);
int safegets(char *buffer, int n);
char *strupr (char *string);
int MatchACK(char *buf);
int SyncNextSample( void );
void CheckRtcDelta(void);
int DayOfYear(int yy, int mm, int dd);
int DayOfWeek(int yy, int mm, int dd);
short Sleep(unsigned long us, bool absolute);
void pooOutputIsusHeader(FIL *fptr);
void pooOutputDuraHeader(FIL *fptr);
void DisplaySystemInfo(void);
void DisplayTimes(void);
void DisplayAlarmTime( void );
void DisplayChipSelectSettings(void);
char *DisplayFormattedTime(time_t ptime);
void DisplayLambdaInfo(FIL *fptr);
void DisplayByteVal(char *msgstr, uint8_t ByteVal);
void DisplayFile(void);

int CalcAbs(double *ZeroVals, unsigned short *IntenVals, double *AbsVals, double DCMean, unsigned short fit_pixels);

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

void resetErrorLog(void);



short QueryChar(char *prompt, short defChar, char *scanSet, char *reply);
int QueryNum(char *prompt, char *defFmt, char *scanFmt, unsigned long *value);     // ptr prompt
int QueryFloat(char *prompt, char *defFmt, char *scanFmt, float *value);
short QueryYesNo(char *prompt, short defYes);
int InputLine(char *linebuf, short linelen);
int kbflush(void);





#endif /* APF_UTIL_H_ */
