'#Uses "..\classes\FunctionClass.cls"
'#Uses "..\classes\ParameterClass.cls"
'#Uses "..\include\Const.inc"
'#Uses "..\include\Format.inc"
'#Uses "..\include\Global.inc"
'#Uses "..\include\Pages.inc"
'#Uses "..\include\Port.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   ahe         14.06.00     final version
'
'        ---------------------------------------------------------------------
'        2.0   ahe         20.07.00     final version for C161PI
'                                       - reduzierung von Ch's von 24 auf 4
'                                       - delete P1DIDIS
'                                       - delete ADCON_ADX
'             
'        ---------------------------------------------------------------------
'        3.0   ahe         07.08.00     final version for C161CS/JC/JI
'                                       - only AN0..AN7 and AN12..AN15
'
'        ---------------------------------------------------------------------
'        4.0   ahe         20.02.01     final version for Pegasus
'
'        4.1   ahe         08.08.02     no changes
'        4.2   ste         09.12.02     formula for conversion time changed 
'                                       (spec. changed)
'                                       16 analog inputs
'
'*****************************************************************************


'*****************************************************************************
'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: Control
    '-------------------------------------------------------------------------
    If (Not OnLoadPageControl(ps)) Then Exit Function

    '-------------------------------------------------------------------------
    ' page: TimingControl
    '-------------------------------------------------------------------------
    If (Not OnLoadPageTimingControl(ps)) Then Exit Function

    '-------------------------------------------------------------------------
    ' end of pages
    '-------------------------------------------------------------------------
    
    '-------------------------------------------------------------------------
    ' call function to initialize the menu
    '-------------------------------------------------------------------------
    If (Not InitMenu(ps, "ADC\NormalControl")) 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

    OnChangingPage = False
    
    Dim sVarPre As String
    Dim vIndex  As Variant

    vIndex = ps.Parameters.v("x")
    sVarPre = "ADC" & vIndex

    
    '-------------------------------------------------------------------------
    'page: Control
    '-------------------------------------------------------------------------
    If (Not OnChangingPageControl (ps, Pagename)) Then Exit Function

    '-------------------------------------------------------------------------
    'page: TimingControl
    '-------------------------------------------------------------------------
    If (Not OnChangingPageTimingControl (ps, Pagename)) 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

    OnShow = False
    
    Dim sVarPre As String
    Dim vIndex  As Variant

    vIndex = ps.Parameters.v("x")
    sVarPre = "ADC" & vIndex


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

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

    OnShow = True
    Exit Function

OnShowError:
    Errorbox "Can not show page.", Err.Description
    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

    OnClick = True
    Exit Function

OnClickError:
    Errorbox "Can not Text in OnClick.", 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\NormalControl")) Then Exit Function

    OnMenuClick = True
    Exit Function

OnMenuClickError:
    Exit Function
End Function
    
    

