//{{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
{
private:
   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, unsigned char *values, bool msg);

   unsigned char _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 &pChar);
   bool writeCommPort(BYTE *pBuf, DWORD dwCount);
   bool writeCommPort(char *pBuf);

   void write();
   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, LONG &n);

   CString settingsFile;

// 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()

public:
   unsigned char         Const(int offset);
   unsigned char         Const(int offset, char value);
   unsigned char         ConstRaw(int offset);
   unsigned char         ConstRaw(int offset, char value);

   int                   commPortNumber;
   int                   timerInterval;

   double                kpaPlotVals[8];
   double                rpmPlotVals[8];
   double                vePlotVals[8][8];
   int                   rpmk;
   int                   nCylinders;
   int                   mapType;
   int                   strokeType;
   int                   portType;
   int                   nInjectors;
   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;

   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 unsigned char  adf [256];
   static unsigned char  barf[256];
   static unsigned char  kpaf[256];
   static unsigned char  thf [256];
          unsigned char  tpsf[256];

   static int maprange[8];
   static int rpmrange[8];

   int  alphaN();
   void alphaN(int);
   int  egoType();
   void egoType(int);
   int  baroCorr();
   void baroCorr(int);

   // 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(unsigned char rBuf[]);
   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);
   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_)

