VERSION 1.0 CLASS
BEGIN
  MultiUse = -1  'True
END
Attribute VB_Name = "PortPin"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = True
Attribute VB_PredeclaredId = False
Attribute VB_Exposed = True
Option Explicit
'*****************************************************************************
' Module:
'        PortPin.cls
'-----------------------------------------------------------------------------
' Description:
'        This file contains the properties and functions of this class.
'
'-----------------------------------------------------------------------------
' History:
'                 
'        Rev. Author         Date        Description
'        ---------------------------------------------------------------------
'        1.0  ste            29.09.99    final version
'        1.1  ste            23.02.00    private variables (bDP, bODP, bP, bPUDEN, bPUDSEL, bPHEN)
'                                        deleted
'
'
'*****************************************************************************

'-----------------------------------------------------
' type for alternate names which belongs to a pin
'-----------------------------------------------------
Const MAX_ALTERNATE_NAMES_FOR_EACH_PIN = 10

Private Type TypeAlternameNames
	s(MAX_ALTERNATE_NAMES_FOR_EACH_PIN) As String	
End Type

'-----------------------------------------------------
' variables for pins
'-----------------------------------------------------
' number of pins
Private lCount As Long
' name
Private sNameArr() As String
' name index
Private lNameArr() As Long
' port number
Private vPortArr() As Variant
' pin number
Private iPinArr() As Integer
' description
Private sDescriptionArr() As String

'bUse
Private bUseArr() As Boolean
'bExclusive
Private bExclusiveArr() As Boolean


'alternate pins
Private sAlternatenames() As TypeAlternameNames
' number of alternate names for each pin
Private lAlternatePinCount() As Long


'-----------------------------------------------------
' variables for alternate names of pins
'-----------------------------------------------------
' number of alternate names
Private lAlternateCount As Long
' alternate names
Private sAlternateNameArr() As String
' alternate names index
Private lAlternateNameArr() As Long
' alternate pin name arr
Private lAlternateNamePinNameArr() As String
' names of pins fot the alternate names
Private sAlternateNamePinNameArr() As String
' description
Private sAlternateDescriptionArr() As String
' flag that shows if the alternate pin should be used exclusive
Private bAlternateExclusiveArr() As Boolean
' number of the alternate output function of a pin
Private iAlternateFunctionArr() As Integer

'*****************************************************************************
'Description:
'        This function adds a new pin to this class.
'-----------------------------------------------------------------------------
'Parameters:
'        sName           name of the pin (key)
'        vPort           number of the port
'        iPin            number of the pin
'        sDescription    string with the description of the pin
'        bUse            flag that shows if the pin is availible
'        bExclusive      flag that shows if the pin should be used exclusive
'-----------------------------------------------------------------------------
'Returns:
'        True            on successfull operation
'        False           else
'*****************************************************************************
Public Function Add(sName As String, vPort As Variant, iPin As Integer, Optional sDescription As String = "", _
                    Optional bUse As Boolean = False, Optional bExclusive As Boolean = False) As Boolean

  On Error GoTo AddError
  
  Add = False
  
  ' increment the counter
  lCount = lCount + 1

  ' redim arrays in order to add the new entry
  ReDim Preserve sNameArr(lCount)
  ReDim Preserve lNameArr(lCount)

 	Redim Preserve vPortArr(lCount)
 	Redim Preserve iPinArr(lCount)  
 	Redim Preserve sDescriptionArr(lCount)
 	Redim Preserve bUseArr(lCount)
 	Redim Preserve bExclusiveArr(lCount)

 	Redim Preserve sAlternatenames(lCount)
 	Redim Preserve lAlternatePinCount(lCount)

  ' set the values
  sNameArr(lCount) = sName
  lNameArr(lCount) = lCount
    
  vPortArr(lCount) = vPort
  iPinArr(lCount) = iPin
 	sDescriptionArr(lCount) = sDescription
 	bUseArr(lCount) = bUse
  bExclusiveArr(lCount) = bExclusive

 	lAlternatePinCount(lCount) = 0

  Add = True
    
  Exit Function

