Attribute VB_Name = "Module1"
'#Uses "..\classes\FunctionClass.cls
'#Uses "..\include\Const.inc"
'#Uses "..\include\Format.inc"
'#Uses "..\include\Global.inc"
'#Uses "..\include\Port.inc"
'#Uses "..\include\Pages.inc"
'#Uses "Include.inc"

Option Explicit
'*****************************************************************************
' Module:
'        ADC
'-----------------------------------------------------------------------------
' Description:
'        This file contains functions which are create controls for the
'        module 'ADC'.
'        Furthermore it contains functions for the event handling of
'        these controls.
'
'-----------------------------------------------------------------------------
' History:
'
'        Rev. Author         Date        Description
'        ---------------------------------------------------------------------
'        1.0    als       03.12.04     initial version for XC866
'
'*****************************************************************************


'*****************************************************************************
'Description:
'        This function is called from DAvE just before the Peripherals
'        window is shown.
'-----------------------------------------------------------------------------
'Parameters:
'        ps              reference to the Peripherals object
'-----------------------------------------------------------------------------
'Returns:
'        True            on successfull operation
'        False           else
'*****************************************************************************
Function OnLoad(ps As Peripherals) As Boolean
    On Error GoTo OnLoadError

    OnLoad = False

    '-------------------------------------------------------------------------
    ' page: General
    '-------------------------------------------------------------------------
    If (Not OnLoadPageChannelGeneral(ps)) Then Exit Function

    '-------------------------------------------------------------------------
    ' page: Notes
    '-------------------------------------------------------------------------
    If (Not OnLoadPageNotes(ps)) Then Exit Function
    
    ' call function to initialize the menu
    If (Not InitMenu(ps, "ADC\Channel")) Then Exit Function

    OnLoad = True
    Exit Function

OnLoadError:
    Exit Function
End Function


'*****************************************************************************
'Description:
'        This function is called from DAvE just before a page
'        is shown.
'-----------------------------------------------------------------------------
'Parameters:
'        ps              reference to the Peripherals object
'        Page            no. of the page (starts with 1)
'        Pagename        name of the page
'-----------------------------------------------------------------------------
'Returns:
'        True            on successfull operation
'        False           else
'*****************************************************************************
Function OnChangingPage(ps As Peripherals, Page As Integer, Pagename As String) As Boolean
    On Error GoTo OnChangingPageError

'    Dim vIndex As Variant
    Dim sVarPre As String
    Dim vNumber As Variant

    vNumber = ps.Parameters.v("Number")
'    vIndex = ps.Parameters.v("x")
    sVarPre = "ADC"

    OnChangingPage = False

    '-------------------------------------------------------------------------
    'page: General
    '-------------------------------------------------------------------------
    If (Not OnChangingPageChannelGeneral(ps, Pagename)) Then Exit Function

    '-------------------------------------------------------------------------
    'page: Notes
    '-------------------------------------------------------------------------
    If (Not OnChangingPageNotes(ps, Pagename, sVarPre & vNumber)) Then Exit Function


    OnChangingPage = True
    Exit Function

OnChangingPageError:
    Exit Function
End Function


'*****************************************************************************
'Description:
'        This function is called from DAvE in two cases:
'
'        1.
'            The contents of the peripherals window must be refreshed.
'            In this case the 'Controlname' parameter is a empty string
'            and the values array 'v()' contains nothing.
'
'        2.
'            A control on the peripherals has been changed.
'            In this case the 'Controlname' parameter contains the name
'            of the changed control and the values array 'v()' contains the
'            changed value(s).
'
'-----------------------------------------------------------------------------
'Parameters:
'        ps              reference to the Peripherals object
'        Controlname     name of the changed control (for case 2)
'        v()             array with the changed values (for case 2)
'        Page            no. of the page (starts with 1)
'        Pagename        name of the page
'-----------------------------------------------------------------------------
'Returns:
'        True            on successfull operation
'        False           else
'*****************************************************************************
Function OnShow(ps As Peripherals, Controlname As String, v() As Variant, Page As Integer, Pagename As String) As Boolean
    On Error GoTo OnShowError

    Dim sVarPre As String
    Dim vNumber As Variant

    vNumber = ps.Parameters.v("Number")
    
    sVarPre = "ADC"

    OnShow = False

    '-------------------------------------------------------------------------
    ' page: General
    '-------------------------------------------------------------------------
    If (Not OnShowPageChannelGeneral(ps, Controlname, Pagename, v)) Then Exit Function

    '-------------------------------------------------------------------------
    ' page: Notes
    '-------------------------------------------------------------------------
    If (Not OnShowPageNotes(ps, Controlname, Pagename, sVarPre & vNumber, v)) Then Exit Function


    OnShow = True
    Exit Function

