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

Option Explicit
'*****************************************************************************
' Module:
'        IO
'-----------------------------------------------------------------------------
' Description:
'        This file contains functions which are create controls for the
'        module 'IO'.
'        Furthermore it contains functions for the event handling of
'        these controls.
'
'-----------------------------------------------------------------------------
' History:
'
'        Rev. Author         Date        Description
'        ---------------------------------------------------------------------
'        0.1  Sunil          06-10-04    initial version
'        0.2  Sunil          13-12-04    when tristate is selected under Pull Device Page
'                                        Px_PUDSEL is reset to default value ("1").
'
'
'*****************************************************************************

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

    OnLoad = False

    If (Not OnLoadPagePortx(ps)) Then Exit Function
    If (Not OnLoadPagePullDevice(ps)) Then Exit Function
    If (Not OnLoadPageParameters(ps)) Then Exit Function
    If (Not OnLoadPageNotes(ps)) Then Exit Function

    ' call function to initialize the menu
    If (Not InitMenu(ps, "IO\Dialog" & ps.Parameters.v("PortIndex"))) Then Exit Function

    OnLoad = True

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 successful operation
'        False           else
'*****************************************************************************
Function OnChangingPage(ps As Peripherals, Page As Integer, Pagename As String) As Boolean
Dim Port As String

        OnChangingPage = False
        
        Port = ps.Parameters.v("PortIndex")
    
        If (Not OnChangingPagePortx(ps, Pagename)) Then Exit Function
        If (Not OnChangingPagePullDevice(ps, Pagename)) Then Exit Function
        If (Not OnChangingPageParameters(ps, Pagename)) Then Exit Function
        If (Not OnChangingPageNotes(ps, Pagename, "P" & Port)) Then Exit Function
        OnChangingPage = True

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 successful operation
'        False           else
'*****************************************************************************
Function OnShow(ps As Peripherals, Controlname As String, v() As Variant, Page As Integer, Pagename As String) As Boolean
Dim Port As String
        OnShow = False

            Port = ps.Parameters.v("PortIndex")
            
            If (Not OnShowPagePortx(ps, Controlname, Pagename, v)) Then Exit Function
            If (Not OnShowPagePullDevice(ps, Controlname, Pagename, v)) Then Exit Function
            If (Not OnShowPageParameters(ps, Controlname, Pagename, v)) Then Exit Function
            If (Not OnShowPageNotes(ps, Controlname, Pagename, "P" & Port, v)) Then Exit Function

        OnShow = True
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 successful operation
'        False           else
'*****************************************************************************
Function OnMenuClick(ps As Peripherals, Toolname As String, ToolId As Long, Tooltext As String) As Boolean
    On Error GoTo OnMenuClickError

    OnMenuClick = False

    If (Not MenuClick(ps, Toolname, ToolId, Tooltext, "IO\Dialog" & 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 successful operation
'        False           else
'*****************************************************************************
Public Function OnLoadPagePortx(ps As Peripherals) As Boolean
  On Error Resume Next
  Dim Port As String
  
  OnLoadPagePortx = False

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

  OnLoadPagePortx = 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 successful operation
'        False           else
'*****************************************************************************
Function OnChangingPagePortx(ps As Peripherals, Pagename As String) As Boolean
 On Error Resume Next
Dim vPort As Variant
Dim iFirstPin, iLastPin, iPin, iDumPin  As Integer
Dim sPin As String
Dim cp As PortPin

  OnChangingPagePortx = False
  
  Set cp = ObjectCollection("PORTPINS")
  
  vPort = ps.Parameters.v("PortIndex")
  
  If (Not ps.ExtenderCollection("Port " & vPort).Loaded) And (Pagename = "Port " & vPort) Then
    ps.ExtenderCollection("Port " & vPort).Loaded = True
  
        
        iFirstPin = ps.Parameters.v("FirstPinIndex")
        iLastPin = ps.Parameters.v("LastPinIndex")
        
        If (Not ps.AddFrame("fraPagex", 100, 50, 8450, 5325, "")) Then Exit Function

        If (Not ps.AddLabel("lblFunction" & vPort, 0, 200, 200, 1575, 380, "Port Function:")) Then Exit Function
        If vPort = 2 Then
            If (Not ps.AddLabel("lblDirection" & vPort, 0, 3000, 200, 1575, 380, "Input Driver:")) Then Exit Function
        Else
            If (Not ps.AddLabel("lblDirection" & vPort, 0, 3000, 200, 1575, 380, "Port Direction:")) Then Exit Function
        End If
        If (Not ps.AddLabel("lblOpenDrain" & vPort, 0, 5100, 200, 2000, 380, "Push Pull / Open Drain:")) Then Exit Function
        If (Not ps.AddLabel("lblOutput" & vPort, 0, 7200, 200, 1575, 380, "Output Value:")) Then Exit Function
        
        iDumPin = 0
    
        For iPin = iFirstPin To iLastPin
            
              sPin = "P" & vPort & "_" & iPin
              If cp.Use(sPin) Then
                If (Not ps.AddCheck("chkUse" & sPin, 200, 300 * iDumPin + 400, 2000, 380, "Use P" & vPort & "." & iPin & " as general IO")) Then Exit Function
                If vPort = 2 Then
                    If (Not ps.AddCheck("chkDriver" & sPin, 3000, 300 * iDumPin + 400, 2000, 380, "Deactivate input driver")) Then Exit Function
                Else
                    If (Not ps.AddInOut("optDir" & sPin, 3000, 300 * iDumPin + 475)) Then Exit Function
                End If
                If (Not ps.AddCheck("chkOD" & sPin, 5100, 300 * iDumPin + 400, 1100, 380, "Open drain")) Then Exit Function
                If (Not ps.AddCheck("chkHigh" & sPin, 7200, 300 * iDumPin + 400, 650, 380, "High")) Then Exit Function
                iDumPin = iDumPin + 1
            End If
        Next iPin
    
  End If
  OnChangingPagePortx = 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 successful operation
'        False           else
'*****************************************************************************
Public Function OnShowPagePortx(ps As Peripherals, Controlname As String, Pagename As String, v() As Variant) As Boolean
 On Error GoTo OnShowPagePortxError
 
  Dim bDisabled As Boolean
  Dim r As ProjectData
  Dim p As Parameters
  Dim c As Collection
  Dim e As Collection
  Dim iPort As Integer
  Dim iFirstPin As Integer
  Dim iLastPin As Integer
  Dim iPin As Integer
  Dim sPin As String
  Dim sDirectionReg As String
  Dim sOutReg As String
  Dim sOpenDrainReg As String
  Dim sPort As String
  Dim cp As PortPin
 
  Set r = ps.ProjectData
  Set c = ps.ControlCollection
  Set e = ps.ExtenderCollection
  Set p = ps.Parameters
  Set cp = ObjectCollection("PORTPINS")

  OnShowPagePortx = False
  
  iPort = p.v("PortIndex")
  iFirstPin = p.v("FirstPinIndex")
  iLastPin = p.v("LastPinIndex")
     
  If Pagename = "Port " & iPort Then
        
    '---------------------------------------------
    ' (action)
    '---------------------------------------------
    
    For iPin = iFirstPin To iLastPin
        sPin = "P" & iPort & "_" & iPin
        
        If cp.Use(sPin) Then
            sDirectionReg = "P" & iPort & "_DIR_P" & iPin
            sOutReg = "P" & iPort & "_DATA_P" & iPin
            sOpenDrainReg = "P" & iPort & "_OD_P" & iPin
            
      '--------------------------------------------------------------------------------
      ' control: chkUsePx_y (action)
      '--------------------------------------------------------------------------------
        If (Controlname = "chkUse" & sPin) Then
              If (v(1) = 0 And r.v("MuxFor_" & sPin) <> 0) Then
                        ' the pin is no longer used for general I/O
                  r.v(sDirectionReg) = 0 'set to input direction
                  If iPort <> 2 Then
                    r.v(sOutReg) = 0    'set port pin back to reset value
                    r.v(sOpenDrainReg) = 0
                  End If
               End If
          r.v("MuxFor_" & sPin) = v(1)
        End If
        
      '------------------------------------------------------------------------------------
      ' control: chkDriverPx_y
      '------------------------------------------------------------------------------------
        If iPort = 2 Then
            If (Controlname = "chkDriver" & sPin) Then
                r.v(sDirectionReg) = v(1)
                If v(1) = 1 Then
                    r.v("MuxFor_" & sPin) = 2
                Else
                    r.v("MuxFor_" & sPin) = 1
                End If
            End If
        End If
          
      '------------------------------------------------------------------------------------
      ' control: optDirPx_y (action)
      '------------------------------------------------------------------------------------
          If iPort <> 2 Then
          
              If (Controlname = "optDir" & sPin) Then
                If (v(1) = 0) Then
                    r.v(sOutReg) = 0    'direction=input --> set port pin back to reset value
                    r.v("MuxFor_" & sPin) = 1
                Else
                    r.v("MuxFor_" & sPin) = 2
                End If
                r.v(sDirectionReg) = v(1)
              End If
              
    
    '------------------------------------------------------------------------------------
    ' control: chkODx_y (action)
    '------------------------------------------------------------------------------------
              If (Controlname = "chkOD" & sPin) Then
                    r.v(sOpenDrainReg) = v(1)
              End If
          
    '------------------------------------------------------------------------------------
    ' control: chkHighx_y (action)
    '------------------------------------------------------------------------------------
              If (Controlname = "chkHigh" & sPin) Then
                    r.v(sOutReg) = v(1)
              End If
          
          End If
      
        End If
      Next iPin
    
    '--------------------------------------------
    ' (show)
    '--------------------------------------------
    
    ' Port - 2 is input only port so direction, open drain and output value control
    ' are deactivated
    
    If iPort = 2 Then
        For iPin = iFirstPin To iLastPin
            sPin = "P" & iPort & "_" & iPin
            sDirectionReg = "P" & iPort & "_DIR_P" & iPin
           ' c("optDir" & sPin).Enabled = False
           ' e("optDir" & sPin).DisabledTooltipText = TOOLTIP_DISABLED_IF & FormatDisabledToolTip("Port-2 is Input only Port")
            c("chkOD" & sPin).Enabled = False
            e("chkOD" & sPin).DisabledTooltipText = TOOLTIP_DISABLED_IF & FormatDisabledToolTip("Port-2 is Input only Port")
            c("chkHigh" & sPin).Enabled = False
            e("chkHigh" & sPin).DisabledTooltipText = TOOLTIP_DISABLED_IF & FormatDisabledToolTip("Port-2 is Input only Port")
            
            With c("chkUse" & sPin)
                bDisabled = (cp.Use(sPin) = False) Or (IsGeneralPinFree(r, sPin) = False)
                .Enabled = Not (bDisabled)
                .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 available")
                    Else
                        .DisabledTooltipText = .DisabledTooltipText & GetDisabledGeneralPinTooltip(r, sPin)
                    End If
                Else
                    .EnabledToolTipText = GetEnabledGeneralPinTooltip(r, sPin)
                End If
            End With

            With c("chkDriver" & sPin)
                bDisabled = (r.v("MuxFor_" & sPin) = 0)
                .Enabled = Not (bDisabled)
                .Value = r.v(sDirectionReg)
            End With

            With e("chkDriver" & sPin)
                If (bDisabled) Then
                    .DisabledTooltipText = TOOLTIP_DISABLED_IF
                    If (cp.Use(sPin) = False) Then
                        .DisabledTooltipText = .DisabledTooltipText & FormatDisabledToolTip("the pin is not available")
                    Else
                        .DisabledTooltipText = .DisabledTooltipText & GetDisabledGeneralPinTooltip(r, sPin)
                            If (r.v("MuxFor_" & sPin) = 0) Then
                              .DisabledTooltipText = .DisabledTooltipText & FormatDisabledToolTip _
                                                     ("the pin is NOT selected as general IO")
                            End If
                    End If
                Else
                    .EnabledToolTipText = GetEnabledGeneralPinTooltip(r, sPin)
                End If
            End With
            
        Next iPin
      
        OnShowPagePortx = True
        Exit Function
        
     End If
     
      ' -------------------------------------------------------
      ' Ports 0 , 1  and 3
      ' -------------------------------------------------------
      
      For iPin = iFirstPin To iLastPin
        sPin = "P" & iPort & "_" & iPin
        If cp.Use(sPin) Then
            sDirectionReg = "P" & iPort & "_DIR_P" & iPin
            sOutReg = "P" & iPort & "_DATA_P" & iPin
            sOpenDrainReg = "P" & iPort & "_OD_P" & iPin
            
      '------------------------------------------------------------------------------------
      ' control: chk_UsePx_y (show)
      '------------------------------------------------------------------------------------
              
            With c("chkUse" & sPin)
                bDisabled = (cp.Use(sPin) = False) Or (IsGeneralPinFree(r, sPin) = False)
                .Enabled = Not (bDisabled)
                .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 available")
                    Else
                        .DisabledTooltipText = .DisabledTooltipText & GetDisabledGeneralPinTooltip(r, sPin)
                    End If
                Else
                    .EnabledToolTipText = GetEnabledGeneralPinTooltip(r, sPin)
                End If
            End With
      
      '------------------------------------------------------------------------------------
      ' control: optDirPx_y (show)
      '------------------------------------------------------------------------------------
      
            With c("optDir" & sPin)
                bDisabled = (r.v("MuxFor_" & sPin) = 0)
                .Enabled = Not (bDisabled)
                .Value = r.v(sDirectionReg) = 1
            End With
            
            With e("optDir" & sPin)
                If (bDisabled) Then
                    .DisabledTooltipText = TOOLTIP_DISABLED_IF
                    If (cp.Use(sPin) = False) Then
                        .DisabledTooltipText = .DisabledTooltipText & FormatDisabledToolTip("the pin is not available")
                    Else
                        .DisabledTooltipText = .DisabledTooltipText & GetDisabledGeneralPinTooltip(r, sPin)
                            If (r.v("MuxFor_" & sPin) = 0) Then
                              .DisabledTooltipText = .DisabledTooltipText & FormatDisabledToolTip _
                                                     ("the pin is NOT selected as general IO")
                            End If
                    End If
                Else
                    .EnabledToolTipText = GetEnabledGeneralPinTooltip(r, sPin)
                End If
            End With
            
      '------------------------------------------------------------------------------------
      ' control: optODPx_y (show)
      '------------------------------------------------------------------------------------
        
            With c("chkOD" & sPin)
                '  disabled if not output direction (considering the alternative output functions)
                bDisabled = (r.v(sDirectionReg) = 0)
                .Enabled = Not (bDisabled)
                .Value = r.v(sOpenDrainReg)
            End With
            
            With e("chkOD" & sPin)
              If (bDisabled) Then
                If Not (r.v("MuxFor_" & sPin) = "1") Then
                    .DisabledTooltipText = TOOLTIP_DISABLED_IF & FormatDisabledToolTip _
                                     ("the pin is NOT selected as general IO")
                Else
                    .DisabledTooltipText = TOOLTIP_DISABLED_IF & FormatDisabledToolTip _
                                     ("the pin is NOT used as output")
                End If
              Else
                ' tooltip if control is enabled
              End If
            End With
            
      '------------------------------------------------------------------------------------
      ' control: chkHighPx_y (show)
      '------------------------------------------------------------------------------------
      
            With c("chkHigh" & sPin)
                ' disabled if not general IO or not output direction
                bDisabled = (r.v("MuxFor_" & sPin) = 0) Or (r.v(sDirectionReg) = 0)
                .Enabled = Not (bDisabled)
                .Value = r.v(sOutReg)
            End With
           
           With e("chkHigh" & sPin)
                If (bDisabled) Then
                  If Not (r.v("MuxFor_" & sPin) = "1") Then
                    .DisabledTooltipText = TOOLTIP_DISABLED_IF & FormatDisabledToolTip _
                                     ("the pin is NOT selected as general IO")
                  Else
                    .DisabledTooltipText = TOOLTIP_DISABLED_IF & 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
  

  OnShowPagePortx = True
  Exit Function

OnShowPagePortxError:
    Errorbox "error", 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 successful operation
'        False           else
'*****************************************************************************
Public Function OnLoadPagePullDevice(ps As Peripherals) As Boolean
  On Error Resume Next

  OnLoadPagePullDevice = False

  If (Not ps.AddPage("Pull Device")) Then Exit Function

  OnLoadPagePullDevice = 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 successful operation
'        False           else
'*****************************************************************************
Function OnChangingPagePullDevice(ps As Peripherals, Pagename As String) As Boolean
  On Error Resume Next
  Dim iPin, iFirstPin, iLastPin, iDumPin As Integer
  Dim vPort As Variant
  Dim bPinAvailable As Boolean
  Dim sPin As String
  Dim c As Collection
  Dim cp As PortPin
  
  OnChangingPagePullDevice = False
  
  Set cp = ObjectCollection("PORTPINS")
   Set c = ps.ControlCollection
   
  If (Not ps.ExtenderCollection("Pull Device").Loaded) And (Pagename = "Pull Device") Then
    ps.ExtenderCollection("Pull Device").Loaded = True
  
        vPort = ps.Parameters.v("PortIndex")
        iFirstPin = ps.Parameters.v("FirstPinIndex")
        iLastPin = ps.Parameters.v("LastPinIndex")
        '-------------------------------
        ' frame: frmPUDEN
        '-------------------------------
        If (Not ps.AddFrame("fraPullDevice", 100, 100, 4100, 5200, "Pull Device")) Then Exit Function
        
          iDumPin = 0
          
          For iPin = iFirstPin To iLastPin
             sPin = "P" & vPort & "_" & iPin
             If cp.Use(sPin) Then
                If (Not ps.AddLabel("lblPullSel" & sPin, 0, 200, 300 * iDumPin + 350, 1575, 380, "P" & vPort & "." & iPin & " pull device")) Then Exit Function
                If (Not ps.AddCombo("cboPullSel" & sPin, 1825, 300 * iDumPin + 300, 2000, 315)) Then Exit Function
              
                With ps.ControlCollection("cboPullSel" & sPin)
                    .Clear
                    .AddItem "Tristate"
                    .AddItem "Pull down device"
                    .AddItem "Pull up device"
                End With
              
              iDumPin = iDumPin + 1
           End If
           
          Next iPin
            
  End If
  
  OnChangingPagePullDevice = 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 successful operation
'        False           else
'*****************************************************************************
Public Function OnShowPagePullDevice(ps As Peripherals, Controlname As String, Pagename As String, v() As Variant) As Boolean
  On Error GoTo OnShowPagePullDeviceError
  
  Dim bDisabled As Boolean
  Dim r As ProjectData
  Dim p As Parameters
  Dim c As Collection
  Dim e As Collection
  Dim iPin, iFirstPin, iLastPin As Integer
  Dim sPort As String
  Dim sPullupPulldownEnableReg As String
  Dim sPullupPulldownSelectReg As String
  Dim sPin As String
  Dim bPinAvailable As Boolean
  Dim cp As PortPin
    
  OnShowPagePullDevice = False
  Set c = ps.ControlCollection
  Set p = ps.Parameters
  Set r = ps.ProjectData
  Set cp = ObjectCollection("PORTPINS")
        
        
        If Pagename = "Pull Device" Then
        
            sPort = p.v("PortIndex")
            iFirstPin = p.v("FirstPinIndex")
            iLastPin = p.v("LastPinIndex")
            
            '--------------------------------------------
            ' (action)
            '--------------------------------------------
            
            For iPin = iFirstPin To iLastPin
                
                sPin = "P" & sPort & "_" & iPin
                
                If cp.Use(sPin) Then
                    If Controlname = "cboPullSel" & sPin Then
                        sPullupPulldownEnableReg = "P" & sPort & "_PUDEN_P" & iPin
                        sPullupPulldownSelectReg = "P" & sPort & "_PUDSEL_P" & iPin
                        
                        Select Case (v(1))
                            Case ("Tristate")
                                r.v(sPullupPulldownEnableReg) = 0
                                r.v(sPullupPulldownSelectReg) = 1
                            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
            
            '--------------------------------------------
            ' (show)
            '--------------------------------------------
            For iPin = iFirstPin To iLastPin
                
                sPin = "P" & sPort & "_" & iPin
                If cp.Use(sPin) Then
                    sPullupPulldownEnableReg = "P" & sPort & "_PUDEN_P" & iPin
                    sPullupPulldownSelectReg = "P" & sPort & "_PUDSEL_P" & iPin
                    
                    With c("cboPullSel" & sPin)
            '          disable condition
                      bDisabled = False
                      .Enabled = Not (bDisabled)
                      If (r.v(sPullupPulldownEnableReg) = 0) Then .ListIndex = 0
                      If (r.v(sPullupPulldownEnableReg) = 1 And r.v(sPullupPulldownSelectReg) = 0) Then .ListIndex = 1
                      If (r.v(sPullupPulldownEnableReg) = 1 And r.v(sPullupPulldownSelectReg) = 1) Then .ListIndex = 2
                    End With
                    
                End If
                
            Next iPin
            
        End If ' If Pagename
              
  OnShowPagePullDevice = True
  Exit Function

OnShowPagePullDeviceError:
Errorbox "error", Err.Description
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 successful 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 successful operation
'        False           else
'*****************************************************************************
Function OnChangingPageParameters(ps As Peripherals, Pagename As String) As Boolean
On Error GoTo ParameterErr

 Dim iPin, iFirstPin, iLastPin, iDumPin As Integer
 Dim vPort As Variant
 Dim sPin  As String
 Dim c As Collection
 Dim cp As PortPin
 
 OnChangingPageParameters = False
 Set c = ps.ControlCollection
 Set cp = ObjectCollection("PORTPINS")
 
 If (Not ps.ExtenderCollection("Parameters").Loaded) And (Pagename = "Parameters") Then
    ps.ExtenderCollection("Parameters").Loaded = True
    
    vPort = ps.Parameters.v("PortIndex")
    iFirstPin = ps.Parameters.v("FirstPinIndex")
    iLastPin = ps.Parameters.v("LastPinIndex")
        
    If (Not ps.AddFrame("fraParameters", 100, 100, 4175, 5200, "Parameters")) Then Exit Function
    
    iDumPin = 0
    For iPin = iFirstPin To iLastPin
        sPin = "P" & vPort & "_" & iPin
        If cp.Use(sPin) Then
            If (Not ps.AddLabel("lbl" & sPin, 0, 200, 350 + iDumPin * 300, 1575, 380, CStr(sPin))) Then Exit Function
            If (Not ps.AddText("txt" & sPin, 1825, 300 + iDumPin * 300, 1900, 315)) Then Exit Function
            c("txt" & sPin).Text = sPin
            iDumPin = iDumPin + 1
        End If
    Next iPin
    
 End If
 
 OnChangingPageParameters = True
 
 Exit Function
 
ParameterErr:
    Errorbox "Error", Err.Description
    
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 successful operation
'        False           else
'*****************************************************************************
Function OnShowPageParameters(ps As Peripherals, Controlname As String, Pagename As String, v() As Variant) As Boolean
 On Error GoTo OnShowError
  
  Dim r As ProjectData
  Dim p As Parameters
  Dim c As Collection
  Dim e As Collection

  Dim iPort As Variant
  Dim iPin As Integer
  Dim iFirstPin As Integer
  Dim iLastPin As Integer
  Dim sPin As String
  Dim cp As PortPin

  OnShowPageParameters = False

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

  iPort = 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" & iPort & "_" & iPin
        '--------------------------------------------------------------------------------
        ' control: txtPx_y (action)
        '--------------------------------------------------------------------------------
        If (cp.Use(sPin) = True) Then
            If (Controlname = "txt" & sPin) Then
                If (IsInputFormatOK(v(1), FORMAT_PARAMETERS, c("lbl" & sPin).Caption)) Then
                    r.v("P" & iPort & "_NAME_" & sPin) = v(1)
                Else
                    Exit Function
                End If
            End If
        End If
    Next iPin
  '------------------------------------------------------------------------------------
  ' page: Parameters (show)
  '------------------------------------------------------------------------------------
    For iPin = iFirstPin To iLastPin
        sPin = "P" & iPort & "_" & iPin
        If (cp.Use(sPin) = True) Then
            '------------------------------------------------------------------------------
            ' control: txtPx_y (show)
            '------------------------------------------------------------------------------
            c("txt" & sPin).Text = FormatTextOutput(r.v("P" & iPort & "_NAME_" & sPin), "S")
        End If
    Next iPin

  End If         ' if Pagename

  OnShowPageParameters = True
  Exit Function

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

 
