Attribute VB_Name = "modControls"
Option Explicit

' From X11Color.h : header file
Public Const colorAmptekBlue = &HFCCE7A
Public Const colorBlue = &HFF0000
Public Const colorGreen = &H8000&
Public Const colorLightGray = &HD3D3D3
Public Const colorLightSlateGray = &H998877
Public Const colorLightSteelBlue = &HDEC4B0
Public Const colorRed = &HFF&
Public Const colorWhite = &HFFFFFF
Public Const colorYellow = &HFFFF&
Public Const colorSilver = &HC0C0C0

Public HwCfgDP5 As String
Public cstrRawCfgIn As String
Public HwCfgReady As Boolean
Public HwCfgExReady As Boolean
Public Dp5CmdList As New Collection
Public HwCfgDP5Out As String
Public HwRdBkDP5Out As String

Public SCAEnabled As Boolean
Public HwScaCfgDP5 As String
Public cstrRawScaCfgIn As String
Public HwScaCfgReady As Boolean

' Shared Communications Interface Settings
Public CurrentInterface As Byte         ' selected communications interface (0=RS232/1=USB/2=ETHERNET)
Public isDppConnected As Boolean        ' CurrentInterface selected and device found
Public ComPort As Long                  ' Serial Port Settings

' Ethernet Settings
Public NetfinderActive As Boolean
Public SockAddr As Long
Public cstrSockAddr As String
Public InetPort As Long



Public Function CreateFullReadBackCmd()
    Dim cstrCfg As String
    Dim isHVSE As Boolean
    Dim isPAPS As Boolean
    Dim isTECS As Boolean
    Dim isVOLU As Boolean
    Dim isCON1 As Boolean
    Dim isCON2 As Boolean
    Dim isINOF As Boolean
    Dim isBoot As Boolean

    isHVSE = CBool(((STATUS.DEVICE_ID <> dppPX5) And STATUS.PC5_PRESENT) Or STATUS.DEVICE_ID = dppPX5)
    isPAPS = CBool(STATUS.DEVICE_ID <> dppDP5G)
    isTECS = CBool(((STATUS.DEVICE_ID = dppDP5) And STATUS.PC5_PRESENT) Or (STATUS.DEVICE_ID <> dppDP5G))
    isVOLU = CBool(STATUS.DEVICE_ID = dppPX5)
    isCON1 = CBool(STATUS.DEVICE_ID <> dppDP5)
    isCON2 = CBool(STATUS.DEVICE_ID <> dppDP5)
    isINOF = CBool(STATUS.DEVICE_ID <> dppDP5G)
    isBoot = CBool(STATUS.DEVICE_ID = dppDP5)

    cstrCfg = ""
    cstrCfg = cstrCfg + "RESC=?;"
    cstrCfg = cstrCfg + "CLCK=?;"
    cstrCfg = cstrCfg + "TPEA=?;"
    cstrCfg = cstrCfg + "GAIF=?;"
    cstrCfg = cstrCfg + "GAIN=?;"
    cstrCfg = cstrCfg + "RESL=?;"
    cstrCfg = cstrCfg + "TFLA=?;"
    cstrCfg = cstrCfg + "TPFA=?;"
    cstrCfg = cstrCfg + "PURE=?;"
    cstrCfg = cstrCfg + "RTDE=?;"
    cstrCfg = cstrCfg + "MCAS=?;"
    cstrCfg = cstrCfg + "MCAC=?;"
    cstrCfg = cstrCfg + "SOFF=?;"
    cstrCfg = cstrCfg + "AINP=?;"
    If (isINOF) Then cstrCfg = cstrCfg + "INOF=?;"
    cstrCfg = cstrCfg + "GAIA=?;"
    cstrCfg = cstrCfg + "CUSP=?;"
    cstrCfg = cstrCfg + "PDMD=?;"
    cstrCfg = cstrCfg + "THSL=?;"
    cstrCfg = cstrCfg + "TLLD=?;"
    cstrCfg = cstrCfg + "THFA=?;"
    cstrCfg = cstrCfg + "DACO=?;"
    cstrCfg = cstrCfg + "DACF=?;"
    cstrCfg = cstrCfg + "RTDS=?;"
    cstrCfg = cstrCfg + "RTDT=?;"
    cstrCfg = cstrCfg + "BLRM=?;"
    cstrCfg = cstrCfg + "BLRD=?;"
    cstrCfg = cstrCfg + "BLRU=?;"
    cstrCfg = cstrCfg + "GATE=?;"
    cstrCfg = cstrCfg + "AUO1=?;"
    cstrCfg = cstrCfg + "PRET=?;"
    cstrCfg = cstrCfg + "PRER=?;"
    cstrCfg = cstrCfg + "PREC=?;"
    cstrCfg = cstrCfg + "PRCL=?;"
    cstrCfg = cstrCfg + "PRCH=?;"
    If (isHVSE) Then cstrCfg = cstrCfg + "HVSE=?;"
    If (isTECS) Then cstrCfg = cstrCfg + "TECS=?;"
    If (isPAPS) Then cstrCfg = cstrCfg + "PAPS=?;"
    cstrCfg = cstrCfg + "SCOE=?;"
    cstrCfg = cstrCfg + "SCOT=?;"
    cstrCfg = cstrCfg + "SCOG=?;"
    cstrCfg = cstrCfg + "MCSL=?;"
    cstrCfg = cstrCfg + "MCSH=?;"
    cstrCfg = cstrCfg + "MCST=?;"
    cstrCfg = cstrCfg + "AUO2=?;"
    cstrCfg = cstrCfg + "TPMO=?;"
    cstrCfg = cstrCfg + "GPED=?;"
    cstrCfg = cstrCfg + "GPIN=?;"
    cstrCfg = cstrCfg + "GPME=?;"
    cstrCfg = cstrCfg + "GPGA=?;"
    cstrCfg = cstrCfg + "GPMC=?;"
    cstrCfg = cstrCfg + "MCAE=?;"
    If (isVOLU) Then cstrCfg = cstrCfg + "VOLU=?;"
    If (isCON1) Then cstrCfg = cstrCfg + "CON1=?;"
    If (isCON2) Then cstrCfg = cstrCfg + "CON2=?;"
    If (isBoot) Then cstrCfg = cstrCfg + "BOOT=?;"
    CreateFullReadBackCmd = cstrCfg
