#pragma once
#pragma unmanaged
#include <windows.h>
#include "reader2.h"
#pragma unmanaged
namespace EpdBase
{
 class Epd2U
	{

	public:
		bool isBG ;
		bool isNG ;
		bool VersionOK;
		bool hasAlarm = false;

	public:
		WORD port = 3;
		const WORD MaxDevices = 10;
		WORD DeviceCount = 0;
		DWORD DeviceIDs[10];
		DWORD CurrentDeviceID;

		WORD AlarmCtrl = 0b01111110;
		//WORD AlarmCtrl = 0b01111010; // NO sound
		WORD EndSessionControl = 0b00010110;

		BYTE displayConfig[56] =  { 0,0,1,1,0,0,0,0,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 };

		DWORD EpdClock;
		DWORD RealTime;
		WORD IssueCount;
		DWORD OffTime;
		WORD OperatingStatus;
		WORD ErrorStatus;
		WORD AlarmStatus;
		WORD OtherAlarms;
		WORD EEPROMBadSectors;


		WORD CommsInitialized = 0;
		WORD InitRv = 0;
		WORD DiscRV = 0;
		WORD ConRV = 0;
		WORD OpenRV=0;
		WORD ReadStatusRV = 0;
		WORD CommitRV = 0;
		WORD OffRV = 0;
		WORD OnRV = 0;
		WORD EndSessionRV = 0;
		WORD ReadConfigRV = 0;
		WORD ReadDoseTresholdRV = 0;
		WORD ReadAlarmRateThresholdsRV = 0;
		WORD ReadCalibrationDataRV = 0;
		WORD ReadWearerRV = 0;
		WORD ReadEpdIDRV = 0;
		WORD ReadDosesQualityRV;
		WORD ReadPeaksRV = 0;

		WORD ClearDoseRV;
		WORD ClearTotalDoseRV;
		WORD ClearPeakRatesRV;
		WORD DeIssueEpdRV = 0;

		WORD WriteAlarmDoseThresholdsRV = 0;
		WORD WriteAlarmRateThresholdsRV = 0;
		WORD WriteWearerNameRV = 0;

		WORD IssueEpdRV = 0;

		WORD Error=0;

		WORD ErrorSource;
		WORD ErrorReason;
		WORD ErrorData;

		//Identification
		
		DWORD EpdID=0;
		WORD HardVersion;
		WORD SoftVersion;
		WORD FunctionFlags;

		WORD EpdType = 0xff;

		SYSTEMTIME tRealTime;

		//Wearer
		
		#define WearerIDLength  12
		BYTE WearerID[(WearerIDLength)+1];
		#define WearerNameLength  22
		BYTE WearerName[WearerNameLength+1];
		

		// Dose
		
		DWORD Hp10Dose;
		DWORD Hp07Dose;
		DWORD Hp10Total;
		DWORD Hp07Total;
		WORD Knocks;
		WORD Resets;
		WORD QualityData;

		//Peaks
		DWORD Hp10Peak;
		DWORD Hp07Peak;
		DWORD Hp10Time;
		DWORD Hp07Time;
		long long Hp10FTime , Hp07FTime;


		//AlarmDoseTreshold
		DWORD Hp10THDose1;
		DWORD Hp10THDose2;
		DWORD Hp07THDose;

		DWORD Hp10Rate1On; 
		DWORD Hp10Rate2On;
		DWORD Hp07RateOn;
		DWORD Hp10Rate1Off; 
		DWORD Hp10Rate2Off;
		DWORD Hp07RateOff;

		
		WORD K1;
		WORD K2;
		WORD K3;
		WORD K4;
		WORD K5;
		WORD K6;
		WORD SG;
		WORD BC;
		WORD FB;
		WORD HG;

		DWORD	D1;
		DWORD	D2;
		DWORD	D3;
		DWORD	D4;
		DWORD  CountsClock;

		DWORD	BaseD1;
		DWORD	BaseD2;
		DWORD	BaseD3;
		DWORD	BaseD4;
		DWORD  BaseCountsClock;

		
		//Config
		WORD CountDownTime;
		DWORD ReturnTime;
		WORD ChirpRate;
		WORD TeleBaudSetting;
		WORD SelfTestInterval;
		WORD BatteryLoadTestInt;
		WORD GeneralControl;
		WORD CommsTimeout;
		WORD InhibitTimeout;
		WORD MinTxInterval;
		WORD MaxTxIntervals;
		WORD RandomiseWindow;
		WORD DoseIncThresh;

		//OperatingStatus
		BYTE DetectorsOn=0;
		BYTE EPDIssued = 0;
		BYTE EPDLocked = 0;
		BYTE EPDTesting = 0;
		BYTE EPDProcSec= 0;
		BYTE EPDFaulty = 0;


		//ERrrostatus
		BYTE EPDNewFault = 0;
		BYTE EPDComFault = 0;
		BYTE EPDCalFault = 0;
		BYTE EPDDetFault = 0;
		BYTE EPDEprFault = 0;
		BYTE EPDTrhFault = 0;
		BYTE EPDOthFault = 0;


		//AlarmStatus
		BYTE AlarmDoseHP101G = 0;
		BYTE AlarmDoseHP102G = 0;
		BYTE AlarmDoseHP07N = 0;
		BYTE AlarmRateHP101G = 0;
		BYTE AlarmRateHP102G = 0;
		BYTE AlarmRateHP07N = 0;

		//OtherAlarm
		BYTE OtherAlarmRateHp101G = 0;
		BYTE OtherAlarmRateHp102G = 0;
		BYTE OtherAlarmRateHp07N = 0;
		BYTE OtherAlarmReturn = 0;
		BYTE OtherAlarmBatLow = 0;
		BYTE OtherAlarmCarrier= 0;
		BYTE OtherAlarmBatVolt = 0;



		DWORD Password = 9;


		BYTE byBatADC;
		BYTE bySupplyADC;
		BYTE byStatus3;
		BYTE byStatus4;

		int maxDiscoverTime = 10000;
		int ConnectTries = 0;
		LARGE_INTEGER WaitDiscovery;
		BYTE ConnectTimerFired;
		int StartDiscover(int EpdTimeout);
		int Open(DWORD DeviceID);
		void Close();
		int CloseAndSignal(WORD EndSessionControl, WORD duration);
		void DecodeFunctionFlags();

		void DecodeStatus();
		
		Epd2U(int comport);
		WORD WriteConfig();
		DWORD StartCom();
		void EndCom();
		WORD ReadStatus();
		WORD EPDReadExtraStatus();
		WORD EPDReadConfig();
		WORD TurnOff();
		WORD TurnOn();
		WORD ReadCal();
		WORD WriteCal();
		WORD ReadTresHolds();
		
		WORD ReadDoses();

		WORD PrepareForIssue();

		WORD DeIssue();
		WORD Issue();
		int GetAll(bool Close);
		int GetAllDeissue();
		~Epd2U();

		int ConnectAndStatus(int tokenSourceTimeout);
		int IssueAndCheck(float gain);
		
		public:
			HANDLE hWaitTimer = 0;
	};
}