AddError:
    Exit Function
End Function


'*****************************************************************************
'Description:
'        This function adds a new alternate name for the last added pin to 
'        this class.
'-----------------------------------------------------------------------------
'Parameters:
'        sName           name of the alternate pin (key)
'        sDescription    string with the description of the pin
'        bExclusive      flag that shows if the pin should be used exclusive
'        iFunction       number of the alternate output function of this pin 
'                        (used for the ALTSEL registers), -1 means the pin is input
'-----------------------------------------------------------------------------
'Returns:
'        True            on successfull operation
'        False           else
'*****************************************************************************
Public Function AddAlternatename(sName As String, Optional sDescription As String = "", _
                                 Optional bExclusive As Boolean = False, Optional iFunction As Integer = -1) As Boolean
  On Error GoTo AddAlternatenameError
  
  AddAlternatename = False
  
  ' increment the counter
  lAlternateCount = lAlternateCount + 1

  ' redim arrays in order to add the new entry
  ReDim Preserve sAlternateNameArr(lAlternateCount)
  ReDim Preserve lAlternateNameArr(lAlternateCount)

  ReDim Preserve sAlternateNamePinNameArr(lAlternateCount)
 	Redim Preserve sAlternateDescriptionArr(lAlternateCount)
 	Redim Preserve bAlternateExclusiveArr(lAlternateCount)
 	Redim Preserve iAlternateFunctionArr(lAlternateCount)

  ' set the values
  sAlternateNameArr(lAlternateCount) = sName
  lAlternateNameArr(lAlternateCount) = lAlternateCount

	 sAlternateDescriptionArr(lAlternateCount) = sDescription
	 bAlternateExclusiveArr(lAlternateCount) = bExclusive
	 iAlternateFunctionArr(lAlternateCount) = iFunction

    
 	sAlternateNamePinNameArr(lAlternateCount) = sNameArr(lCount)

 	lAlternatePinCount(lCount) = lAlternatePinCount(lCount) + 1
	 sAlternatenames(lCount).s(lAlternatePinCount(lCount)) = sName

  AddAlternatename = True
  Exit Function

AddAlternatenameError:
  Exit Function
End Function


'*****************************************************************************
'Description:
'        This function sort both alternate names and pins.
'-----------------------------------------------------------------------------
'Parameters:
'-----------------------------------------------------------------------------
'Returns:
'        True            on successfull operation
'        False           else
'*****************************************************************************
Public Function Sort() As Boolean
  On Error Resume Next

  ' sort the arrays
  Sort = MergeSortStart(sNameArr, lNameArr) And MergeSortStart(sAlternateNameArr, lAlternateNameArr)
End Function


'*****************************************************************************
'Description:
'        This property returns the pin name (e.g. P3_7) for an alternate name.
'-----------------------------------------------------------------------------
'Parameters:
'        sName           the name of the alternate pin (specified
'                        with function 'AddAlternateName' in install.inc)
'-----------------------------------------------------------------------------
'Returns:
'        The pin name of the alternate pin.
'*****************************************************************************
Public Property Get PinName(sName As String) As String
	 On Error Resume Next
	 PinName = sAlternateNamePinNameArr(SearchAlternateName(sName))
End Property


'*****************************************************************************
'Description:
'        This property returns the description of the alternate name.
'-----------------------------------------------------------------------------
'Parameters:
'        sName           the name of the alternate pin (specified
'                        with function 'AddAlternateName' in install.inc)
'-----------------------------------------------------------------------------
'Returns:
'        The description of the alternate pin.
'*****************************************************************************
Public Property Get AlternateDescription(sName As String) As String
	 On Error Resume Next
	 AlternateDescription = sAlternateDescriptionArr(SearchAlternateName(sName))
End Property


