VERSION 1.0 CLASS
BEGIN
  MultiUse = -1  'True
END
Attribute VB_Name = "clsSCA_Ctrl"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = True
Attribute VB_PredeclaredId = False
Attribute VB_Exposed = False
Option Explicit

'--------------------------------------------------------------------------------------
'@doc clsSCA_Ctrl
'
'@module clsSCA_Ctrl Class | Single Channel Analyser (SCA) Functions
'
' Copyright (c)2005 Amptek, All Rights Reserved
'
'@devnote Version: v20051214
'
'@head3 clsSCA_Ctrl Members |
'@index | clsSCA_Ctrl
'
'--------------------------------------------------------------------------------------

Private Type SCACHANNEL
    ChEnabled As Integer
    LL As Long
    UL As Long
End Type

Private SCACh(0 To 7) As SCACHANNEL
Private ChIndex As Integer

Private chkEnableChannel As CheckBox
Private txtUpperLevelThreshold As TextBox
Private txtLowerLevelThreshold As TextBox
Private cboSelectSingleChannelAnalyzer As ComboBox

Public Sub SCA_Setup(chkEnableChannelObj As CheckBox, _
                        txtUpperLevelThresholdObj As TextBox, _
                        txtLowerLevelThresholdObj As TextBox, _
                        cboSelectSingleChannelAnalyzerObj As ComboBox)

    Dim i As Integer
    For i = 0 To 7
        SCACh(i).ChEnabled = False
        SCACh(i).LL = 0
        SCACh(i).UL = 0
    Next
    
    Set chkEnableChannel = chkEnableChannelObj
    Set txtUpperLevelThreshold = txtUpperLevelThresholdObj
    Set txtLowerLevelThreshold = txtLowerLevelThresholdObj
    Set cboSelectSingleChannelAnalyzer = cboSelectSingleChannelAnalyzerObj
    
    cboSelectSingleChannelAnalyzer.ListIndex = 0
    ChIndex = 0
    Dim strText As String
    chkEnableChannel.value = IntBool(CBool(SCACh(ChIndex).ChEnabled))
    txtLowerLevelThreshold = SCACh(ChIndex).LL
    txtUpperLevelThreshold = SCACh(ChIndex).UL
End Sub

Private Sub UpdateSCACtrls()
    chkEnableChannel.value = IntBool(CBool(SCACh(ChIndex).ChEnabled))
    txtLowerLevelThreshold.Text = Trim(Str(SCACh(ChIndex).LL))
    txtUpperLevelThreshold.Text = Trim(Str(SCACh(ChIndex).UL))
End Sub

Public Function GetEnable(Index As Integer) As Integer
    GetEnable = SCACh(Index).ChEnabled
End Function

Public Function GetLL(Index As Integer) As Long
    GetLL = SCACh(Index).LL
End Function

Public Function GetUL(Index As Integer) As Long
    GetUL = SCACh(Index).UL
End Function

Public Sub SelectSCA(Index As Integer)
    ChIndex = Index
    If (ChIndex < 0) Then ChIndex = 0
    UpdateSCACtrls
End Sub

Public Sub EnableSCA(EnableVal As Integer)
    SCACh(ChIndex).ChEnabled = EnableVal
    UpdateSCACtrls
End Sub

Private Function GetDecDigits(strTextIn As String) As String
    Dim i As Integer
    Dim strText As String
    Dim tmpStr As String
    Dim ch As String
    tmpStr = strTextIn
    strText = ""
    For i = 1 To Len(tmpStr)    '48 to 57
        ch = Mid(tmpStr, i, 1)
        If ((Asc(ch) > 47) And (Asc(ch) < 58)) Then   'append Char
            strText = strText & ch
        End If
    Next
    GetDecDigits = strText
End Function

Private Function GetLevelValFromstr(strLVal As Variant) As Long
    Dim tmpLVal As Long
    Dim tmpStr As String
    Dim newStr As String
    If Not IsNumeric(strLVal) Then
        tmpStr = Trim(strLVal)  'remove outside spaces
        If (Len(tmpStr) = 0) Then   'test if null 'if zero length assign 0
            GetLevelValFromstr = 0
        Else    'remove spaces and nondigits (may be spaces or chars)
            newStr = GetDecDigits(tmpStr)
            If (Len(newStr) = 0) Then   'test if null again 'if zero length assign 0
                GetLevelValFromstr = 0
            Else                        'get value
                 GetLevelValFromstr = newStr
            End If
        End If
    Else
        GetLevelValFromstr = strLVal
    End If