'-----------------------------------------------------------------------------
' local functions
'-----------------------------------------------------------------------------

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

  OnLoadPageControl = False

  If (Not ps.AddPage("Control")) Then Exit Function

  OnLoadPageControl = 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 OnChangingPageControl(ps As Object, Pagename As String) As Boolean
  On Error Resume Next

  OnChangingPageControl = False
  
  If (Not ps.ExtenderCollection("Control").Loaded) And (Pagename = "Control") Then
    ps.ExtenderCollection("Control").Loaded = True
  

    '-------------------------------
    ' frame: frmADM
    '-------------------------------
    If (Not ps.AddFrame("frmADM",100,100, 4150, 1950, _
      "Mode Selection (ADM)")) Then Exit Function
        If (Not ps.AddOption("optADM_0", 200, 300, 2900, 300, _
          "Fixed channel single conversion")) Then Exit Function
        If (Not ps.AddOption("optADM_1", 200, 675, 3025, 300, _
          "Fixed channel continuous conversion")) Then Exit Function
        If (Not ps.AddOption("optADM_2", 200, 1050, 2800, 300, _
          "Auto scan single conversion")) Then Exit Function
        If (Not ps.AddOption("optADM_3", 200, 1425, 2700, 300, _
          "Auto scan continuous conversion")) Then Exit Function

    '-------------------------------
    ' frame: frmRES
    '-------------------------------
    If (Not ps.AddFrame("frmRES",4400,100, 4150, 1050, _
      "Converter Resolution (RES)")) Then Exit Function
        If (Not ps.AddOption("optRES_0", 200, 300, 2900, 300, _
          "10-bit resolution")) Then Exit Function
        If (Not ps.AddOption("optRES_1", 200, 650, 3025, 300, _
          "8-bit resolution")) Then Exit Function

    '-------------------------------
    ' frame: frmADCH
    '-------------------------------
    If (Not ps.AddFrame("frmADCH",4400,1200, 4150, 850, _
      "Analog Channel Input Selection (ADCH)")) Then Exit Function
        If (Not ps.AddCombo("cboADCH", 1600, 325, 1815, 315)) Then Exit Function
        If (Not ps.AddLabel("lblADCH", 0, 200, 375, 1275, 315, _
          "Analog channels")) Then Exit Function

    '-------------------------------
    ' frame: frmADWR
    '-------------------------------
    If (Not ps.AddFrame("frmADWR",100,2100, 4150, 800, _
      "Wait for Read Control")) Then Exit Function
        If (Not ps.AddCheck("chkADWR", 200, 300, 3675, 300, _
          "Enable wait for read mode (ADWR)")) Then Exit Function

    '-------------------------------
    ' frame: frmADST
    '-------------------------------
    If (Not ps.AddFrame("frmADST",4400,2100, 4150, 800, _
      "Start Control")) Then Exit Function
        If (Not ps.AddCheck("chkADST", 200, 300, 3575, 300, _
          "Start conversion after the initialization (ADST)")) Then Exit Function

    '-------------------------------
    ' frame: frmADCIN
    '-------------------------------
    If (Not ps.AddFrame("frmADCIN",100,2950, 4150, 800, _
      "Channel Injection")) Then Exit Function
        If (Not ps.AddCheck("chkADCIN", 200, 300, 3070, 300, _
          "Enable channel injection (ADCIN)")) Then Exit Function

    '-------------------------------
    ' frame: frmCHNR
    '-------------------------------
    If (Not ps.AddFrame("frmCHNR",4400,2950, 4150, 800, _
      "Analog Channel for Injected Conversion (CHNR)")) Then Exit Function
        If (Not ps.AddCombo("cboCHNR", 1600, 300, 1815, 315)) Then Exit Function
        If (Not ps.AddLabel("lblCHNR", 0, 200, 350, 1275, 310, _
          "Analog channel")) Then Exit Function

  End If
  OnChangingPageControl = 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 OnShowPageControl(ps As Object, Controlname As String, Pagename As String, v() As Variant) As Boolean
  On Error GoTo OnShowPageControlError

  OnShowPageControl = False

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

  vIndex = ps.Parameters.v("x")
  sVarPre = "ADC" & vIndex
  sRegPre = sVarPre & "_"

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

  If (Pagename = "Control") Then
    '-------------------------------------------------------------------------
    ' page: Control (action)
    '-------------------------------------------------------------------------
    Select Case (Controlname)
        '---------------------------------------------------------------------
        ' control: optADM_0 (action)
        '---------------------------------------------------------------------
        Case "optADM_0"
            r.v(sRegPre & "CON_ADM") = 0

        '---------------------------------------------------------------------
        ' control: optADM_1 (action)
        '---------------------------------------------------------------------
        Case "optADM_1"
            r.v(sRegPre & "CON_ADM") = 1

        '---------------------------------------------------------------------
        ' control: optADM_2 (action)
        '---------------------------------------------------------------------
        Case "optADM_2"
            r.v(sRegPre & "CON_ADM") = 2

        '---------------------------------------------------------------------
        ' control: optADM_3 (action)
        '---------------------------------------------------------------------
        Case "optADM_3"
            r.v(sRegPre & "CON_ADM") = 3

        '---------------------------------------------------------------------
        ' control: optRES_0 (action)
        '---------------------------------------------------------------------
        Case "optRES_0"
            r.v(sRegPre & "CON1_RES") = 0

        '---------------------------------------------------------------------
        ' control: optRES_1 (action)
        '---------------------------------------------------------------------
        Case "optRES_1"
            r.v(sRegPre & "CON1_RES") = 1

        '---------------------------------------------------------------------
        ' control: cboADCH (action)
        '---------------------------------------------------------------------
        Case "cboADCH"
            For i = 0 To 15
                If (v(1) = "Analog channel " & i) Then
                    r.v(sRegPre & "CON_ADCH") = i
                End If
            Next i

        '---------------------------------------------------------------------
        ' control: chkADST (action)
        '---------------------------------------------------------------------
        Case "chkADST"
            r.v(sRegPre & "CON_ADST") = v(1)

        '---------------------------------------------------------------------
        ' control: chkADWR (action)
        '---------------------------------------------------------------------
        Case "chkADWR"
            r.v(sRegPre & "CON_ADWR") = v(1)
            If (v(1) = 0) Then
                r.v(sRegPre & "CON_ADCIN") = 0
                r.v(sVarPre & "_USE_InjectChan") = 0
                r.v(sVarPre & "_USE_StartInject") = 0
                r.v(sVarPre & "_USE_ReadInject") = 0
                r.v(sVarPre & "_USE_InjectReady") = 0
            Else
                r.v(sVarPre & "_USE_OverWrite") = 0
            End If

        '---------------------------------------------------------------------
        ' control: chkADCIN (action)
        '---------------------------------------------------------------------
        Case "chkADCIN"
            r.v(sRegPre & "CON_ADCIN") = v(1)
            If (v(1) = 0) Then
                r.v(sVarPre & "_USE_InjectChan") = 0
                r.v(sVarPre & "_USE_StartInject") = 0
                r.v(sVarPre & "_USE_ReadInject") = 0
                r.v(sVarPre & "_USE_InjectReady") = 0
            End If

        '---------------------------------------------------------------------
        ' control: cboCHNR (action)
        '---------------------------------------------------------------------
        Case "cboCHNR"
            For i = 0 To 15
                If (v(1) = "Analog channel " & i) Then
                    r.v(sRegPre & "DAT2_CHNR") = i
                End If
            Next i

      Case Else
    End Select

    '-------------------------------------------------------------------------
    ' page: Control (show)
    '-------------------------------------------------------------------------
    bDisabled = (IsModuleDisabled(r, vIndex) Or r.v(sRegPre & "CTR0_MD") = 1)

    sDisabledTooltip = ""
    sDisabledTooltip = TOOLTIP_DISABLED_IF

    If (IsModuleDisabled(r, vIndex)) Then
        sDisabledTooltip = sDisabledTooltip & GetTooltipModuleDisabled(r, vIndex)
    End If
    If (r.v(sRegPre & "CTR0_MD") = 1) Then
        sDisabledTooltip = sDisabledTooltip & FormatDisabledTooltip("enhanced mode is selected")
    End If

    SetDefaultPropertiesForAllControlsOnFrame(c("frmADM").Caption, c, e, bDisabled, sDisabledTooltip, "")
    SetDefaultPropertiesForAllControlsOnFrame(c("frmRES").Caption, c, e, bDisabled, sDisabledTooltip, "")
    SetDefaultPropertiesForAllControlsOnFrame(c("frmADCH").Caption, c, e, bDisabled, sDisabledTooltip, "")
    SetDefaultPropertiesForAllControlsOnFrame(c("frmADST").Caption, c, e, bDisabled, sDisabledTooltip, "")
    SetDefaultPropertiesForAllControlsOnFrame(c("frmADWR").Caption, c, e, bDisabled, sDisabledTooltip, "")

    '-------------------------------------------------------------------------
    ' control: optADM_0 (show)
    '-------------------------------------------------------------------------
    With c("optADM_0")
        .Value = -CInt(r.v(sRegPre & "CON_ADM") = 0)
    End With

    '-------------------------------------------------------------------------
    ' control: optADM_1 (show)
    '-------------------------------------------------------------------------
    With c("optADM_1")
        .Value = -CInt(r.v(sRegPre & "CON_ADM") = 1)
    End With

    '-------------------------------------------------------------------------
    ' control: optADM_2 (show)
    '-------------------------------------------------------------------------
    With c("optADM_2")
        .Value = -CInt(r.v(sRegPre & "CON_ADM") = 2)
    End With

    '-------------------------------------------------------------------------
    ' control: optADM_3 (show)
    '-------------------------------------------------------------------------
    With c("optADM_3")
        .Value = -CInt(r.v(sRegPre & "CON_ADM") = 3)
    End With

    '-------------------------------------------------------------------------
    ' control: optRES_0 (show)
    '-------------------------------------------------------------------------
    With c("optRES_0")
        .Value = -CInt(r.v(sRegPre & "CON1_RES") = 0)
    End With

    '-------------------------------------------------------------------------
    ' control: optRES_1 (show)
    '-------------------------------------------------------------------------
    With c("optRES_1")
        .Value = -CInt(r.v(sRegPre & "CON1_RES") = 1)
    End With

    '-------------------------------------------------------------------------
    ' control: cboADCH (show)
    '-------------------------------------------------------------------------
    With c("cboADCH")
        .Clear
        For i = 0 To 15
            If (r.Exist(sVarPre & "_PARA_ANA_" & i)) Then
                .AddItem "Analog channel " & i
                If (r.v(sRegPre & "CON_ADCH") = i) Then
                    .ListIndex = .ListCount - 1
                End If
            End If
        Next i

