//{{AFX_INCLUDES()
//}}AFX_INCLUDES
#if !defined(AFX_MSDATABASE_H__370F93E0_432F_11D6_9D64_8241A2D08E2F__INCLUDED_)
#define AFX_MSDATABASE_H__370F93E0_432F_11D6_9D64_8241A2D08E2F__INCLUDED_

// $Id$

#if _MSC_VER > 1000
#  pragma once
#endif // _MSC_VER > 1000

#include <assert.h>

//------------------------------------------------------------------------------
// msDatabase dialog

class msDatabase : public CDialog
{
public:
   enum datalogType { classicLog, fullLog, rawLog };

private:
   datalogType _logType;
   bool    _recording;
   FILE   *_recordfile;

   int     _pageNo;
   bool    _dualTable;
   void    getVersion();
   void    sendPage(int pageNo=-1);

   char cwd[_MAX_PATH];

   bool charIn(char c, char *set);
   bool readTable(char *fileName, WORD *values, bool msg);

   BYTE _const[257]; // Ack, this should be busted up some.

   bool _loaded;  // Memory image is valid from either file or MS.
   bool _changed; // Memory image is different than MS.
   bool _burned;  // MS RAM and flash are different.

   HANDLE hPortHandle;         // Handle to COM port

   bool openCommPort ();
   void closeCommPort();
   bool readCommPort (BYTE &pBYTE);
   bool writeCommPort(BYTE *pBuf, DWORD dwCount);
   bool writeCommPort(char *pBuf);

   void dumpOne(FILE *, int);

   bool _logging;
   enum logType { rd, wr };
   void log(logType lt, bool ok, BYTE *buf, int len);

// Construction
public:
   msDatabase(CWnd* pParent = NULL);   // standard constructor
  ~msDatabase();

   static bool number(char *s, BYTE &n);
   static bool number(char *s, WORD &n);
   static bool number(char *s, LONG &n);

   static CString settingsFile;
   void           readSettings();
   void           writeSettings();

// Dialog Data
   //{{AFX_DATA(msDatabase)
	enum { IDD = IDD_MSDATABASE };
	//}}AFX_DATA


// Overrides
   // ClassWizard generated virtual function overrides
   //{{AFX_VIRTUAL(msDatabase)
   protected:
      virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
   //}}AFX_VIRTUAL

// Implementation
protected:

   // Generated message map functions
   //{{AFX_MSG(msDatabase)
      DECLARE_EVENTSINK_MAP()
	//}}AFX_MSG
   DECLARE_MESSAGE_MAP()

private:
   static WORD  adf [256];
   static WORD  barf[256];
   static WORD  thf [256];
          WORD  mtf [256];
          WORD  tpsf[256]; // Transfer fn from adc to %.

   int nSquirts1();
   int nSquirts2();

public:
          BYTE  tpsAdcLo, tpsAdcHi; // From % to adc.
   // Make this one private someday.
   static WORD  kpaf[256];

public:
   BYTE         Const(int offset);
   BYTE         Const(int offset, BYTE value);
   BYTE         ConstRaw(int offset);
   BYTE         ConstRaw(int offset, BYTE value);

   int                   commPortNumber;
   int                   timerInterval;

   double                kpaPlotVals[8];
   double                rpmPlotVals[8];
   double                vePlotVals[8][8];
   int                   rpmk;
   int                   nCylinders();
   int                   setCylinders(int);
   int                   mapType;
   int                   strokeType;
   int                   portType;
   int                   nInjectors();
   int                   setInjectors(int);
   int                   engType;
   double                cid;
   double                injectorFlow;
   double                afr;
   double                reqFuel;
   int                   nSquirts();// Number of squirts per cycle per injector.
   double                dutyCycle1;// Current injector duty cycle.
   double                dutyCycle2;
   double                pulseWidth1;
   double                pulseWidth2;
   double                cycleTime; // Milliseconds per cycle at current rpm.
   double                vBatt;     // Current MS power supply voltage.
   int                   maxmap;
   int                   throttle;  // From runtime vars.
   int                   map;
   double                mat;       // Temperatures calculated from runtime vars.
   double                coolant;
   double                controllerVersion;
   bool                  controllerReset; // Reset detected.
   int                   controllerResetCount;

   BYTE tpsFromPct(BYTE pct);
   BYTE pctFromTps(BYTE tps);
   void tpsSet(BYTE adcLo, BYTE adcHi);

   double tempFromDb(double t);
   double dbFromTemp(double t);
   void   fixThermoLabel(CStatic &lbl, char *fmt=NULL, double value=0.0);

   typedef enum { Celsius, Fahrenheit } thermType;
   static thermType therm;

   static int maprange[8];
   static int rpmrange[8];

   int  alphaN();
   void alphaN(int);
   int  egoType();
   void egoType(int);
   int  baroCorr();
   void baroCorr(int);

   int  interpolateVE(int rpm, int map);

   // Configuration storage/retrieval.
   void readConfig (); // PCC config files.
   void writeConfig();

   void save();    // Controller configuration file.
   void saveAs();
   void open();
   void veExport();
   void veImport();

   bool getRuntime(BYTE rBuf[]);
   void putConst(); // Sends and burns whole thing.
   void putConstByte(int offset, int value, bool sendit=false);
   void putConstByte(int offset);
   bool getConst(int pageNo=-1); // -1 means "current page."
   void burnConst();
   bool getMemory(BYTE pageNumber, BYTE buffer[256]); // pageNumber is MSB of full 16-bit address.

   bool toggleCommLogging();

   bool dualTable();
   int  pageNo();
   int  setPageNo(int pageNo);

   void dump();

   void setRecording(bool state, datalogType LogType=classicLog);
   bool recording();

   bool changed();
   bool loaded ();
   bool burned ();
};

//{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations immediately before the previous line.

//------------------------------------------------------------------------------
#endif // !defined(AFX_MSDATABASE_H__370F93E0_432F_11D6_9D64_8241A2D08E2F__INCLUDED_)

