Files
CHJ/user/Compute/UnitConverter.c
2026-03-20 21:19:53 +08:00

456 lines
13 KiB
C
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include "../main/SystemInclude.h"
u16 flowDisUnit, flowComUnit, AccDisUnit, AccComputeUnit, AccComUnit;
long double lastUnit, currentUnit;
/******************************************************************************/
void CombinedUnitByFlowRate(void)
{
switch(flowDisUnit)
{
#if(ENABLE_SCCM_SL)
case SCCM: flowUnit = SCCM_SL; break;
#endif
case SLPM:
if(AccDisUnit == SL) flowUnit = SLPM_SL;
else if(AccDisUnit == NM3) flowUnit = SLPM_NM3;
else flowUnit = SLPM_NM3;
break;
#if(ENABLE_NCMH_NM3)
case NCMH: flowUnit = NCMH_NM3; break;
#endif
#if(ENABLE_SCFM_SCF)
case SCFM: flowUnit = SCFM_SCF; break;
#endif
#if(ENABLE_KGH_KG)
case KGH: flowUnit = KGH_KG; break;
#endif
#if(ENABLE_SCFH_SCF)
case SCFH: flowUnit = SCFH_SCF; break;
#endif
}
}
/******************************************************************************/
void CombinedUnitByACC(void)
{
switch(AccDisUnit)
{
case SL:
if(flowDisUnit == SLPM) flowUnit = SLPM_SL;
else if(flowDisUnit == NM3) flowUnit = SLPM_NM3;
#if(ENABLE_SCCM_SL)
else if(flowDisUnit == SCCM) flowUnit = SCCM_SL;
#endif
else flowUnit = SLPM_NM3;
break;
case NM3:
if(flowDisUnit == SLPM) flowUnit = SLPM_NM3;
#if(ENABLE_NCMH_NM3)
else if(flowDisUnit == NM3) flowUnit = NCMH_NM3;
#endif
else flowUnit = SLPM_NM3;
break;
case SCF:
#if(ENABLE_SCFM_SCF)
flowUnit = SCFM_SCF;
#elif(ENABLE_SCFH_SCF)
flowUnit = SCFH_SCF;
#endif
break;
#if(ENABLE_KGH_KG)
case KGH: flowUnit = KGH_KG; break;
#endif
}
}
/******************************************************************************/
u16 JudgeflowUserUnit(u16 judgeUnit)
{
switch(judgeUnit)
{
#if(ENABLE_SCCM_SL)
case SCCM: return 1;
#endif
#if(ENABLE_SLPM_NM3)
case SLPM: return 1;
#endif
#if(ENABLE_NCMH_NM3)
case NCMH: return 1;
#endif
#if(ENABLE_SCFM_SCF)
case SCFM: return 1;
#endif
#if(ENABLE_SCFH_SCF)
case SCFH: return 1;
#endif
#if(ENABLE_KGH_KG)
case KGH: return 1;
#endif
default: return 0;
}
}
/******************************************************************************/
u16 JudgeflowAccUnit(u16 judgeUnit)
{
switch(judgeUnit)
{
#if(ENABLE_SCCM_SL)
case ML: return 1;
#endif
#if(ENABLE_SLPM_NM3)||(ENABLE_SLPM_SL)
case SL: return 1;
#endif
#if((ENABLE_SLPM_NM3)||(ENABLE_NCMH_NM3))
case NM3: return 1;
#endif
#if((ENABLE_SCFM_SCF)||(ENABLE_SCFH_SCF))
case SCF: return 1;
#endif
#if(ENABLE_KGH_KG)
case KG: return 1;
#endif
default: return 0;
}
}
/******************************************************************************/
u16 JudgeflowUnit(u16 judgeUnit)
{
switch(judgeUnit)
{
//--------------------------------------------------------------------------
#ifndef ENABLE_SCCM_SL
#pragma message("[undefined] ENABLE_SCCM_SL")
#elif(ENABLE_SCCM_SL)
case SCCM_SL:
flowDisUnit = SCCM;
flowComUnit = SCCM;
AccDisUnit = SL;
AccComputeUnit = SL;
AccComUnit = SL;
break;
#endif
//--------------------------------------------------------------------------
#ifndef ENABLE_SLPM_NM3
#pragma message("[undefined] ENABLE_SLPM_NM3")
#elif(ENABLE_SLPM_NM3)
case SLPM_NM3:
flowDisUnit = SLPM;
flowComUnit = SLPM;
AccDisUnit = NM3;
AccComputeUnit = NM3;
AccComUnit = NM3;
break;
#endif
//--------------------------------------------------------------------------
#ifndef ENABLE_NCMH_NM3
#pragma message("[undefined] ENABLE_NCMH_NM3")
#elif(ENABLE_NCMH_NM3)
case NCMH_NM3:
flowDisUnit = NCMH;
#ifndef ENABLE_FIXED_UART_FR
#pragma message("[undefined] ENABLE_FIXED_UART_FR")
#elif(ENABLE_FIXED_UART_FR)
flowComUnit = SLPM;
#else
flowComUnit = NCMH;
#endif
AccDisUnit = NM3;
AccComputeUnit = NM3;
AccComUnit = NM3;
break;
#endif
//--------------------------------------------------------------------------
#ifndef ENABLE_SCFM_SCF
#pragma message("[undefined] ENABLE_SCFM_SCF")
#elif(ENABLE_SCFM_SCF)
case SCFM_SCF:
flowDisUnit = SCFM;
flowComUnit = SCFM;
AccDisUnit = SCF;
AccComputeUnit = SCF;
AccComUnit = SCF;
break;
#endif
//--------------------------------------------------------------------------
#ifndef ENABLE_KGH_KG
#pragma message("[undefined] ENABLE_KGH_KG")
#elif(ENABLE_KGH_KG)
case KGH_KG:
flowDisUnit = KGH;
flowComUnit = KGH;
AccDisUnit = KG;
AccComputeUnit = KG;
AccComUnit = KG;
break;
#endif
//--------------------------------------------------------------------------
#ifndef ENABLE_SLPM_SL
#pragma message("[undefined] ENABLE_SLPM_SL")
#elif(ENABLE_SLPM_SL)
case SLPM_SL:
flowDisUnit = SLPM;
flowComUnit = SLPM;
AccDisUnit = SL;
AccComputeUnit = ACC_STD_UINT;
#ifndef ENABLE_FIXED_UART_FR
#pragma message("[undefined] ENABLE_FIXED_UART_FR")
#elif(ENABLE_FIXED_UART_FR)
AccComUnit = ACC_STD_UINT;
#else
AccComUnit = SL;
#endif
break;
#endif
//--------------------------------------------------------------------------
#ifndef ENABLE_SCFH_SCF
#pragma message("[undefined] ENABLE_SCFH_SCF")
#elif(ENABLE_SCFH_SCF)
case SCFH_SCF:
flowDisUnit = SCFH;
flowComUnit = SCFH;
AccDisUnit = SCF;
AccComputeUnit = SCF;
AccComUnit = SCF;
break;
#endif
default: return 1;
}
return 0;
}
/******************************************************************************/
u32 ConvertFlowrate(u32 flowRateBuf, u16 targetUnit, u16 originalUnit)
{
if(targetUnit == originalUnit) return flowRateBuf;
long double flowRateTemp;
switch(originalUnit)
{
case SCCM: lastUnit = (long double)ONE_SCCM_TO_STD_FR; break;
case SLPM: lastUnit = (long double)ONE_SLPM_TO_STD_FR; break;
case NCMH: lastUnit = (long double)ONE_NCMH_TO_STD_FR; break;
case SCFM: lastUnit = (long double)ONE_SCFM_TO_STD_FR; break;
case KGH: lastUnit = (long double)ONE_KGH_TO_STD_FR; break;
case SCFH: lastUnit = (long double)ONE_SCFH_TO_STD_FR; break;
default: lastUnit = 1;
}
switch(targetUnit)
{
case SCCM: flowRateTemp = (long double)ONE_STD_FR_TO_SCCM; break;
case SLPM: flowRateTemp = (long double)ONE_STD_FR_TO_SLPM; break;
case NCMH:
flowRateTemp = (long double)ONE_STD_FR_TO_NCMH;
break;
case SCFM: flowRateTemp = (long double)ONE_STD_FR_TO_SCFM; break;
case KGH: flowRateTemp = (long double)ONE_STD_FR_TO_KGH; break;
case SCFH: flowRateTemp = (long double)ONE_STD_FR_TO_SCFH; break;
default: flowRateTemp = 1;
}
flowRateTemp *= (long double)lastUnit;
flowRateTemp *= (long double)flowRateBuf;
return (u32)flowRateTemp;
}
/******************************************************************************/
void ConvertAllFlowRateAndSave(u16 NewFlowUnit)
{
u16 I;
long double totalFlowRateCompute;
u16 originalFlowUnit, originalAccComputeUnit;
unsigned long long int tmpQWordA;
originalFlowUnit = flowComUnit;
originalAccComputeUnit = AccComputeUnit;
if(JudgeflowUnit(NewFlowUnit)) return;
disable_interrupts();
// flowUnit
// max flowRate
maxFlowRate = ConvertFlowrate(maxFlowRate, flowComUnit, originalFlowUnit);
tempDev.DWord[0] = maxFlowRate;
maxFlowRate = WriteLongParameterToMemory(OUT_MAX_FLOW);
tempL.DWord = maxFlowRate;
maxFlowRateH = tempL.Word[1];
maxFlowRateL = tempL.Word[0];
// min flowRate
minFlowRate = ConvertFlowrate(minFlowRate, flowComUnit, originalFlowUnit);
tempDev.DWord[0] = minFlowRate;
minFlowRate = WriteLongParameterToMemory(OUT_MIN_FLOW);
tempL.DWord = minFlowRate;
minFlowRateH = tempL.Word[1];
minFlowRateL = tempL.Word[0];
// alarmUpperFlowRate = ConvertFlowrate(alarmUpperFlowRate, flowComUnit, originalFlowUnit);
// tempDev.DWord[0] = alarmUpperFlowRate;
// alarmUpperFlowRate = WriteShortParameterToEEPROM(HIGH_FLOW_ALARM, HIGH_FLOW_ALARM_WIDTH);
//
// alarmLowerFlowRate = ConvertFlowrate(alarmLowerFlowRate, flowComUnit, originalFlowUnit);
// tempDev.DWord[0] = alarmLowerFlowRate;
// alarmLowerFlowRate = WriteParameterToEEPROM(LOW_FLOW_ALARM, LOW_FLOW_ALARM_WIDTH);
// correct data
for(I = 0; I < (u16)dcoefNum; I++)
{
dcoefNode[I] = ConvertFlowrate(dcoefNode[I], flowComUnit, originalFlowUnit);
StoreDevCoefNodeAndValue(I);
}
// Convert ACC FlowRate
// to STD flowunit
if(originalAccComputeUnit != AccComputeUnit)
{
// У׼<D0A3><D7BC><EFBFBD><EFBFBD>ΪSLPMʱ<4D><CAB1><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ΪNM3,6λС<CEBB><D0A1>
switch(originalAccComputeUnit)
{
//case ML: lastUnit = (long double)ONE_ML_TO_STD_ACC; break;
case SL: lastUnit = (long double)ONE_SL_TO_STD_ACC; break;
case NM3: lastUnit = (long double)ONE_NCM_TO_STD_ACC; break;
case SCF: lastUnit = (long double)ONE_SCF_TO_STD_ACC; break;
case KG: lastUnit = (long double)ONE_KG_TO_STD_ACC; break;
default: lastUnit = 1;
}
// to User FlowUnit
switch(AccComputeUnit)
{
//case ML: totalFlowRateCompute = (long double)ONE_STD_ACC_TO_ML; break;
case SL: totalFlowRateCompute = (long double)ONE_STD_ACC_TO_SL; break;
case NM3: totalFlowRateCompute = (long double)ONE_STD_ACC_TO_NCM; break;
case SCF: totalFlowRateCompute = (long double)ONE_STD_ACC_TO_SCF; break;
case KG: totalFlowRateCompute = (long double)ONE_STD_ACC_TO_KG; break;
default: totalFlowRateCompute = 1;
}
ReleaseTotalToDisArray();
ReleaseToIntAndDecimalBuf();
totalFlowRateCompute *= (long double)flowTotalBuffer;
totalFlowRateCompute *= lastUnit;
flowTotalBuffer = (unsigned long long int)totalFlowRateCompute;
// convert HEX TO BCD
tmpQWordA = flowTotalBuffer;
tmpQWordA /= 1000000000;
tmpLA = (u32)tmpQWordA;
tmpQWordA *= 1000000000;
flowTotalBuffer -= tmpQWordA;
tmpLB = (u32)flowTotalBuffer;
ConvertHEXToBCDArray(tmpLB, &Dis2[5], 9, HIGH_FIRST);
ConvertHEXToBCDArray(tmpLA, &Dis2[0], 5, HIGH_FIRST);
SendToTotalArray();
ReleaseToIntAndDecimalBufForUart();
for(I=0; I <= REC_ACC_DEPTH; I++) FRAMWriteTotal();
lastRemaining = 0;
flowAccCumulationRemaining = 0;
}
//===========================================================================
tempL.Word[0] = NewFlowUnit;
flowUnit = WriteShortParameterToMemory(FLOW_UNIT);
}
//******************************************************************************
u16 ReturnStartUnitBit(u16 stdBit)
{
switch(AccComputeUnit)
{
case SL:
if(AccComUnit == ML) stdBit += 3;
break;
case NM3:
if(AccComUnit == SL) stdBit += 3;
break;
}
return stdBit;
}
//******************************************************************************
// V2004
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>λΪSLPM<50><4D>NCMHʱ<48><CAB1><EFBFBD><EFBFBD>С<EFBFBD><D0A1>λ 1mL
// Flow Unit = SLPM: (((Flow*60)/1000)*1000)/TimeBase
// Flow Unit = NCMH: (Flow*1000)/TimeBase
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ŵ<EFBFBD><C5B4><EFBFBD><EFBFBD><EFBFBD>
// FRUnitForHour[] ={28800, 14400, 7200, 3600, 1800, 900}
// FRUnitForMinute[] ={480, 240, 120, 60, 30, 15}
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>0xFFFFFFF/(1000/15):65000000
u16 ConvertTimeBaseAndUnit(u16 unit, u16 timeBaseIndex)
{
switch(unit)
{
case SCCM: // ACC->SLPM
case SLPM: // ACC->SLPM
AccComputeTimeBase = (u32)FRUnitForMinute[timeBaseIndex];
return 1;
case SCFM:
AccComputeTimeBase = (u32)FRUnitForMinute[timeBaseIndex];
break;
default: AccComputeTimeBase = (u32)FRUnitForHour[timeBaseIndex];
break;
}
return 1000;
}
//******************************************************************************
void UnitConverterInit(void)
{
//ReadMultiByteFromEEPROM(CALIB_DIAMETER, tempL.Byte, CALIB_DIAMETER_WIDTH, PARA_EEPROM);
//if(tempL.Byte[CALIB_DIAMETER_CRC] != CRC8(tempL.Byte, CALIB_DIAMETER_CRC)) flowUnit = CALIB_DIAMETER_DEFAULT;
//else flowUnit = tempL.Word[0];
flowUnit = (u16)ReadShortParameterFromMemory(FLOW_UNIT, FLOW_UNIT_DEFAULT);
if(JudgeflowUnit(flowUnit))
{
flowUnit = FLOW_UNIT_DEFAULT;
JudgeflowUnit(flowUnit);
}
}