OnShowError:
    Exit Function
End Function


'*****************************************************************************
'Description:
'        This function is called from DAvE if the user clicks on a button.
'-----------------------------------------------------------------------------
'Parameters:
'        ps              reference to the Peripherals object
'        Controlname     name of the button
'-----------------------------------------------------------------------------
'Returns:
'        True            on successfull operation
'        False           else
'*****************************************************************************
Function OnClick(ps As Peripherals, Controlname As String) As Boolean
    On Error GoTo OnClickError

    OnClick = False

    Dim np As Peripherals
    Dim i As Integer


    OnClick = True
    Exit Function

OnClickError:
    Errorbox "Can not process the click", Err.Description
    Exit Function
End Function


'*****************************************************************************
'Description:
'        This function is called from DAvE if the user clicks on a menu entry.
'-----------------------------------------------------------------------------
'Parameters:
'        ps              reference to the Peripherals object
'        Toolname        name of the entry
'        ToolId          id of the entry
'        Tooltext        additional text of the entry
'-----------------------------------------------------------------------------
'Returns:
'        True            on successfull operation
'        False           else
'*****************************************************************************
Function OnMenuClick(ps As Peripherals, Toolname As String, ToolId As Long, Tooltext As String) As Boolean
    On Error GoTo OnMenuClickError

    OnMenuClick = False

    If (Not MenuClick(ps, Toolname, ToolId, Tooltext, "ADC\Channel")) Then Exit Function

    OnMenuClick = True
    Exit Function

OnMenuClickError:
    Exit Function
End Function
    
'*****************************************************************************
'Description:
'        This function is called from the OnLoad function of the associated
'        module in order to add the page 'ChannelGeneral'.
'-----------------------------------------------------------------------------
'Parameters:
'        ps               reference to the Peripherals object
'-----------------------------------------------------------------------------
'Returns:
'        True            on successfull operation
'        False           else
'*****************************************************************************
Public Function OnLoadPageChannelGeneral(ps As Object) As Boolean
  On Error Resume Next

  OnLoadPageChannelGeneral = False

  If (Not ps.AddPage("General Settings")) Then Exit Function

  OnLoadPageChannelGeneral = True
End Function



