'#Uses "..\classes\FunctionClass.cls
'#Uses "..\classes\ParameterClass.cls"
'#Uses "..\classes\Port.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:
'        IO
'-----------------------------------------------------------------------------
' Description:
'        This file contains functions which are create controls for this
'        module.
'        Furthermore it contains functions for the event handling of
'        these controls.
'
'-----------------------------------------------------------------------------
' History:
'                 
'        Rev. Author         Date        Description
'        ---------------------------------------------------------------------
'        1.0  ste            02.02.00    final version for TC10GP
'             ste            23.02.00    many changes for TC1775
'             
'        ---------------------------------------------------------------------
'        3.0  ahe            02.02.00    final version for C161PI
'                                        - added page 'DriverControl'
'
'        ---------------------------------------------------------------------
'        4.0  ahe            23.05.01    final version for Pegasus
'                                         - no changes
'
'*****************************************************************************


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

    '-------------------------------------------------------------------------
    ' page: TemperatureCompensation
    '-------------------------------------------------------------------------
    If (Not OnLoadPageTemperatureCompensation(ps)) Then Exit Function

    '-------------------------------------------------------------------------
    ' page: DriverControl
    '-------------------------------------------------------------------------
    If (Not OnLoadPageDriverControl(ps)) Then Exit Function

    '-------------------------------------------------------------------------
    ' page: Functions
    '-------------------------------------------------------------------------
    If (Not OnLoadPageFunctions(ps)) Then Exit Function

    '-------------------------------------------------------------------------
    ' page: Parameters
    '-------------------------------------------------------------------------
    If (Not OnLoadPageParameters(ps)) Then Exit Function

    '-------------------------------------------------------------------------
    ' page: Notes
    '-------------------------------------------------------------------------
    If (Not OnLoadPageNotes(ps)) Then Exit Function

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

    
    '-------------------------------------------------------------------------
    'page: Ports
    '-------------------------------------------------------------------------
    If (Not OnChangingPagePorts (ps, Pagename)) Then Exit Function

    '-------------------------------------------------------------------------
    'page: TemperatureCompensation
    '-------------------------------------------------------------------------
    If (Not OnChangingPageTemperatureCompensation (ps, Pagename)) Then Exit Function

    '-------------------------------------------------------------------------
    'page: DriverControl
    '-------------------------------------------------------------------------
    If (Not OnChangingPageDriverControl (ps, Pagename)) Then Exit Function

    '-------------------------------------------------------------------------
    'page: Functions
    '-------------------------------------------------------------------------
    If (Not OnChangingPageFunctions (ps, Pagename)) Then Exit Function

    '-------------------------------------------------------------------------
    'page: Parameters
    '-------------------------------------------------------------------------
    If (Not OnChangingPageParameters (ps, Pagename)) Then Exit Function

    '-------------------------------------------------------------------------
    'page: Notes
    '-------------------------------------------------------------------------
    If (Not OnChangingPageNotes (ps, Pagename, "IO")) 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


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

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

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

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

    '-------------------------------------------------------------------------
    ' page: Parameters
    '-------------------------------------------------------------------------
    If (Not OnShowPageParameters(ps, Controlname, Pagename, v)) Then Exit Function 
       
    '-------------------------------------------------------------------------
    ' page: Notes
    '-------------------------------------------------------------------------
    If (Not OnShowPageNotes(ps, Controlname, Pagename, "IO", 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

    Dim np As Peripherals
    Dim cPorts As DAvECollection
    Dim Port As Port

    OnClick = False

    Set cPorts = ObjectCollection("PORTS")

    '------------------------------------------------------------------------------------
    ' button: cmdPx
    '------------------------------------------------------------------------------------
    For Each Port In cPorts
      If (Controlname = "cmdP" & Port.PortNumber) Then
          Set np = New Peripherals
          np.Parameters.Add ("PortIndex")
          np.Parameters.v("PortIndex") = Port.PortNumber
          np.Parameters.Add ("FirstPinIndex")
          np.Parameters.v("FirstPinIndex") = Port.FirstPin
          np.Parameters.Add ("LastPinIndex")
          np.Parameters.v("LastPinIndex") = Port.LastPin
          Set np.ProjectData = ps.ProjectData
          np.Caption = "Configure " & Port.Description
          np.Scriptfile = DefGetPath("Scripts") & "IO\PORTx.DAVE"
          np.Show

      		' only for the test scripts
          On Error Resume Next
       		ObjectCollection.Add np, "IO_Port" & Port.PortNumber
          On Error GoTo OnClickError
      End If
    Next Port

    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, "IO\Dialog")) 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 a page.