End Function

Public Function RemoveCmd(cstrCmd As String, cstrCfgData As String) As String
    Dim iStart, iEnd, iCmd As Long
    Dim cstrNew As String
    Dim strLeft As String
    Dim strRight As String

    cstrNew = ""
    RemoveCmd = cstrCfgData
    If (Len(cstrCfgData) < 7) Then Exit Function    ' no data
    If (Len(cstrCmd) <> 4) Then Exit Function       ' bad command
    iCmd = InStr(1, cstrCfgData, cstrCmd)
    If (iCmd = 0) Then Exit Function                     ' cmd not found
    iStart = iCmd
    iEnd = InStr(iCmd, cstrCfgData, ";")
    If (iEnd = 0) Then Exit Function
    If (iEnd <= iStart) Then Exit Function                 ' unknown error
    strLeft = Left(cstrCfgData, iStart - 1)
    strRight = Mid(cstrCfgData, iEnd + 1)
    cstrNew = strLeft & strRight
    RemoveCmd = cstrNew
End Function

'removes selected command
Public Function RemoveCmdByDeviceType(DeviceType As Byte, cstrCfgDataIn As String) As String
    Dim cstrCfgData As String
    Dim isHVSE As Boolean
    Dim isPAPS As Boolean
    Dim isTECS As Boolean
    Dim isVOLU As Boolean
    Dim isCON1 As Boolean
    Dim isCON2 As Boolean
    Dim isINOF As Boolean
    Dim isBoot As Boolean

    cstrCfgData = cstrCfgDataIn
    RemoveCmdByDeviceType = cstrCfgData

    isHVSE = CBool(((STATUS.DEVICE_ID <> dppPX5) And STATUS.PC5_PRESENT) Or STATUS.DEVICE_ID = dppPX5)
    isPAPS = CBool(STATUS.DEVICE_ID <> dppDP5G)
    isTECS = CBool(((STATUS.DEVICE_ID = dppDP5) And STATUS.PC5_PRESENT) Or (STATUS.DEVICE_ID <> dppDP5G))
    isVOLU = CBool(STATUS.DEVICE_ID = dppPX5)
    isCON1 = CBool(STATUS.DEVICE_ID <> dppDP5)
    isCON2 = CBool(STATUS.DEVICE_ID <> dppDP5)
    isINOF = CBool(STATUS.DEVICE_ID <> dppDP5G)
    isBoot = CBool(STATUS.DEVICE_ID = dppDP5)

    If (Not isHVSE) Then cstrCfgData = RemoveCmd("HVSE", cstrCfgData)   'High Voltage Bias
    If (Not isPAPS) Then cstrCfgData = RemoveCmd("PAPS", cstrCfgData)   'Preamp Voltage
    If (Not isTECS) Then cstrCfgData = RemoveCmd("TECS", cstrCfgData)   'Cooler Temperature
    If (Not isVOLU) Then cstrCfgData = RemoveCmd("VOLU", cstrCfgData)   'px5 speaker
    If (Not isCON1) Then cstrCfgData = RemoveCmd("CON1", cstrCfgData)   'connector 1
    If (Not isCON2) Then cstrCfgData = RemoveCmd("CON2", cstrCfgData)   'connector 2
    If (Not isINOF) Then cstrCfgData = RemoveCmd("INOF", cstrCfgData)   'input offset
    If (Not isBoot) Then cstrCfgData = RemoveCmd("BOOT", cstrCfgData)   'PC5 On At StartUp

    RemoveCmdByDeviceType = cstrCfgData
End Function

