'#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: InjectionModeControl
    '-------------------------------------------------------------------------
    If (Not OnLoadPageInjectionModeControl(ps)) Then Exit Function

    '-------------------------------------------------------------------------
    ' end of pages
    '-------------------------------------------------------------------------
    
    '-------------------------------------------------------------------------
    ' call function to initialize the menu
    '-------------------------------------------------------------------------
    If (Not InitMenu(ps, "ADC\EnhancedControl")) 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: InjectionModeControl
    '-------------------------------------------------------------------------
    If (Not OnChangingPageInjectionModeControl (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: InjectionModeControl
    '-------------------------------------------------------------------------
    If (Not OnShowPageInjectionModeControl(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\EnhancedControl")) 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, 1950, _
      "Converter Resolution (RES)")) Then Exit Function
        If (Not ps.AddOption("optRES_1", 200, 600, 2900, 300, _
          "8-bit resolution")) Then Exit Function
        If (Not ps.AddOption("optRES_0", 200, 1300, 3025, 300, _
          "10-bit resolution")) Then Exit Function

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

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

    '-------------------------------
    ' frame: frmCTR2_ADCTC
    '-------------------------------
    If (Not ps.AddFrame("frmCTR2_ADCTC",100,3150, 4150, 1000, _
      "Conversion Time Control (ADCTC)")) Then Exit Function
        If (Not ps.AddCombo("cboCTR2_ADCTC", 1600, 400, 2400, 300)) Then Exit Function
        If (Not ps.AddLabel("lblCTR2_ADCTC", 0, 200, 400, 1275, 380, _
          "Conversion basic clock [tbc]")) Then Exit Function

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

    '-------------------------------
    ' frame: frmCTR2_ADSTC
    '-------------------------------
    If (Not ps.AddFrame("frmCTR2_ADSTC",100,4200, 4150, 1000, _
      "Sample Time Control (ADSTC)")) Then Exit Function
        If (Not ps.AddCombo("cboCTR2_ADSTC", 1600, 400, 2400, 300)) Then Exit Function
        If (Not ps.AddLabel("lblCTR2_ADSTC", 0, 200, 450, 1275, 300, _
          "Sample time [ts]")) Then Exit Function

    '-------------------------------
    ' frame: frmTime
    '-------------------------------
    If (Not ps.AddFrame("frmConfTime",4400,4200, 4150, 1000, _
      "Complete Conversion Time")) Then Exit Function
        If (Not ps.AddLabel("txtConfTime", 1, 1800, 400, 1000, 300)) Then Exit Function
        If (Not ps.AddLabel("lblConfTime", 0, 200, 450, 1500, 380, _
          "Complete time [xx]")) 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
  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 = "Control") Then
    '-------------------------------------------------------------------------
    ' page: Control (action)
    '-------------------------------------------------------------------------
    Select Case (Controlname)
        '---------------------------------------------------------------------
        ' control: optADM_0 (action)
        '---------------------------------------------------------------------
        Case "optADM_0"
            r.v(sRegPre & "CTR0_ADM") = 0

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

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

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

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

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

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

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

        '---------------------------------------------------------------------
        ' control: chkADWR (action)
        '---------------------------------------------------------------------
        Case "chkADWR"
            r.v(sRegPre & "CTR0_ADWR") = v(1)
            If (v(1) = 0) Then
                r.v(sRegPre & "CTR0_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: cboCTR2_ADCTC (action)
        '---------------------------------------------------------------------
        Case "cboCTR2_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 & "CTR2_ADCTC") = i
                End If
            Next i

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

      Case Else
    End Select

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

    sDisabledTooltip = ""
    sDisabledTooltip = TOOLTIP_DISABLED_IF

    If (IsModuleDisabled(r, vIndex)) Then
        sDisabledTooltip = sDisabledTooltip & GetTooltipModuleDisabled(r, vIndex)
    End If
    If (r.v(sRegPre & "CTR0_MD") = 0) Then
        sDisabledTooltip = sDisabledTooltip & FormatDisabledTooltip("compatibility 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, "")
    SetDefaultPropertiesForAllControlsOnFrame(c("frmCTR2_ADCTC").Caption, c, e, bDisabled, sDisabledTooltip, "")
    SetDefaultPropertiesForAllControlsOnFrame(c("frmCTR2_ADSTC").Caption, c, e, bDisabled, sDisabledTooltip, "")
    SetDefaultPropertiesForAllControlsOnFrame(c("frmConfTime").Caption, c, e, bDisabled, sDisabledTooltip, "")



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

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

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

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

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

    '-------------------------------------------------------------------------
    ' control: optRES_1 (show)
    '-------------------------------------------------------------------------
    With c("optRES_1")
        .Value = -CInt(r.v(sRegPre & "CTR2_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 & "CTR0_ADCH") = i) Then
                    .ListIndex = .ListCount - 1
                End If

            End If
        Next i

'        If (r.v(sRegPre & "CTR0_ADCH") > 7) Then
'            If (r.Exist(sVarPre & "_PARA_ANA_10")) Then
'                'for XC164CS
'                .ListIndex = r.v(sRegPre & "CTR0_ADCH") - 2
'            Else
'                'for XC161CJ
'                .ListIndex = r.v(sRegPre & "CTR0_ADCH") - 4
'            End If
'        Else
'            .ListIndex = r.v(sRegPre & "CTR0_ADCH")
'        End If

    End With

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

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

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

    '-------------------------------------------------------------------------
    ' control: cboADCH (show)
    '-------------------------------------------------------------------------
    With c("cboCTR2_ADCTC")
        .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 & "CTR2_ADCTC")

    End With

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


    End With

    '-------------------------------------------------------------------------
    ' control: cboADCH (show)
    '-------------------------------------------------------------------------
    With c("txtConfTime")
'        uTime = ConvertTime(( (4 * r.v(sRegPre & "CTR2_ADSTC") + 56 - 8 * _
'                r.v(sRegPre & "CTR2_RES")) * (r.v(sRegPre & "CTR2_ADCTC") + 1) + 2) _
'                / GetADCModuleClock(r, vIndex))
        uTime = ConvertTime(( (4 * r.v(sRegPre & "CTR2_ADSTC") + 56 - 8 * _
                r.v(sRegPre & "CTR2_RES")) * (r.v(sRegPre & "CTR2_ADCTC") + 1) + 6) _
                / GetADCModuleClock(r, vIndex))
        .Caption = Format(uTime.dValue, "0.000")
        c("lblConfTime").Caption = "Complete time [" & uTime.sUnit & "]"

    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 OnLoadPageInjectionModeControl(ps As Object) As Boolean
  On Error Resume Next

  OnLoadPageInjectionModeControl = False

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

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

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

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

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

    '-------------------------------
    ' frame: frmADCTS
    '-------------------------------
    If (Not ps.AddFrame("frmADCTS",100,1150, 4150, 1500, _
       "Channel Injection Trigger Input Select (ADCTS)")) Then Exit Function
        If (Not ps.AddOption("optADCTS_0", 200, 300, 3800, 380, _
           "No channel injection trigger input by hardware")) Then Exit Function
        If (Not ps.AddOption("optADCTS_1", 200, 650, 3800, 380, _
           "Trigger input CAPCOM1/2 selected")) Then Exit Function
        If (Not ps.AddOption("optADCTS_2", 200, 1000, 3800, 380, _
           "Trigger input CCU6 selected")) Then Exit Function

    '-------------------------------
    ' frame: frmRES
    '-------------------------------
    If (Not ps.AddFrame("frmCTR2IN_RES",4400,1150, 4150, 1500, _
      "Injection Converter Resolution (RES)")) Then Exit Function
        If (Not ps.AddOption("optCTR2IN_RES_1", 200, 400, 2900, 300, _
          "8-bit resolution")) Then Exit Function
        If (Not ps.AddOption("optCTR2IN_RES_0", 200, 900, 3025, 300, _
          "10-bit resolution")) Then Exit Function

    '-------------------------------
    ' frame: frmCTR2IN_ADCTC
    '-------------------------------
    If (Not ps.AddFrame("frmCTR2IN_ADCTC",100,2700, 4150, 1000, _
      "Injection Conversion Time Control (ADCTC)")) Then Exit Function
        If (Not ps.AddCombo("cboCTR2IN_ADCTC", 1600, 400, 2400, 300)) Then Exit Function
        If (Not ps.AddLabel("lblCTR2IN_ADCTC", 0, 200, 400, 1275, 380, _
          "Conversion basic clock [tbc]")) Then Exit Function

    '-------------------------------
    ' frame: frmCTR2IN_ADSTC
    '-------------------------------
    If (Not ps.AddFrame("frmCTR2IN_ADSTC",4400,2700, 4150, 1000, _
      "Injection Sample Time Control (ADSTC)")) Then Exit Function
        If (Not ps.AddCombo("cboCTR2IN_ADSTC", 1600, 400, 2400, 300)) Then Exit Function
        If (Not ps.AddLabel("lblCTR2IN_ADSTC", 0, 200, 450, 1275, 300, _
          "Sample time [ts]")) Then Exit Function

    '-------------------------------
    ' frame: frmCTR2INTime
    '-------------------------------
    If (Not ps.AddFrame("frmCTR2INTime",100,3750, 4150, 1000, _
      "Complete Injection Conversion Time")) Then Exit Function
        If (Not ps.AddLabel("txtCTR2INTime", 1, 1800, 400, 1000, 300)) Then Exit Function
        If (Not ps.AddLabel("lblCTR2INTime", 0, 200, 450, 1500, 380, _
          "Complete time [xx]")) Then Exit Function



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

  OnShowPageInjectionModeControl = 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 = "Injection Mode Control") Then
    '-------------------------------------------------------------------------
    ' page: InjectionModeControl (action)
    '-------------------------------------------------------------------------
    Select Case (Controlname)

        '---------------------------------------------------------------------
        ' control: chkCTR0_ADCIN (action)
        '---------------------------------------------------------------------
        Case "chkCTR0_ADCIN"
            r.v(sRegPre & "CTR0_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: optCTR2IN_RES_0 (action)
        '---------------------------------------------------------------------
        Case "optCTR2IN_RES_0"
            r.v(sRegPre & "CTR2IN_RES") = 0

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

        '---------------------------------------------------------------------
        ' 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

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

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

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

        
        '---------------------------------------------------------------------
        ' control: cboCTR2IN_ADCTC (action)
        '---------------------------------------------------------------------
        Case "cboCTR2IN_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 & "CTR2IN_ADCTC") = i
                End If
            Next i

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

      Case Else
    End Select

    '-------------------------------------------------------------------------
    ' page: InjectionModeControl (show)
    '-------------------------------------------------------------------------
    bDisabled = (IsModuleDisabled(r, vIndex) Or r.v(sRegPre & "CTR0_MD") = 0 Or _
                 r.v(sRegPre & "CTR0_ADWR") = 0)

    sDisabledTooltip = ""
    sDisabledTooltip = TOOLTIP_DISABLED_IF

    If (IsModuleDisabled(r, vIndex)) Then
        sDisabledTooltip = sDisabledTooltip & GetTooltipModuleDisabled(r, vIndex)
    End If
    If (r.v(sRegPre & "CTR0_MD") = 0) Then
        sDisabledTooltip = sDisabledTooltip & _
          FormatDisabledTooltip("compatibility mode is selected")
    End If
    If (r.v(sRegPre & "CTR0_ADWR") = 0) Then
        sDisabledTooltip = sDisabledTooltip & _
          FormatDisabledTooltip("'wait for read mode' is disabled")
    End If

    SetDefaultPropertiesForAllControlsOnFrame(c("frmCTR0_ADCIN").Caption, c, e, bDisabled, sDisabledTooltip, "")

    '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    bDisabled = bDisabled Or r.v(sRegPre & "CTR0_ADCIN") = 0

    If (r.v(sRegPre & "CTR0_ADCIN") = 0) Then
        sDisabledTooltip = sDisabledTooltip & _
          FormatDisabledTooltip("channel injection is disabled")
    End If

    SetDefaultPropertiesForAllControlsOnFrame(c("frmCHNR").Caption, c, e, bDisabled, sDisabledTooltip, "")
    SetDefaultPropertiesForAllControlsOnFrame(c("frmCTR2IN_RES").Caption, c, e, bDisabled, sDisabledTooltip, "")
    SetDefaultPropertiesForAllControlsOnFrame(c("frmADCTS").Caption, c, e, bDisabled, sDisabledTooltip, "")
    SetDefaultPropertiesForAllControlsOnFrame(c("frmCTR2IN_ADCTC").Caption, c, e, bDisabled, sDisabledTooltip, "")
    SetDefaultPropertiesForAllControlsOnFrame(c("frmCTR2IN_ADSTC").Caption, c, e, bDisabled, sDisabledTooltip, "")
    SetDefaultPropertiesForAllControlsOnFrame(c("frmCTR2INTime").Caption, c, e, bDisabled, sDisabledTooltip, "")

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

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

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

    '-------------------------------------------------------------------------
    ' control: cboCHNR (show)
    '-------------------------------------------------------------------------
    With c("cboCHNR")
        .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

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

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

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

    '-------------------------------------------------------------------------
    ' control: cboCTR2IN_ADCH (show)
    '-------------------------------------------------------------------------
    With c("cboCTR2IN_ADCTC")
        .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 & "CTR2IN_ADCTC")

    End With

    '-------------------------------------------------------------------------
    ' control: cboCTR2IN_ADCH (show)
    '-------------------------------------------------------------------------
    With c("cboCTR2IN_ADSTC")
        .Clear
        For i = 0 To 63
            uTime = ConvertTime(4 * (i + 2) * _
                    (r.v(sRegPre & "CTR2IN_ADCTC") + 1) / GetADCModuleClock(r, vIndex))
            .AddItem "tbc * " & 4 * (i + 2) & "   (ts = " & _
                     Format(uTime.dValue, "0.000")  & " " & uTime.sUnit & ")"
        Next i
        .ListIndex = r.v(sRegPre & "CTR2IN_ADSTC")


    End With

    '-------------------------------------------------------------------------
    ' control: txtCTR2INTime (show)
    '-------------------------------------------------------------------------
    With c("txtCTR2INTime")
'        uTime = ConvertTime(( (4 * r.v(sRegPre & "CTR2IN_ADSTC") + 56 - 8 * _
'                r.v(sRegPre & "CTR2IN_RES")) * (r.v(sRegPre & "CTR2IN_ADCTC") + 1) + 2) _
'                / GetADCModuleClock(r, vIndex))
        uTime = ConvertTime(( (4 * r.v(sRegPre & "CTR2IN_ADSTC") + 56 - 8 * _
                r.v(sRegPre & "CTR2IN_RES")) * (r.v(sRegPre & "CTR2IN_ADCTC") + 1) + 6) _
                / GetADCModuleClock(r, vIndex))
        .Caption = Format(uTime.dValue, "0.000")
        c("lblCTR2INTime").Caption = "Complete time [" & uTime.sUnit & "]"

    End With



  End If

  OnShowPageInjectionModeControl = True
  Exit Function

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