'-----------------------------------------------------------------------------
'Parameters:
'        ps               reference to the Peripherals object
'-----------------------------------------------------------------------------
'Returns:
'        True            on successfull operation
'        False           else
'*****************************************************************************
Public Function OnLoadPagePorts(ps As Peripherals) As Boolean
  On Error Resume Next
  OnLoadPagePorts = False

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

  OnLoadPagePorts = 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
'*****************************************************************************
Public Function OnChangingPagePorts(ps As Peripherals, Pagename As String) As Boolean
  On Error Resume Next
  OnChangingPagePorts = False

  Dim cPorts As DAvECollection
  Dim Port As Port
  Dim i As Integer

  Set cPorts = ObjectCollection("PORTS")

  If (Not ps.ExtenderCollection("Ports").Loaded) And (Pagename = "Ports") Then
    ps.ExtenderCollection("Ports").Loaded = True
    If (Not ps.AddFrame("fraIO1", 100, 100, 8400, 4000, "")) Then Exit Function 
      i = 0 
      For Each Port In cPorts
        If (Not ps.AddButton("cmdP" & Port.PortNumber, 200 + 4300 * (i MOD 2), 400 + (i \ 2) * 600, 3600, 350, "Configure " & Port.Description)) Then Exit Function
        i = i + 1
      Next Port
  End if

  OnChangingPagePorts = 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 OnShowPagePorts(ps As Peripherals, Controlname As String, Pagename As String, v() As Variant) As Boolean
  On Error Goto OnShowPagePortsError

  OnShowPagePorts = False
     
  Dim r As ProjectData
  Dim c As Collection
  Dim e As Collection
  Dim bDisabled As Boolean

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

  If (Pagename = "Ports") Then
    '-------------------------------------------------------------------------
    ' page: Ports (action)
    '-------------------------------------------------------------------------
'    Select Case Controlname

'    End Select     ' case Controlname    

    '-------------------------------------------------------------------------
    ' page: Ports (show)
    '-------------------------------------------------------------------------

  End If         ' if Pagename

  OnShowPagePorts = True
  Exit Function

OnShowPagePortsError:
  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 'TemperatureCompensation'.