End Function

Public Sub SetSCALL(strLLVal As Variant)
    Dim tmpLLVal As Long
    tmpLLVal = GetLevelValFromstr(strLLVal)
    If (tmpLLVal < 0) Then tmpLLVal = 0
    If (tmpLLVal >= &H10000) Then tmpLLVal = &HFFFF
    SCACh(ChIndex).LL = tmpLLVal
    If ((Val(txtLowerLevelThreshold) <> tmpLLVal) Or (Not IsNumeric(strLLVal))) Then
        UpdateSCACtrls
    End If
End Sub

Public Sub SetSCAUL(strULVal As Variant)
    Dim tmpULVal As Long
    tmpULVal = GetLevelValFromstr(strULVal)
    If (tmpULVal < 0) Then tmpULVal = 0
    If (tmpULVal >= &H8000&) Then tmpULVal = &H7FFF
    SCACh(ChIndex).UL = tmpULVal
    If ((Val(txtUpperLevelThreshold) <> tmpULVal) Or (Not IsNumeric(strULVal))) Then
        UpdateSCACtrls
    End If
End Sub

Private Sub Class_Terminate()
    Set chkEnableChannel = Nothing
    Set txtUpperLevelThreshold = Nothing
    Set txtLowerLevelThreshold = Nothing
    Set cboSelectSingleChannelAnalyzer = Nothing
End Sub

' called by config class to init sca vars
Public Sub SCA4BytesToValsIndex(SCAIndex As Integer, ul4Bytes As Long)
    SCACh(SCAIndex).UL = SCA4BytesToUL(ul4Bytes)
    SCACh(SCAIndex).LL = SCA4BytesToLL(ul4Bytes)
    SCACh(SCAIndex).ChEnabled = SCA4BytesToEN(ul4Bytes)
End Sub

' called by config class to save sca vars
Public Function SCAValsTo4BytesIndex(SCAIndex As Integer) As Long
    Dim ul4Bytes As Long
    ul4Bytes = SCAValsTo4Bytes(SCACh(SCAIndex).ChEnabled, SCACh(SCAIndex).LL, SCACh(SCAIndex).UL)
    SCAValsTo4BytesIndex = ul4Bytes
End Function

Public Function SCAValsTo4Bytes(bChEnabled As Integer, LL As Long, UL As Long) As Long
    Dim ul4Bytes As Long
    Dim ChEnabled As Long
    ChEnabled = IntBool(Not (bChEnabled = 0))                  'value can be 0=false, or 1=true,-1=true
    ul4Bytes = LL             ' lower byte
    ul4Bytes = ul4Bytes + (ShiftLeft(UL, 16) And &H7FFF0000)             ' upper byte (7 bits only)
    ul4Bytes = ul4Bytes + ShiftLeft(ChEnabled, 31)
    SCAValsTo4Bytes = ul4Bytes
End Function

Public Function SCA4BytesToUL(ul4Bytes As Long) As Long
    Dim tmpULVal As Long
    tmpULVal = ShiftRight(ul4Bytes, 16) And &H7FFF&       ' shift to lower 2 bytes, mask En bit
    SCA4BytesToUL = tmpULVal
End Function

Public Function SCA4BytesToLL(ul4Bytes As Long) As Long
    Dim tmpLLVal As Long
    tmpLLVal = ul4Bytes And &HFFFF&
    SCA4BytesToLL = tmpLLVal
End Function

Public Function SCA4BytesToEN(ul4Bytes As Long) As Boolean
    Dim tmpENVal As Boolean
    tmpENVal = ShiftRight(ul4Bytes, 31) And &H1    ' shift En bit to low pos
    SCA4BytesToEN = CBool(tmpENVal)                 'change to bool type
End Function

Public Sub SetSCA8FromPresetCountInfo(PresetCount As Long, LowChannel As Long, HighChannel As Long)
    Dim LL As Long
    Dim UL As Long
    Dim ChEnabled As Integer
    Dim SCAVal As Long

    ChEnabled = IntBool(CBool(PresetCount))
    LL = LowChannel
    UL = HighChannel
    SCAVal = SCAValsTo4Bytes(ChEnabled, LL, UL)
    Call SCA4BytesToValsIndex(7, SCAVal)
    If (ChIndex = 7) Then SelectSCA 7
End Sub