'        If (r.v(sRegPre & "CON_ADCH") > 7) Then
'            If (r.Exist(sVarPre & "_PARA_ANA_10")) Then
'                .ListIndex = r.v(sRegPre & "CON_ADCH") - 2
'            Else
'                .ListIndex = r.v(sRegPre & "CON_ADCH") - 4
'            End IF
'        Else
'            .ListIndex = r.v(sRegPre & "CON_ADCH")
'        End If

    End With

    '-------------------------------------------------------------------------
    ' control: lblADCH (show)
    '-------------------------------------------------------------------------
    With c("lblADCH")
    End With

    '-------------------------------------------------------------------------
    ' control: chkADST (show)
    '-------------------------------------------------------------------------
    With c("chkADST")
        .Value = -CInt(r.v(sRegPre & "CON_ADST") = 1)
    End With

    '-------------------------------------------------------------------------
    ' control: chkADWR (show)
    '-------------------------------------------------------------------------
    With c("chkADWR")
        .Value = -CInt(r.v(sRegPre & "CON_ADWR") = 1)
    End With

    '-------------------------------------------------------------------------
    ' control: chkADCIN (show)
    '-------------------------------------------------------------------------
    With c("chkADCIN")
        bDisabled = (IsModuleDisabled(r, vIndex) Or r.v(sRegPre & "CON_ADWR") = 0 Or _
                     r.v(sRegPre & "CTR0_MD") = 1)
        .Enabled = Not(bDisabled)
        .Value = -CInt(r.v(sRegPre & "CON_ADCIN") = 1)
    End With

    With e("chkADCIN")
      If (bDisabled) Then
        .DisabledTooltipText = TOOLTIP_DISABLED_IF
        If (IsModuleDisabled(r, vIndex)) Then
          .DisabledTooltipText = .DisabledTooltipText & GetTooltipModuleDisabled(r, vIndex)
        End If
        If (r.v(sRegPre & "CON_ADWR") = 0) Then
          .DisabledTooltipText = .DisabledTooltipText & FormatDisabledTooltip("'wait for read mode' is disabled")
        End If
        If (r.v(sRegPre & "CTR0_MD") = 1) Then
          .DisabledTooltipText = .DisabledTooltipText & FormatDisabledTooltip("enhanced mode is selected")
        End If
      End If
    End With

    '-------------------------------------------------------------------------
    ' control: cboCHNR (show)
    '-------------------------------------------------------------------------
    With c("cboCHNR")
        bDisabled = (IsModuleDisabled(r, vIndex) Or r.v(sRegPre & "CON_ADWR") = 0 Or _
                    r.v(sRegPre & "CON_ADCIN") = 0 Or _
                    r.v(sRegPre & "CTR0_MD") = 1)
        .Enabled = Not(bDisabled)
        .Clear
        For i = 0 To 15
            If (r.Exist(sVarPre & "_PARA_ANA_" & i)) Then
                .AddItem "Analog channel " & i
                If (r.v(sRegPre & "DAT2_CHNR") = i) Then
                    .ListIndex = .ListCount - 1
                End If
            End If
        Next i
       