'-----------------------------------------------------------------------------
'Parameters:
'        ps               reference to the Peripherals object
'-----------------------------------------------------------------------------
'Returns:
'        True            on successfull operation
'        False           else
'*****************************************************************************
Public Function OnLoadPageTemperatureCompensation(ps As Object) As Boolean
  On Error Resume Next

  OnLoadPageTemperatureCompensation = False

  Dim r As Object
  Set r = ps.ProjectData

  If (r.Regs.SfrFind("PTCR") = True) Then
    If (Not ps.AddPage("Temperature Compensation")) Then Exit Function
  End If

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

  OnChangingPageTemperatureCompensation = False
  
  Dim r As Object
  Set r = ps.ProjectData

  If (r.Regs.SfrFind("PTCR") = True) Then
    If (Not ps.ExtenderCollection("Temperature Compensation").Loaded) And (Pagename = "Temperature Compensation") Then
      ps.ExtenderCollection("Temperature Compensation").Loaded = True
   
      '-------------------------------
      ' frame: frmTCS
      '-------------------------------
      If (Not ps.AddFrame("frmTCS",100,100, 6000, 1200, "Temperature Compensation Source (TCS)")) Then Exit Function
          If (Not ps.AddOption("optTCS_0", 200, 300, 4900, 380, "Port logic is controlled by the temperature compensation sensor")) Then Exit Function
          If (Not ps.AddOption("optTCS_1", 200, 700, 4900, 380, "Port logic is controlled by software via bitfield TCC")) Then Exit Function

      '-------------------------------
      ' frame: frmTCC
      '-------------------------------
      If (Not ps.AddFrame("frmTCC",100,1500, 6000, 1200, "Temperature Compensation Control (TCC)")) Then Exit Function
          If (Not ps.AddLabel("lblTCC", 0, 200, 525, 1800, 300, "Compensation mode")) Then Exit Function
          If (Not ps.AddCombo("cboTCC", 2250, 500, 2600, 315)) Then Exit Function

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

  OnShowPageTemperatureCompensation = False

  Dim r As Object
  Dim c As Object
  Dim e As Object
  Dim bDisabled As Boolean

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

  If (Pagename = "Temperature Compensation") Then
    If (r.Regs.SfrFind("PTCR") = True) Then
      '-------------------------------------------------------------------------
      ' page: TemperatureCompensation (action)
      '-------------------------------------------------------------------------
      Select Case (Controlname)
          '---------------------------------------------------------------------
          ' control: optTCS_0 (action)
          '---------------------------------------------------------------------
          Case "optTCS_0"
               r.v("PTCR_TCS") = 0

          '---------------------------------------------------------------------
          ' control: optTCS_1 (action)
          '---------------------------------------------------------------------
          Case "optTCS_1"
               r.v("PTCR_TCS") = 1

          '---------------------------------------------------------------------
          ' control: cboTCC (action)
          '---------------------------------------------------------------------
          Case "cboTCC"
                Select Case v(1)
                  Case "low (-40C to 0C)"
                     r.v("PTCR_TCC") = 0
                  Case "normal (0C to 60C)"
                     r.v("PTCR_TCC") = 1
                  Case "high (60C to 125C)"
                     r.v("PTCR_TCC") = 2
                  Case "very high (125C to 150C)"
                     r.v("PTCR_TCC") = 3
                End Select


        Case Else
      End Select

      '-------------------------------------------------------------------------
      ' page: TemperatureCompensation (show)
      '-------------------------------------------------------------------------

      '-------------------------------------------------------------------------
      ' control: optTCS_0 (show)
      '-------------------------------------------------------------------------
      With c("optTCS_0")
        ' disable condition
        bDisabled = False
        .Enabled = Not(bDisabled)
        ' show condition
        .Value = (r.v("PTCR_TCS") = 0)
      End With

      With e("optTCS_0")
        If (bDisabled) Then
          ' tooltip if control is disabled 
  '        .DisabledTooltipText = TOOLTIP_DISABLED_IF
        Else
          ' tooltip if control is enabled 
        End If
      End With

      '-------------------------------------------------------------------------
      ' control: optTCS_1 (show)
      '-------------------------------------------------------------------------
      With c("optTCS_1")
        ' disable condition
        bDisabled = False
        .Enabled = Not(bDisabled)
        ' show condition
        .Value = (r.v("PTCR_TCS") = 1)
      End With

      With e("optTCS_1")
        If (bDisabled) Then
          ' tooltip if control is disabled 
  '        .DisabledTooltipText = TOOLTIP_DISABLED_IF
        Else
          ' tooltip if control is enabled 
        End If
      End With

      '-------------------------------------------------------------------------
      ' control: cboTCC (show)
      '-------------------------------------------------------------------------
      With c("cboTCC")
        ' disable condition
        bDisabled = r.v("PTCR_TCS") = 0
        .Enabled = Not(bDisabled)
        .Clear
        .AddItem "low (-40C to 0C)"
        .AddItem "normal (0C to 60C)"
        .AddItem "high (60C to 125C)"
        .AddItem "very high (125C to 150C)"
        .ListIndex = r.v("PTCR_TCC")
      End With

      With e("cboTCC")
        If (bDisabled) Then
          ' tooltip if control is disabled 
          .DisabledTooltipText = TOOLTIP_DISABLED_IF
          .DisabledTooltipText = .DisabledTooltipText & FormatDisabledTooltip("the port logic is controlled by the temperature compensation sensor")
        Else
          ' tooltip if control is enabled 
        End If
      End With

      '-------------------------------------------------------------------------
      ' control: lblTCC (show)
      '-------------------------------------------------------------------------
      With c("lblTCC")
        ' same as for cboTCC
        .Enabled = Not(bDisabled)
      End With

      With e("lblTCC")
        If (bDisabled) Then
          ' tooltip if control is disabled 
          .DisabledTooltipText = e("cboTCC").DisabledTooltipText
        Else
          ' tooltip if control is enabled 
        End If
      End With
    End If
  End If

  OnShowPageTemperatureCompensation = True
  Exit Function

