You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
mitlib.pub/MITLIB/Utility/Utility.h

347 lines
8.4 KiB
C++

#if !defined UTILITY
#define UTILITY
#include <math.h>
#include <Afxmt.h>
#include <afxtempl.h>
#include <memory>
#ifdef BUILD_UTILITYDLL
#define UTILITYDLL __declspec(dllexport)
#else
#define UTILITYDLL __declspec(dllimport)
#endif
void UTILITYDLL DebugLog(LPSTR lpFormat, ...);
void UTILITYDLL DebugLog(bool bTraceData, LPSTR lpFormat, ...);
void UTILITYDLL DebugLog(LPCTSTR lpName, bool bTraceData, LPSTR lpFormat, ...);
void UTILITYDLL DebugLogTiming(LPSTR lpFormat, ...);
void UTILITYDLL DebugLogTiming(bool bTraceData, LPSTR lpFormat, ...);
void UTILITYDLL DebugLogTiming(LPCTSTR lpName, bool bTraceData, LPSTR lpFormat, ...);
void UTILITYDLL StartDebugLog();
void UTILITYDLL StopDebugLog();
void UTILITYDLL StartDebugTimingLog();
void UTILITYDLL StopDebugTimingLog();
bool UTILITYDLL IsDebugLogStarted();
bool UTILITYDLL IsDebugLogTimingStarted();
// All GUI Activities Events Log
void UTILITYDLL GUIEventLog(LPSTR lpFormat, ...);
void UTILITYDLL StartGUIEventLog();
void UTILITYDLL StopGUIEventLog();
class UTILITYDLL CMyWait
{
public:
DWORD Wait(HANDLE handle, DWORD dwTimeout);
DWORD Wait(LPHANDLE handle, BOOL bWaitForAll, DWORD dwTimeout, int nNoOfHandle);
CMyWait();
virtual ~CMyWait();
};
class UTILITYDLL CEvt : public CMyWait
{
public:
// gary 05jun03
CEvt(); // create an object without name, this object can only be used in the local thread
CEvt(LPCTSTR pszNo); // create an object with the name pszNo
HANDLE GetEvt(BOOL bState);
~CEvt();
//Attributes
CString m_name;
CEvent m_event;
BOOL m_bModified;
BOOL m_bSignaled;
// gary 05jun03
// protected:
BOOL CEvt::operator =(const BOOL bState);
BOOL CEvt::operator ==(const BOOL bState);
BOOL CEvt::operator !=(const BOOL bState);
// gary 05jun03
// Operations
BOOL WaitEvt(DWORD dwTimeOut=INFINITE);
BOOL WaitResetEvt(DWORD dwTimeOut=INFINITE);
BOOL Reset(void);
BOOL Set(void);
CString GetEvtName();
private:
// gary 05jun03
CString m_csName;
CEvent m_evtActivate;
CEvent m_evtDeactivate;
// gary 05jun03
};
class UTILITYDLL CHighResPerformanceCounter
{
public:
LONGLONG GetCurrentTimeSpanInMsec();
// default constructor
CHighResPerformanceCounter();
// default destructor
virtual ~CHighResPerformanceCounter();
// record start count
void StartCnt(void);
// record stop count
void StopCnt(void);
// get current counter
LONGLONG GetCurrentCnt();
// get time span in msec
LONGLONG GetTimeSpanInMsec();
// cal msec time span in count
static LONGLONG CalMsecTimeSpanInCnt(LONGLONG lMsec);
// cal time span in msec
static LONGLONG CalTimeSpanInMsec(LONGLONG lcount);
// cal time span in sec
static long double CalTimeSpanInSec(LONGLONG lcount);
// cal time span in mins
static double CalTimeSpanInMin(LONGLONG lCnt);
// cal time span in hours
static double CalTimeSpanInHr(LONGLONG lCnt);
private:
// high resolution frequnecy
static LARGE_INTEGER m_lFreq;
// Start count
LARGE_INTEGER m_lStartCnt;
// Stop count
LARGE_INTEGER m_lStopCnt;
};
class UTILITYDLL CIniFile
{
public:
CIniFile();
virtual ~CIniFile();
void SetFileName(LPCTSTR sFileName);
void SetSectionName(LPCTSTR m_sSectionName);
// Set Keyname
void SetKeyName(LPCTSTR sKeyName);
// Get Keyname
LPCTSTR GetKeyName();
// Get SectionName
LPCTSTR GetSectionName();
// Get FileName
LPCTSTR GetFileName();
BOOL WriteString(LPCTSTR lpKeyName, LPCTSTR lpString);
BOOL WriteLong(LPCTSTR lpKeyName, long lLong);
BOOL WriteDouble(LPCTSTR lpKeyName, double dDouble);
CString GetString(LPCTSTR lpKeyName, LPCTSTR lpDefaultString, BOOL bCreateIfFail = TRUE);
long GetLong(LPCTSTR lpKeyName, long lDefaultLong, BOOL bCreateIfFail = TRUE);
double GetDouble(LPCTSTR lpKeyName, double dDefaultDouble, BOOL bCreateIfFail = TRUE);
// write longlong value to file
BOOL WriteLongLong(LPCTSTR lpKeyName, LONGLONG lLong);
// get longlong value from file
LONGLONG GetLongLong(LPCTSTR lpKeyName, LONGLONG lDefaultLong, BOOL bCreateIfFail = TRUE);
// write longlong value to file
static BOOL WriteLongLong(LPCTSTR pszFileName, LPCTSTR pszSectionName, LPCTSTR lpKeyName, LONGLONG lLong);
// get long long value from file
static LONGLONG GetLongLong(LPCTSTR pszFileName, LPCTSTR pszSectionName, LPCTSTR lpKeyName, LONGLONG lDefaultLong, BOOL bCreateIfFail = TRUE);
static BOOL WriteString(LPCTSTR pszFileName, LPCTSTR pszSectionName, LPCTSTR lpKeyName, LPCTSTR lpString);
static BOOL WriteLong(LPCTSTR pszFileName, LPCTSTR pszSectionName, LPCTSTR lpKeyName, long lLong);
static BOOL WriteDouble(LPCTSTR pszFileName, LPCTSTR pszSectionName, LPCTSTR lpKeyName, double dDouble);
static CString GetString(LPCTSTR pszFileName, LPCTSTR pszSectionName, LPCTSTR lpKeyName, LPCTSTR lpDefaultString, BOOL bCreateIfFail = TRUE);
static long GetLong(LPCTSTR pszFileName, LPCTSTR pszSectionName, LPCTSTR lpKeyName, long lDefaultLong, BOOL bCreateIfFail = TRUE);
static double GetDouble(LPCTSTR pszFileName, LPCTSTR pszSectionName, LPCTSTR lpKeyName, double dDefaultDouble, BOOL bCreateIfFail = TRUE);
// attributes
private:
char m_sFileName[50];
char m_sSectionName[50];
char m_sKeyName[50];
};
/////////////////////////////////////////////////////////////////////////////
// CMyPoint
class UTILITYDLL CMyPoint
{
public:
CMyPoint()
{
x = 0;
y = 0;
}
double x;
double y;
};
/////////////////////////////////////////////////////////////////////////////
// CThreePoint
// x3, y3
// _______________
// | |
// | - |
// | } |
// | + __|___- |
// | | |
// | | |
// | + |
// |_______________|
//
// x1, y1 x2, y2
//
// dx = x3 - x1 / No of y pt
// dy = y2 - y1 / No of x pt
/////////////////////////////////////////////////////////////////////////////
// tan theta = adjacent / opposite = y / x
// cos theta = adjacent / hypo = x / hypo
// sine theta = opposite / hypo = y / hypo
// cotan theta = opposite / adjacent = x / y
// secant theta = hypo / adjacent = hypo / x
// cosecant theta = hypo / opposite = hypo / y
#define PI 3.1415926535897932384626433832795
#define RAD_TO_DEGREE 180/PI // 1 RADIAN = 57.29577951<EFBFBD>
class UTILITYDLL CThreePoint
{
public:
CThreePoint();
enum
{
PT1,
PT2,
PT3,
};
double Getx(int iPt);
double Gety(int iPt);
void Setx(int iPt, double x);
void Sety(int iPt, double y);
void Setx(double p1, double p2, int iPitchStep = 0);
void Setx(double p1, double p2, double p3, int iStep = 0, int iPitchStep = 0);
void Sety(double p1, double p2, int iStep = 0);
void Sety(double p1, double p2, double p3, int iStep = 0, int iPitchStep = 0);
double Pitchx(void);
double Pitchy(void);
double Dx(void);
double Dy(void);
double Xcentre(void);
double Ycentre(void);
// get theta in radian
double Theta();
// get theta in degree
double ThetaInDegree();
double x,
y;
protected:
CMyPoint pt1,
pt2,
pt3;
CMyPoint delta;
CMyPoint pitch;
};
//class CInput;
class UTILITYDLL CTrigger : public CMyWait
{
public:
CTrigger();
virtual ~CTrigger();
void InsertTrigger(CEvt* pEvt, BOOL bState);
void InsertTrigger(HANDLE handle);
// void InsertInput(CInput* pInput, BOOL bState, DWORD dwDebounce, DWORD dwTimeout);
DWORD Wait(DWORD dwTimeOut = INFINITE);
BOOL WaitForAll(DWORD dwTimeOut = INFINITE);
void Reset();
protected:
DWORD m_dwCount;
USHORT m_usModuleNo;
CTypedPtrArray<CPtrArray, HANDLE> m_aHandle;
};
class UTILITYDLL CSharedMemory : public CObject
{
public:
CSharedMemory();
virtual ~CSharedMemory();
DWORD CreateSharedMem(LPCTSTR pszName, DWORD dwBytes);
// Will return 0 or ERROR_ALREADY_EXISTS if it already exists.
LPVOID Open(DWORD dwTimeout = INFINITE); // SM must be opened before use. The pointer
// returns must be type casted to it type
// Returns NULL if the SM was not created ot failed
BOOL Close(); // SM should be closed after use so that others can open the SM
BOOL IsOpen() { return m_bOpen; }; // Check if SM is opened
DWORD GetLength() { return m_dwLength; };
#ifdef _DEBUG
virtual void AssertValid() const;
virtual void Dump(CDumpContext& dc) const;
#endif
protected:
DECLARE_DYNAMIC(CSharedMemory)
private:
HANDLE m_hMutex;
LPVOID m_pvData;
HANDLE m_hMapping;
BOOL m_bOpen;
DWORD m_dwLength;
};
#endif