425 lines
No EOL
13 KiB
C++
425 lines
No EOL
13 KiB
C++
#include "pch.h"
|
|
#include "MK3Base.h"
|
|
#pragma unmanaged
|
|
|
|
HANDLE Ev1 = INVALID_HANDLE_VALUE;
|
|
MK3::Epd2U* current;
|
|
|
|
void WINAPI completion(CommsHandle hComms, CompletionToken token)
|
|
{
|
|
SetEvent(Ev1);
|
|
}
|
|
MK3::Epd2U::Epd2U(int _comport)
|
|
{
|
|
port = _comport;
|
|
comPort[3] = 48 + _comport;
|
|
CommsInitialized = 0;
|
|
epdComsHandle = INVALID_HANDLE_VALUE;
|
|
//SetAutoCommit(0); alleen epd2
|
|
Error = 123;
|
|
if (_comport == 0) return;
|
|
}
|
|
|
|
WORD MK3::Epd2U::WriteConfig()
|
|
{
|
|
if (port == 0) return 1;
|
|
return 0; // WriteDisplayConfig(00, 2, 40, 10, 2, displayConfig, 56);
|
|
}
|
|
|
|
DWORD MK3::Epd2U::StartCom()
|
|
{
|
|
ErrorReason = 0;
|
|
int mx = 10;
|
|
if (port == 0) {
|
|
CommsInitialized = 1;
|
|
return 1;
|
|
}
|
|
if (CommsInitialized == 0) {
|
|
epdComsHandle = CreateReaderInterface();
|
|
InitRv = OpenReader(epdComsHandle, comPort);
|
|
if (InitRv == 0)
|
|
{
|
|
SetMultipleDiscoveryMode(epdComsHandle, false);
|
|
//SetRetryCount(epdComsHandle, retry);
|
|
//SetDiscoveryTimeslotPeriod(port, DiscoveryTimeslotPeriod);
|
|
//DiscoveryTimeout(epdComsHandle,DiscoveryExpirationPeriod);
|
|
CommsInitialized = 1;
|
|
}
|
|
}
|
|
return InitRv;
|
|
}
|
|
|
|
MK3::Epd2U::~Epd2U()
|
|
{
|
|
if (epdComsHandle != INVALID_HANDLE_VALUE) DestroyReaderInterface(epdComsHandle);
|
|
CommsInitialized = 0;
|
|
}
|
|
|
|
void WINAPI discovery(CommsHandle hComms, DiscoveryId const discovered[], int32_t count)
|
|
{
|
|
if (count > 0)
|
|
{
|
|
current->CurrentDeviceID = discovered[0].Id;
|
|
current->DeviceCount = count;
|
|
memcpy(¤t->Epd, &discovered[0], sizeof(DiscoveryId));
|
|
SetEvent(Ev1);
|
|
}
|
|
}
|
|
|
|
int MK3::Epd2U::StartDiscover()
|
|
{
|
|
int32_t rv,rv2;
|
|
|
|
//System::Diagnostics::Trace::WriteLine("StartDiscover");
|
|
Error = 0;
|
|
if (port == 0) {
|
|
DeviceCount = 1;
|
|
DeviceIDs[0] = 768;
|
|
return 1;
|
|
}
|
|
//
|
|
//Cancel(epdComsHandle);
|
|
//FlushDiscoveryCache(port);
|
|
//FlushBuffers(port);
|
|
int tries = 25; DeviceCount = 0; DiscRV = 1;
|
|
while (tries-- > 0 && DeviceCount == 0)
|
|
{
|
|
//memset(DevceIDs, 0, sizeof(DeviceIDs));
|
|
current = this;
|
|
Ev1 = CreateEvent(NULL, TRUE,FALSE,NULL);
|
|
rv = StartDiscovery(epdComsHandle, discovery);
|
|
if (rv==0) rv = WaitForSingleObject(Ev1, 10000);
|
|
}
|
|
if (rv == 0) DiscRV=Connect(epdComsHandle, Epd, true);
|
|
//System::Diagnostics::Trace::WriteLine(System::String::Format("Discover DeviceCount = {0}", DeviceCount));
|
|
|
|
return DiscRV;
|
|
}
|
|
|
|
void MK3::Epd2U::Open(DiscoveryId DeviceID)
|
|
{
|
|
CurrentDeviceID = DeviceID.Id;
|
|
if (port > 0) {
|
|
ConRV = Connect(epdComsHandle, DeviceID,true);
|
|
//if (ConRV == 0) Error = GetErrorDetails(&ErrorSource, &ErrorReason, &ErrorData);
|
|
//else {
|
|
Open(DeviceID); // OpenDevice(port, CurrentDeviceID);
|
|
//if (OpenRV == 0) Error = GetErrorDetails(&ErrorSource, &ErrorReason, &ErrorData);
|
|
}
|
|
//}
|
|
else { OpenRV = ConRV = 1; }
|
|
}
|
|
void MK3::Epd2U::CloseDev()
|
|
{
|
|
if (port > 0) {
|
|
//Close();// CloseDevice();
|
|
Disconnect(epdComsHandle);
|
|
}
|
|
//DeinitComms(port); CommsInitialized = 0;
|
|
}
|
|
|
|
//void MK3::Epd2U::DecodeFunctionFlags()
|
|
//{
|
|
// EpdType = (FunctionFlags & 0x1F00) >> 8;
|
|
//}
|
|
|
|
|
|
byte checkOpStatus(OpStatusFlags status, OpStatusFlags flag)
|
|
{
|
|
if (((uint32_t)status & (uint32_t)flag) == (uint32_t)flag) return 1; else return 0;
|
|
}
|
|
byte checkFailStatus(FaultStatusFlags status, FaultStatusFlags flag)
|
|
{
|
|
if (((uint32_t)status & (uint32_t)flag) == (uint32_t)flag) return 1; else return 0;
|
|
}
|
|
|
|
byte checkStatus(AlarmStatusFlags status, AlarmStatusFlags flag)
|
|
{
|
|
uint32_t ustatus = (uint32_t)status;
|
|
uint32_t uflag = (uint32_t)flag;
|
|
if ((ustatus & uflag) == uflag) return 1; else return 0;
|
|
}
|
|
|
|
byte checkStatus( DoseAlarmFlags status, DoseAlarmFlags flag)
|
|
{
|
|
if (((uint32_t)status & (uint32_t)flag) == (uint32_t)flag) return 1; else return 0;
|
|
}
|
|
|
|
void MK3::Epd2U::DecodeStatus()
|
|
{
|
|
DetectorsOn = checkOpStatus(epd3status.OperatingStatus, OpStatusFlags::EpdOn);
|
|
EPDIssued = checkOpStatus(epd3status.OperatingStatus, OpStatusFlags::EpdIssued);
|
|
EPDLocked = checkOpStatus(epd3status.OperatingStatus, OpStatusFlags::GainAdjustable);
|
|
EPDTesting = checkOpStatus(epd3status.OperatingStatus, OpStatusFlags::DetectorTestRequested);
|
|
EPDFaulty = checkFailStatus(epd3status.FaultStatus, FaultStatusFlags::EpdFaulty);
|
|
|
|
EPDNewFault = checkFailStatus(epd3status.FaultStatus, FaultStatusFlags::EpdFaulty);
|
|
EPDComFault = checkFailStatus(epd3status.FaultStatus, FaultStatusFlags::EarlyCommsTermination);
|
|
EPDCalFault = checkFailStatus(epd3status.FaultStatus, FaultStatusFlags::NotCalibrated);
|
|
EPDDetFault = checkFailStatus(epd3status.FaultStatus, FaultStatusFlags::DetectorTestFail);
|
|
EPDEprFault = checkFailStatus(epd3status.FaultStatus, FaultStatusFlags::EepromFailure);
|
|
EPDTrhFault = checkFailStatus(epd3status.FaultStatus, FaultStatusFlags::BadThreshold);
|
|
|
|
for (int ix = 0; ix < 4; ix++)
|
|
{
|
|
DoseAlarmFlags status = epd3status.DoseAlarms[ix].Status;
|
|
switch ((MeasurementId)epd3status.DoseAlarms[ix].Id)
|
|
{
|
|
case MeasurementId::Meas_Hp07:
|
|
case MeasurementId::Meas_Hp10N:
|
|
AlarmDoseHP07N = checkStatus(status ,DoseAlarmFlags::DoseAlarm);
|
|
AlarmRateHP07N = checkStatus(status, DoseAlarmFlags::RateAlarm) ;
|
|
break;
|
|
case MeasurementId::Meas_Hp10:
|
|
case MeasurementId::Meas_Hp10G:
|
|
AlarmDoseHP101G = checkStatus(status, DoseAlarmFlags::DoseAlarm);
|
|
AlarmDoseHP102G = AlarmDoseHP101G;
|
|
AlarmRateHP101G = checkStatus(status, DoseAlarmFlags::RateAlarm);
|
|
AlarmRateHP102G = AlarmRateHP101G;
|
|
break;
|
|
}
|
|
}
|
|
OtherAlarmReturn = checkStatus(epd3status.AlarmStatus, AlarmStatusFlags::ReturnForRead);
|
|
OtherAlarmBatLow = checkStatus(epd3status.AlarmStatus, AlarmStatusFlags::LowBattery);
|
|
OtherAlarmCarrier = checkStatus(epd3status.AlarmStatus, AlarmStatusFlags::TelemetryAlert);
|
|
OtherAlarmBatVolt = checkStatus(epd3status.AlarmStatus, AlarmStatusFlags::LowBattery);
|
|
}
|
|
|
|
|
|
WORD MK3::Epd2U::ReadStatus()
|
|
{
|
|
int32_t r;
|
|
|
|
OperatingStatus = (OpStatusFlags)0U;
|
|
AlarmStatus = (AlarmStatusFlags)0U;
|
|
ErrorStatus = (FaultStatusFlags)0;
|
|
IssueCount = OtherAlarms = EEPROMBadSectors = 0;
|
|
RealTime = EpdClock = OffTime = EpdID = 0;
|
|
ErrorSource = ErrorReason = ErrorData = 0;
|
|
HardVersion = SoftVersion = FunctionFlags = 0;
|
|
|
|
ResetEvent(Ev1);
|
|
token = BeginReadStatus(epdComsHandle, completion);
|
|
r = Commit(epdComsHandle);
|
|
r = WaitForSingleObject(Ev1, 10000); // no time-out interval
|
|
r = EndReadStatus(epdComsHandle, token, &epd3status);
|
|
|
|
IssueCount = epd3status.IssueCount;
|
|
OperatingStatus = epd3status.OperatingStatus;
|
|
AlarmStatus = epd3status.AlarmStatus;
|
|
ErrorStatus = epd3status.FaultStatus;
|
|
OtherAlarms = epd3status.OtherBits;
|
|
|
|
|
|
Error = 0; ReadStatusRV = 0;
|
|
|
|
uint32_t EpdID32;
|
|
uint32_t capabilities;
|
|
MarkNumber_t MarkNumber;
|
|
VersionNumber_t VersionNumber;
|
|
|
|
ResetEvent(Ev1);
|
|
token = BeginReadEpdIdentities(epdComsHandle, completion);
|
|
r = Commit(epdComsHandle);
|
|
r = WaitForSingleObject(Ev1, 10000); // no time-out interval
|
|
r = EndReadEpdIdentities(epdComsHandle, token, &EpdID32, (EpdTypes*)&EpdType, &capabilities, &MarkNumber, &VersionNumber, NULL, 0);
|
|
|
|
EpdID = EpdID32;
|
|
|
|
DecodeStatus();
|
|
|
|
memset(WearerID, 0, WearerIDLength + 1);
|
|
memset(WearerName, 0, WearerNameLength + 1);
|
|
|
|
if (port == 0) {
|
|
EpdID = 4567;
|
|
EpdType = 0;
|
|
FunctionFlags = 0x000;
|
|
Epd.Gen = EpdGeneration::None;
|
|
Epd.MaxBaud = 0;
|
|
Epd.Id = EpdID;
|
|
return 1;
|
|
}
|
|
|
|
uint8_t length = 0;
|
|
|
|
if (Epd.Gen == EpdGeneration::Mk2 ) {
|
|
counterIdLen = 4;
|
|
counterIds[0] = (uint8_t)CounterType::Counter_Hg1; // MK2 HG;
|
|
counterIds[1] = (uint8_t)CounterType::Counter_Sg1; // MK2 SG;
|
|
counterIds[2] = (uint8_t)CounterType::Counter_Sg2; // MK2 BC;
|
|
counterIds[3] = (uint8_t)CounterType::Counter_Fb1; // MK2 FB;
|
|
}
|
|
else {
|
|
counterIdLen = 4;
|
|
counterIds[0] = (uint8_t)CounterType::Counter_Hg1; // MK2 HG;
|
|
counterIds[1] = (uint8_t)CounterType::Counter_Sg1; // MK2 SG;
|
|
counterIds[2] = (uint8_t)CounterType::Counter_Sg2; // MK2 BC;
|
|
counterIds[3] = (uint8_t)CounterType::Counter_Fb1; // MK2 FB;
|
|
}
|
|
|
|
for (int ix = 0; ix < 10; ix++) {
|
|
Counts[ix].type = 0; Counts[ix].value = 0;
|
|
}
|
|
countsRead = 0; utc = 0; seconds = 0;
|
|
|
|
|
|
//There are only four counters in Mk2 EPD.TruDose has six counters
|
|
//uint8_t x = sizeof(Counts);
|
|
ResetEvent(Ev1);
|
|
token = BeginReadCounts(epdComsHandle, counterIds, 4, completion);
|
|
r = Commit(epdComsHandle);
|
|
r = WaitForSingleObject(Ev1, 10000); // no time-out interval
|
|
r = EndReadCounts(epdComsHandle,token,Counts,10, &countsRead,&utc,&seconds);
|
|
// - 101 = InvalidParam
|
|
// - 104 = FunctionFail,
|
|
|
|
|
|
IdString_t mk3WearerID;
|
|
//Mk2 EPDs only support Name and Primary ID
|
|
ResetEvent(Ev1);
|
|
token = BeginReadWearerId(epdComsHandle, static_cast<uint16_t>(WearerIdType::WearerId_Name), completion);
|
|
r = Commit(epdComsHandle);
|
|
r = WaitForSingleObject(Ev1, 10000); // no time-out interval
|
|
r = EndReadWearerId(epdComsHandle, token, &mk3WearerID);
|
|
if (mk3WearerID.Length > WearerNameLength) mk3WearerID.Length = WearerNameLength;
|
|
memcpy(WearerName, mk3WearerID.Utf8String, mk3WearerID.Length); WearerName[mk3WearerID.Length] = 0;
|
|
|
|
ResetEvent(Ev1);
|
|
token = BeginReadWearerId(epdComsHandle, static_cast<uint16_t>(WearerIdType::WearerId_Primary), completion);
|
|
r = Commit(epdComsHandle);
|
|
r = WaitForSingleObject(Ev1, 10000); // no time-out interval
|
|
r = EndReadWearerId(epdComsHandle, token, &mk3WearerID);
|
|
if (mk3WearerID.Length > WearerIDLength) mk3WearerID.Length = WearerIDLength;
|
|
memcpy(this->WearerID, mk3WearerID.Utf8String, mk3WearerID.Length); WearerName[mk3WearerID.Length] = 0;
|
|
for (int i = 0; i < mk3WearerID.Length; i++)
|
|
if (WearerID[i] == 0xb) WearerID[i] = '0'; else WearerID[i] |= 0x30;
|
|
|
|
//1601-01-01T00:00:00Z.
|
|
|
|
FILETIME fUTC;
|
|
TimeFunctions::EpdTimeToFileTime(utc, &fUTC);
|
|
FileTimeToSystemTime(&fUTC, &tRealTime);
|
|
|
|
|
|
//CommitRV = ReadEpdStatus(&EpdClock, &RealTime, &IssueCount, &OffTime, &OperatingStatus, &ErrorStatus, &AlarmStatus, &OtherAlarms, &EEPROMBadSectors);
|
|
//if (port == 0) {
|
|
// EpdClock = 123; ErrorStatus = 0;
|
|
//};
|
|
//if (CommitRV == 1) CommitRV = Commit();
|
|
//if (CommitRV == 1) CommitRV = ReadEpdID(
|
|
// &EpdID,
|
|
// &HardVersion,
|
|
// &SoftVersion,
|
|
// &FunctionFlags);
|
|
//if (CommitRV == 1) CommitRV = Commit();
|
|
//if (CommitRV == 0) {
|
|
// Error = GetErrorDetails(&ErrorSource, &ErrorReason, &ErrorData);
|
|
// ReadStatusRV = ErrorReason;
|
|
// return 0;
|
|
//}
|
|
//else {
|
|
// if (CurrentDeviceID == 0) CurrentDeviceID = EpdID;
|
|
// time_t t = RealTime;
|
|
// TimeFunctions::UnixTimeToSystemTime(t, &tRealTime);
|
|
// DecodeStatus();
|
|
// DecodeFunctionFlags();
|
|
//}
|
|
|
|
//if (CommitRV == 1 && EPDIssued == 1)
|
|
//{
|
|
// CommitRV = ReadWearer(
|
|
// WearerID,
|
|
// WearerIDLength,
|
|
// WearerName,
|
|
// WearerNameLength);
|
|
// if (CommitRV == 1) CommitRV = Commit();
|
|
// if (CommitRV == 0) {
|
|
// Error = GetErrorDetails(&ErrorSource, &ErrorReason, &ErrorData);
|
|
// ReadStatusRV = ErrorReason;
|
|
// return 0;
|
|
// }
|
|
// else {
|
|
// }
|
|
//}
|
|
return CommitRV;
|
|
}
|
|
|
|
WORD MK3::Epd2U::ReadDoses()
|
|
{
|
|
if (port == 0) {
|
|
Hp10Dose = Hp07Dose = Hp10Total = Hp07Total = Knocks = Resets = QualityData = 21;
|
|
Hp10Peak = Hp07Peak = Hp10Time = Hp07Time = 1000;
|
|
ErrorSource = ErrorReason = ErrorData = 0;
|
|
K1 = 1; K2 = 2; K3 = 3; K4 = 4; K5 = 5; K6 = 6;
|
|
SG = 7; BC = 8; FB = 9; HG = 10;
|
|
return 1;
|
|
}
|
|
ReadDosesQualityRV = ReadPeaksRV = CommitRV = 0;
|
|
|
|
numDoses = 0; utc = 0;
|
|
ResetEvent(Ev1);
|
|
//token = BeginReadDoses(epdComsHandle, new uint8_t[]{ 0, 1 }, 2 , completion);
|
|
token = BeginReadDoses(epdComsHandle, nullptr, 0, completion);
|
|
int32_t r = Commit(epdComsHandle);
|
|
r = WaitForSingleObject(Ev1, 10000); // no time-out interval
|
|
r = EndReadDoses(epdComsHandle, token, doses, 2, &numDoses, &utc);
|
|
|
|
|
|
//int32_t EPDDLL_API EndReadQualityData(CommsHandle hComms, CompletionToken token, Mk3QualityData_t * quality )
|
|
//ReadDosesQualityRV = ReadDosesQuality(
|
|
// &Hp10Dose,
|
|
// &Hp07Dose,
|
|
// &Hp10Total,
|
|
// &Hp07Total,
|
|
// &Knocks,
|
|
// &Resets,
|
|
// &QualityData);
|
|
|
|
//if (ReadDosesQualityRV == 0) {
|
|
// Error = GetErrorDetails(&ErrorSource, &ErrorReason, &ErrorData);
|
|
// return 0;
|
|
//}
|
|
|
|
//BeginReadPeakRates(CommsHandle hComms, uint8_t ids[], uint8_t length, CompletionCallback callback )
|
|
/*ReadPeaksRV = ReadPeaks(
|
|
&Hp10Peak,
|
|
&Hp07Peak,
|
|
&Hp10Time,
|
|
&Hp07Time);
|
|
if (ReadPeaksRV == 0) {
|
|
Error = GetErrorDetails(&ErrorSource, &ErrorReason, &ErrorData);
|
|
return 0;
|
|
}*/
|
|
|
|
// EPD2 :
|
|
// int32_t EPDDLL_API EndReadCalibrationData_R2 (CommsHandle hComms, CompletionToken token, uint16_t *HGSens10, uint16_t *SGSens10, uint16_t *HGSens07, uint16_t *SGSens07, uint16_t *FBSens07, uint16_t *BCSens07, uint16_t *SGthresh, uint16_t *BCthresh, uint16_t *FBthresh, uint16_t *HGthresh)
|
|
// EPD3
|
|
//
|
|
//CompletionToken EPDDLL_API BeginReadSensitivities_R3(CommsHandle hComms, uint8_t sensIds[], uint8_t length, CompletionCallback callback )
|
|
//ReadCalibrationData(
|
|
// &K1,
|
|
// &K2,
|
|
// &K3,
|
|
// &K4,
|
|
// &K5,
|
|
// &K6,
|
|
// &SG,
|
|
// &BC,
|
|
// &FB,
|
|
// &HG);
|
|
//CommitRV = Commit();
|
|
//if (CommitRV == 0) Error = GetErrorDetails(&ErrorSource, &ErrorReason, &ErrorData);
|
|
return CommitRV;
|
|
}
|
|
//WORD EpdBase::Epd2U::PrepareForIssue()
|
|
//{
|
|
// if (port == 0) return 1;
|
|
// CommitRV = WriteConfig();
|
|
// if (CommitRV) CommitRV = Commit();
|
|
// if (CommitRV) CommitRV = ClearDose();
|
|
// if (CommitRV) CommitRV = ClearTotalDose();
|
|
// if (CommitRV) CommitRV = ClearPeakRates();
|
|
// if (CommitRV) CommitRV = Commit();
|
|
// return CommitRV;
|
|
//}
|