OnShowPageTemperatureCompensationError:
  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 'DriverControl'.
'-----------------------------------------------------------------------------
'Parameters:
'        ps               reference to the Peripherals object
'-----------------------------------------------------------------------------
'Returns:
'        True            on successfull operation
'        False           else
'*****************************************************************************
Public Function OnLoadPageDriverControl(ps As Object) As Boolean
  On Error Resume Next

  OnLoadPageDriverControl = False

  Dim r As Object
  Set r = ps.ProjectData

  If (r.Regs.SfrFind("PDCR") = True) Then
    If (Not ps.AddPage("Driver Control")) Then Exit Function
  End If

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

  OnChangingPageDriverControl = False
  
  Dim r As Object
  Set r = ps.ProjectData

  If (r.Regs.SfrFind("PDCR") = True) Then
    If (Not ps.ExtenderCollection("Driver Control").Loaded) And (Pagename = "Driver Control") Then
      ps.ExtenderCollection("Driver Control").Loaded = True
   
      '-------------------------------
      ' frame: frmBIPEC
      '-------------------------------
      If (Not ps.AddFrame("frmBIPEC",100,100, 6000, 2000, "Bus Interface Pins Edge Characteristic (BIPEC)")) Then Exit Function
          If (Not ps.AddOption("optBIPEC_0", 200, 300, 4900, 380, "Fast edge mode, rise/fall times depend on the drivers dimensioning.")) Then Exit Function
          If (Not ps.AddOption("optBIPEC_1", 200, 700, 4900, 380, "Reduced edge mode.")) Then Exit Function
          If (Not ps.AddLabel("lblBIPEC_Info", 0, 200, 1100, 4900, 2000, "Note:" & vbcrlf & "This setting is valid only for ports P0, P1, P4 and pins P6.4-0, #RD, #WR, ALE, CLKOUT, #BHE/#WRH and #READY" & vbcrlf & "(emulation mode only).")) Then Exit Function

      '-------------------------------
      ' frame: frmNBPEC
      '-------------------------------
      If (Not ps.AddFrame("frmNBPEC",100,2200, 6000, 2000, "Non-Bus Pins Edge Characteristic (NBPEC)")) Then Exit Function
          If (Not ps.AddOption("optNBPEC_0", 200, 300, 4900, 380, "Fast edge mode, rise/fall times depend on the drivers dimensioning.")) Then Exit Function
          If (Not ps.AddOption("optNBPEC_1", 200, 700, 4900, 380, "Reduced edge mode.")) Then Exit Function
          If (Not ps.AddLabel("lblNBPEC_Info", 0, 200, 1100, 4900, 2000, "Note:" & vbcrlf & "This setting is valid only for ports P2, P3 and pins P6.7-5 and #RSTOUT")) Then Exit Function

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

  OnShowPageDriverControl = False

  Dim r As Object
  Dim c As Object
  Dim e As Object
  Dim bDisabled As Boolean

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

  If (Pagename = "Driver Control") Then
    If (r.Regs.SfrFind("PDCR") = True) Then
      '-------------------------------------------------------------------------
      ' page: DriverControl (action)
      '-------------------------------------------------------------------------
      Select Case (Controlname)
          '---------------------------------------------------------------------
          ' control: optBIPEC_0 (action)
          '---------------------------------------------------------------------
          Case "optBIPEC_0"
               r.v("PDCR_BIPEC") = 0

          '---------------------------------------------------------------------
          ' control: optBIPEC_1 (action)
          '---------------------------------------------------------------------
          Case "optBIPEC_1"
               r.v("PDCR_BIPEC") = 1

          '---------------------------------------------------------------------
          ' control: optNBPEC_0 (action)
          '---------------------------------------------------------------------
          Case "optNBPEC_0"
               r.v("PDCR_NBPEC") = 0

          '---------------------------------------------------------------------
          ' control: optNBPEC_1 (action)
          '---------------------------------------------------------------------
          Case "optNBPEC_1"
               r.v("PDCR_NBPEC") = 1

        Case Else
      End Select

      '-------------------------------------------------------------------------
      ' page: DriverControl (show)
      '-------------------------------------------------------------------------

      '-------------------------------------------------------------------------
      ' control: optBIPEC_0 (show)
      '-------------------------------------------------------------------------
      With c("optBIPEC_0")
        ' disable condition
        bDisabled = False
        .Enabled = Not(bDisabled)
        ' show condition
        .Value = (r.v("PDCR_BIPEC") = 0)
      End With

      With e("optBIPEC_0")
        If (bDisabled) Then
          ' tooltip if control is disabled 
  '        .DisabledTooltipText = TOOLTIP_DISABLED_IF
        Else
          ' tooltip if control is enabled 
        End If
      End With

      '-------------------------------------------------------------------------
      ' control: optBIPEC_1 (show)
      '-------------------------------------------------------------------------
      With c("optBIPEC_1")
        ' disable condition
        bDisabled = False
        .Enabled = Not(bDisabled)
        ' show condition
        .Value = (r.v("PDCR_BIPEC") = 1)
      End With

      With e("optBIPEC_1")
        If (bDisabled) Then
          ' tooltip if control is disabled 
  '        .DisabledTooltipText = TOOLTIP_DISABLED_IF
        Else
          ' tooltip if control is enabled 
        End If
      End With

      '-------------------------------------------------------------------------
      ' control: optNBPEC_0 (show)
      '-------------------------------------------------------------------------
      With c("optNBPEC_0")
        ' disable condition
        bDisabled = False
        .Enabled = Not(bDisabled)
        ' show condition
        .Value = (r.v("PDCR_NBPEC") = 0)
      End With

      With e("optNBPEC_0")
        If (bDisabled) Then
          ' tooltip if control is disabled 
  '        .DisabledTooltipText = TOOLTIP_DISABLED_IF
        Else
          ' tooltip if control is enabled 
        End If
      End With

      '-------------------------------------------------------------------------
      ' control: optNBPEC_1 (show)
      '-------------------------------------------------------------------------
      With c("optNBPEC_1")
        ' disable condition
        bDisabled = False
        .Enabled = Not(bDisabled)
        ' show condition
        .Value = (r.v("PDCR_NBPEC") = 1)
      End With

      With e("optNBPEC_1")
        If (bDisabled) Then
          ' tooltip if control is disabled 
  '        .DisabledTooltipText = TOOLTIP_DISABLED_IF
        Else
          ' tooltip if control is enabled 
        End If
      End With

    End If
  End If

  OnShowPageDriverControl = True
  Exit Function