'*****************************************************************************
'Description:
'        This function is called from the OnChangingPage function of the
'        associated module in order load all controls for the requested page
'        if they are not already loaded.
'-----------------------------------------------------------------------------
'Parameters:
'        ps               reference to the Peripherals object
'        Pagename         requested page name
'-----------------------------------------------------------------------------
'Returns:
'        True            on successfull operation
'        False           else
'*****************************************************************************
Function OnChangingPageChannelGeneral(ps As Object, Pagename As String) As Boolean
  On Error Resume Next

  OnChangingPageChannelGeneral = False
  
  If (Not ps.ExtenderCollection("General Settings").Loaded) And (Pagename = "General Settings") Then
    ps.ExtenderCollection("General Settings").Loaded = True
  
    '-------------------------------
    ' frame: frm0
    '-------------------------------
    If (Not ps.AddFrame("frmGlobal", 120, 120, 4200, 840, "Global")) Then Exit Function
        If (Not ps.AddCheck("chkUSE_CHANNEL_x", 240, 240, 2640, 360, "Enable Channel " & ps.Parameters.v("Number"))) Then Exit Function

    '-------------------------------
    ' frame: frmLimitCheckCnt
    '-------------------------------
    If (Not ps.AddFrame("frmLimitCheckCnt", 120, 1080, 4200, 4080, "Limit Check Control (CHCTR" & ps.Parameters.v("Number") & "_LCC) " & "(Channel Interrupt )")) Then Exit Function
        If (Not ps.AddOption("optLimitCheck0", 120, 240, 3960, 380, "No trigger, the channel interrupt is disabled.")) Then Exit Function
        If (Not ps.AddOption("optLimitCheck1", 120, 720, 3960, 380, "Channel interrupt is generated if the conversion result is not in area I")) Then Exit Function
        If (Not ps.AddOption("optLimitCheck2", 120, 1200, 3960, 360, "Channel interrupt is generated if the conversion result is not in area II")) Then Exit Function
        If (Not ps.AddOption("optLimitCheck3", 120, 1680, 3960, 380, "Channel interrupt is generated if the conversion result is not in area III")) Then Exit Function
        If (Not ps.AddOption("optLimitCheck4", 120, 2160, 3960, 380, "Channel interrupt is always generated (regardless of the boundaries)")) Then Exit Function
        If (Not ps.AddOption("optLimitCheck5", 120, 2640, 3960, 380, "Channel interrupt is generated if the conversion result is in area I.")) Then Exit Function
        If (Not ps.AddOption("optLimitCheck6", 120, 3120, 3960, 380, "Channel interrupt is generated if the conversion result is in area II")) Then Exit Function
        If (Not ps.AddOption("optLimitCheck7", 120, 3600, 3960, 380, "Channel interrupt is generated if the conversion result is in area III")) Then Exit Function

    '-------------------------------
    ' frame: frmResultRegister
    '-------------------------------
    If (Not ps.AddFrame("frmResultRegister", 4440, 120, 4200, 840, "Result Register (CHCTR" & ps.Parameters.v("Number") & "_RESRSEL)")) Then Exit Function
        If (Not ps.AddLabel("lblResultRegister", 0, 120, 360, 2160, 240, "Selected result register ")) Then Exit Function
        If (Not ps.AddCombo("cboResultRegister", 2400, 360, 1680, 315)) Then Exit Function

    '-------------------------------
    ' frame: frmInterruptNodePointer
    '-------------------------------
    If (Not ps.AddFrame("frmInterruptControl", 4440, 1080, 4200, 1020, "Interrupt Node Pointer (CHINPR)")) Then Exit Function
        If (Not ps.AddOption("optInterruptNodePointer0", 120, 240, 1600, 380, "SR0 activated")) Then Exit Function
        If (Not ps.AddOption("optInterruptNodePointer1", 120, 600, 1600, 380, "SR1 activated")) Then Exit Function
  
  End If
  OnChangingPageChannelGeneral = True
End Function



'*****************************************************************************
'Description:
'        This function is called from the OnShow function of the associated
'        module. See the description for the OnShow function of the
'        associated module for details.
'-----------------------------------------------------------------------------
'Parameters:
'        ps              reference to the Peripherals object
'        Controlname     control name
'        Pagename        current page name
'        v()             value array
'-----------------------------------------------------------------------------
'Returns:
'        True            on successfull operation
'        False           else
'*****************************************************************************
Public Function OnShowPageChannelGeneral(ps As Object, Controlname As String, Pagename As String, v() As Variant) As Boolean
  On Error GoTo OnShowPageChannelGeneralError

  OnShowPageChannelGeneral = False

  Dim SharedInt As Integer
  Dim r As ProjectData
  Dim c As Object
  Dim e As Object
  Dim i As Integer
  Dim bDisabled As Boolean
  Dim vNumber As Variant
  Dim vIndex As Variant
  Dim vToolIndex As Variant
    Dim sVarPre As String
    Dim sRegPre As String

  sVarPre = "ADC"
  sRegPre = sVarPre & "_"

  vNumber = ps.Parameters.v("Number")
  vIndex = ps.Parameters.v("x")

  Set r = ps.ProjectData
  Set c = ps.ControlCollection
  Set e = ps.ExtenderCollection

  If (Pagename = "General Settings") Then
    '-------------------------------------------------------------------------
    ' page: ChannelGeneral (action)
    '-------------------------------------------------------------------------
    Select Case (Controlname)
        
        Case "chkUSE_CHANNEL_x"
          r.v(sVarPre & "_USE_CHANNEL_" & vNumber) = v(1)
          If (r.v(sVarPre & "_USE_CHANNEL_" & vNumber) = 1) Then
            Call RequestAlternateInputPin(r, "AN" & vNumber)
          Else
            Call ResetChannelRegister(r, vNumber)
          End If
          