'*****************************************************************************
'Description:
'        This property sets the description of the alternate name.
'-----------------------------------------------------------------------------
'Parameters:
'        sName           the name of the alternate pin (specified
'                        with function 'AddAlternateName' in install.inc)
'        sDescription    description of the alternate pin
'-----------------------------------------------------------------------------
'Returns:
'*****************************************************************************
Public Property Let AlternateDescription(sName As String, sDescription As String)
	 On Error Resume Next
 	sAlternateDescriptionArr(SearchAlternateName(sName)) = sDescription
End Property


'*****************************************************************************
'Description:
'        This property returns the information if the pin should be used 
'        exclusive.
'-----------------------------------------------------------------------------
'Parameters:
'        sName           the name of the alternate pin (specified
'                        with function 'AddAlternateName' in install.inc)
'-----------------------------------------------------------------------------
'Returns:
'        True            the pin should be used exclusive
'        False           else
'*****************************************************************************
Public Property Get AlternateExclusive(sName As String) As Boolean
	 On Error Resume Next
	 AlternateExclusive = bAlternateExclusiveArr(SearchAlternateName(sName))
End Property


Public Property Let AlternateExclusive(sName As String, bExclusive As Boolean)
	 On Error Resume Next
 	bAlternateExclusiveArr(SearchAlternateName(sName)) = bExclusive
End Property


'*****************************************************************************
'Description:
'        This property returns the number of the alternate output function of 
'        this pin (used for the ALTSEL registers).
'-----------------------------------------------------------------------------
'Parameters:
'        sName           the name of the alternate pin (specified
'                        with function 'AddAlternateName' in install.inc)
'-----------------------------------------------------------------------------
'Returns:
'        The number of the alternate output function
'*****************************************************************************
Public Property Get AlternateFunctionNr(sName As String) As Integer
	 On Error Resume Next
	 AlternateFunctionNr = iAlternateFunctionArr(SearchAlternateName(sName))
End Property


Public Property Let AlternateFunctionNr(sName As String, iNumber As Integer)
	 On Error Resume Next
 	iAlternateFunctionArr(SearchAlternateName(sName)) = iNumber
End Property


'*****************************************************************************
'Description:
'        This property returns the name of an alternate pin from this pin.
'        An index is used to specify the number of the alternate pin name.
'-----------------------------------------------------------------------------
'Parameters:
'        sName           the name of the pin (specified
'                        with function 'AddPin' in install.inc)
'        lIndex          number of the alternate pin name
'-----------------------------------------------------------------------------
'Returns:
'        The name of the alternate pin
'*****************************************************************************
Public Property Get AlternateName(sName As String, lIndex As Long) As String
	 On Error Resume Next
	 AlternateName = sAlternatenames(SearchPinName(sName)).s(lIndex)
End Property


'*****************************************************************************
'Description:
'        This property returns the number of alternate pin names of this pin.
'-----------------------------------------------------------------------------
'Parameters:
'        sName           the name of the pin (specified
'                        with function 'AddPin' in install.inc)
'-----------------------------------------------------------------------------
'Returns:
'        The number of alternate pin names.
'*****************************************************************************
Public Property Get AlternateNameCount(sName As String) As Long
	 On Error Resume Next
	 AlternateNameCount = lAlternatePinCount(SearchPinName(sName))
End Property


'*****************************************************************************
'Description:
'        This property returns the description of the pin.
'-----------------------------------------------------------------------------
'Parameters:
'        sName           the name of the pin (specified
'                        with function 'AddPin' in install.inc)
'-----------------------------------------------------------------------------
'Returns:
'        The description of the pin.
'*****************************************************************************
Public Property Get Description(sName As String) As String
   On Error Resume Next    
	  Description = sDescriptionArr(SearchPinName(sName))
End Property

Public Property Let Description(sName As String, sNew As String)
  On Error Resume Next
 	sDescriptionArr(SearchPinName(sName)) = sNew
End Property


'*****************************************************************************
'Description:
'        This property returns the port number from the pin name.
'-----------------------------------------------------------------------------
'Parameters:
'        sName           the name of the pin (specified
'                        with function 'AddPin' in install.inc)
'-----------------------------------------------------------------------------
'Returns:
'        The port number from the pin name.
'*****************************************************************************
Public Property Get Port(sName As String) As Variant
  On Error Resume Next   
  Port = vPortArr(SearchPinName(sName))