OnShowPageDriverControlError:
  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 a page.
'-----------------------------------------------------------------------------
'Parameters:
'        ps               reference to the Peripherals object
'-----------------------------------------------------------------------------
'Returns:
'        True            on successfull operation
'        False           else
'*****************************************************************************
Public Function OnLoadPageFunctions(ps As Peripherals) As Boolean
  On Error Resume Next
  OnLoadPageFunctions = False

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

  OnLoadPageFunctions = 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
'*****************************************************************************
Public Function OnChangingPageFunctions(ps As Peripherals, Pagename As String) As Boolean
  On Error Resume Next
  OnChangingPageFunctions = False

  Dim i As Integer
  Dim aFunctions(1 To 16) As String


  If (Not ps.ExtenderCollection("Functions").Loaded) And (Pagename = "Functions") Then
    ps.ExtenderCollection("Functions").Loaded = True
 
    '-------------------------------
    ' frame: fra_Init
    '-------------------------------
    If (Not ps.AddFrame("fra_Init", 100 , 100, 4150, 700, CAPTION_FRA_USE_INIT)) Then Exit Function
      If (Not ps.AddCheck("chk_Init", 150, 275, 200, 275, "")) Then Exit Function
      If (Not ps.AddText("txt_Init", 400, 250, 3600, 325)) Then Exit Function

    '-------------------------------
    ' frame: fra_CFile
    '-------------------------------
    If (Not ps.AddFrame("fra_CFile", 4400 , 100, 4150, 700, CAPTION_FRA_CFILE)) Then Exit Function
      If (Not ps.AddText("txt_CFile", 1475, 250, 2500, 325)) Then Exit Function
      If (Not ps.AddLabel("lbl_CFile", 0, 200, 300, 1250, 275, CAPTION_LBL_CFILE)) Then Exit Function


    '-------------------------------
    ' frame: fraLibrary1
    '-------------------------------
    ' initialize the array
    For i = Lbound(aFunctions) To Ubound(aFunctions)
      aFunctions(i) = ""
    Next i

    ' Functions and Macros
    i = Lbound(aFunctions)

    aFunctions(i) = "ReadPin"
    i = i + 1
    aFunctions(i) = "SetPin"
    i = i + 1
    aFunctions(i) = "ResetPin"
    i = i + 1
    aFunctions(i) = "TogglePin"
    i = i + 1
    aFunctions(i) = "SetPinIn"
    i = i + 1
    aFunctions(i) = "SetPinOut"
    i = i + 1
    aFunctions(i) = "ReadPort"
    i = i + 1
    aFunctions(i) = "WritePort"
    i = i + 1
    aFunctions(i) = "SetPortIn"
    i = i + 1
    aFunctions(i) = "SetPortOut"
    i = i + 1

    If (Not ps.AddFrame("fraLibrary1", 100 , 900, 4150, 4400, CAPTION_FRA_LIBRARY1)) Then Exit Function
      For i = Lbound(aFunctions) To Ubound(aFunctions)
        If Not(aFunctions(i) = "") Then
          If (Not ps.AddCheck("chk_" & aFunctions(i), 150, 325 + (300 * (i - 1)), 200, 275, "")) Then Exit Function
          If (Not ps.AddText("txt_" & aFunctions(i), 400, 300 + (300 * (i - 1)), 3600, 325)) Then Exit Function
        End If
      Next i


 
  End If

  OnChangingPageFunctions = 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 OnShowPageFunctions(ps As Peripherals, Controlname As String, Pagename As String, v() As Variant) As Boolean
  On Error Goto OnShowPageFunctionsError 
  OnShowPageFunctions = False

  Dim r As ProjectData
  Dim c As Collection
  Dim e As Collection
  Dim bDisabled As Boolean
  Dim func As FunctionClass
  Dim sName As String
  Dim vIndex As Variant
  Dim sIndex As String
  Dim sPrefix As String
  Dim l As Long
  Dim d As String
  Dim a(0 To 9) As String
  Dim i As Integer

  vIndex = ps.Parameters.v("x")   
  sIndex = "IO" & vIndex
  sPrefix = sIndex & "_"

  Set r = ps.ProjectData
  Set c = ps.ControlCollection
  Set e = ps.ExtenderCollection
  Set func = New FunctionClass

  If (Pagename = "Functions") Then

    '-------------------------------------------------------------------------
    ' page: Functions (action)
    '-------------------------------------------------------------------------
    RefreshFunctionDescriptions(vIndex, func, r)

    If (Not UpdateFunction(sIndex, ps, func , Controlname, v)) Then Exit Function

    '-------------------------------------------------------------------------
    ' page: Functions (show)
    '-------------------------------------------------------------------------
    Set func = New FunctionClass
    RefreshFunctionDescriptions(vIndex, func, r)

    sName = sIndex & "_NAME_Init" 
    ' disable condition
    func.Disabled(sName) = False
    ' tooltip if control is disabled
    d = GetDisabledFunctionToolTipEntry(func, sName)
    func.DisabledToolTip(sName) = d


    a(0) = sIndex & "_NAME_ReadPin"
    a(1) = sIndex & "_NAME_SetPin" 
    a(2) = sIndex & "_NAME_ResetPin"
    a(3) = sIndex & "_NAME_TogglePin"
    a(4) = sIndex & "_NAME_SetPinIn"
    a(5) = sIndex & "_NAME_SetPinOut"
    a(6) = sIndex & "_NAME_ReadPort"
    a(7) = sIndex & "_NAME_WritePort"
    a(8) = sIndex & "_NAME_SetPortIn"
    a(9) = sIndex & "_NAME_SetPortOut"


    For i = 0 To 9
      sName = a(i)
      ' disable condition
      func.Disabled(sName) = r.v(sPrefix & "USE_Init") = 0
      ' tooltip if control is disabled
      d = GetDisabledFunctionToolTipEntry(func, sName)
      If (r.v(sPrefix & "USE_Init") = 0) Then 
        d = d & FormatDisabledToolTip(TOOLTIP_DESELECTED_INIT)
      End If
      func.DisabledToolTip(sName) = d
    Next i

    If (Not ShowFunction(sIndex, ps, func , v)) Then Exit Function

  End If         ' if Pagename

  OnShowPageFunctions = True
  Exit Function