'        If (r.v(sRegPre & "DAT2_CHNR") > 7) Then
'            If (r.Exist(sVarPre & "_PARA_ANA_10")) Then
'                .ListIndex = r.v(sRegPre & "DAT2_CHNR") - 2
'            Else
'                .ListIndex = r.v(sRegPre & "DAT2_CHNR") - 4
'            End If
'        Else
'            .ListIndex = r.v(sRegPre & "DAT2_CHNR")
'        End If
        
    End With

    With e("cboCHNR")
      If (bDisabled) Then
        .DisabledTooltipText = TOOLTIP_DISABLED_IF
        If (IsModuleDisabled(r, vIndex)) Then
          .DisabledTooltipText = .DisabledTooltipText & GetTooltipModuleDisabled(r, vIndex)
        End If
        If (r.v(sRegPre & "CON_ADWR") = 0) Then
          .DisabledTooltipText = .DisabledTooltipText & FormatDisabledTooltip("'wait for read mode' is disabled")
        End If
        If (r.v(sRegPre & "CON_ADCIN") = 0) Then
          .DisabledTooltipText = .DisabledTooltipText & FormatDisabledTooltip("channel injection is disabled")
        End If
        If (r.v(sRegPre & "CTR0_MD") = 1) Then
          .DisabledTooltipText = .DisabledTooltipText & FormatDisabledTooltip("enhanced mode is selected")
        End If
      End If
    End With

    '-------------------------------------------------------------------------
    ' control: lblCHNR (show)
    '-------------------------------------------------------------------------
    With c("lblCHNR")
        .Enabled = Not(bDisabled)
    End With

    With e("lblCHNR")
      If (bDisabled) Then
        .DisabledTooltipText = e("cboCHNR").DisabledTooltipText
      End If
    End With

  End If

  OnShowPageControl = True
  Exit Function

