'#Uses "..\include\Const.inc"
'#Uses "..\include\Format.inc"
'#Uses "..\include\Global.inc"
'#Uses "..\include\Pages.inc"
'#Uses "..\classes\PortPin.cls"
'#Uses "..\include\Port.inc"
Option Explicit
'*****************************************************************************
' Module:
'        Portx
'-----------------------------------------------------------------------------
' Description:
'        This file contains functions which are create controls for the
'        module 'Portx'.
'        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
'        1.1  ste            23.02.00    many changes for TC1775
'        1.2  ahe            19.07.00    template
'        1.3  ahe            21.07.00    InOut position auf 450
'
'        ---------------------------------------------------------------------
'        3.0  ahe            02.02.00    final version for C161PI
'                                        - changes 'As PortPin' to 'As Object'
'
'        ---------------------------------------------------------------------
'        4.0  ahe            23.05.01    final version for Pegasus
'                                         - changes pages OutputCharacteristic
'
'*****************************************************************************


'*****************************************************************************
'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: Portx
    '------------------------------------------------------------------------------------
    If (Not OnLoadPagePortx(ps)) Then Exit Function
    '------------------------------------------------------------------------------------
    ' page: Input Characteristic
    '------------------------------------------------------------------------------------
    If (Not OnLoadPageInputCharacteristic(ps)) Then Exit Function
    '------------------------------------------------------------------------------------
    ' page: Output Characteristic
    '------------------------------------------------------------------------------------
    If (Not OnLoadPageOutputCharacteristic(ps)) Then Exit Function
    '------------------------------------------------------------------------------------
    ' page: Pad Hold
    '------------------------------------------------------------------------------------
    If (Not OnLoadPagePadHold(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\Port" & ps.Parameters.v("PortIndex"))) Then Exit Function

    OnLoad = True
    Exit Function

OnLoadError:
    Exit Function
End Function


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

    Dim vPort As Variant

    OnChangingPage = False

    vPort = ps.Parameters.v("PortIndex")

    '---------------------------------------------------------
    'page: Portx
    '---------------------------------------------------------
    If (Not OnChangingPagePortx(ps, Pagename)) Then Exit Function
    '---------------------------------------------------------
    'page: Input Characteristic
    '---------------------------------------------------------
    If (Not OnChangingPageInputCharacteristic(ps, Pagename)) Then Exit Function
    '---------------------------------------------------------
    'page: Output Characteristic
    '---------------------------------------------------------
    If (Not OnChangingPageOutputCharacteristic(ps, Pagename)) Then Exit Function
    '---------------------------------------------------------
    'page: Pad Hold
    '---------------------------------------------------------
    If (Not OnChangingPagePadHold(ps, Pagename)) Then Exit Function
    '---------------------------------------------------------
    'page: Parameters
    '---------------------------------------------------------
    If (Not OnChangingPageParameters(ps, Pagename)) Then Exit Function
    '---------------------------------------------------------
    'page: Notes
    '---------------------------------------------------------
    If (Not OnChangingPageNotes(ps, Pagename, "IO_P" & vPort)) 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 vPort As Variant

    vPort = ps.Parameters.v("PortIndex")

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

    '------------------------------------------------------------------------------------
    ' page: Input Characteristic
    '------------------------------------------------------------------------------------
    If (Not OnShowPageInputCharacteristic(ps, Controlname, Pagename, v)) Then Exit Function

    '------------------------------------------------------------------------------------
    ' page: Output Characteristic
    '------------------------------------------------------------------------------------
    If (Not OnShowPageOutputCharacteristic(ps, Controlname, Pagename, v)) Then Exit Function

    '------------------------------------------------------------------------------------
    ' page: Pad Hold
    '------------------------------------------------------------------------------------
    If (Not OnShowPagePadHold(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_P" & vPort, v)) Then Exit Function

    '------------------------------------------------------------------------------------
    ' End Controlls
    '------------------------------------------------------------------------------------

    OnShow = True
    Exit Function

OnShowError:
    Exit Function
End Function


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

    OnClick = False

     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\Port" & ps.Parameters.v("PortIndex"))) 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 OnLoadPagePortx(ps As Peripherals) As Boolean
  On Error GoTo OnLoadPagePortxError
  Dim vPort As Variant

  OnLoadPagePortx = False

  vPort = ps.Parameters.v("PortIndex")
  If (Not ps.AddPage("Port " & vPort)) Then Exit Function

  OnLoadPagePortx = True
  Exit Function

OnLoadPagePortxError:
  Exit Function
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 OnChangingPagePortx(ps As Peripherals, Pagename As String) As Boolean
  On Error GoTo OnChangingPagePortxError
  Dim r As ProjectData
  Dim p As Parameters
  Dim vPort As Variant
  Dim iPin As Integer
  Dim iFirstPin As Integer
  Dim iLastPin As Integer
  Dim sPin As String
  Dim cp As Object
  Dim sDirectionReg As String
  Dim sOpenDrainReg As String
  Dim sOutputReg As String
  Dim bDirectionReg As Boolean
  Dim bOpenDrainReg As Boolean
  Dim bOutputReg As Boolean
  Dim iLeftPosition As Integer

  Dim c As Collection
  Dim e As Collection

  OnChangingPagePortx = False

  Set r = ps.ProjectData
  Set p = ps.Parameters
  Set c = ps.ControlCollection
  Set e = ps.ExtenderCollection
  Set p = ps.Parameters
  Set cp = ObjectCollection("PORTPINS")


  vPort = p.v("PortIndex")
  iFirstPin = p.v("FirstPinIndex")
  iLastPin = p.v("LastPinIndex")

  If (Not e("Port " & vPort).Loaded) And (Pagename = "Port " & vPort) Then
    e("Port " & vPort).Loaded = True

    bDirectionReg = False
    bOpenDrainReg = False
    bOutputReg = False

    If (Not ps.AddFrame("fraPx", 100 , 50, 8450, 5325, "")) Then Exit Function


    If (Not ps.AddLabel("lblFunctionP" & vPort, 0 , 200, 200, 1575, 380, "Functionality:")) Then Exit Function
    iLeftPosition = 6000

    For iPin = iFirstPin To iLastPin
      sPin = "P" & vPort & "_" & iPin

      If (Not ps.AddCheck("chkUse" & sPin, 200 , 300  * iPin + 400, 2500, 380, cp.Description(sPin))) Then Exit Function

      sDirectionReg = GetDirectionRegName(vPort, iPin)
      If (r.Regs.BitFind(sDirectionReg) = True) Then
        If (Not ps.AddInOut("optD" & sPin, 3000 , 300  * iPin + 450)) Then Exit Function
        If (bDirectionReg = False) Then
          If (Not ps.AddLabel("lblDirectionP" & vPort, 0 , 3000, 200, 1575, 380, "Direction:")) Then Exit Function
          bDirectionReg = True
        End If
      End If

      sOpenDrainReg = GetOpenDrainRegName(vPort, iPin)
      If (r.Regs.BitFind(sOpenDrainReg) = True) Then
        If (Not ps.AddCheck("chkOD" & sPin, 5100 , 300  * iPin + 400, 2100, 380, "Open drain")) Then Exit Function
        If (bOpenDrainReg = False) Then
          If (Not ps.AddLabel("lblOpenDrainP" & vPort, 0 , 4900, 200, 2000, 380, "Push Pull / Open Drain:")) Then Exit Function
          bOpenDrainReg = True
'         iLeftPosition = 7000
        End If
      End If
      iLeftPosition = 7000

      sDirectionReg = GetDirectionRegName(vPort, iPin)
      sOutputReg = GetOutputRegName(vPort, iPin)
      If ((r.Regs.BitFind(sOutputReg) = True) And (r.Regs.BitFind(sDirectionReg) = True)) Then
        If (Not ps.AddCheck("chk" & sPin, iLeftPosition + 200 , 300  * iPin + 400, 1100, 380, "high")) Then Exit Function
        If (bOutputReg = False) Then
          If (Not ps.AddLabel("lblOutputP" & vPort, 0 , iLeftPosition, 200, 1575, 380, "Output Value:")) Then Exit Function
          bOutputReg = True
        End If
      End If
    Next iPin
  End If

  OnChangingPagePortx = True
  Exit Function

OnChangingPagePortxError:
  Errorbox "Can not show page", Err.Description
  Exit Function
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 OnShowPagePortx(ps As Peripherals, Controlname As String, Pagename As String, v() As Variant) As Boolean
  On Error Goto OnShowPagePortxError

  OnShowPagePortx = False
  
  Dim bDisabled As Boolean
  Dim r As ProjectData
  Dim p As Parameters
  Dim c As Collection
  Dim e As Collection
  Dim vPort As Variant
  Dim iFirstPin As Integer
  Dim iLastPin As Integer
  Dim iPin As Integer
  Dim sPin As String
  Dim sPort As String
  Dim cp As Object
  Dim sDirectionReg As String
  Dim sOpenDrainReg As String
  Dim sOutputReg As String
  Dim sPullupPulldownEnableReg As String
  Dim sPullupPulldownSelectReg As String
  Dim sOutputDriverEnableReg As String


  Set r = ps.ProjectData
  Set c = ps.ControlCollection
  Set e = ps.ExtenderCollection
  Set p = ps.Parameters
  Set cp = ObjectCollection("PORTPINS")

  vPort = p.v("PortIndex")
  iFirstPin = p.v("FirstPinIndex")
  iLastPin = p.v("LastPinIndex")

  '------------------------------------------------------------------------------------
  ' page: Portx (action)
  '------------------------------------------------------------------------------------
  If (Pagename = "Port " & vPort) Then
    sPort = "P" & vPort               ' P2
    For iPin = iFirstPin To iLastPin
      sPin = sPort & "_" & iPin                                                ' e.g P2_0
      sDirectionReg = GetDirectionRegName(vPort, iPin)
      sOpenDrainReg = GetOpenDrainRegName(vPort, iPin)
      sOutputReg = GetOutputRegName(vPort, iPin)
      sPullupPulldownEnableReg = GetPullupPulldownEnableRegName(vPort, iPin)
      sPullupPulldownSelectReg = GetPullupPulldownSelectRegName(vPort, iPin)
      sOutputDriverEnableReg = GetOutputDriverEnableRegName(vPort, iPin)

      '--------------------------------------------------------------------------------
      ' control: chkUSEPx_y (action)
      '--------------------------------------------------------------------------------
      ' action on clicking the selected control
      If (cp.Use(sPin) = TRUE) Then
        If (Controlname =  "chkUse" & sPin) Then

          If (v(1) = 0 And r.v("MuxFor_" & sPin) = 2) Then  ' deselect general pin if he was output
            If (r.Regs.BitFind(sDirectionReg) = True) Then
              r.v(sDirectionReg) = 0
            End If

            If (r.Regs.BitFind(sOpenDrainReg) = True) Then
              r.v(sOpenDrainReg) = 0
            End If

            If (r.Regs.BitFind(sOutputReg) = True) Then
              r.v(sOutputReg) = 0
            End If

'            If (r.Regs.BitFind(sPullupPulldownEnableReg) = True) Then
'              r.v(sPullupPulldownEnableReg) = 0
'            End If
'            If (r.Regs.BitFind(sPullupPulldownSelectReg) = True) Then
'              r.v(sPullupPulldownSelectReg) = 0
'            End If

            If (r.Regs.BitFind(sOutputDriverEnableReg) = True) Then
              r.v(sOutputDriverEnableReg) = 0
            End If
          End If
          r.v("MuxFor_" & sPin) = v(1)

        End If
      End If

      '------------------------------------------------------------------------------------
      ' control: optDPx_y (action)
      '------------------------------------------------------------------------------------
      If (r.Regs.BitFind(sDirectionReg) = True) Then
        ' action on clicking the selected control
        If (Controlname =  "optD" & sPin) Then
          r.v(sDirectionReg) = v(1)
          r.v("MuxFor_" & sPin) = 1 + v(1)

          If (v(1) = 0) Then ' switch back to input
            If (r.Regs.BitFind(sOpenDrainReg) = True) Then
               r.v(sOpenDrainReg) = 0
            End If

            If (r.Regs.BitFind(sOutputReg) = True) Then
              r.v(sOutputReg) = 0
            End If

'            If (r.Regs.BitFind(sPullupPulldownEnableReg) = True) Then
'              r.v(sPullupPulldownEnableReg) = 0
'            End If
'            If (r.Regs.BitFind(sPullupPulldownSelectReg) = True) Then
'              r.v(sPullupPulldownSelectReg) = 0
'            End If

            If (r.Regs.BitFind(sOutputDriverEnableReg) = True) Then
              r.v(sOutputDriverEnableReg) = 0
            End If
          End If

        End If
      End If

      '------------------------------------------------------------------------------------
      ' control: chkODx_y (action)
      '------------------------------------------------------------------------------------
      If (r.Regs.BitFind(sOpenDrainReg) = True) Then
        ' action on clicking the selected control
        If (Controlname =  "chkOD" & sPin) Then
          r.v(sOpenDrainReg) = v(1)
        End If
      End If

      '------------------------------------------------------------------------------------
      ' control: chkOUTx_y (action)
      '------------------------------------------------------------------------------------
      If (r.Regs.BitFind(sOutputReg) = True) Then
        ' action on clicking the selected control
        If (Controlname =  "chk" & sPin) Then
          r.v(sOutputReg) = v(1)
        End If
      End If

    Next iPin

    '------------------------------------------------------------------------------------
    ' page: Portx (show)
    '------------------------------------------------------------------------------------
    For iPin = iFirstPin To iLastPin
      sPin = sPort & "_" & iPin                                  ' e.g. P2_0
      sDirectionReg = GetDirectionRegName(vPort, iPin)
      sOpenDrainReg = GetOpenDrainRegName(vPort, iPin)
      sOutputReg = GetOutputRegName(vPort, iPin)

      '------------------------------------------------------------------------------------
      ' control: chk_USEPx_y (show)
      '------------------------------------------------------------------------------------
      With c("chkUse" & sPin)
        ' disable condition
        bDisabled = (cp.Use(sPin) = False) OR (IsGeneralInputPinFree(r, sPin) = False)
        .Enabled = Not(bDisabled)
        ' show condition
        .Value = -CInt (Not(r.v("MuxFor_" & sPin) = 0))
      End With

      With e("chkUse" & sPin)
        If (bDisabled) Then
          .DisabledTooltipText = TOOLTIP_DISABLED_IF
          If (cp.Use(sPin) = FALSE) Then
            .DisabledToolTipText = .DisabledToolTipText & FormatDisabledToolTip("the pin is not availible")
          Else
            .DisabledToolTipText = .DisabledToolTipText & GetDisabledGeneralPinTooltip(r, sPin)
          End If
        Else
          ' tooltip if control is enabled 
          .EnabledToolTipText = GetEnabledGeneralPinTooltip (r, sPin)
        End If
      End With

      '------------------------------------------------------------------------------------
      ' control: optDIRx_y (show)
      '------------------------------------------------------------------------------------
      If (r.Regs.BitFind(sDirectionReg) = True) Then
        With c("optD" & sPin)
          ' disable condition
          bDisabled = (r.v("MuxFor_" & sPin) = 0) OR (IsGeneralOutputPinFree(r, sPin) = False) 
         .Enabled = Not(bDisabled)
          ' show condition
          .Value = r.v(sDirectionReg) = 1
        End With
      

        With e("optD" & sPin)
          If (bDisabled) Then
            ' tooltip if control is disabled 
            .DisabledTooltipText = TOOLTIP_DISABLED_IF
            .DisabledTooltipText = .DisabledTooltipText & GetDisabledGeneralDirectionTooltip(r, sPin)
            If (r.v("MuxFor_" & sPin) = 0) Then            
              .DisabledTooltipText = .DisabledTooltipText & FormatDisabledToolTip _
                                     ("the pin is NOT selected as general IO")
            End If
          Else
            ' tooltip if control is enabled 
          End If
        End With
      End If

      '------------------------------------------------------------------------------------
      ' control: chkODPx_y (show)
      '------------------------------------------------------------------------------------
      If (r.Regs.BitFind(sOpenDrainReg) = True) Then
        With c("chkOD" & sPin)
          ' disable condition
          bDisabled = (r.v(sDirectionReg) = 0)
          .Enabled = Not(bDisabled)
          ' show condition
          .Value = -CInt(r.v(sOpenDrainReg) = 1)
        End With

        With e("chkOD" & sPin)
          If (bDisabled) Then
            .DisabledTooltipText = TOOLTIP_DISABLED_IF & FormatDisabledToolTip _
                                 ("the pin is NOT used as output")
          Else
            ' tooltip if control is enabled 
          End If
        End With
      End If

      '------------------------------------------------------------------------------------
      ' control: chkPx_y (show)
      '------------------------------------------------------------------------------------
      If ((r.Regs.BitFind(sOutputReg) = True) And (r.Regs.BitFind(sDirectionReg) = True)) Then
        With c("chk" & sPin)
          ' disable condition
          bDisabled = (r.v("MuxFor_" & sPin) <> 2)  ' not general output
          .Enabled = Not(bDisabled)
          ' show condition
          .Value = -CInt(r.v(sOutputReg) = 1)
        End With

        With e("chk" & sPin)
          If (bDisabled) Then
            .DisabledTooltipText = TOOLTIP_DISABLED_IF & FormatDisabledToolTip _
                                 ("the pin is NOT used as general output")
          Else
            ' tooltip if control is enabled 
          End If
        End With
      End If

    Next iPin
  End If         ' if Pagename

  OnShowPagePortx = True
  Exit Function

OnShowPagePortxError:
  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 OnLoadPageInputCharacteristic(ps As Peripherals) As Boolean
  On Error GoTo OnLoadPageInputCharacteristicError                              
  Dim p As Parameters
  Dim r As ProjectData
  Dim vPort As Variant
  Dim iPin As Integer
  Dim iFirstPin As Integer
  Dim iLastPin As Integer
  Dim sPin As String
  Dim cPort As DAvECollection
  Dim bPageExist As Boolean
  Dim sPullupPulldownEnableReg As String
  Dim sPullupPulldownSelectReg As String

  OnLoadPageInputCharacteristic = False

  Set r = ps.ProjectData
  Set p = ps.Parameters
  Set cPort = ObjectCollection("PORTS")

  vPort = p.v("PortIndex")
  iFirstPin = p.v("FirstPinIndex")
  iLastPin = p.v("LastPinIndex")

  bPageExist = False


  For iPin = iFirstPin To iLastPin  ' check if not empty page
    sPin = "P" & vPort & "_" & iPin
    sPullupPulldownEnableReg = GetPullupPulldownEnableRegName(vPort, iPin)
    sPullupPulldownSelectReg = GetPullupPulldownSelectRegName(vPort, iPin)
    If (r.Regs.BitFind(sPullupPulldownEnableReg) = True And r.Regs.BitFind(sPullupPulldownSelectReg) = True) Then
      bPageExist = True
      Exit For
    End If
  Next iPin


  If (bPageExist = False) Then
    If (cPort("P" & vPort).InputThresholdType > 0) Then
      bPageExist = True
    End If
  End If

  If (bPageExist = True) Then
    If (Not ps.AddPage("Input Characteristic")) Then Exit Function
  End If


  OnLoadPageInputCharacteristic = True
  Exit Function

OnLoadPageInputCharacteristicError:
  Exit Function
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 OnChangingPageInputCharacteristic(ps As Peripherals, Pagename As String) As Boolean
  On Error Goto OnChangingPageInputCharacteristicError
  Dim p As Parameters
  Dim r As ProjectData
  Dim vPort As Variant
  Dim iPin As Integer
  Dim iByte As Integer
  Dim iNible As Integer
  Dim iFirstPin As Integer
  Dim iLastPin As Integer
  Dim sPin As String
  Dim cPort As DAvECollection
  Dim sPullupPulldownEnableReg As String
  Dim sPullupPulldownSelectReg As String
  Dim sInputConfigurationReg As String
  Dim iLeftPosition As Integer

  OnChangingPageInputCharacteristic = False

  Set r = ps.ProjectData
  Set p = ps.Parameters
  Set cPort = ObjectCollection("PORTS")


  vPort = p.v("PortIndex")
  iFirstPin = p.v("FirstPinIndex")
  iLastPin = p.v("LastPinIndex")

  If (Pagename = "Input Characteristic") Then
    If (Not ps.ExtenderCollection("Input Characteristic").Loaded) Then
      ps.ExtenderCollection("Input Characteristic").Loaded = True

      iLeftPosition = 100        ' left position for the second frame

      For iPin = iFirstPin To iLastPin  ' check if not empty frame
        sPin = "P" & vPort & "_" & iPin
        sPullupPulldownEnableReg = GetPullupPulldownEnableRegName(vPort, iPin)
        sPullupPulldownSelectReg = GetPullupPulldownSelectRegName(vPort, iPin)
        If (r.Regs.BitFind(sPullupPulldownEnableReg) = True And r.Regs.BitFind(sPullupPulldownSelectReg) = True) Then
          '-------------------------------
          ' frame: frmPUDEN
          '-------------------------------
          iLeftPosition = 4400        ' left position for the second frame
          If (Not ps.AddFrame("fraPUDEN", 100 , 100, 4200, 5200, "Pull Device")) Then Exit Function
            For iPin = iFirstPin To iLastPin
              sPin = "P" & vPort & "_" & iPin
              sPullupPulldownEnableReg = GetPullupPulldownEnableRegName(vPort, iPin)
              sPullupPulldownSelectReg = GetPullupPulldownSelectRegName(vPort, iPin)
              If (r.Regs.BitFind(sPullupPulldownEnableReg) = True And r.Regs.BitFind(sPullupPulldownSelectReg) = True) Then
                If (Not ps.AddLabel("lblPEN" & iPin,0 , 200 , 300  * iPin + 350, 1700, 380, "P" & vPort & "." & iPin & " pull device")) Then Exit Function
                If (Not ps.AddCombo("cboPEN" & iPin, 2000 , 300  * iPin + 300, 2000, 315)) Then Exit Function
                With ps.ControlCollection("cboPEN" & iPin)
                  .Clear
                  .AddItem "Tristate"
                  .AddItem "Pull down device"
                  .AddItem "Pull up device"
                End With
              End If
            Next iPin
          Exit For
        End If
      Next iPin


      Select Case (cPort("P" & vPort).InputThresholdType)
          Case 1  ' for each pin
              For iPin = iFirstPin To iLastPin  ' check if not empty frame
                sPin = "P" & vPort & "_" & iPin
                sInputConfigurationReg = GetInputConfigurationRegNamePinwise(vPort, iPin)
                If (r.Regs.BitFind(sInputConfigurationReg) = True) Then
                  '-------------------------------
                  ' frame: frmPICON
                  '-------------------------------
                  If (Not ps.AddFrame("fraPICON", iLeftPosition , 100, 4200, 5200, "Input Threshold Type")) Then Exit Function
                    For iPin = iFirstPin To iLastPin
                      sPin = "P" & vPort & "_" & iPin
                      sInputConfigurationReg = GetInputConfigurationRegNamePinwise(vPort, iPin)
                      If (r.Regs.BitFind(sInputConfigurationReg) = True) Then
                        If (Not ps.AddLabel("lblPICON" & iPin,0 , 200 , 300  * iPin + 350, 1700, 380, "P" & vPort & "." & iPin & " threshold type")) Then Exit Function
                        If (Not ps.AddCombo("cboPICON" & iPin, 2000 , 300  * iPin + 300, 2000, 315)) Then Exit Function
                        With ps.ControlCollection("cboPICON" & iPin)
                          .Clear
                          .AddItem "TTL input"
                          .AddItem "CMOS-like input"
                        End With
                      End If
                    Next iPin
                  Exit For
                End If
              Next iPin

          Case 2  ' for each nible
              For iNible = 0 To 3  ' check if not empty frame
                sInputConfigurationReg = GetInputConfigurationRegNameNiblewise(vPort, iNible)
                If (r.Regs.BitFind(sInputConfigurationReg) = True) Then
                  '-------------------------------
                  ' frame: frmPICON
                  '-------------------------------
                  If (Not ps.AddFrame("fraPICON", iLeftPosition , 100, 4200, 5200, "Input Threshold Type")) Then Exit Function
                    
                    For iNible = 0 To 3
                      sInputConfigurationReg = GetInputConfigurationRegNameNiblewise(vPort, iNible)
                      If (r.Regs.BitFind(sInputConfigurationReg) = True) Then
                        If (Not ps.AddLabel("lblPICON" & iNible,0 , 200 , 1200 * iNible + 300, 1700, 380, "P" & vPort & "." & 4 * iNible & " - P" & vPort & "." & 3 + 4 * iNible & " threshold type")) Then Exit Function
                        If (Not ps.AddCombo("cboPICON" & iNible, 2000 , 1200 * iNible + 300, 2000, 315)) Then Exit Function
                        With ps.ControlCollection("cboPICON" & iNible)
                          .Clear
                          .AddItem "TTL input"
                          .AddItem "CMOS-like input"
                        End With
                      End If
                    Next iNible
                  Exit For
                End If
              Next iNible

          Case 3  ' for each byte
              For iByte = 0 To 1  ' check if not empty frame
                sInputConfigurationReg = GetInputConfigurationRegNameBytewise(vPort, iByte)
                If (r.Regs.BitFind(sInputConfigurationReg) = True) Then
                  '-------------------------------
                  ' frame: frmPICON
                  '-------------------------------
                  If (Not ps.AddFrame("fraPICON", iLeftPosition , 100, 4200, 5200, "Input Threshold Type")) Then Exit Function
                    
                    For iByte = 0 To 1
                      sInputConfigurationReg = GetInputConfigurationRegNameBytewise(vPort, iByte)
                      If (r.Regs.BitFind(sInputConfigurationReg) = True) Then
                        If (Not ps.AddLabel("lblPICON" & iByte,0 , 200 , 2400  * iByte + 300, 1700, 380, "P" & vPort & "." & 8 * iByte & " - P" & vPort & "." & 7 + 8 * iByte & " threshold type")) Then Exit Function
                        If (Not ps.AddCombo("cboPICON" & iByte, 2000 , 2400  * iByte + 300, 2000, 315)) Then Exit Function
                        With ps.ControlCollection("cboPICON" & iByte)
                          .Clear
                          .AddItem "TTL input"
                          .AddItem "CMOS-like input"
                        End With
                      End If
                    Next iByte
                  Exit For
                End If
              Next iByte

          Case 4  ' for each port
              sInputConfigurationReg = GetInputConfigurationRegNamePortwise(vPort)
              If (r.Regs.BitFind(sInputConfigurationReg) = True) Then
                '-------------------------------
                ' frame: frmPICON
                '-------------------------------
                If (Not ps.AddFrame("fraPICON0", iLeftPosition , 100, 4200, 5200, "Input Threshold Type")) Then Exit Function
                  If (Not ps.AddLabel("lblPICON0",0 , 200 , 450, 1700, 380, "P" & vPort & " threshold type")) Then Exit Function
                  If (Not ps.AddCombo("cboPICON0", 2000 , 400, 2000, 315)) Then Exit Function
                  With ps.ControlCollection("cboPICON0")
                    .Clear
                    .AddItem "TTL input"
                    .AddItem "CMOS-like input"
                  End With
              End If

      End Select
    End If
  End If

  OnChangingPageInputCharacteristic = True
  Exit Function

OnChangingPageInputCharacteristicError:
  Errorbox "Can not change page", Err.Description
  Exit Function
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 OnShowPageInputCharacteristic(ps As Peripherals, Controlname As String, Pagename As String, v() As Variant) As Boolean
  On Error Goto OnShowPageInputCharacteristicError
  
  Dim bDisabled As Boolean
  Dim r As ProjectData
  Dim p As Parameters
  Dim c As Collection
  Dim e As Collection
  Dim vPort As Variant
  Dim iFirstPin As Integer
  Dim iLastPin As Integer
  Dim iPin As Integer
  Dim iNible As Integer
  Dim iByte As Integer
  Dim sPin As String
  Dim sPort As String
  Dim cPort As DAvECollection
  Dim sDirectionReg As String
  Dim sPullupPulldownEnableReg As String
  Dim sPullupPulldownSelectReg As String
  Dim sInputConfigurationReg As String


  OnShowPageInputCharacteristic = False

  Set r = ps.ProjectData
  Set c = ps.ControlCollection
  Set e = ps.ExtenderCollection
  Set p = ps.Parameters
  Set cPort = ObjectCollection("PORTS")

  vPort = p.v("PortIndex")
  iFirstPin = p.v("FirstPinIndex")
  iLastPin = p.v("LastPinIndex")

  '------------------------------------------------------------------------------------
  ' page: Input Characteristic (action)
  '------------------------------------------------------------------------------------
  If (Pagename = "Input Characteristic") Then
    sPort = "P" & vPort               ' P2
 
    '--------------------------------------------------------------------------------
    ' control: cboPENx (action)
    '--------------------------------------------------------------------------------
    For iPin = iFirstPin To iLastPin
      sPin = sPort & "_" & iPin                                                ' e.g. P2_0
      sPullupPulldownEnableReg = GetPullupPulldownEnableRegName(vPort, iPin)
      sPullupPulldownSelectReg = GetPullupPulldownSelectRegName(vPort, iPin)

      ' action on clicking the selected control
      If (r.Regs.BitFind(sPullupPulldownEnableReg) = True And r.Regs.BitFind(sPullupPulldownSelectReg) = True) Then
        If (Controlname =  "cboPEN" & iPin) Then
          Select Case (v(1))
              Case ("Tristate")
                  r.v(sPullupPulldownEnableReg) = 0
                  r.v(sPullupPulldownSelectReg) = 0
              Case ("Pull down device")
                  r.v(sPullupPulldownEnableReg) = 1
                  r.v(sPullupPulldownSelectReg) = 0
              Case ("Pull up device")
                  r.v(sPullupPulldownEnableReg) = 1
                  r.v(sPullupPulldownSelectReg) = 1
          End Select
        End If
      End If
    Next iPin

    '--------------------------------------------------------------------------------
    ' control: cboPICONx (action)
    '--------------------------------------------------------------------------------
    Select Case (cPort("P" & vPort).InputThresholdType)
        Case 1  ' for each pin
            For iPin = iFirstPin To iLastPin
              sPin = sPort & "_" & iPin                                                ' e.g. P2_0
              sInputConfigurationReg = GetInputConfigurationRegNamePinwise(vPort, iPin)
              ' action on clicking the selected control
              If (r.Regs.BitFind(sInputConfigurationReg) = True) Then
                If (Controlname =  "cboPICON" & iPin) Then
                  Select Case (v(1))
                      Case ("TTL input")
                          r.v(sInputConfigurationReg) = 0
                      Case ("CMOS-like input")
                          r.v(sInputConfigurationReg) = 1
                  End Select
                End If
              End If
            Next iPin

        Case 2  ' for each nible
            For iNible = 0 To 3
              sInputConfigurationReg = GetInputConfigurationRegNameNiblewise(vPort, iNible)
              ' action on clicking the selected control
              If (r.Regs.BitFind(sInputConfigurationReg) = True) Then
                If (Controlname =  "cboPICON" & iNible) Then
                  Select Case (v(1))
                      Case ("TTL input")
                          r.v(sInputConfigurationReg) = 0
                      Case ("CMOS-like input")
                          r.v(sInputConfigurationReg) = 1
                  End Select
                End If
              End If
            Next iNible

        Case 3  ' for each byte
            For iByte = 0 To 1
              sInputConfigurationReg = GetInputConfigurationRegNameBytewise(vPort, iByte)
              ' action on clicking the selected control
              If (r.Regs.BitFind(sInputConfigurationReg) = True) Then
                If (Controlname =  "cboPICON" & iByte) Then
                  Select Case (v(1))
                      Case ("TTL input")
                          r.v(sInputConfigurationReg) = 0
                      Case ("CMOS-like input")
                          r.v(sInputConfigurationReg) = 1
                  End Select
                End If
              End If
            Next iByte

        Case 4  ' for each port
            sInputConfigurationReg = GetInputConfigurationRegNamePortwise(vPort)
            ' action on clicking the selected control
            If (r.Regs.BitFind(sInputConfigurationReg) = True) Then
              If (Controlname =  "cboPICON0") Then
                Select Case (v(1))
                    Case ("TTL input")
                        r.v(sInputConfigurationReg) = 0
                    Case ("CMOS-like input")
                        r.v(sInputConfigurationReg) = 1
                End Select
              End If
            End If
    End Select

    '------------------------------------------------------------------------------------
    ' page: Input Characteristic (show)
    '------------------------------------------------------------------------------------

    '------------------------------------------------------------------------------------
    ' control: cboPENx (show)
    '------------------------------------------------------------------------------------
    For iPin = iFirstPin To iLastPin
      sPin = sPort & "_" & iPin                                                ' P2_0
      sDirectionReg = GetDirectionRegName(vPort, iPin)
      sPullupPulldownEnableReg = GetPullupPulldownEnableRegName(vPort, iPin)
      sPullupPulldownSelectReg = GetPullupPulldownSelectRegName(vPort, iPin)

      If (r.Regs.BitFind(sPullupPulldownEnableReg) = True And r.Regs.BitFind(sPullupPulldownSelectReg) = True) Then
        With c("cboPEN" & iPin)
          ' disable condition
'          bDisabled = r.v(sDirectionReg) = 1 Or (r.v("MuxFor_" & sPin) = 0 And IsGeneralInputPinFree(r, sPin) = True)
          bDisabled = False
          If (r.Regs.BitFind(sDirectionReg) = True) Then
            bDisabled = r.v(sDirectionReg) = 1
          End If
          .Enabled = Not(bDisabled)
          If (r.v(sPullupPulldownEnableReg) = 0) Then
              .ListIndex = 0
          End If
          If (r.v(sPullupPulldownEnableReg) = 1 AND r.v(sPullupPulldownSelectReg) = 0) Then
              .ListIndex = 1
          End If
          If (r.v(sPullupPulldownEnableReg) = 1 AND r.v(sPullupPulldownSelectReg) = 1) Then
              .ListIndex = 2
          End If
        End With

        With e("cboPEN" & iPin)
          If (bDisabled) Then
            ' tooltip if control is disabled 
            .DisabledTooltipText = TOOLTIP_DISABLED_IF 
            If (r.Regs.BitFind(sDirectionReg) = True) Then
              If (r.v(sDirectionReg) = 1) Then  ' not  input
                .DisabledTooltipText = .DisabledTooltipText & FormatDisabledToolTip("the pin is NOT used as input")
              End If
            End If
'            If (r.v("MuxFor_" & sPin) <> 1 And IsGeneralInputPinFree(r, sPin) = True) Then  ' not general input
'              .DisabledTooltipText = .DisabledTooltipText & FormatDisabledToolTip("the pin is NOT used as general input")
'            End If

'            If (r.v(sDirectionReg) = 1 And IsGeneralInputPinFree(r, sPin) = False) Then   ' not general input
'              .DisabledTooltipText = .DisabledTooltipText & FormatDisabledToolTip("the pin is NOT used as alternate input")
'            End If
          Else
            ' tooltip if control is enabled 
          End If
        End With

        '------------------------------------------------------------------------------------
        ' control: lblPENx (show)
        '------------------------------------------------------------------------------------
        With c("lblPEN" & iPin)
          ' same as for cboPEN
          .Enabled = Not(bDisabled)
        End With

        With e("lblPEN" & iPin)
          If (bDisabled) Then
            ' tooltip if control is disabled 
            .DisabledTooltipText = e("cboPEN" & iPin).DisabledTooltipText
          Else
            ' tooltip if control is enabled 
          End If
        End With
      End If
    Next iPin

    Select Case (cPort("P" & vPort).InputThresholdType)
        Case 1  ' for each pin
            For iPin = iFirstPin To iLastPin
              sPin = sPort & "_" & iPin                                                ' P2_0
              sDirectionReg = GetDirectionRegName(vPort, iPin)
              sInputConfigurationReg = GetInputConfigurationRegNamePinwise(vPort, iPin)

              '------------------------------------------------------------------------------------
              ' control: cboPICONx (show)
              '------------------------------------------------------------------------------------
              If (r.Regs.BitFind(sInputConfigurationReg) = True) Then
                With c("cboPICON" & iPin)
                  ' disable condition
                  bDisabled = False
                  If (r.Regs.BitFind(sDirectionReg) = True) Then
                    bDisabled = r.v(sDirectionReg) = 1
                  End If
'                  bDisabled = r.v(sDirectionReg) = 1 Or (r.v("MuxFor_" & sPin) = 0 And IsGeneralInputPinFree(r, sPin) = True)
                  .Enabled = Not(bDisabled)
                  .ListIndex = r.v(sInputConfigurationReg)
                End With

                With e("cboPICON" & iPin)
                  If (bDisabled) Then
                    ' tooltip if control is disabled 
                    .DisabledTooltipText = TOOLTIP_DISABLED_IF 
                    If (r.Regs.BitFind(sDirectionReg) = True) Then
                      If (r.v(sDirectionReg) = 1) Then  ' not  input
                        .DisabledTooltipText = .DisabledTooltipText & FormatDisabledToolTip("the pin is NOT used as input")
                      End If
                    End If

'                    If (r.v("MuxFor_" & sPin) <> 1 And IsGeneralInputPinFree(r, sPin) = True) Then  ' not general input
'                      .DisabledTooltipText = .DisabledTooltipText & FormatDisabledToolTip("the pin is NOT used as general input")
'                    End If

'                    If (r.v(sDirectionReg) = 1 And IsGeneralInputPinFree(r, sPin) = False) Then   ' not general input
'                      .DisabledTooltipText = .DisabledTooltipText & FormatDisabledToolTip("the pin is NOT used as alternate input")
'                    End If
                  Else
                    ' tooltip if control is enabled 
                  End If
                End With

                '------------------------------------------------------------------------------------
                ' control: lblPICONx (show)
                '------------------------------------------------------------------------------------
                With c("lblPICON" & iPin)
                  ' same as for cboPICON
                  .Enabled = Not(bDisabled)
                End With

                With e("lblPICON" & iPin)
                  If (bDisabled) Then
                    ' tooltip if control is disabled 
                    .DisabledTooltipText = e("cboPICON" & iPin).DisabledTooltipText
                  Else
                    ' tooltip if control is enabled 
                  End If
                End With
              End If

            Next iPin

        Case 2  ' for each nible
            For iNible = 0 To 3
'              sPin = sPort & "_" & iPin                                                ' P2_0
'              sDirectionReg = GetDirectionRegName(vPort, iPin)
              sInputConfigurationReg = GetInputConfigurationRegNameNiblewise(vPort, iNible)
              '------------------------------------------------------------------------------------
              ' control: cboPICONx (show)
              '------------------------------------------------------------------------------------
              If (r.Regs.BitFind(sInputConfigurationReg) = True) Then
                With c("cboPICON" & iNible)
                  ' disable condition
                  bDisabled = False 'r.v(sDirectionReg) = 1 Or (r.v("MuxFor_" & sPin) = 0 And IsGeneralInputPinFree(r, sPin) = True)
                  .Enabled = Not(bDisabled)
                  .ListIndex = r.v(sInputConfigurationReg)
                End With

                With e("cboPICON" & iNible)
                  If (bDisabled) Then
                    ' tooltip if control is disabled 
                    .DisabledTooltipText = TOOLTIP_DISABLED_IF 
'                    If (r.v("MuxFor_" & sPin) <> 1 And IsGeneralInputPinFree(r, sPin) = True) Then  ' not general input
'                      .DisabledTooltipText = .DisabledTooltipText & FormatDisabledToolTip("the pin is NOT used as general input")
'                    End If

'                    If (r.v(sDirectionReg) = 1 And IsGeneralInputPinFree(r, sPin) = False) Then   ' not general input
'                      .DisabledTooltipText = .DisabledTooltipText & FormatDisabledToolTip("the pin is NOT used as alternate input")
'                    End If
                  Else
                    ' tooltip if control is enabled 
                  End If
                End With

                '------------------------------------------------------------------------------------
                ' control: lblPICONx (show)
                '------------------------------------------------------------------------------------
                With c("lblPICON" & iNible)
                  ' same as for cboPICON
                  .Enabled = Not(bDisabled)
                End With

                With e("lblPICON" & iNible)
                  If (bDisabled) Then
                    ' tooltip if control is disabled 
                    .DisabledTooltipText = e("cboPICON" & iNible).DisabledTooltipText
                  Else
                    ' tooltip if control is enabled 
                  End If
                End With
              End If
            Next iNible

        Case 3  ' for each byte
            For iByte = 0 To 1
'              sPin = sPort & "_" & iPin                                                ' P2_0
'              sDirectionReg = GetDirectionRegName(vPort, iPin)
              sInputConfigurationReg = GetInputConfigurationRegNameBytewise(vPort, iByte)
              '------------------------------------------------------------------------------------
              ' control: cboPICONx (show)
              '------------------------------------------------------------------------------------
              If (r.Regs.BitFind(sInputConfigurationReg) = True) Then
                With c("cboPICON" & iByte)
                  ' disable condition
                  bDisabled = False 'r.v(sDirectionReg) = 1 Or (r.v("MuxFor_" & sPin) = 0 And IsGeneralInputPinFree(r, sPin) = True)
                  .Enabled = Not(bDisabled)
                  .ListIndex = r.v(sInputConfigurationReg)
                End With

                With e("cboPICON" & iByte)
                  If (bDisabled) Then
                    ' tooltip if control is disabled 
                    .DisabledTooltipText = TOOLTIP_DISABLED_IF 
'                    If (r.v("MuxFor_" & sPin) <> 1 And IsGeneralInputPinFree(r, sPin) = True) Then  ' not general input
'                      .DisabledTooltipText = .DisabledTooltipText & FormatDisabledToolTip("the pin is NOT used as general input")
'                    End If

'                    If (r.v(sDirectionReg) = 1 And IsGeneralInputPinFree(r, sPin) = False) Then   ' not general input
'                      .DisabledTooltipText = .DisabledTooltipText & FormatDisabledToolTip("the pin is NOT used as alternate input")
'                    End If
                  Else
                    ' tooltip if control is enabled 
                  End If
                End With

                '------------------------------------------------------------------------------------
                ' control: lblPICONx (show)
                '------------------------------------------------------------------------------------
                With c("lblPICON" & iByte)
                  ' same as for cboPICON
                  .Enabled = Not(bDisabled)
                End With

                With e("lblPICON" & iByte)
                  If (bDisabled) Then
                    ' tooltip if control is disabled 
                    .DisabledTooltipText = e("cboPICON" & iByte).DisabledTooltipText
                  Else
                    ' tooltip if control is enabled 
                  End If
                End With
              End If
            Next iByte

        Case 4  ' for each port
            sInputConfigurationReg = GetInputConfigurationRegNamePortwise(vPort)
            '------------------------------------------------------------------------------------
            ' control: cboPICON (show)
            '------------------------------------------------------------------------------------
            If (r.Regs.BitFind(sInputConfigurationReg) = True) Then
              With c("cboPICON0")
                ' disable condition
                bDisabled = False 'hier muessen alle Pins geprft werden
                .Enabled = Not(bDisabled)
                .ListIndex = r.v(sInputConfigurationReg)
              End With

              With e("cboPICON0")
                If (bDisabled) Then
                  ' tooltip if control is disabled 
                  .DisabledTooltipText = TOOLTIP_DISABLED_IF 
  '                    If (r.v("MuxFor_" & sPin) <> 1 And IsGeneralInputPinFree(r, sPin) = True) Then  ' not general input
  '                      .DisabledTooltipText = .DisabledTooltipText & FormatDisabledToolTip("the pin is NOT used as general input")
  '                    End If

  '                    If (r.v(sDirectionReg) = 1 And IsGeneralInputPinFree(r, sPin) = False) Then   ' not general input
  '                      .DisabledTooltipText = .DisabledTooltipText & FormatDisabledToolTip("the pin is NOT used as alternate input")
  '                    End If
                Else
                  ' tooltip if control is enabled 
                End If
              End With

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

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

    End Select

  End If         ' if Pagename

  OnShowPageInputCharacteristic = True
  Exit Function

OnShowPageInputCharacteristicError:
  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 OnLoadPageOutputCharacteristic(ps As Peripherals) As Boolean
  On Error GoTo OnLoadPageOutputCharacteristicError                              
  Dim p As Parameters
  Dim r As ProjectData
  Dim vPort As Variant
  Dim cPort As DAvECollection
  Dim bPageExist As Boolean

  OnLoadPageOutputCharacteristic = False

  Set r = ps.ProjectData
  Set p = ps.Parameters
  Set cPort = ObjectCollection("PORTS")

  vPort = p.v("PortIndex")

  bPageExist = False


  If (cPort("P" & vPort).OutputEdgeType > 0 Or cPort("P" & vPort).OutputDriverType > 0) Then
    bPageExist = True
  End If

  If (bPageExist = True) Then
    If (Not ps.AddPage("Output Characteristic")) Then Exit Function
  End If

  OnLoadPageOutputCharacteristic = True
  Exit Function

OnLoadPageOutputCharacteristicError:
  Exit Function
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 OnChangingPageOutputCharacteristic(ps As Peripherals, Pagename As String) As Boolean
  On Error Goto OnChangingPageOutputCharacteristicError
  Dim p As Parameters
  Dim r As ProjectData
  Dim vPort As Variant
  Dim iPin As Integer
  Dim iNible As Integer
  Dim iByte As Integer
  Dim iFirstPin As Integer
  Dim iLastPin As Integer
  Dim sPin As String
  Dim cPort As DAvECollection
  Dim sOutputEdgeCharcReg As String
  Dim sOutputDriverCharcReg As String
  Dim iLeftPosition As Integer

  OnChangingPageOutputCharacteristic = False

  Set r = ps.ProjectData
  Set p = ps.Parameters
  Set cPort = ObjectCollection("PORTS")


  vPort = p.v("PortIndex")
  iFirstPin = p.v("FirstPinIndex")
  iLastPin = p.v("LastPinIndex")

  If (Pagename = "Output Characteristic") Then
    If (Not ps.ExtenderCollection("Output Characteristic").Loaded) Then
      ps.ExtenderCollection("Output Characteristic").Loaded = True

      iLeftPosition = 100        ' left position for the second frame

      Select Case (cPort("P" & vPort).OutputEdgeType)
          Case 1  ' for each pin
              For iPin = iFirstPin To iLastPin  ' check if not empty frame
                sPin = "P" & vPort & "_" & iPin
                sOutputEdgeCharcReg = GetOutputEdgeCharcRegNamePinwise(vPort, iPin)
                If (r.Regs.BitFind(sOutputEdgeCharcReg) = True) Then
                  iLeftPosition = 4400        ' left position for the second frame
                  '-------------------------------
                  ' frame: frmPOCON_PECx
                  '-------------------------------
                  If (Not ps.AddFrame("fraPOCON_PECx", 100 , 100, 4200, 5200, "Edge Characteristic Control")) Then Exit Function
                    For iPin = iFirstPin To iLastPin
                      sPin = "P" & vPort & "_" & iPin
                      sOutputEdgeCharcReg = GetOutputEdgeCharcRegNamePinwise(vPort, iPin)
                      If (r.Regs.BitFind(sOutputEdgeCharcReg) = True) Then
                        If (Not ps.AddLabel("lblPOCON_PEC" & iPin,0 , 200 , 300  * iPin + 350, 1700, 380, "Edge of P" & vPort & "." & iPin )) Then Exit Function
                        If (Not ps.AddCombo("cboPOCON_PEC" & iPin, 2000 , 300  * iPin + 300, 2000, 315)) Then Exit Function
                        With ps.ControlCollection("cboPOCON_PEC" & iPin)
                          .Clear
                          .AddItem "Sharp edge mode"
                          .AddItem "Medium edge mode"
                          .AddItem "Soft edge mode"
                          .AddItem "No edge mode"
                        End With
                      End If
                    Next iPin
                  Exit For
                End If
              Next iPin

          Case 2  ' for each nible
              For iNible = 0 To 3  ' check if not empty frame
                sOutputEdgeCharcReg = GetOutputEdgeCharcRegNameNiblewise(vPort, iNible)
                If (r.Regs.BitFind(sOutputEdgeCharcReg) = True) Then
                  iLeftPosition = 4400        ' left position for the second frame
                  '-------------------------------
                  ' frame: frmPOCON_PECx
                  '-------------------------------
                  If (Not ps.AddFrame("fraPOCON_PECx", 100 , 100, 4200, 5200, "Edge Characteristic Control")) Then Exit Function
                    For iNible = 0 To 3
                      sOutputEdgeCharcReg = GetOutputEdgeCharcRegNameNiblewise(vPort, iNible)
                      If (r.Regs.BitFind(sOutputEdgeCharcReg) = True) Then
                        If (Not ps.AddLabel("lblPOCON_PEC" & iNible,0 , 200 , 1200 * iNible + 350, 1700, 380, "Edge of P" & vPort & "." & 4 * iNible & " - P" & vPort & "." & 3 + 4 * iNible)) Then Exit Function
                        If (Not ps.AddCombo("cboPOCON_PEC" & iNible, 2000 , 1200 * iNible + 300, 2000, 315)) Then Exit Function
                        With ps.ControlCollection("cboPOCON_PEC" & iNible)
                          .Clear
                          .AddItem "Sharp edge mode"
                          .AddItem "Medium edge mode"
                          .AddItem "Soft edge mode"
                          .AddItem "No edge mode"
                        End With
                      End If
                    Next iNible
                  Exit For
                End If
              Next iNible

          Case 3  ' for each byte
              For iByte = 0 To 1  ' check if not empty frame
                sOutputEdgeCharcReg = GetOutputEdgeCharcRegNameBytewise(vPort, iByte)
                If (r.Regs.BitFind(sOutputEdgeCharcReg) = True) Then
                  iLeftPosition = 4400        ' left position for the second frame
                  '-------------------------------
                  ' frame: frmPOCON_PECx
                  '-------------------------------
                  If (Not ps.AddFrame("fraPOCON_PECx", 100 , 100, 4200, 5200, "Edge Characteristic Control")) Then Exit Function
                    For iByte = 0 To 1
                      sOutputEdgeCharcReg = GetOutputEdgeCharcRegNameBytewise(vPort, iByte)
                      If (r.Regs.BitFind(sOutputEdgeCharcReg) = True) Then
                        If (Not ps.AddLabel("lblPOCON_PEC" & iByte,0 , 200 , 2400 * iByte + 350, 1700, 380, "Edge of P" & vPort & "." & 8 * iByte & " - P" & vPort & "." & 7 + 8 * iByte)) Then Exit Function
                        If (Not ps.AddCombo("cboPOCON_PEC" & iByte, 2000 , 2400 * iByte + 300, 2000, 315)) Then Exit Function
                        With ps.ControlCollection("cboPOCON_PEC" & iByte)
                          .Clear
                          .AddItem "Sharp edge mode"
                          .AddItem "Medium edge mode"
                          .AddItem "Soft edge mode"
                          .AddItem "No edge mode"
                        End With
                      End If
                    Next iByte
                  Exit For
                End If
              Next iByte

          Case 4  ' for each port
              sOutputEdgeCharcReg = GetOutputEdgeCharcRegNamePortwise(vPort)
              If (r.Regs.BitFind(sOutputEdgeCharcReg) = True) Then
                iLeftPosition = 4400        ' left position for the second frame
                '-------------------------------
                ' frame: frmPOCON_PEC
                '-------------------------------
                If (Not ps.AddFrame("fraPOCON_PEC", 100 , 100, 4200, 5200, "Edge Characteristic Control")) Then Exit Function
                  If (Not ps.AddLabel("lblPOCON_PEC",0 , 200 , 450, 1700, 380, "Edge of P" & vPort)) Then Exit Function
                  If (Not ps.AddCombo("cboPOCON_PEC", 2000 , 400, 2000, 315)) Then Exit Function
                  With ps.ControlCollection("cboPOCON_PEC")
                    .Clear
                    .AddItem "Sharp edge mode"
                    .AddItem "Medium edge mode"
                    .AddItem "Soft edge mode"
                    .AddItem "No edge mode"
                  End With
              End If

      End Select


      Select Case (cPort("P" & vPort).OutputDriverType)
          Case 1  ' for each pin

          Case 2  ' for each nible
              For iNible = 0 To 3  ' check if not empty frame
                sOutputDriverCharcReg = GetOutputDriverCharcRegNameNiblewise(vPort, iNible)
                If (r.Regs.BitFind(sOutputDriverCharcReg) = True) Then
                  '-------------------------------
                  ' frame: frmPOCON_PDCx
                  '-------------------------------
                  If (Not ps.AddFrame("fraPOCON_PDCx", iLeftPosition , 100, 4200, 5200, "Driver Characteristic Control")) Then Exit Function
                    For iNible = 0 To 3
                      sPin = "P" & vPort & "_" & iNible
                      sOutputDriverCharcReg = GetOutputDriverCharcRegNameNiblewise(vPort, iNible)
                      If (r.Regs.BitFind(sOutputDriverCharcReg) = True) Then
                        If (Not ps.AddLabel("lblPOCON_PDC" & iNible,0 , 200 , 1200  * iNible + 350, 1700, 380, "Driver of P" & vPort & "." & 4 * iNible & " - P" & vPort & "." & 3 + 4 * iNible)) Then Exit Function
                        If (Not ps.AddCombo("cboPOCON_PDC" & iNible, 2000 , 1200  * iNible + 300, 2000, 315)) Then Exit Function
                        With ps.ControlCollection("cboPOCON_PDC" & iNible)
                          .Clear
                          .AddItem "Strong driver"
                          .AddItem "Medium driver"
                          '.AddItem "Weak driver"
                        End With
                      End If
                    Next iNible
                  Exit For
                End If
              Next iNible

          Case 3  ' for each byte

          Case 4  ' for each port

      End Select
    End If
  End If

  OnChangingPageOutputCharacteristic = True
  Exit Function

OnChangingPageOutputCharacteristicError:
  Errorbox "Can not change page", Err.Description
  Exit Function
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 OnShowPageOutputCharacteristic(ps As Peripherals, Controlname As String, Pagename As String, v() As Variant) As Boolean
  On Error Goto OnShowPageOutputCharacteristicError
  
  Dim bDisabled As Boolean
  Dim r As ProjectData
  Dim p As Parameters
  Dim c As Collection
  Dim e As Collection
  Dim vPort As Variant
  Dim iFirstPin As Integer
  Dim iLastPin As Integer
  Dim iPin As Integer
  Dim iNible As Integer
  Dim iByte As Integer
  Dim sPin As String
  Dim sPort As String
  Dim cPort As DAvECollection
  Dim sDirectionReg As String
  Dim sOutputEdgeCharcReg As String
  Dim sOutputDriverCharcReg As String


  OnShowPageOutputCharacteristic = False

  Set r = ps.ProjectData
  Set c = ps.ControlCollection
  Set e = ps.ExtenderCollection
  Set p = ps.Parameters
  Set cPort = ObjectCollection("PORTS")

  vPort = p.v("PortIndex")
  iFirstPin = p.v("FirstPinIndex")
  iLastPin = p.v("LastPinIndex")

  '------------------------------------------------------------------------------------
  ' page: Output Characteristic (action)
  '------------------------------------------------------------------------------------
  If (Pagename = "Output Characteristic") Then
    sPort = "P" & vPort               ' P2

    '--------------------------------------------------------------------------------
    ' control: cboPOCON_PECx (action)
    '--------------------------------------------------------------------------------
    Select Case (cPort("P" & vPort).OutputEdgeType)
        Case 2  ' for each nible
            For iNible = 0 To 3
              sOutputEdgeCharcReg = GetOutputEdgeCharcRegNameNiblewise(vPort, iNible)
              ' action on clicking the selected control
              If (r.Regs.BitFind(sOutputEdgeCharcReg) = True) Then
                If (Controlname =  "cboPOCON_PEC" & iNible) Then
                  Select Case (v(1))
                      Case ("Sharp edge mode")
                          r.v(sOutputEdgeCharcReg) = (r.v(sOutputEdgeCharcReg) \ 4) * 2^2 + 0
                      Case ("Medium edge mode")
                          r.v(sOutputEdgeCharcReg) = (r.v(sOutputEdgeCharcReg) \ 4) * 2^2 + 1
                      Case ("Soft edge mode")
                          r.v(sOutputEdgeCharcReg) = (r.v(sOutputEdgeCharcReg) \ 4) * 2^2 + 2
                      Case ("No edge mode")
                          r.v(sOutputEdgeCharcReg) = 3
                  End Select
                End If
              End If
            Next iNible

    End Select


    '--------------------------------------------------------------------------------
    ' control: cboPOCON_PDCx (action)
    '--------------------------------------------------------------------------------
    Select Case (cPort("P" & vPort).OutputDriverType)
        Case 1  ' for each pin

        Case 2  ' for each nible
            For iNible = 0 To 3
              sOutputDriverCharcReg = GetOutputDriverCharcRegNameNiblewise(vPort, iNible)
              ' action on clicking the selected control
              If (r.Regs.BitFind(sOutputDriverCharcReg) = True) Then
                If (Controlname =  "cboPOCON_PDC" & iNible) Then
                  Select Case (v(1))
                      Case ("Strong driver")
                          r.v(sOutputDriverCharcReg) = (r.v(sOutputDriverCharcReg) And 3)
                      Case ("Medium driver")
                          r.v(sOutputDriverCharcReg) = (r.v(sOutputDriverCharcReg) And 3) + 4
                  End Select
                End If
              End If
            Next iNible

        Case 3  ' for each byte

        Case 4  ' for each port

    End Select

    '------------------------------------------------------------------------------------
    ' page: Output Characteristic (show)
    '------------------------------------------------------------------------------------

    Select Case (cPort("P" & vPort).OutputEdgeType)
        Case 1  ' for each pin

        Case 2  ' for each nible
            For iNible = 0 To 3
'              sPin = sPort & "_" & iPin                                                ' P2_0
'              sDirectionReg = GetDirectionRegName(vPort, iPin)
              sOutputEdgeCharcReg = GetOutputEdgeCharcRegNameNiblewise(vPort, iNible)

              '------------------------------------------------------------------------------------
              ' control: cboPOCON_PECx (show)
              '------------------------------------------------------------------------------------
              If (r.Regs.BitFind(sOutputEdgeCharcReg) = True) Then
                With c("cboPOCON_PEC" & iNible)
                  ' disable condition
                  bDisabled = False 'r.v(sDirectionReg) = 0
                  .Enabled = Not(bDisabled)
                  .ListIndex = (r.v(sOutputEdgeCharcReg) And &H3)
                End With

                With e("cboPOCON_PEC" & iNible)
                  If (bDisabled) Then
                    ' tooltip if control is disabled 
                    .DisabledTooltipText = TOOLTIP_DISABLED_IF 
'                    If (r.v(sDirectionReg) = 0) Then   ' not output
'                      .DisabledTooltipText = .DisabledTooltipText & FormatDisabledToolTip("the pin is NOT used as output")
'                    End If
                  Else
                    ' tooltip if control is enabled 
                  End If
                End With

                '------------------------------------------------------------------------------------
                ' control: lblPOCON_PECx (show)
                '------------------------------------------------------------------------------------
                With c("lblPOCON_PEC" & iNible)
                  ' same as for cboPOCON_PECx
                  .Enabled = Not(bDisabled)
                End With

                With e("lblPOCON_PEC" & iNible)
                  If (bDisabled) Then
                    ' tooltip if control is disabled 
                    .DisabledTooltipText = e("cboPOCON_PEC" & iNible).DisabledTooltipText
                  Else
                    ' tooltip if control is enabled 
                  End If
                End With
              End If
            Next iNible

        Case 3  ' for each byte

        Case 4  ' for each port

    End Select


    Select Case (cPort("P" & vPort).OutputDriverType)
        Case 1  ' for each pin

        Case 2  ' for each nible
            For iNible = 0 To 3
'              sPin = sPort & "_" & iNible                                                ' P2_0
'              sDirectionReg = GetDirectionRegName(vPort, iNible)
              sOutputDriverCharcReg = GetOutputDriverCharcRegNameNiblewise(vPort, iNible)
              '------------------------------------------------------------------------------------
              ' control: cboPOCON_PDCx (show)
              '------------------------------------------------------------------------------------
              If (r.Regs.BitFind(sOutputDriverCharcReg) = True) Then
                With c("cboPOCON_PDC" & iNible)
                  ' disable condition
                  If (r.v(sOutputDriverCharcReg) = 3) Then
                    .Clear
                    .AddItem "Weak driver"
                    .ListIndex = 0
                    bDisabled = True 'r.v(sDirectionReg) = 0
                  Else
                    .Clear
                    .AddItem "Strong driver"
                    .AddItem "Medium driver"
                    .ListIndex = (r.v(sOutputDriverCharcReg) And &HC) \ 4
                    bDisabled = False 'r.v(sDirectionReg) = 0
                End If
                  .Enabled = Not(bDisabled)
                End With

                With e("cboPOCON_PDC" & iNible)
                  If (bDisabled) Then
                    ' tooltip if control is disabled 
                    .DisabledTooltipText = TOOLTIP_DISABLED_IF 
                      .DisabledTooltipText = .DisabledTooltipText & FormatDisabledToolTip("no edge mode is selected")
'                    If (r.v(sDirectionReg) = 0) Then   ' not output
'                      .DisabledTooltipText = .DisabledTooltipText & FormatDisabledToolTip("the pin is NOT used as output")
'                    End If
                  Else
                    ' tooltip if control is enabled 
                  End If
                End With

                '------------------------------------------------------------------------------------
                ' control: lblPOCON_PDCx (show)
                '------------------------------------------------------------------------------------
                With c("lblPOCON_PDC" & iNible)
                  ' same as for cboPOCON_PDCx
                  .Enabled = Not(bDisabled)
                End With

                With e("lblPOCON_PDC" & iNible)
                  If (bDisabled) Then
                    ' tooltip if control is disabled 
                    .DisabledTooltipText = e("cboPOCON_PDC" & iNible).DisabledTooltipText
                  Else
                    ' tooltip if control is enabled 
                  End If
                End With
              End If
            Next iNible

        Case 3  ' for each byte

        Case 4  ' for each port

    End Select


  End If         ' if Pagename

  OnShowPageOutputCharacteristic = True
  Exit Function

OnShowPageOutputCharacteristicError:
  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 OnLoadPagePadHold(ps As Peripherals) As Boolean
  On Error GoTo OnLoadPagePadHoldError                              
  Dim p As Parameters
  Dim r As ProjectData
  Dim vPort As Variant
  Dim iPin As Integer
  Dim iFirstPin As Integer
  Dim iLastPin As Integer
  Dim sPin As String
  Dim sOutputDriverEnableReg As String

  OnLoadPagePadHold = False

  Set r = ps.ProjectData
  Set p = ps.Parameters

  vPort = p.v("PortIndex")
  iFirstPin = p.v("FirstPinIndex")
  iLastPin = p.v("LastPinIndex")

  For iPin = iFirstPin To iLastPin  ' check if not empty page
    sPin = "P" & vPort & "_" & iPin
    sOutputDriverEnableReg = GetOutputDriverEnableRegName(vPort, iPin)
    If (r.Regs.BitFind(sOutputDriverEnableReg) = True) Then
      If (Not ps.AddPage("Pad Hold")) Then Exit Function
      Exit For
    End If
  Next iPin

  OnLoadPagePadHold = True
  Exit Function

OnLoadPagePadHoldError:
  Exit Function
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 OnChangingPagePadHold(ps As Peripherals, Pagename As String) As Boolean
  On Error Goto OnChangingPagePadHoldError
  Dim p As Parameters
  Dim r As ProjectData
  Dim vPort As Variant
  Dim iPin As Integer
  Dim iFirstPin As Integer
  Dim iLastPin As Integer
  Dim sPin As String
  Dim sOutputDriverEnableReg As String

  OnChangingPagePadHold = False

  Set r = ps.ProjectData
  Set p = ps.Parameters

  vPort = p.v("PortIndex")
  iFirstPin = p.v("FirstPinIndex")
  iLastPin = p.v("LastPinIndex")

  If (Pagename = "Pad Hold") Then
    If (Not ps.ExtenderCollection("Pad Hold").Loaded) Then
      ps.ExtenderCollection("Pad Hold").Loaded = True

      For iPin = iFirstPin To iLastPin  ' check if not empty frame
        sPin = "P" & vPort & "_" & iPin
        sOutputDriverEnableReg = GetOutputDriverEnableRegName(vPort, iPin)
        If (r.Regs.BitFind(sOutputDriverEnableReg) = True) Then
          '-------------------------------
          ' frame: frmPHEN
          '-------------------------------
          If (Not ps.AddFrame("fraPHEN", 100 , 100, 4200, 5200, "Output Driver Enable In Power Down Mode")) Then Exit Function
            For iPin = iFirstPin To iLastPin
              sPin = "P" & vPort & "_" & iPin
              sOutputDriverEnableReg = GetOutputDriverEnableRegName(vPort, iPin)
              If (r.Regs.BitFind(sOutputDriverEnableReg) = True) Then
                If (Not ps.AddCheck("chkPHEN" & iPin, 200 , 300  * iPin + 250, 3900, 380, "Enable P" & vPort & "." & iPin & " output driver in power down mode")) Then Exit Function
              End If
            Next iPin
          Exit For
        End If
      Next iPin

    End If
  End If

  OnChangingPagePadHold = True
  Exit Function

OnChangingPagePadHoldError:
  Errorbox "Can not change page", Err.Description
  Exit Function
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 OnShowPagePadHold(ps As Peripherals, Controlname As String, Pagename As String, v() As Variant) As Boolean
  On Error Goto OnShowPagePadHoldError
  
  Dim bDisabled As Boolean
  Dim r As ProjectData
  Dim p As Parameters
  Dim c As Collection
  Dim e As Collection
  Dim vPort As Variant
  Dim iFirstPin As Integer
  Dim iLastPin As Integer
  Dim iPin As Integer
  Dim sPin As String
  Dim sPort As String
'  Dim cp As Object
'  Dim cPort As DAvECollection
  Dim sDirectionReg As String
  Dim sOutputDriverEnableReg As String


  OnShowPagePadHold = False

  Set r = ps.ProjectData
  Set c = ps.ControlCollection
  Set e = ps.ExtenderCollection
  Set p = ps.Parameters
'  Set cp = ObjectCollection("PORTPINS")
'  Set cPort = ObjectCollection("PORTS")

  vPort = p.v("PortIndex")
  iFirstPin = p.v("FirstPinIndex")
  iLastPin = p.v("LastPinIndex")

  '------------------------------------------------------------------------------------
  ' page: Pad Hold (action)
  '------------------------------------------------------------------------------------
  If (Pagename = "Pad Hold") Then
    sPort = "P" & vPort               ' P2
 
    For iPin = iFirstPin To iLastPin
      sPin = sPort & "_" & iPin                                                ' e.g. P2_0
      sOutputDriverEnableReg = GetOutputDriverEnableRegName(vPort, iPin)

      '--------------------------------------------------------------------------------
      ' control: chkPHENx (action)
      '--------------------------------------------------------------------------------
      ' action on clicking the selected control
      If (r.Regs.BitFind(sOutputDriverEnableReg) = True) Then
        If (Controlname =  "chkPHEN" & iPin) Then
          r.v(sOutputDriverEnableReg) = v(1)
        End If
      End If

    Next iPin

    '------------------------------------------------------------------------------------
    ' page: Pad Hold (show)
    '------------------------------------------------------------------------------------
    For iPin = iFirstPin To iLastPin
      sPin = sPort & "_" & iPin                                                ' P2_0
      sDirectionReg = GetDirectionRegName(vPort, iPin)
      sOutputDriverEnableReg = GetOutputDriverEnableRegName(vPort, iPin)
      '------------------------------------------------------------------------------------
      ' control: chkPHENx (show)
      '------------------------------------------------------------------------------------
      If (r.Regs.BitFind(sOutputDriverEnableReg) = True) Then
        With c("chkPHEN" & iPin)
          ' disable condition
          bDisabled = r.v(sDirectionReg) <> 1
          .Enabled = Not(bDisabled)
          .Value = -CInt(r.v(sOutputDriverEnableReg) = 1)
        End With

        With e("chkPHEN" & iPin)
          If (bDisabled) Then
            ' tooltip if control is disabled 
            .DisabledTooltipText = TOOLTIP_DISABLED_IF 
            If (r.v(sDirectionReg) <> 1) Then   ' not output
              .DisabledTooltipText = .DisabledTooltipText & FormatDisabledToolTip("the pin is NOT used as output")
            End If
          Else
            ' tooltip if control is enabled 
          End If
        End With
      End If
    Next iPin

  End If         ' if Pagename

  OnShowPagePadHold = True
  Exit Function

OnShowPagePadHoldError:
  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
  Dim r As ProjectData
  Dim p As Parameters
  Dim vPort As Variant
  Dim iPin As Integer
  Dim iFirstPin As Integer
  Dim iLastPin As Integer
  Dim sPin As String
  Dim cp As Object
  Dim sIndex As String

  Dim c As Collection
  Dim e As Collection

  OnChangingPageParameters = False

  Set r = ps.ProjectData
  Set p = ps.Parameters
  Set c = ps.ControlCollection
  Set e = ps.ExtenderCollection
  Set p = ps.Parameters
  Set cp = ObjectCollection("PORTPINS")

  sIndex = "IO"

  vPort = p.v("PortIndex")
  iFirstPin = p.v("FirstPinIndex")
  iLastPin = p.v("LastPinIndex")

  If (Not e("Parameters").Loaded) And (Pagename = "Parameters") Then
    e("Parameters").Loaded = True
    If (Not ps.AddFrame("fraPara", 100 , 100, 4150, 5200, "Parameters")) Then Exit Function
    For iPin = iFirstPin To iLastPin
      sPin = "P" & vPort & "_" & iPin
      If (cp.Use(sPin) = TRUE) Then
        If (Not ps.AddText("txt_" & sPin, 200, 300 + iPin * 300, 3800, 325)) Then Exit Function
      End If
    Next iPin
  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
  
  Dim r As ProjectData
  Dim p As Parameters
  Dim c As Collection
  Dim e As Collection
  Dim bDisabled As Boolean

  Dim vPort As Variant
  Dim iPin As Integer
  Dim iFirstPin As Integer
  Dim iLastPin As Integer
  Dim sPin As String
  Dim cp As Object
  Dim sIndex As String

  OnShowPageParameters = False

  Set r = ps.ProjectData
  Set p = ps.Parameters
  Set c = ps.ControlCollection
  Set e = ps.ExtenderCollection
  Set cp = ObjectCollection("PORTPINS")

  sIndex = "IO"

  vPort = p.v("PortIndex")
  iFirstPin = p.v("FirstPinIndex")
  iLastPin = p.v("LastPinIndex")

  '------------------------------------------------------------------------------------
  ' page: Parameters (action)
  '------------------------------------------------------------------------------------
  If (Pagename = "Parameters") Then
    For iPin = iFirstPin To iLastPin
      sPin = "P" & vPort & "_" & iPin
      '--------------------------------------------------------------------------------
      ' control: txtPx_y (action)
      '--------------------------------------------------------------------------------
      ' action on clicking the selected control
      If (cp.Use(sPin) = TRUE) Then
        If (Controlname =  "txt_" & sPin) Then
          If (IsInputFormatOK(v(1), FORMAT_PARAMETERS, sPin, , )) Then
            r.v(sIndex & "_PARA_" & sPin) = v(1)
          Else
            Exit Function
          End If
        End If
      End If
    Next iPin
  '------------------------------------------------------------------------------------
  ' page: Parameters (show)
  '------------------------------------------------------------------------------------
    For iPin = iFirstPin To iLastPin
      sPin = "P" & vPort & "_" & iPin
      If (cp.Use(sPin) = True) Then
        '------------------------------------------------------------------------------
        ' control: txtPx_y (show)
        '------------------------------------------------------------------------------
        With c("txt_" & sPin)
          ' disable condition
          bDisabled = False
          .Enabled = Not(bDisabled)
          ' show condition
          .Text = FormatTextOutput(r.v(sIndex & "_PARA_" & sPin), "S")
        End With

        With e("txt_" & sPin)
          If (bDisabled) Then
            ' tooltip if control is disabled 
'            .DisabledTooltipText = TOOLTIP_DISABLED_IF
'            .DisabledToolTipText = .DisabledToolTipText & ""
          Else
            ' tooltip if control is enabled 
            .EnabledTooltipText = FormatNormalTooltip(TOOLTIP_ENABLED_PARAMETER_NAME)
          End If
        End With

      End If
    Next iPin

  End If         ' if Pagename

  OnShowPageParameters = True
  Exit Function

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

 