'        Case "optLimitCheck0"
'          r.v(sRegPre & "CHCTR" & vNumber & "_LCC") = 0
'
'        Case "optLimitCheck1"
'          r.v(sRegPre & "CHCTR" & vNumber & "_LCC") = 1
'
'        Case "optLimitCheck2"
'          r.v(sRegPre & "CHCTR" & vNumber & "_LCC") = 2
'
'        Case "optLimitCheck3"
'          r.v(sRegPre & "CHCTR" & vNumber & "_LCC") = 3
'
'        Case "optLimitCheck4"
'          r.v(sRegPre & "CHCTR" & vNumber & "_LCC") = 4
'
'        Case "optLimitCheck5"
'          r.v(sRegPre & "CHCTR" & vNumber & "_LCC") = 5
'
'        Case "optLimitCheck6"
'          r.v(sRegPre & "CHCTR" & vNumber & "_LCC") = 6
'
'        Case "optLimitCheck7"
'          r.v(sRegPre & "CHCTR" & vNumber & "_LCC") = 7
'
'        Case "cboResultRegister"
'            r.v(sRegPre & "CHCTR" & vNumber & "_RESRSEL") = c("cboResultRegister").ListIndex
'
'        Case "optInterruptNodePointer0"
'          r.v(sRegPre & "CHINPR_CHINP" & vNumber) = 0
'
'        Case "optInterruptNodePointer1"
'          r.v(sRegPre & "CHINPR_CHINP" & vNumber) = 1
        Case "optLimitCheck0"
          r.v(sRegPre & "CHCTR" & vNumber & "_LCC") = 0
        Case "optLimitCheck1"
          If (IsCpuTypeOK(r)) Then
             'MsgBox MSG_SELECT_XINTR6_INTERRUPTS
             If (r.v("INTR_MSGEN") = 1) Then
                 SharedInt = MsgBox(MSG_SELECT_XINTR6_INTERRUPTS, vbYesNo + vbQuestion, "DAvE")
                 If (SharedInt = 6) Then
                     r.v("INTR_MSGEN") = 1
                 ElseIf (SharedInt = 7) Then
                     r.v("INTR_MSGEN") = 0
                     SharedInt = MsgBox(MSGBOX_SHAREDINT_ENABLE, vbInformation, "DAvE")
                 End If
             End If
             r.v(sRegPre & "CHCTR" & vNumber & "_LCC") = 1
          Else
              r.v(sRegPre & "CHCTR" & vNumber & "_LCC") = 1
          End If
        Case "optLimitCheck2"
          If (IsCpuTypeOK(r)) Then
               'MsgBox MSG_SELECT_XINTR6_INTERRUPTS
               If (r.v("INTR_MSGEN") = 1) Then
                   SharedInt = MsgBox(MSG_SELECT_XINTR6_INTERRUPTS, vbYesNo + vbQuestion, "DAvE")
                   If (SharedInt = 6) Then
                       r.v("INTR_MSGEN") = 1
                   ElseIf (SharedInt = 7) Then
                       r.v("INTR_MSGEN") = 0
                       SharedInt = MsgBox(MSGBOX_SHAREDINT_ENABLE, vbInformation, "DAvE")
                   End If
               End If
               r.v(sRegPre & "CHCTR" & vNumber & "_LCC") = 2
          Else
               r.v(sRegPre & "CHCTR" & vNumber & "_LCC") = 2
          End If
        Case "optLimitCheck3"
          If (IsCpuTypeOK(r)) Then
              'MsgBox MSG_SELECT_XINTR6_INTERRUPTS
              If (r.v("INTR_MSGEN") = 1) Then
                  SharedInt = MsgBox(MSG_SELECT_XINTR6_INTERRUPTS, vbYesNo + vbQuestion, "DAvE")
                  If (SharedInt = 6) Then
                      r.v("INTR_MSGEN") = 1
                  ElseIf (SharedInt = 7) Then
                      r.v("INTR_MSGEN") = 0
                      SharedInt = MsgBox(MSGBOX_SHAREDINT_ENABLE, vbInformation, "DAvE")
                  End If
              End If
              r.v(sRegPre & "CHCTR" & vNumber & "_LCC") = 3
          Else
                r.v(sRegPre & "CHCTR" & vNumber & "_LCC") = 3
          End If
        
        Case "optLimitCheck4"
          If (IsCpuTypeOK(r)) Then
               'MsgBox MSG_SELECT_XINTR6_INTERRUPTS
               If (r.v("INTR_MSGEN") = 1) Then
                   SharedInt = MsgBox(MSG_SELECT_XINTR6_INTERRUPTS, vbYesNo + vbQuestion, "DAvE")
                   If (SharedInt = 6) Then
                      r.v("INTR_MSGEN") = 1
                   ElseIf (SharedInt = 7) Then
                      r.v("INTR_MSGEN") = 0
                      SharedInt = MsgBox(MSGBOX_SHAREDINT_ENABLE, vbInformation, "DAvE")
                   End If
               End If
               r.v(sRegPre & "CHCTR" & vNumber & "_LCC") = 4
          Else
                 r.v(sRegPre & "CHCTR" & vNumber & "_LCC") = 4
          End If
        
        Case "optLimitCheck5"
          If (IsCpuTypeOK(r)) Then
              'MsgBox MSG_SELECT_XINTR6_INTERRUPTS
              If (r.v("INTR_MSGEN") = 1) Then
                   SharedInt = MsgBox(MSG_SELECT_XINTR6_INTERRUPTS, vbYesNo + vbQuestion, "DAvE")
                   If (SharedInt = 6) Then
                      r.v("INTR_MSGEN") = 1
                   ElseIf (SharedInt = 7) Then
                      r.v("INTR_MSGEN") = 0
                      SharedInt = MsgBox(MSGBOX_SHAREDINT_ENABLE, vbInformation, "DAvE")
                   End If
              End If
              r.v(sRegPre & "CHCTR" & vNumber & "_LCC") = 5
          Else
                 r.v(sRegPre & "CHCTR" & vNumber & "_LCC") = 5
          End If
        Case "optLimitCheck6"
          If (IsCpuTypeOK(r)) Then
               'MsgBox MSG_SELECT_XINTR6_INTERRUPTS
               If (r.v("INTR_MSGEN") = 1) Then
                   SharedInt = MsgBox(MSG_SELECT_XINTR6_INTERRUPTS, vbYesNo + vbQuestion, "DAvE")
                   If (SharedInt = 6) Then
                      r.v("INTR_MSGEN") = 1
                   ElseIf (SharedInt = 7) Then
                      r.v("INTR_MSGEN") = 0
                      SharedInt = MsgBox(MSGBOX_SHAREDINT_ENABLE, vbInformation, "DAvE")
                   End If
               End If
               r.v(sRegPre & "CHCTR" & vNumber & "_LCC") = 6
          Else
                  r.v(sRegPre & "CHCTR" & vNumber & "_LCC") = 6
          End If
        
        Case "optLimitCheck7"
          If (IsCpuTypeOK(r)) Then
               'MsgBox MSG_SELECT_XINTR6_INTERRUPTS
               If (r.v("INTR_MSGEN") = 1) Then
                   SharedInt = MsgBox(MSG_SELECT_XINTR6_INTERRUPTS, vbYesNo + vbQuestion, "DAvE")
                   If (SharedInt = 6) Then
                      r.v("INTR_MSGEN") = 1
                   ElseIf (SharedInt = 7) Then
                      r.v("INTR_MSGEN") = 0
                      SharedInt = MsgBox(MSGBOX_SHAREDINT_ENABLE, vbInformation, "DAvE")
                   End If
               End If
               r.v(sRegPre & "CHCTR" & vNumber & "_LCC") = 7
          Else
                  r.v(sRegPre & "CHCTR" & vNumber & "_LCC") = 7
          End If

        Case "cboResultRegister"
            r.v(sRegPre & "CHCTR" & vNumber & "_RESRSEL") = c("cboResultRegister").ListIndex

        Case "optInterruptNodePointer0"
          r.v(sRegPre & "CHINPR_CHINP" & vNumber) = 0
          
        Case "optInterruptNodePointer1"
          r.v(sRegPre & "CHINPR_CHINP" & vNumber) = 1
          
    End Select

    With c("chkUSE_CHANNEL_x")
      bDisabled = ((r.v(SCU_PREFIX & "PMCON1_ADC_DIS") = 1) Or (Not IsAlternatePinFree(r, "AN" & vNumber)) Or IsClocktoohigh(r))
      .Enabled = Not bDisabled
      .Value = r.v(sVarPre & "_USE_CHANNEL_" & vNumber)
    End With

    With e("chkUSE_CHANNEL_x")
      If (bDisabled) Then
        .DisabledToolTipText = TOOLTIP_DISABLED_IF
        If (IsModuleClockDisabled(r, vToolIndex)) Then
            .DisabledToolTipText = .DisabledToolTipText & GetTooltipModuleClockDisabled(r, vToolIndex)
        ElseIf (IsClocktoohigh(r)) Then
            .DisabledToolTipText = .DisabledToolTipText & GetTooltipModuleClocktooHigh(r)
        Else
            .DisabledToolTipText = .DisabledToolTipText & GetDisabledAlternatePinTooltip(r, "AN" & vNumber)
        End If
      End If
    End With

    bDisabled = (r.v(sVarPre & "_USE_CHANNEL_" & vNumber) = 0) Or (r.v(SCU_PREFIX & "PMCON1_ADC_DIS") = 1) Or IsClocktoohigh(r)
    Call IsInterruptEnabled(r)
    For i = 0 To 7
        With c("optLimitCheck" & i)
          .Enabled = Not bDisabled
          .Value = r.v(sRegPre & "CHCTR" & vNumber & "_LCC") = i
        End With
    
        With e("optLimitCheck" & i)
          If (bDisabled) Then
            .DisabledToolTipText = TOOLTIP_DISABLED_IF
            If (IsModuleClockDisabled(r, vToolIndex)) Then
                .DisabledToolTipText = .DisabledToolTipText & GetTooltipModuleClockDisabled(r, vToolIndex)
            Else
                .DisabledToolTipText = .DisabledToolTipText & FormatDisabledToolTip("ADC Channel " & vNumber & " Not Enabled")
            End If
          End If
        End With
    Next i
   
    bDisabled = (r.v(sVarPre & "_USE_CHANNEL_" & vNumber) = 0) Or (r.v(SCU_PREFIX & "PMCON1_ADC_DIS") = 1) Or IsClocktoohigh(r)

    With c("lblResultRegister")
      .Enabled = Not bDisabled
    End With

    With c("cboResultRegister")
        .Clear
       .Enabled = Not (bDisabled)
       For i = 0 To 3
        .AddItem " Register " & i
        Next i
            .ListIndex = r.v(sRegPre & "CHCTR" & vNumber & "_RESRSEL")
    End With

    With e("cboResultRegister")
      If (bDisabled) Then
        .DisabledToolTipText = TOOLTIP_DISABLED_IF
        If (IsModuleClockDisabled(r, vToolIndex)) Then
            .DisabledToolTipText = .DisabledToolTipText & GetTooltipModuleClockDisabled(r, vToolIndex)
        Else
            .DisabledToolTipText = .DisabledToolTipText & FormatDisabledToolTip("ADC Channel " & vNumber & " Not Enabled")
        End If
      End If
    End With
   
    bDisabled = (r.v(sVarPre & "_USE_CHANNEL_" & vNumber) = 0) Or (r.v(SCU_PREFIX & "PMCON1_ADC_DIS") = 1) Or IsClocktoohigh(r)
    
   For i = 0 To 1
        With c("optInterruptNodePointer" & i)
          .Enabled = Not bDisabled
          .Value = -CInt(r.v(sRegPre & "CHINPR_CHINP" & vNumber) = i)
        End With
    
        With e("optInterruptNodePointer" & i)
          If (bDisabled) Then
            .DisabledToolTipText = TOOLTIP_DISABLED_IF
            If (IsModuleClockDisabled(r, vToolIndex)) Then
                .DisabledToolTipText = .DisabledToolTipText & GetTooltipModuleClockDisabled(r, vToolIndex)
            Else
                .DisabledToolTipText = .DisabledToolTipText & FormatDisabledToolTip("ADC Channel " & vNumber & " Not Enabled")
            End If
          End If
        End With
   Next i

  End If

  OnShowPageChannelGeneral = True
  Exit Function

OnShowPageChannelGeneralError:
  Exit Function
End Function