OnShowPageControlError:
  Errorbox "Can not show page", Err.Description
  Exit Function
End Function

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

  OnLoadPageTimingControl = False

  If (Not ps.AddPage("Timing Control")) Then Exit Function

  OnLoadPageTimingControl = 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 OnChangingPageTimingControl(ps As Object, Pagename As String) As Boolean
  On Error Resume Next

  OnChangingPageTimingControl = False
  
  If (Not ps.ExtenderCollection("Timing Control").Loaded) And (Pagename = "Timing Control") Then
    ps.ExtenderCollection("Timing Control").Loaded = True
  

    '-------------------------------
    ' frame: frmICST
    '-------------------------------
    If (Not ps.AddFrame("frmICST",100,100, 4150, 1200, _
      "Improved Conversion and Sample Timing")) Then Exit Function
        If (Not ps.AddCheck("chkICST", 200, 500, 3800, 380, _
            "Enable improved conversion and sample time control functionality (ICST)")) Then Exit Function

    '-------------------------------
    ' frame: frmTime
    '-------------------------------
    If (Not ps.AddFrame("frmTime",4400,100, 4150, 1200, _
      "Complete Conversion Time")) Then Exit Function
        If (Not ps.AddLabel("txtTime", 1, 1800, 500, 1000, 300)) Then Exit Function
        If (Not ps.AddLabel("lblTime", 0, 200, 550, 1500, 380, _
          "Complete time [xx]")) Then Exit Function

    '-------------------------------
    ' frame: frmADCTC
    '-------------------------------
    If (Not ps.AddFrame("frmADCTC",100,1350, 4150, 1200, _
      "Conversion Time Control (ADCTC)")) Then Exit Function
        If (Not ps.AddCombo("cboADCTC", 1600, 500, 2400, 300)) Then Exit Function
        If (Not ps.AddLabel("lblADCTC", 0, 200, 500, 1275, 380, _
          "Conversion basic clock [tbc]")) Then Exit Function

    '-------------------------------
    ' frame: frmADSTC
    '-------------------------------
    If (Not ps.AddFrame("frmADSTC",100,2600, 4150, 1200, _
      "Sample Time Control (ADSTC)")) Then Exit Function
        If (Not ps.AddCombo("cboADSTC", 1600, 500, 2400, 300)) Then Exit Function
        If (Not ps.AddLabel("lblADSTC", 0, 200, 550, 1275, 300, _
          "Sample time [tsc]")) Then Exit Function


    '-------------------------------
    ' frame: frmCON1_ADCTC
    '-------------------------------
    If (Not ps.AddFrame("frmCON1_ADCTC",4400,1350, 4150, 1200, _
      "Improved Conversion Time Control (ADCTC)")) Then Exit Function
        If (Not ps.AddCombo("cboCON1_ADCTC", 1600, 500, 2400, 300)) Then Exit Function
        If (Not ps.AddLabel("lblCON1_ADCTC", 0, 200, 500, 1275, 380, _
          "Conversion basic clock [tbc]")) Then Exit Function

    '-------------------------------
    ' frame: frmCON1_ADSTC
    '-------------------------------
    If (Not ps.AddFrame("frmCON1_ADSTC",4400,2600, 4150, 1200, _
      "Improved Sample Time Control (ADSTC)")) Then Exit Function
        If (Not ps.AddCombo("cboCON1_ADSTC", 1600, 500, 2400, 300)) Then Exit Function
        If (Not ps.AddLabel("lblCON1_ADSTC", 0, 200, 550, 1275, 300, _
          "Sample time [ts]")) Then Exit Function


  End If
  OnChangingPageTimingControl = 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 OnShowPageTimingControl(ps As Object, Controlname As String, Pagename As String, v() As Variant) As Boolean
  On Error GoTo OnShowPageTimingControlError

  OnShowPageTimingControl = False

  Dim r                 As ProjectData
  Dim c                 As Object
  Dim e                 As Object
  Dim sVarPre           As String
  Dim vIndex            As Variant
  Dim sRegPre           As String
  Dim bDisabled         As Boolean
  Dim i                 As Integer
  Dim sDisabledTooltip  As String
  Dim uTime             As UnitType

  vIndex = ps.Parameters.v("x")
  sVarPre = "ADC" & vIndex
  sRegPre = sVarPre & "_"

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

  If (Pagename = "Timing Control") Then
    '-------------------------------------------------------------------------
    ' page: TimingControl (action)
    '-------------------------------------------------------------------------
    Select Case (Controlname)
        '---------------------------------------------------------------------
        ' control: chkICST (action)
        '---------------------------------------------------------------------
        Case "chkICST"
            r.v(sRegPre & "CON1_ICST") = v(1)

        '---------------------------------------------------------------------
        ' control: cboADCTC (action)
        '---------------------------------------------------------------------
        Case "cboADCTC"

            uTime = ConvertTime(2 / GetADCModuleClock(r, vIndex))
            If (v(1) = "fcpu / 2   (tbc = " & Format(uTime.dValue, "0.000")  & " " & _
                       uTime.sUnit & ")") Then
                r.v(sRegPre & "CON_ADCTC") = 1
                r.v(sVArPre & "_Tbc_Factor") = 2
            End If

            uTime = ConvertTime(4 / GetADCModuleClock(r, vIndex))
            If (v(1) = "fcpu / 4   (tbc = " & Format(uTime.dValue, "0.000")  & " " & _
                       uTime.sUnit & ")") Then
                r.v(sRegPre & "CON_ADCTC") = 0
                r.v(sVArPre & "_Tbc_Factor") = 4
            End If

            uTime = ConvertTime(8 / GetADCModuleClock(r, vIndex))
            If (v(1) = "fcpu / 8   (tbc = " & Format(uTime.dValue, "0.000")  & " " & _
                       uTime.sUnit & ")") Then
                r.v(sRegPre & "CON_ADCTC") = 3
                r.v(sVArPre & "_Tbc_Factor") = 8
            End If

            uTime = ConvertTime(16 / GetADCModuleClock(r, vIndex))
            If (v(1) = "fcpu / 16 (tbc = " & Format(uTime.dValue, "0.000")  & " " & uTime.sUnit & ")") Then
                r.v(sRegPre & "CON_ADCTC") = 2
                r.v(sVArPre & "_Tbc_Factor") = 16
            End If

        '---------------------------------------------------------------------
        ' control: cboADCTC (action)
        '---------------------------------------------------------------------
        Case "cboADSTC"
            For i = 0 To 3
                uTime = ConvertTime(2^(3 + i) * r.v(sVArPre & "_Tbc_Factor") / GetADCModuleClock(r, vIndex))
                If (v(1) = "tbc * " & 2^(3 + i) & "   (tsc = " & Format(uTime.dValue, "0.000")  & " " & uTime.sUnit & ")") Then
                    r.v(sRegPre & "CON_ADSTC") = i
                End If
            Next i

        '---------------------------------------------------------------------
        ' control: cboADCTC (action)
        '---------------------------------------------------------------------
        Case "cboCON1_ADCTC"
            For i = 0 To 63
                uTime = ConvertTime((i + 1) / GetADCModuleClock(r, vIndex))
                If (v(1) = "fcpu / " & i + 1 & " (tbc = " & Format(uTime.dValue, "0.000")  & " " & _
                           uTime.sUnit & ")") Then
                    r.v(sRegPre & "CON1_ADCTC") = i
                End If
            Next i

        '---------------------------------------------------------------------
        ' control: cboADCTC (action)
        '---------------------------------------------------------------------
        Case "cboCON1_ADSTC"
            For i = 0 To 63
                uTime = ConvertTime(4 * (i + 2) * _
                    (r.v(sRegPre & "CON1_ADCTC") + 1) / GetADCModuleClock(r, vIndex))
                If (v(1) = "tbc * " & 4 * (i + 2) & "   (ts = " & _
                     Format(uTime.dValue, "0.000")  & " " & uTime.sUnit & ")") Then
                    r.v(sRegPre & "CON1_ADSTC") = i
                End If
            Next i

      Case Else
    End Select

    '-------------------------------------------------------------------------
    ' page: TimingControl (show)
    '-------------------------------------------------------------------------
    bDisabled = (IsModuleDisabled(r, vIndex) Or r.v(sRegPre & "CTR0_MD") = 1)

    sDisabledTooltip = ""
    sDisabledTooltip = TOOLTIP_DISABLED_IF

    If (IsModuleDisabled(r, vIndex)) Then
        sDisabledTooltip = sDisabledTooltip & GetTooltipModuleDisabled(r, vIndex)
    End If
    If (r.v(sRegPre & "CTR0_MD") = 1) Then
        sDisabledTooltip = sDisabledTooltip & FormatDisabledTooltip("enhanced mode is selected")
    End If

    SetDefaultPropertiesForAllControlsOnPage(Pagename, c, e, bDisabled, sDisabledTooltip, "")

    '-------------------------------------------------------------------------
    ' control: chkICST (show)
    '-------------------------------------------------------------------------
    With c("chkICST")
        .Value = -CInt(r.v(sRegPre & "CON1_ICST") = 1)
    End With

    '-------------------------------------------------------------------------
    ' control: cboADCH (show)
    '-------------------------------------------------------------------------
    With c("cboADCTC")
        bDisabled = (IsModuleDisabled(r, vIndex) Or _
                     r.v(sRegPre & "CTR0_MD") = 1 Or _
                     r.v(sRegPre & "CON1_ICST") = 1)
        .Enabled = Not(bDisabled)
        c("lblADCTC").Enabled = Not(bDisabled)

        .Clear
        uTime = ConvertTime(2 / GetADCModuleClock(r, vIndex))
        .AddItem "fcpu / 2   (tbc = " & Format(uTime.dValue, "0.000")  & " " & uTime.sUnit & ")"
        uTime = ConvertTime(4 / GetADCModuleClock(r, vIndex))
        .AddItem "fcpu / 4   (tbc = " & Format(uTime.dValue, "0.000")  & " " & uTime.sUnit & ")"
        uTime = ConvertTime(8 / GetADCModuleClock(r, vIndex))
        .AddItem "fcpu / 8   (tbc = " & Format(uTime.dValue, "0.000")  & " " & uTime.sUnit & ")"
        uTime = ConvertTime(16 / GetADCModuleClock(r, vIndex))
        .AddItem "fcpu / 16 (tbc = " & Format(uTime.dValue, "0.000")  & " " & uTime.sUnit & ")"
        
        If (r.v(sRegPre & "CON_ADCTC") = 0) Then
          .ListIndex = 1
        ElseIf (r.v(sRegPre & "CON_ADCTC") = 1) Then
          .ListIndex = 0
        ElseIf (r.v(sRegPre & "CON_ADCTC") = 2) Then
          .ListIndex = 3
        ElseIf (r.v(sRegPre & "CON_ADCTC") = 3) Then
          .ListIndex = 2
        End If

    End With

    With e("cboADCTC")
      If (bDisabled) Then
        .DisabledTooltipText = TOOLTIP_DISABLED_IF
        If (IsModuleDisabled(r, vIndex)) Then
          .DisabledTooltipText = .DisabledTooltipText & GetTooltipModuleDisabled(r, vIndex)
        End If
        If (r.v(sRegPre & "CTR0_MD") = 1) Then
          .DisabledTooltipText = .DisabledTooltipText & FormatDisabledTooltip("enhanced mode is selected")
        End If
        If (r.v(sRegPre & "CON1_ICST") = 1) Then
          .DisabledTooltipText = .DisabledTooltipText & _
            FormatDisabledTooltip("improved conversion and sample time control functionality is enabled")
        End If
      End If
    End With

    '-------------------------------------------------------------------------
    ' control: cboADCH (show)
    '-------------------------------------------------------------------------
    With c("cboADSTC")
        .Enabled = Not(bDisabled)
        c("lblADSTC").Enabled = Not(bDisabled)
        .Clear
        For i = 0 To 3
            uTime = ConvertTime(2^(3 + i) * r.v(sVArPre & "_Tbc_Factor") / GetADCModuleClock(r, vIndex))
            .AddItem "tbc * " & 2^(3 + i) & "   (tsc = " & Format(uTime.dValue, "0.000")  & " " & uTime.sUnit & ")"
        Next i
        .ListIndex = r.v(sRegPre & "CON_ADSTC")


    End With

    With e("cboADSTC")
      If (bDisabled) Then
        .DisabledTooltipText = e("cboADCTC").DisabledTooltipText
      End If
    End With

    '-------------------------------------------------------------------------
    ' control: cboADCH (show)
    '-------------------------------------------------------------------------
    With c("cboCON1_ADCTC")
        bDisabled = (IsModuleDisabled(r, vIndex) Or _
                     r.v(sRegPre & "CTR0_MD") = 1 Or _
                     r.v(sRegPre & "CON1_ICST") = 0)
        .Enabled = Not(bDisabled)
        c("lblCON1_ADCTC").Enabled = Not(bDisabled)

        .Clear
        For i = 0 To 63
            uTime = ConvertTime((i + 1) / GetADCModuleClock(r, vIndex))
            .AddItem "fcpu / " & i + 1 & " (tbc = " & Format(uTime.dValue, "0.000")  & " " & uTime.sUnit & ")"
        Next i
        .ListIndex = r.v(sRegPre & "CON1_ADCTC")

    End With

    With e("cboCON1_ADCTC")
      If (bDisabled) Then
        .DisabledTooltipText = TOOLTIP_DISABLED_IF
        If (IsModuleDisabled(r, vIndex)) Then
          .DisabledTooltipText = .DisabledTooltipText & GetTooltipModuleDisabled(r, vIndex)
        End If
        If (r.v(sRegPre & "CTR0_MD") = 1) Then
          .DisabledTooltipText = .DisabledTooltipText & FormatDisabledTooltip("enhanced mode is selected")
        End If
        If (r.v(sRegPre & "CON1_ICST") = 0) Then
          .DisabledTooltipText = .DisabledTooltipText & _
            FormatDisabledTooltip("improved conversion and sample time control functionality is disabled")
        End If
      End If
    End With

    '-------------------------------------------------------------------------
    ' control: cboADCH (show)
    '-------------------------------------------------------------------------
    With c("cboCON1_ADSTC")
        .Enabled = Not(bDisabled)
        c("lblCON1_ADSTC").Enabled = Not(bDisabled)
        .Clear
        For i = 0 To 63
            uTime = ConvertTime(4 * (i + 2) * _
                    (r.v(sRegPre & "CON1_ADCTC") + 1) / GetADCModuleClock(r, vIndex))
            .AddItem "tbc * " & 4 * (i + 2) & "   (ts = " & _
                     Format(uTime.dValue, "0.000")  & " " & uTime.sUnit & ")"
        Next i
        .ListIndex = r.v(sRegPre & "CON1_ADSTC")


    End With

    With e("cboCON1_ADSTC")
      If (bDisabled) Then
        .DisabledTooltipText = e("cboCON1_ADCTC").DisabledTooltipText
      End If
    End With

    '-------------------------------------------------------------------------
    ' control: cboADCH (show)
    '-------------------------------------------------------------------------
    With c("txtTime")
        If (r.v(sRegPre & "CON1_ICST") = 0) Then