End Property

Public Property Let Port(sName As String, vNew As Variant)
  On Error Resume Next
  vPortArr(SearchPinName(sName)) = vNew
End Property


'*****************************************************************************
'Description:
'        This property returns the pin number from the pin name.
'-----------------------------------------------------------------------------
'Parameters:
'        sName           the name of the pin (specified
'                        with function 'AddPin' in install.inc)
'-----------------------------------------------------------------------------
'Returns:
'        The pin number from the pin name.
'*****************************************************************************
Public Property Get Pin(sName As String) As Integer
  On Error Resume Next    
  Pin = iPinArr(SearchPinName(sName))
End Property

Public Property Let Pin(sName As String, iNew As Integer)
  On Error Resume Next
  iPinArr(SearchPinName(sName)) = iNew
End Property


'*****************************************************************************
'Description:
'        This property returns the information if the pin is availible.
'-----------------------------------------------------------------------------
'Parameters:
'        sName           the name of the pin (specified
'                        with function 'AddPin' in install.inc)
'-----------------------------------------------------------------------------
'Returns:
'        True            the pin is availible
'        False           else
'*****************************************************************************
Public Property Get Use(sName As String) As Boolean
  On Error Resume Next
  Use = bUseArr(SearchPinName(sName))
End Property

Public Property Let Use(sName As String, bNew As Boolean)
  On Error Resume Next
  bUseArr(SearchPinName(sName)) = bNew
End Property


'*****************************************************************************
'Description:
'        This property returns the information if the pin should be used 
'        exclusive.
'-----------------------------------------------------------------------------
'Parameters:
'        sName           the name of the pin (specified
'                        with function 'AddPin' in install.inc)
'-----------------------------------------------------------------------------
'Returns:
'        True            the pin should be used exclusive
'        False           else
'*****************************************************************************
Public Property Get Exclusive(sName As String) As Boolean
  On Error Resume Next
  Exclusive = bExclusiveArr(SearchPinName(sName))
End Property

Public Property Let Exclusive(sName As String, bNew As Boolean)
  On Error Resume Next
  bExclusiveArr(SearchPinName(sName)) = bNew
End Property


'*****************************************************************************
'Description:
'        This function returns the index of the pin name.
'-----------------------------------------------------------------------------
'Parameters:
'        sName           the name of the pin (specified
'                        with function 'AddPin' in install.inc)
'-----------------------------------------------------------------------------
'Returns:
'        Index of the pin name.
'*****************************************************************************
Private Function SearchPinName(sName As String) As Long
	Static lLastIndex As Long
	Static sLastName As String

	If (sLastName = sName) Then
		SearchPinName = lLastIndex
	Else
		sLastName = sName
		lLastIndex = BinarySearch(sName, sNameArr, lNameArr)
		SearchPinName = lLastIndex
	End if
End Function


'*****************************************************************************
'Description:
'        This function returns the index of the alternate pin name.
'-----------------------------------------------------------------------------
'Parameters:
'        sName           the name of the alternate pin (specified
'                        with function 'AddAlternateName' in install.inc)
'-----------------------------------------------------------------------------
'Returns:
'        Index of the alternate pin name.
'*****************************************************************************
Private Function SearchAlternateName(sName As String) As Long
	Static lLastIndex As Long
	Static sLastName As String

	If (sLastName = sName) Then
		SearchAlternateName = lLastIndex	
	Else
		sLastName = sName
		lLastIndex = BinarySearch(sName, sAlternateNameArr, lAlternateNameArr)
		SearchAlternateName = lLastIndex
	End if
End Function


'-----------------------------------------------------
' *** 
'-----------------------------------------------------
Private Sub Class_Initialize()
  On Error Resume Next
  lCount = 0
	 lAlternateCount = 0
End Sub

'-----------------------------------------------------
' *** 
'-----------------------------------------------------
Private Sub Class_Terminate()
End Sub
