Attribute VB_Name = "modFileIO"
Option Explicit

Public DppStatusMCA As String
Public DppConfigMCA As String

'This function will check to make sure that a file exists. It will
'return True if the file was found and False if it was not found.
'Example: If Not FileExists("autoexec.bat") Then...
Function FileExists(ByVal sFilename As String) As Boolean
    Dim I As Integer
    On Error Resume Next
    I = Len(Dir(sFilename))
    If Err Or I = 0 Or Trim(sFilename) = "" Then
        FileExists = False
    Else
        FileExists = True
    End If
End Function

Public Function GetCfgFile(ceFile As File, strFilename As String) As Variant
    Dim strFileArr() As String
    Dim iSize As Integer
    Dim TextLine As String
    Dim lRes As Long
    Dim I As Long

    iSize = 0
    ceFile.Open strFilename, fsModeInput, fsAccessRead  ' Open the list file.
    Do While Not ceFile.EOF                                 ' Add the contents.
        ReDim Preserve strFileArr(iSize)
        strFileArr(iSize) = ""
        TextLine = ceFile.LineInputString
        strFileArr(iSize) = TextLine
        iSize = iSize + 1
    Loop
    ceFile.Close
    GetCfgFile = strFileArr
End Function

Public Function Inc(ByRef varIdx As Variant) As Variant
    varIdx = varIdx + 1
    Inc = varIdx
End Function

Public Sub RecallConfig(varCfg As Variant)
    Dim varIdx As Long
    Dim lRiseCfg As Long
    
    AudibleCounter = 0      ' audio volume setting always set to mute in ppc demo
    MCSTimebase = 0         ' 10mS, mcs not enabled in ppc demo
    AcqMode = 0             ' acquisition mode 0=MCA,1=MCS
    MCSEnable = 1           ' MCS Enable (Always ON, Gate Signal-if low prevents counts to current mcs channel)
    
    varIdx = -1
'    varCfg (varIdx)
'    On Error GoTo 0             ' clear error handling
    Call Inc(varIdx)
    lRiseCfg = CLng(Val(varCfg(varIdx)))
    'MsgBox lRiseCfg
    SetRiseAndAvg lRiseCfg
    'MsgBox Rise
    'MsgBox Avg
    Call Inc(varIdx)
    FlatTop = CInt(Val(varCfg(varIdx)))
    Call Inc(varIdx)
    SlowChThreshold = CByte(Val(varCfg(varIdx)))
    Call Inc(varIdx)
    FastChThreshold = CByte(Val(varCfg(varIdx)))
    Call Inc(varIdx)
    OutputOffset = Val(varCfg(varIdx))
    Call Inc(varIdx)
    Select Case varCfg(varIdx)
        Case "PUROn"
            PUREnable = True
        Case "PUROff"
            PUREnable = False
    End Select
    Call Inc(varIdx)
    DetReset = CByte(Val(varCfg(varIdx)))
    Call Inc(varIdx)
    Select Case varCfg(varIdx)
        Case "DAC1"
            DAC = 0
            DAC_Off = False
        Case "DAC2"
            DAC = 1
            DAC_Off = False
        Case "DAC3"
            DAC = 2
            DAC_Off = False
        Case "DAC4"
            DAC = 3
            DAC_Off = False
        Case "DAC5"
            If ((DAC > 3) Or (DAC < 0)) Then DAC = 2
            DAC_Off = True
    End Select
    Call Inc(varIdx)
    Select Case varCfg(varIdx)
        Case "MCA256"
            MCA = 4
            MCAEnable = True
        Case "MCA512"
            MCA = 3
            MCAEnable = True
        Case "MCA1024"
            MCA = 2
            MCAEnable = True
        Case "MCA2048"
            MCA = 1
            MCAEnable = True
        Case "MCA4096"
            MCA = 0
            MCAEnable = True
    End Select
    'MCAChannels is the cpp and dppapi name
    MCAChannels = MCA           ' number of channels 4=256,3=512,2=1024,1=2048,0=4096,5=8192

    Call Inc(varIdx)
    Select Case varCfg(varIdx)
        Case "COM1", "COM2", "COM3", "COM4", "COM5", "COM6", "COM7", "COM8", "COM9"
            Port = CInt(Val(Mid(varCfg(varIdx), 4, 1)))
        Case Else
            Port = 1
    End Select
    Call Inc(varIdx)
    Select Case varCfg(varIdx)
        Case "RTDOn"
            RTDOn = True
        Case "RTDOff"
            RTDOn = False
    End Select
    Call Inc(varIdx)
    RTDSlow = CByte(Val(varCfg(varIdx)))
    Call Inc(varIdx)
    RTDFast = CByte(Val(varCfg(varIdx)))
    Call Inc(varIdx)
    CoarseGain = CInt(Val(varCfg(varIdx)))
    Call Inc(varIdx)
    If varCfg(varIdx) = "AutoBaselineOn" Then
        BaselineOn = True
    ElseIf varCfg(varIdx) = "AutoBaselineOff" Then
        BaselineOn = False
    End If
    Call Inc(varIdx)
    BLR = CByte(Val(varCfg(varIdx)))
    Call Inc(varIdx)
    If varCfg(varIdx) = "BufferSelectA" Then
        BufferSelect = BufferSelectA
    ElseIf varCfg(varIdx) = "BufferSelectB" Then
        BufferSelect = BufferSelectB
    ElseIf varCfg(varIdx) = "BufferSelectHW" Then
        BufferSelect = BufferSelectHW
    End If
    BufferSelect = BufferSelectA        'BufferSelect = A for ppc demo
    Call Inc(varIdx)
    If varCfg(varIdx) = "GateOff" Then
        Gate = GateOff
    ElseIf varCfg(varIdx) = "GateHigh" Then
        Gate = GateHigh
    ElseIf varCfg(varIdx) = "GateLow" Then
        Gate = GateLow
    End If
    Call Inc(varIdx)
    SetPresetTimeFromStr varCfg(varIdx)
    Call Inc(varIdx)
    FineGain = CInt(Val(varCfg(varIdx)))
    Call Inc(varIdx)
    TEC_On = CBool(Val(varCfg(varIdx)))
    Call Inc(varIdx)
    Pwr5VA_On = CBool(Val(varCfg(varIdx)))
    Call Inc(varIdx)
    Pwr8_5SEL_On = CBool(Val(varCfg(varIdx)))
    Call Inc(varIdx)
    XR100_On = CBool(Val(varCfg(varIdx)))
    Call Inc(varIdx)
    Pwr9V_On = CBool(Val(varCfg(varIdx)))
    Call Inc(varIdx)
    InvertCG = CBool(Val(varCfg(varIdx)))
    Call Inc(varIdx)
    InputOffset = CLng(Val(varCfg(varIdx)))
    Call Inc(varIdx)
    PoleZero = CInt(Val(varCfg(varIdx)))
    Call Inc(varIdx)
    AuxOut = CByte(Val(varCfg(varIdx)))
    Call Inc(varIdx)
    HV = CInt(Val(varCfg(varIdx)))
    Call Inc(varIdx)
    TEC = CInt(Val(varCfg(varIdx)))
