#ifndef __RIO_H__
#define __RIO_H__


#define MSBON(B)  {B |= 0x80;}
#define MSBOFF(B) {B &= ~0x80;}
#define MAXLENGTH		13

#define ACK_CHAR		0xFC
#define NAK_CHAR		0xFD
#define RESTART_CHAR	0xFE
#define IDLE_CHAR		0xFF
#define RX_ACK_BIT		0x0001
#define RX_NAK_BIT		0x0002


class CString
{
public:
	char m_sz[256];
public:
	CString()
	{
		m_sz[0] = '0';
	};
	~CString()
	{
	}

	void _cdecl Format(const char* pcszFormat, ...)
	{
		va_list va_marker;

		va_start(va_marker, pcszFormat);
		vsprintf_s(m_sz, pcszFormat, va_marker);
		va_end(va_marker);
	}

	operator const char* () const { return &m_sz[0]; }
	CString& operator = (const char* pcszText)
	{
		strcpy_s(m_sz, pcszText);
		return *this;
	}
};

class CListBox
{
public:
public:
	CListBox() {}
	~CListBox() {}
	int GetCount() const { return 0; }
	void AddString(const char* pcszText) {}
	void DeleteString(int nindex) {}
};

class CRIO : public C232Comm
{
public:
	CListBox&	m_ListBox;
	BYTE		m_ba[2048];	
	BYTE		*m_pStart;
	const BYTE	*m_pEnd;
	int			m_nCount;
	DWORD		m_dwFlags;
	BYTE		m_baRead[128];
	int			m_nState;
	int			m_nRead;
	int			m_nLeft;
	BYTE		m_bCheck;
	BYTE		m_baReplies[32];
    int			m_nReplies;
	int			TestModeActive;

	DWORD		m_dwAnalogRequest;
public:
	CRIO (CListBox& ListBox) : m_ListBox (ListBox) { 
		Init (); 
	}
	virtual ~CRIO () {};

public:	
	void Init ();
	void RIOBordState (BYTE ID);
	void DoEventLoop();
	void EventLoop (const BYTE *buf, int nLen);
    void AddReply (BYTE bReply);
    void CheckReplies ();
	void CheckAnalogRequest();
    void SendCommand (const BYTE* pbPacket);
    void SendPacket (const BYTE* pbPacket, int nLen);	
	void SetLamp (int lampNumber, int state);

	void GeneralReset();
	void ResetThrottle ();
	void RequestCheck();
	void RequestVersion();
	void RequestAnalogUpdate();

public:
	enum RIOCommand {
			rio_CheckRequest = 0x80, // 128
			rio_VersionRequest,		 // 129
			rio_AnalogRequest,		 // 130
			rio_ResetRequest,		 // 131
			rio_LampRequest,		 // 132
			rio_CheckReply,          // 133
			rio_VersionReply,        // 134
			rio_AnalogReply,         // 135
			rio_ButtonPressed,       // 136
			rio_ButtonReleased,      // 137
			rio_KeyPressed,          // 138
			rio_KeyReleased,         // 139
			rio_TestModeChange       // 140
	};

	enum RIOStatusType {
		BoardOk, BoardMissing, BoardBad, LampBad, RestartCount, AbandonCount, FullBufferCount
	};

	enum LampState {
			solid=0, flashSlow=1, flashMed=2, flashFast=3,
			state1Off=0x00, state1Dim=0x04, state1Bright=0x0C,
			state2Off=0x00, state2Dim=0x10, state2Bright=0x30,
	};

	enum {
		LampSolidOff    = solid + state1Off    + state2Off,
		LampSolidDim    = solid + state1Dim    + state2Dim,
		LampSolidBright = solid + state1Bright + state2Bright
	};
};

#endif