'            uTime = ConvertTime(( 2^(3 + r.v(sRegPre & "CON_ADSTC")) * _
'                    r.v(sVArPre & "_Tbc_Factor") + r.v(sVArPre & "_Tbc_Factor") * _
'                    (48 - 8 * r.v(sRegPre & "CON1_RES")) + 2) / GetADCModuleClock(r, vIndex))
            uTime = ConvertTime(( 2^(3 + r.v(sRegPre & "CON_ADSTC")) * _
                    r.v(sVArPre & "_Tbc_Factor") + r.v(sVArPre & "_Tbc_Factor") * _
                    (48 - 8 * r.v(sRegPre & "CON1_RES")) + 6) / GetADCModuleClock(r, vIndex))
            .Caption = Format(uTime.dValue, "0.000")
            c("lblTime").Caption = "Complete time [" & uTime.sUnit & "]"
        Else
'            uTime = ConvertTime(( (4 * r.v(sRegPre & "CON1_ADSTC") + 56 - 8 * _
'                    r.v(sRegPre & "CON1_RES")) * (r.v(sRegPre & "CON1_ADCTC") + 1) + 2) _
'                    / GetADCModuleClock(r, vIndex))
            uTime = ConvertTime(( (4 * r.v(sRegPre & "CON1_ADSTC") + 56 - 8 * _
                    r.v(sRegPre & "CON1_RES")) * (r.v(sRegPre & "CON1_ADCTC") + 1) + 6) _
                    / GetADCModuleClock(r, vIndex))
            .Caption = Format(uTime.dValue, "0.000")
            c("lblTime").Caption = "Complete time [" & uTime.sUnit & "]"
        End If

    End With



  End If

  OnShowPageTimingControl = True
  Exit Function

OnShowPageTimingControlError:
  Errorbox "Can not show page", Err.Description
  Exit Function
End Function