End Sub

'''Private Function TestDppFirmware(dblNewFirmware As Double) As Boolean
'''    Dim bFirmwareOK As Boolean
'''    bFirmwareOK = False
'''    If (DppFirmwareRev >= dblNewFirmware) Then
'''        bFirmwareOK = True
'''    End If
'''    TestDppFirmware = bFirmwareOK
'''End Function

''Private Sub EnableDppControls(Optional ForcePX4OnlyOn As Boolean = False)
''    Dim bPX4 As Boolean
''    Dim bHasMCS As Boolean
''    Dim bHasTuneFastThresh As Boolean
''
''    bPX4 = CBool(DppDevice)
''    If (ForcePX4OnlyOn) Then bPX4 = True
''
''    '=== MCA Controls ===
''    bHasMCS = TestDppFirmware(3.11)    ' fw >= v3.11(2nd vers) has mcs
''    If Not ForcePX4OnlyOn Then
''        cboAcqMode.Enabled = bHasMCS
''        lblcboAcqMode.Enabled = bHasMCS
''        cboMCSTimebase.Enabled = False      'this is done because the controls are not initialized yet
''        lblMCSTimebase.Enabled = False
''    Else
''        cboAcqMode.Enabled = True
''        lblcboAcqMode.Enabled = True
''        cboMCSTimebase.Enabled = True      'this is done because the controls are not initialized yet
''        lblMCSTimebase.Enabled = True
''    End If
''
''    '=== Shaping Controls ===
''    bHasTuneFastThresh = TestDppFirmware(3.11) ' fw >= v3.11 has fast thresh tune
''    'GetDlgItem(IDC_TUNEFASTTHRESHOLD)->EnableWindow(bHasTuneFastThresh)    'no vb control yet
''
''    '=== Gain & Pole Zero Controls ===
''    'GetDlgItem(IDC_NORMALAUTOINPUTOFFSET)->EnableWindow(bPX4)  'don't test for these (2) yet
''    'GetDlgItem(IDC_FINEAUTOINPUTOFFSET)->EnableWindow(bPX4)
''    txtPZ.Enabled = bPX4
''    udPZ.Enabled = bPX4
''    lblPZValue.Enabled = bPX4
''    txtInputOffset.Enabled = bPX4
''    udInputOffset.Enabled = bPX4
''    lblInputOffset.Enabled = bPX4
''    lblInputOffsetFlag.Enabled = bPX4
''    cboInputPolarity.Enabled = bPX4
''    lblcboInvertEnable.Enabled = bPX4
''
''    '=== Power Controls ===
''    txtHV.Enabled = bPX4
''    udHV.Enabled = bPX4
''    fraHighVoltageBias.Enabled = bPX4
''    lblHVFlag.Enabled = bPX4
''    cboHVSelect.Enabled = bPX4
''    cboPreampVoltage.Enabled = bPX4
''    lblcboPreampVoltage.Enabled = bPX4
''    txtTEC.Enabled = bPX4
''    udTEC.Enabled = bPX4
''    fraTemperature.Enabled = bPX4
''    lblSetPoint.Enabled = bPX4
''    lblTECFlag.Enabled = bPX4
''
''    '=== Miscellaneous Controls ===
''    cboAux.Enabled = bPX4
''    lblAux.Enabled = bPX4
''    cboAudibleCounterSettings.Enabled = bPX4
''    lblcboAudibleCounterSettings.Enabled = bPX4
''
''    cmdEnablePX4OnlyControls.Visible = Not bPX4
''    cmdEnablePX4OnlyControls.Enabled = Not bPX4
''End Sub
''
''


