modSCA Source Code

Option Explicit 

Public Const SCA_SECTION = "DP5 SCA Configuration" 
Public Const CFG_SECTION = "DP5 Configuration File" 

Public Type scaRegister 
    Index As Variant 
    Low As Variant 
    High As Variant 
    OutPut As Variant 
End Type 

Public Type scaSetup 
    strIniFilename As String 
    Channels As Variant 
    HaveChannels As Boolean 
    Index As Variant 
    HaveIndex As Boolean 
    PulseWidth As Variant 
    sca(15) As scaRegister 
End Type 

Public sca As scaSetup 

Public Function SplitData(strData As StringByRef varValue As Variant, strDefault As VariantAs Boolean 
    Dim lSep As Long 
    Dim strChan As String 
    lSep = InStr(strData, ";")
    If (lSep > 0) Then      'file has setting, separate comments from data, if any
        varValue = Trim(Left(strData, lSep - 1))
        SplitData = True 
    Else 
        varValue = strDefault 
        SplitData = False 
    End If 
End Function 

Public Function varValueToIndex(varData As VariantAs Long 
    Dim strCase As String 
    strCase = UCase(CStr(varData))
    Select Case strCase 
        Case "OFF" 
            varValueToIndex = 0 
        Case "HI" 
            varValueToIndex = 1 
        Case "LOW" 
            varValueToIndex = 2 
        Case "256" 
            varValueToIndex = 0 
        Case "512" 
            varValueToIndex = 1 
        Case "1024" 
            varValueToIndex = 2 
        Case "2048" 
            varValueToIndex = 3 
        Case "4096" 
            varValueToIndex = 4 
        Case "8192" 
            varValueToIndex = 5 
        Case "100" 
            varValueToIndex = 0 
        Case "1000" 
            varValueToIndex = 1 
        Case "1" 
            varValueToIndex = 0 
        Case "2" 
            varValueToIndex = 1 
        Case "3" 
            varValueToIndex = 2 
        Case "4" 
            varValueToIndex = 3 
        Case "5" 
            varValueToIndex = 4 
        Case "6" 
            varValueToIndex = 5 
        Case "7" 
            varValueToIndex = 6 
        Case "8" 
            varValueToIndex = 7 
        Case "HIGH" 
            varValueToIndex = 1 
        Case "HIG" 
            varValueToIndex = 1 
        Case "LO" 
            varValueToIndex = 2 
        Case Else 
            varValueToIndex = 0 
    End Select 
End Function 

'these must be in the correct order, old sca settings will be deleted and new appended to end
Public Sub SaveSCASetupINI(sca As scaSetup)
    Dim strData As String 
    Dim Index As Long 

    If (Not sca.HaveChannels) Then 
        strData = CStr(sca.Channels) & ";" 
        SaveToIniEx sca.strIniFilename, CFG_SECTION, "MCAC", strData 
    End If 

    Index = Val(sca.Index)

    Call DeleteIniSettingEx(sca.strIniFilename, CFG_SECTION, "SCAI")
    Call DeleteIniSettingEx(sca.strIniFilename, CFG_SECTION, "SCAO")
    Call DeleteIniSettingEx(sca.strIniFilename, CFG_SECTION, "SCAL")
    Call DeleteIniSettingEx(sca.strIniFilename, CFG_SECTION, "SCAH")
    Call DeleteIniSettingEx(sca.strIniFilename, CFG_SECTION, "SCAW")

    strData = CStr(sca.PulseWidth) & ";" 
    SaveToIniEx sca.strIniFilename, CFG_SECTION, "SCAW", strData 
End Sub 

Public Sub SaveSCASettingsINI(sca As scaSetup, Index As Long)
    Dim strIdx As String 
    Dim strData As String 
    strIdx = CStr(Index)

    strData = CStr(sca.sca(Index - 1).OutPut) & ";" 
    SaveToIniEx sca.strIniFilename, SCA_SECTION, "SCAO" & strIdx, strData 

    strData = CStr(sca.sca(Index - 1).Low) & ";" 
    SaveToIniEx sca.strIniFilename, SCA_SECTION, "SCAL" & strIdx, strData 

    strData = CStr(sca.sca(Index - 1).High) & ";" 
    SaveToIniEx sca.strIniFilename, SCA_SECTION, "SCAH" & strIdx, strData 
End Sub 

Public Sub GetSCASetupINI(sca As scaSetup)
    Dim lSep As Long 
    Dim strValue As String 
    Dim strComment As String 
    Dim strData As String 
    Dim Index As Long 

    strData = GetFromIniEx(sca.strIniFilename, CFG_SECTION, "MCAC", "1024")
    sca.HaveChannels = SplitData(strData, sca.Channels, "1024")

    strData = GetFromIniEx(sca.strIniFilename, CFG_SECTION, "SCAW", "100")
    Call SplitData(strData, sca.PulseWidth, "100")

    strData = GetFromIniEx(sca.strIniFilename, CFG_SECTION, "SCAI", "1")
    sca.HaveIndex = SplitData(strData, sca.Index, "1")

    If (sca.HaveIndex) Then     'update sca from latest config data
        Index = Val(sca.Index)

        strData = GetFromIniEx(sca.strIniFilename, CFG_SECTION, "SCAO", strData)
        Call SplitData(strData, sca.sca(Index - 1).OutPut, "OFF")

        strData = GetFromIniEx(sca.strIniFilename, CFG_SECTION, "SCAL", strData)
        Call SplitData(strData, sca.sca(Index - 1).Low, "1")

        strData = GetFromIniEx(sca.strIniFilename, CFG_SECTION, "SCAH", strData)
        Call SplitData(strData, sca.sca(Index - 1).High, "1024")
    End If 
End Sub 

Public Sub GetSCASettingsINI(sca As scaSetup, Index As Long)
    Dim lSep As Long 
    Dim strValue As String 
    Dim strComment As String 
    Dim strIdx As String 
    Dim strData As String 

    strIdx = CStr(Index)

    sca.sca(Index - 1).Index = strIdx       'for future use

    'skip updating data that was updated from the configuration settings
    If (sca.HaveIndex And (Val(sca.Index) = Index)) Then Exit Sub 

    strData = GetFromIniEx(sca.strIniFilename, SCA_SECTION, "SCAO" & strIdx, strData)
    Call SplitData(strData, sca.sca(Index - 1).OutPut, "OFF")

    strData = GetFromIniEx(sca.strIniFilename, SCA_SECTION, "SCAL" & strIdx, strData)
    Call SplitData(strData, sca.sca(Index - 1).Low, "1")

    strData = GetFromIniEx(sca.strIniFilename, SCA_SECTION, "SCAH" & strIdx, strData)
    Call SplitData(strData, sca.sca(Index - 1).High, "1024")
End Sub 

'                                       1234567
'expecting data format strCMD=strData; (CMD1=?;)
Public Sub ReadSCASetting(ByRef sca As scaSetup, strSCAInfo As String)
    Dim strCmd As String 
    Dim strData As String 
    Dim lSep As Long 
    Dim lTerm As Long 

    lSep = InStr(strSCAInfo, "=")
    lTerm = InStr(strSCAInfo, ";")
    If (lSep <> 5) Then Exit Sub 
    If (lTerm < 7) Then Exit Sub 
    strCmd = UCase(Left(strSCAInfo, 4))
    strData = UCase(Mid(strSCAInfo, 6, lTerm - 6))
    Select Case strCmd 
        Case "MCAC" 
            sca.Channels = strData 
        Case "SCAW" 
            sca.PulseWidth = strData 
        Case "SCAI" 
            sca.Index = strData 
            sca.HaveIndex = True 
            sca.sca(Val(sca.Index) - 1).Index = strData 
        Case "SCAL" 
            If (Val(sca.Index) > 0) Then sca.sca(Val(sca.Index) - 1).Low = strData 
        Case "SCAH" 
            If (Val(sca.Index) > 0) Then sca.sca(Val(sca.Index) - 1).High = strData 
        Case "SCAO" 
            If (Val(sca.Index) > 0) Then sca.sca(Val(sca.Index) - 1).OutPut = strData 
        Case Else 
            'unknown data error
    End Select 
End Sub 

'reads dpp command string removes and returns byref next dpp config command
'functions returns true if command found, false otherwise
Public Function GetNextCMD(ByRef strCMDList As StringByRef strCmd As StringAs Boolean 
    Dim strCMDs As String 
    Dim strCMDOut As String 
    Dim lNextCMD As Long 

    GetNextCMD = False 
    If (Len(strCMDList) < 7) Then Exit Function     '1234=6;8
    lNextCMD = InStr(strCMDList, ";")
    If (lNextCMD < 7) Then Exit Function            '1234=6;8
    strCmd = UCase(Left(strCMDList, lNextCMD))
    strCMDList = Mid(strCMDList, lNextCMD + 1)
    GetNextCMD = True 
End Function 

Public Sub SCACfgParser(ByRef sca As scaSetup, strSCASettings As String)
    Dim strCMDList As String 
    Dim strCmd As String 
    Dim idxCMDs As Long 
    Dim CMDMax As Long 
    Dim bHaveCMD As Boolean 

    If (Len(strSCASettings) < 7) Then Exit Sub 
    strCMDList = strSCASettings 
    strCmd = "" 
    CMDMax = CLng(Len(strSCASettings) \ 7) + 1     'loop the max number of times
    For idxCMDs = 0 To CMDMax 
        bHaveCMD = GetNextCMD(strCMDList, strCmd)
        If (bHaveCMD) Then 
            ReadSCASetting sca, strCmd 
        Else 
            Exit For 
        End If 
    Next 
End Sub 

Public Function SCAString(strCmd As String, strData As StringOptional bDppMsg As Boolean)
    Dim str As String 
    str = UCase(strCmd)
    str = str & "=" 
    str = str & UCase(strData) & ";" 
    If (Not bDppMsg) Then 
        str = str & vbNewLine 
    End If 
    SCAString = str 
End Function 

'these must be in the correct order, old sca settings will be delected and new appended to end
Public Function SCASetupString(sca As scaSetup, Optional bDppMsg As Boolean = True)
    Dim strData As String 
    Dim Index As Long 
    Dim strSCAsus As String 
    Dim str As String 

    strSCAsus = "" 
    If (Not sca.HaveChannels) Then 
        str = SCAString("MCAC", CStr(sca.Channels), bDppMsg)
        strSCAsus = strSCAsus & str 
    End If 

    strData = CStr(sca.PulseWidth)
    str = SCAString("SCAW", strData, bDppMsg)
    strSCAsus = strSCAsus & str 

    SCASetupString = strSCAsus 
End Function 

Public Function SCASettingsString(sca As scaSetup, Index As LongOptional bDppMsg As Boolean = True)
    Dim strIdx As String 
    Dim strData As String 
    Dim strSCAsets As String 
    Dim str As String 

    strIdx = CStr(Index)

    strData = CStr(sca.sca(Index - 1).Index)
    str = SCAString("SCAI", strData, bDppMsg)
    strSCAsets = strSCAsets & str 

    strData = CStr(sca.sca(Index - 1).OutPut)
    str = SCAString("SCAO", strData, bDppMsg)
    strSCAsets = strSCAsets & str 

    strData = CStr(sca.sca(Index - 1).Low)
    str = SCAString("SCAL", strData, bDppMsg)
    strSCAsets = strSCAsets & str 

    strData = CStr(sca.sca(Index - 1).High)
    str = SCAString("SCAH", strData, bDppMsg)
    strSCAsets = strSCAsets & str 

    SCASettingsString = strSCAsets 
End Function 

Public Function SCAStringALL(sca As scaSetup, Optional bDppMsg As Boolean = True)
    Dim idxSCA As Long 
    Dim strSCA As String 

    strSCA = SCASetupString(sca, bDppMsg)
    For idxSCA = 1 To 8 
        strSCA = strSCA & SCASettingsString(sca, idxSCA, bDppMsg)
    Next 
    SCAStringALL = strSCA 
End Function 

Public Function SCAStringALLDisplay(sca As scaSetup)
    Dim idxSCA As Long 
    Dim strSCA As String 
    Dim bDppMsg As Boolean 

    bDppMsg = True 
    strSCA = SCASetupString(sca, bDppMsg)
    For idxSCA = 1 To 8 
        strSCA = strSCA & vbNewLine 
        strSCA = strSCA & SCASettingsString(sca, idxSCA, bDppMsg)
    Next 
    SCAStringALLDisplay = strSCA 
End Function 

Public Sub InitSCA()
    Dim idxSCA As Long 
    sca.strIniFilename = "" 
    sca.HaveChannels = False 
    sca.Channels = "1024" 
    sca.Index = 1 
    sca.PulseWidth = "100" 
    For idxSCA = 0 To 15 
        sca.sca(idxSCA).Low = 1 
        sca.sca(idxSCA).High = sca.Channels 
        If (idxSCA < 8) Then 
            sca.sca(idxSCA).OutPut = "OFF" 
        End If 
    Next 
End Sub