OnShowPageFunctionsError:
  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 a page.
'-----------------------------------------------------------------------------
'Parameters:
'        ps               reference to the Peripherals object
'-----------------------------------------------------------------------------
'Returns:
'        True            on successfull operation
'        False           else
'*****************************************************************************
Public Function OnLoadPageParameters(ps As Peripherals) As Boolean
  On Error Resume Next
  OnLoadPageParameters = False

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

  OnLoadPageParameters = 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
'*****************************************************************************
Public Function OnChangingPageParameters(ps As Peripherals, Pagename As String) As Boolean
  On Error Resume Next

  OnChangingPageParameters = False

  If (Not ps.ExtenderCollection("Parameters").Loaded) And (Pagename = "Parameters") Then
    ps.ExtenderCollection("Parameters").Loaded = True

    '-------------------------------------------------------------------------
    ' frame: frmCAPCOMx_HFile
    '-------------------------------------------------------------------------
    If (Not ps.AddFrame("fra_HFile", 100 , 100, 4150, 700, CAPTION_FRA_HFILE)) Then Exit Function
      If (Not ps.AddText("txt_HFile", 1475, 250, 2500, 325)) Then Exit Function
      If (Not ps.AddLabel("lbl_HFile",0 , 200 , 300, 1250, 300, CAPTION_LBL_HFILE)) Then Exit Function   
    

  End If

  OnChangingPageParameters = 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 OnShowPageParameters(ps As Peripherals, Controlname As String, Pagename As String, v() As Variant) As Boolean
  On Error Goto OnShowPageParametersError
  OnShowPageParameters = False
    
  Dim r As ProjectData
  Dim c As Collection
  Dim e As Collection
  Dim para As ParameterClass
  Dim vIndex As Variant
  Dim sVarPre As String
  Dim sRegPre As String
  Dim sName As String
  Dim d As String
  Dim i As Integer

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

  Set r = ps.ProjectData
  Set c = ps.ControlCollection
  Set e = ps.ExtenderCollection
  Set para = New ParameterClass


  If (Pagename = "Parameters") Then

    '-------------------------------------------------------------------------
    ' page: Parameters (action)
    '-------------------------------------------------------------------------
    RefreshParameterDescriptions(vIndex, para, r)

    If (Not UpdateParameter(sVarPre, ps, para , Controlname, v)) Then Exit Function


    '-------------------------------------------------------------------------
    ' page: Parameters (show)
    '-------------------------------------------------------------------------

    Set para = New ParameterClass
    RefreshParameterDescriptions(vIndex, para, r)

'    sName = sIndex & "_PARA_T2" 
'    ' disable condition
'    para.Disabled(sName) = True
'    ' tooltip if control is disabled
'    d = FormatDisabledTooltip("Fred ...")
'    para.DisabledToolTip(sName) = d

    If (Not ShowParameter(sVarPre, ps, para, v)) Then Exit Function

  End If         ' if Pagename

  OnShowPageParameters = True
  Exit Function

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

