Attribute VB_Name = "Global"
'#Uses "..\classes\FunctionClass.cls
'#Uses "..\classes\InterruptClass.cls"
'#Uses "..\classes\ParameterClass.cls
'#Uses "..\classes\LinFrame.cls"
'#Uses "..\classes\LinSignal.cls"
'#Uses "..\classes\LinNode.cls"
'#Uses "Format.inc"
'#Uses "Const.inc"



Option Explicit
'*****************************************************************************
' @Doc
' @Module        GLOBAL.INC
'-----------------------------------------------------------------------------
' @Controllers
'
' @Description   This module contains global functions that can
'                be used in all other script files.
'
'-----------------------------------------------------------------------------
' @History       Date           Author     Description
' 2-1-0          24.08.99       ste        final version
'                17.02.00       ahe
'                               ConvertFrequency function
'
'        ----------------------------------------------------------------------
'        4.0    ahe       12.02.01     final version for Pegasus
'                                      Changed the functions:
'                                      - EnableSRN
'                                      - DiableSRN
'                                      - IsISRSelected
'                                      - GenerateIR
'                                      - SetPECChannelToDefault
'                                      - GetSystemClock
'
'        ----------------------------------------------------------------------
'        4.1    ahe       07.08.02     final version for Pegasus
'                                      - change the function SetPECChannelToDefault()
'                                      - change the function GenerateIR()
'
'        4.2    ste       24.02.03     GetMaxSystemClock() added
'
'        ----------------------------------------------------------------------
'        4.3              21.08.04     GetPeripheralClock() added
'                                      PEC SRCPx & DSTPx to User Defined values
'*****************************************************************************

' this type is for the calculation of a time
' it is used in the function ConvertTime()
Type UnitType
  dValue As Double   ' value of the time / frequency
  sUnit As String    ' unit of the time / frequency
  dDivider As Double    ' divider factor referring to s / MHz
End Type
'Global Varibale for LIN
Global bDiagFrm    As Boolean
'Constants for functions UpdateAllPeripherals and UpdateOnePeripherals
Global Const INITIALIZATION = 0          'initialization for a new project
Global Const UPDATE_CLOCK = 1            'clock unpdate
Global Const SET_MODULE_TO_DEFAULT = 3   'set module to the default


'*****************************************************************************
'Description:
'         This function returns the system clock [MHz].
'-----------------------------------------------------------------------------
'Parameters:
'        r                reference to the ProjectData object
'
'-----------------------------------------------------------------------------
'Returns:
'   system clock [MHz]
'*****************************************************************************
Function GetSystemClock(r As ProjectData) As Variant
  On Error GoTo GetSystemClockError

    If (r.v("PLLCON_PLLCTRL") = 3) Then
        GetSystemClock = r.v("MAIN_OSC") * (r.v("PLLCON_PLLMUL") + 1) / _
                         (r.v("PLLCON_PLLIDIV") + 1) / _
                         (r.v("PLLCON_PLLODIV") + 1) / _
                         (1 + r.v("SYSCON1_CPSYS"))
    Else
        GetSystemClock = r.v("MAIN_OSC") / _
                         (r.v("PLLCON_PLLIDIV") + 1) / _
                         (r.v("PLLCON_PLLODIV") + 1) / _
                         (1 + r.v("SYSCON1_CPSYS"))
    End If

    
  Exit Function

GetSystemClockError:
  ErrorBox "Error in function GetSystemClock", Err.Description
  Exit Function
End Function

'*****************************************************************************
'Description:
'         This function returns the peripheral clock [MHz].
'-----------------------------------------------------------------------------
'Parameters:
'        r                reference to the ProjectData object
'
'-----------------------------------------------------------------------------
'Returns:
'   peripheral clock [MHz]
'*****************************************************************************
Function GetPeripheralClock(r As ProjectData) As Variant
  On Error GoTo GetPeripheralClockError

    If (r.v("PLLCON_PLLCTRL") = 3) Then
        GetPeripheralClock = r.v("MAIN_OSC") * (r.v("PLLCON_PLLMUL") + 1) / _
                         (r.v("PLLCON_PLLIDIV") + 1) / _
                         (r.v("PLLCON_PLLODIV") + 1) / _
                         (1 + r.v("SYSCON1_CPSYS"))
                         '/ _                         (1 + r.v("SYSCON1_CPPD"))
    Else
        GetPeripheralClock = r.v("MAIN_OSC") / _
                         (r.v("PLLCON_PLLIDIV") + 1) / _
                         (r.v("PLLCON_PLLODIV") + 1) / _
                         (1 + r.v("SYSCON1_CPSYS"))
                         '/ _                       (1 + r.v("SYSCON1_CPPD"))
    End If

    
  Exit Function

GetPeripheralClockError:
  ErrorBox "Error in function GetPeripheralClock", Err.Description
  Exit Function
End Function

'*****************************************************************************
'Description:
'         This function returns the maximum system clock [MHz].
'-----------------------------------------------------------------------------
'Parameters:
'        r                reference to the ProjectData object
'
'-----------------------------------------------------------------------------
'Returns:
'   system clock [MHz]
'*****************************************************************************
Function GetMaxSystemClock(r As ProjectData) As Double
  On Error Resume Next

  GetMaxSystemClock = r.d("MAIN_CLK_CPU")
End Function

'*****************************************************************************
'Description:
'         This function returns the maximum peripheral clock [MHz].
'-----------------------------------------------------------------------------
'Parameters:
'        r                reference to the ProjectData object
'
'-----------------------------------------------------------------------------
'Returns:
'   peripheral clock [MHz]
'*****************************************************************************
Function GetMaxPeripheralClock(r As ProjectData) As Double
  On Error Resume Next

  GetMaxPeripheralClock = r.d("MAIN_CLK_PD")
End Function
'*****************************************************************************
'Description:
'        This function makes all activities to enable an interrupt.
'        It sets the service request enable bit and selectes the service
'        request routine.
'        Example:
'        Call EnableSRN(r, "SSC0", "SSC0_TIC", "Tx")
'
'        For this example the register SSC0_TIC and the variable
'        SSC0_USE_Tx must exist.
'-----------------------------------------------------------------------------
'Parameters:
'        r                reference to the ProjectData object
'        ModulName        name of the Module (e.g. SSC0)
'        RegName          name of the register (e.g. SSC0_RIC)
'        ISRName          name of the service request routine (e.g Rx)
'-----------------------------------------------------------------------------
'Returns:
'*****************************************************************************
Sub EnableSRN(r As ProjectData, ModuleName As String, RegName As String, ISRName As String)
On Error Resume Next
  
  r.v(RegName & "_IE") = 1

  r.v(ModuleName & "_USE_" & ISRName) = 1

End Sub



'*****************************************************************************
'Description:
'        This function makes all activities to disable an interrupt.
'        It resets the service request enable bit, set the priority to zero
'        and deselectes the service request routine.
'        Example:
'        Call DisableSRN(r, "SSC0", "SSC0_TIC", "Tx")
'
'        For this example the register SSC0_TIC and the variable
'        SSC0_USE_Tx must exist.
'-----------------------------------------------------------------------------
'Parameters:
'        r                reference to the ProjectData object
'        ModuleName       the name of the module (e.g. SSC0)
'        RegName          the name of the register (e.g. SSC0_RIC)
'        ISRName          the name of the service request routine (e.g Rx)
'-----------------------------------------------------------------------------
'Returns:
'*****************************************************************************
Sub DisableSRN(r As ProjectData, ModuleName As String, RegName As String, ISRName As String)
  On Error Resume Next
  Dim iChannel As Integer
  Dim iLevel As Integer
  Dim iGroup As Integer

  iLevel = r.v(RegName & "_ILVL")
  iGroup = r.v(RegName & "_GLVL")

  r.v(RegName & "_IE") = 0
  r.v(RegName & "_GLVL") = 0
  r.v(RegName & "_ILVL") = 0
  
  r.v(ModuleName & "_USE_" & ISRName) = 0
  
  If (iLevel = 14 Or iLevel = 12 Or iLevel = 10 Or iLevel = 8) Then
      iChannel = iGroup
      Call SetPECChannelToDefault(r, iChannel)
  ElseIf (iLevel = 15 Or iLevel = 13 Or iLevel = 11 Or iLevel = 9) Then
      iChannel = iGroup + 4
      Call SetPECChannelToDefault(r, iChannel)
  End If
  
  

End Sub



'*****************************************************************************
'Description:
'        This function checks if the requested interrupt service routine
'        is selected.
'        Example:
'        IsISRSelected(r, "SSC0", "SSC0_TIC", "Tx")
'
'-----------------------------------------------------------------------------
'Parameters:
'        r                reference to the ProjectData object
'        ModuleName       the name of the module (e.g. SSC0)
'        RegName          the name of the service request control register (e.g SSC0_TIC)
'        ISRName          the name of the service request routine (e.g Tx)
'-----------------------------------------------------------------------------
'Returns:
'       Fales             if not seleected
'       True              else
'*****************************************************************************
Function IsISRSelected(r As ProjectData, ModuleName As String, RegName As String, ISRName As String) As Boolean
On Error Resume Next
  IsISRSelected = False
  If (r.v(RegName & "_GLVL") > 0 Or r.v(RegName & "_ILVL") > 0) Then
    IsISRSelected = (r.v(ModuleName & "_USE_Init") = 1 And _
                     r.v(RegName & "_IE") = 1 And _
                     r.v(ModuleName & "_USE_" & ISRName) = 1)
  End If
End Function



'*****************************************************************************
'Description:
'        This function checks if the function is selected.
'        Example:
'        IsIFctSelected(r, "SSC0", "ReadCAPREL")
'
'-----------------------------------------------------------------------------
'Parameters:
'        r                reference to the ProjectData object
'        ModuleName       the name of the module (e.g. SSC0)
'        FctName          the name of the function (e.g ReadCAPREL)
'-----------------------------------------------------------------------------
'Returns:
'       Fales             if not seleected
'       True              else
'*****************************************************************************
Function IsFctSelected(r As ProjectData, ModuleName As String, FctName As String) As Boolean
On Error Resume Next
  IsFctSelected = False
  IsFctSelected = (r.v(ModuleName & "_USE_Init") = 1 And r.v(ModuleName & "_USE_" & FctName) = 1)
End Function


'*****************************************************************************
'Description:
'        This functions generates the statments for the interrupt service
'        request node.
'        Example:
'        Call GenerateIR(f, r, "SSCTIC", "SSCTIE", "transmit")
'
'-----------------------------------------------------------------------------
'Parameters:
'        f                file handle
'        r                reference to the ProjectData object
'        RegName          the name of the service request control register
'                         (e.g. SSCTIC)
'        ISRName          the name of the interrupt node (e.g transmit )
'-----------------------------------------------------------------------------
'Returns:
'*****************************************************************************
Sub GenerateIR(f As Variant, r As ProjectData, RegName As String, IntName As String)
    On Error Resume Next
       
    Dim v           As Variant
    Dim iHelp       As Integer
    Dim iLev        As Integer
    Dim iNotPLEV    As Integer
    Dim d           As String

    If (r.v(RegName & "_IE") = 1) Then
      d = IntName & " service request node configuration:"
      Print #f, FormatComments(2, 1, "", "", d)
    
      If (r.v(RegName & "_ILVL") = 0) Then
        Print #f, FormatCommentsForInit(2, IntName & " interrupt is enabled but NO INTERRUPT WILL BE GENERATED because priority level is 0")
      Else
        Print #f, FormatCommentsForInit(1, IntName & " interrupt priority level (ILVL) = " & Format(r.v(RegName & "_ILVL")))
        Print #f, FormatCommentsForInit(1, IntName & " interrupt group level (GLVL) = " & Format(r.v(RegName & "_GLVL")))
        Print #f, FormatCommentsForInit(1, IntName & " group priority extension (GPX) = " & Format(r.v(RegName & "_GPX")))
      End If
      
      v = 0
      v = v + r.sfr(RegName)
      Print #f, ""
      Print #f, FormatCodeStatemente(2, RegName, "=", FormatTextOutput(v, "H4"), "")
      Print #f, ""

      If (r.v(RegName & "_ILVL") = 8 Or _
          r.v(RegName & "_ILVL") = 10 Or _
          r.v(RegName & "_ILVL") = 12 Or _
          r.v(RegName & "_ILVL") = 14) Then
          
          iHelp = r.v(RegName & "_GLVL")
          iLev = 8
      
      ElseIf (r.v(RegName & "_ILVL") = 9 Or _
          r.v(RegName & "_ILVL") = 11 Or _
          r.v(RegName & "_ILVL") = 13 Or _
          r.v(RegName & "_ILVL") = 15) Then
          
          iHelp = r.v(RegName & "_GLVL") + 4
          iLev = 9
      
      End If

      'PLEV muss mit xIC_ILVL invertiert vergliechen werden
      If (r.v("PECC" & iHelp & "_PLEV") = 0) Then
          iNotPLEV = 3
      ElseIf (r.v("PECC" & iHelp & "_PLEV") = 1) Then
          iNotPLEV = 2
      ElseIf (r.v("PECC" & iHelp & "_PLEV") = 2) Then
          iNotPLEV = 1
      ElseIf (r.v("PECC" & iHelp & "_PLEV") = 3) Then
          iNotPLEV = 0
      End If

      If (r.v(RegName & "_ILVL") > 7 And r.v(RegName & "_GPX") = 0 And _
          ((r.v(RegName & "_ILVL") - iLev) / 2 = iNotPLEV)) Then
        
        '------------------------------------------------------------------------------------------------
        ' Use PEC channel " & iHelp & " for " & r.v("PEC_CHANNEL_" & iHelp) & ":"
        '------------------------------------------------------------------------------------------------
        Print #f, "  ///  Use PEC channel " & iHelp & " for " & r.v("PEC_CHANNEL_" & iHelp) & ":"

        '--------------------------------------------- PECCx_COUNT
        If (r.v("PECC" & iHelp & "_COUNT") = 255) Then
          d = "continuous transfer"
        Else
          If (r.v("PECC" & iHelp & "_COUNT") = 0) Then
            d = "normal interrupt"
          Else
            d = "decrement counter"
          End If
        End If
        Print #f, FormatCommentsForInit(1, d)

        '--------------------------------------------- PECCx_INC
        Select Case (r.v("PECC" & iHelp & "_INC"))
                Case (0)
                      d = "pointers are not modified"
                Case (1)
                      d = "increment destination pointer"
                Case (2)
                      d = "increment source pointer"
                Case (3)
                      d = "increment destination and source pointers"
        End Select
        Print #f, FormatCommentsForInit(1, d)

        '--------------------------------------------- PECCx_BWT
        If (r.v("PECC" & iHelp & "_BWT") = 0) Then
          d = "transfer a word"
        Else
          d = "transfer a byte"
        End If
        Print #f, FormatCommentsForInit(1, d)
        
        '--------------------------------------------- PECCx_EOPINT
        If (r.v("PECC" & iHelp & "_EOPINT") = 0) Then
          d = "service End of PEC interrrupt by a EOP interrupt node is disabled"
        Else
          d = "service End of PEC interrrupt by a EOP interrupt node is enabled"
        End If
        Print #f, FormatCommentsForInit(1, d)

        If (r.v("PECC" & iHelp & "_EOPINT") = 1) Then
            If (r.v("PECISNC_C" & iHelp & "IE") = 0) Then
              d = "End of PEC intrrupt request is disabled"
            Else
              d = "End of PEC intrrupt request is enabled"
            End If
            Print #f, FormatCommentsForInit(1, d)
        End If
        Print #f, ""

        v = 0
        v = v + r.sfr("PECC" & iHelp)
        Print #f, FormatCodeStatemente(2, "PECC" & iHelp, "=", FormatTextOutput(v, "H4"), _
          "load PECC" & iHelp & " control register")
        Print #f, ""


        '--------------------------------------------- SRCPx_SRCPx
        If (r.v("PECC" & iHelp & "_COUNT") <> 0) Then
          If (r.v("PEC_SRCP" & iHelp & "_SRCP" & iHelp) = "0") Then
            'd = "DO NOT FORGET TO SET THE SOURCE-POINTER FOR PECC CHANNEL " & iHelp & " BECAUSE IT IS SELECTED FOR USER DEFINED"
            d = "SOURCE-POINTER for PECC channel " & iHelp & " is set because it is selected for user defined"
            Print #f, FormatCommentsForInit(2, d)
            If (InStr(r.v("MAIN_COMPILER"), "Tasking")) Then
                Print #f, "  SRCP" & iHelp & "  =  (uword)("; FormatTextOutput(r.v("SRCPTR_UD" & iHelp), "H4"); ");      //set source pointer"
            End If
            If (InStr(r.v("MAIN_COMPILER"), "Keil")) Then
                Print #f, "  SRCP" & iHelp & "  =  _sof_("; FormatTextOutput(r.v("SRCPTR_UD" & iHelp), "H4") & ");      //set source pointer"
            End If
          Else
            If (InStr(r.v("MAIN_COMPILER"), "Tasking")) Then
              Print #f, "  SRCP" & iHelp & "  =  (uword)(&" & r.v("PEC_SRCP" & iHelp & "_SRCP" & iHelp) & ");  //set source pointer"
            End If
            If (InStr(r.v("MAIN_COMPILER"), "Keil")) Then
              Print #f, "  SRCP" & iHelp & "  =  _sof_(&" & r.v("PEC_SRCP" & iHelp & "_SRCP" & iHelp) & ");  //set source pointer"
            End If
          End If
        End If

        '--------------------------------------------- DSTPx_DSTPx
        If (r.v("PECC" & iHelp & "_COUNT") <> 0) Then
          Print #f, ""
          If (r.v("PEC_DSTP" & iHelp & "_DSTP" & iHelp) = "0") Then
            'd = "DO NOT FORGET TO SET THE DESTINATION-POINTER FOR PECC CHANNEL " & iHelp & " BECAUSE IT IS SELECTED FOR USER DEFINED"
            d = "DESTINATION-POINTER for PECC channel " & iHelp & " is set because it is selected for user defined"
            Print #f, FormatCommentsForInit(2, d)
            If (InStr(r.v("MAIN_COMPILER"), "Tasking")) Then
                Print #f, "  DSTP" & iHelp & "  =  (uword)("; FormatTextOutput(r.v("DSTPTR_UD" & iHelp), "H4"); ");      //set destination pointer"
            End If
            If (InStr(r.v("MAIN_COMPILER"), "Keil")) Then
                Print #f, "  DSTP" & iHelp & "  =  _sof_("; FormatTextOutput(r.v("DSTPTR_UD" & iHelp), "H4") & ");      //set destination pointer"
            End If
          Else
            If (InStr(r.v("MAIN_COMPILER"), "Tasking")) Then
              Print #f, "  DSTP" & iHelp & "  =  (uword)(&" & r.v("PEC_DSTP" & iHelp & "_DSTP" & iHelp) & ");  //set destination pointer"
            End If
            If (InStr(r.v("MAIN_COMPILER"), "Keil")) Then
              Print #f, "  DSTP" & iHelp & "  =  _sof_(&" & r.v("PEC_DSTP" & iHelp & "_DSTP" & iHelp) & ");  //set destination pointer"
            End If
          End If
        End If

        Print #f, ""
      End If
    End If
    
End Sub



'*****************************************************************************
'Description:
'        This function generates the declaration for the interrupt service
'        routine in the generated code.
'
'-----------------------------------------------------------------------------
'Parameters:
'        r                reference to the ProjectData object
'        ISRName          the name of the service request routine (e.g SSC0_viTx)
'        VectorName       the name of the service request routine vector (e.g TX_INT)
'-----------------------------------------------------------------------------
'Returns:
'        Service request declaration
'*****************************************************************************
Function GenerateIsrDeclaration(r, ISRName As String, VectorName As Variant) As String
    On Error Resume Next
    
    If (InStr(r.v("MAIN_COMPILER"), "Tasking")) Then
        GenerateIsrDeclaration = "interrupt (" & VectorName & ") void " & ISRName & "(void)"
    End If

    If (InStr(r.v("MAIN_COMPILER"), "Keil")) Then
        GenerateIsrDeclaration = "void " & ISRName & "(void) interrupt " & VectorName
    End If

End Function


'*****************************************************************************
'Description:
'        This function converts a time in s to an other unit
'
'-----------------------------------------------------------------------------
'Parameters:
'        dValue           the time in s
'-----------------------------------------------------------------------------
'Returns:
'        .dValue          the new value for the time
'        .sUnit           the new unit of the time
'        .bDivider        the new divider of the new time, referring to s
'*****************************************************************************
Function ConvertTime(dValue As Double) As UnitType
  On Error GoTo ConvertTimeError
  Dim dHelp As Double
  Dim sHelp As String
  Dim dDivider As Double

  dHelp = dValue
  sHelp = "s"
  dDivider = 1

  If (dHelp < 1) Then
    dHelp = dHelp * 1000
    sHelp = "ns"
    dDivider = dDivider / 1000
  End If
 
  If (dHelp >= 1000) Then
    dHelp = dHelp / 1000
    sHelp = "ms"
    dDivider = 1000
    If (dHelp >= 1000) Then
      dHelp = dHelp / 1000
      sHelp = "s"
      dDivider = dDivider * 1000
      If (dHelp >= 60) Then
        dHelp = dHelp / 60
        sHelp = "min"
        dDivider = dDivider * 60
        If (dHelp >= 60) Then
          dHelp = dHelp / 60
          sHelp = "h"
          dDivider = dDivider * 60
          If (dHelp >= 24) Then
            dHelp = dHelp / 24
            sHelp = "days"
            dDivider = dDivider * 24
            If (dHelp >= 30) Then
              dHelp = dHelp / 30
              sHelp = "months"
              dDivider = dDivider * 30
              If (dHelp >= 12) Then
                dHelp = dHelp / 12
                sHelp = "years"
                dDivider = dDivider * 12
              End If
            End If
          End If
        End If
      End If
    End If
  End If

  ConvertTime.dValue = dHelp
  ConvertTime.sUnit = sHelp
  ConvertTime.dDivider = dDivider
     

  Exit Function

ConvertTimeError:
  ConvertTime.dValue = dValue
  ConvertTime.sUnit = "s"
  ConvertTime.dDivider = 1
  Exit Function
End Function


'*****************************************************************************
'Description:
'        This function converts a frequency in MHz to an other unit
'
'-----------------------------------------------------------------------------
'Parameters:
'        dValue           the time in MHz
'-----------------------------------------------------------------------------
'Returns:
'        .dValue          the new value for the frequency
'        .sUnit           the new unit of the frequency
'        .bDivider        the new divider of the new time, referring to MHz
'*****************************************************************************
Function ConvertFrequency(dValue As Double) As UnitType
  On Error GoTo ConvertFrequencyError
  Dim dHelp As Double
  Dim sHelp As String
  Dim dDivider As Double

  dHelp = dValue
  sHelp = "MHz"
  dDivider = 1

  If (dHelp < 1) Then
    dHelp = dHelp * 1000
    sHelp = "kHz"
    dDivider = dDivider / 1000
  
    If (dHelp < 1) Then
      dHelp = dHelp * 1000
      sHelp = "Hz"
      dDivider = dDivider / 1000
    End If
  
  End If
 

  ConvertFrequency.dValue = dHelp
  ConvertFrequency.sUnit = sHelp
  ConvertFrequency.dDivider = dDivider
     

  Exit Function

ConvertFrequencyError:
  ConvertFrequency.dValue = dValue
  ConvertFrequency.sUnit = "MHz"
  ConvertFrequency.dDivider = 1
  Exit Function
End Function


'*****************************************************************************
'Description:
'        This function converts a baud rate in [baud] to an other unit
'
'-----------------------------------------------------------------------------
'Parameters:
'        dValue           the baudrate in [baud]
'-----------------------------------------------------------------------------
'Returns:
'        .dValue          the new value for the baud rate
'        .sUnit           the new unit of the baud rate
'        .bDivider        the new divider of the new baud rate, referring to [baud]
'*****************************************************************************
Function ConvertBaudrate(dValue As Double) As UnitType
  On Error GoTo ConvertBaudrateError
  Dim dHelp As Double
  Dim sHelp As String
  Dim dDivider As Double

  dHelp = dValue
  sHelp = "baud"
  dDivider = 1

  If (dHelp >= 1000) Then
    dHelp = (dHelp / 1000)
    sHelp = "kbaud"
    dDivider = dDivider * 1000
    
    If (dHelp >= 1000) Then
      dHelp = (dHelp / 1000)
      sHelp = "Mbaud"
      dDivider = dDivider * 1000
    End If
  
  End If

  ConvertBaudrate.dValue = dHelp
  ConvertBaudrate.sUnit = sHelp
  ConvertBaudrate.dDivider = dDivider

  Exit Function

ConvertBaudrateError:
  ConvertBaudrate.dValue = dValue
  ConvertBaudrate.sUnit = "baud"
  ConvertBaudrate.dDivider = 1
  Exit Function
End Function


'*****************************************************************************
'Description:
'        This function makes all update activities for the page Functions.
'-----------------------------------------------------------------------------
'Parameters:
'        sIndex          module name (prefix of the functions)
'        ps              reference to the Peripherals object
'        func            reference to the FunctionClass object
'        Controlname     control name
'        v()             value array
'-----------------------------------------------------------------------------
'Returns:
'        True            on successfull operation
'        False           else
'*****************************************************************************
Function UpdateFunction(sIndex As String, ps As Peripherals, func As FunctionClass, Controlname As String, v() As Variant) As Boolean
  On Error GoTo UpdateFunctionError
  UpdateFunction = False

  Dim r As ProjectData
  Dim sName As String
  Dim d As String
  Dim l As Long

  Set r = ps.ProjectData

  ' for each function in this FunctionClass object
  For l = 1 To func.FunctionCount
    ' get the function name which was inserted in 'Include.inc' with 'AddFunction()'
    d = func.FunctionName(l)
    ' get only the third part of the function name (e.g "Init")
    sName = Right(d, Len(d) - InStr(d, "NAME") - Len("NAME"))

    '---------------------------------------------------------------------
    ' control: chk_.. (action)
    '---------------------------------------------------------------------
    If (Controlname = "chk_" & sName) Then
      ' action on clicking the selected control
      r.v(sIndex & "_USE_" & sName) = v(1)
      Exit For
    End If

    '---------------------------------------------------------------------
    ' control: txt_.. (action)
    '---------------------------------------------------------------------
    If (Controlname = "txt_" & sName) Then
      ' action on clicking the selected control
      If (IsInputFormatOK(v(1), FORMAT_FUNCTIONS, , , )) Then
        r.s(d) = v(1)
      Else
        Exit Function
      End If
      Exit For
    End If
  Next l


  '---------------------------------------------------------------------
  ' control: txt_CFile (action)
  '---------------------------------------------------------------------
  If (Controlname = "txt_CFile") Then
    ' action on clicking the selected control
    If (IsInputFormatOK(v(1), FORMAT_FUNCTIONS, , , )) Then
      r.v(sIndex & "_CFile") = v(1)
    Else
      Exit Function
    End If
  End If


  UpdateFunction = True
  Exit Function

UpdateFunctionError:
  ErrorBox "Can not show page", Err.Description
  Exit Function
End Function


'*****************************************************************************
'Description:
'        This function makes all show activities for the page Functions
'-----------------------------------------------------------------------------
'Parameters:
'        sIndex          module name (prefix of the functions)
'        ps              reference to the Peripherals object
'        func            reference to the FunctionClass object
'        v()             value array
'-----------------------------------------------------------------------------
'Returns:
'        True            on successfull operation
'        False           else
'*****************************************************************************
Function ShowFunction(sIndex As String, ps As Peripherals, func As FunctionClass, v() As Variant) As Boolean
  On Error GoTo ShowFunctionError
  ShowFunction = False
    
  Dim r         As ProjectData
  Dim c         As Collection
  Dim e         As Collection
  Dim bDisabled As Boolean
  Dim sName     As String
  Dim d         As String
  Dim l         As Long

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

  ' for each function in this 'func' object
  For l = 1 To func.FunctionCount
    ' get the function name which was insert in 'Include.inc' with 'AddFunction()'
    d = func.FunctionName(l)

    ' get only the third part of the function name (e.g "Init")
    sName = Right(d, Len(d) - InStr(d, "NAME") - Len("NAME"))

    '-------------------------------------------------------------------------
    ' control: chk_.. (show)
    '-------------------------------------------------------------------------
    ' action on showing the control
    With c("chk_" & sName)
      ' disable condition
      bDisabled = func.Disabled(d)
      .Enabled = Not (bDisabled)
      ' show condition
      If (sName = "Init") Then
        .Value = -CInt(r.v(sIndex & "_USE_" & sName) = 1)
      Else
        If (func.FunctionSort(d) = 2) Then ' if ISR
          .Value = -CInt(func.Value(d))
        Else
          .Value = -CInt(r.v(sIndex & "_USE_" & sName) = 1 And r.v(sIndex & "_USE_Init") = 1 And func.Value(d))
        End If
      End If
    End With
    
    With e("chk_" & sName)
      If (bDisabled) Then
        ' tooltip if control is disabled
        .DisabledTooltipText = func.DisabledTooltip(d)
      Else
        ' tooltip if control is enabled
      End If
    End With

    '-------------------------------------------------------------------------
    ' control: txt_... (show)
    '-------------------------------------------------------------------------
    ' action on showing the control
    With c("txt_" & sName)
      ' disable condition
      If (func.FunctionSort(d) = 2) Then ' if ISR
          bDisabled = (r.v(sIndex & "_USE_" & sName) = 0 Or r.v(sIndex & "_USE_Init") = 0 Or Not (func.Value(d)))
      Else
          bDisabled = (r.v(sIndex & "_USE_" & sName) = 0 Or r.v(sIndex & "_USE_Init") = 0)
      End If
      .Enabled = Not (bDisabled)
      ' show condition
      .Text = FormatTextOutput(r.s(d), "S")
    End With

    With e("txt_" & sName)
      If (bDisabled) Then
        ' tooltip if control is disabled
        .DisabledTooltipText = FormatNormalToolTip(TOOLTIP_DISABLED_FUNC_NAME) & vbCrLf & vbCrLf & FormatFunctionToolTip(r, func, d)
      Else
        ' tooltip if control is enabled
        .EnabledTooltipText = FormatFunctionToolTip(r, func, d)
      End If
    End With
  Next l

  '-------------------------------------------------------------------------
  ' control: lbl_CFile (show)
  '-------------------------------------------------------------------------
  ' action on showing the control
  With c("lbl_CFile")
    ' disable condition
    bDisabled = (r.v(sIndex & "_USE_Init") = 0)
    .Enabled = Not (bDisabled)
  End With

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

  '-------------------------------------------------------------------------
  ' control: txt_CFile (show)
  '-------------------------------------------------------------------------
  ' action on showing the control
  With c("txt_CFile")
    ' disable condition
    bDisabled = (r.v(sIndex & "_USE_Init") = 0)
    .Enabled = Not (bDisabled)
    ' show condition
    .Text = FormatTextOutput(r.v(sIndex & "_CFile"), "S")
  End With

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

  ShowFunction = True
  Exit Function

ShowFunctionError:
  ErrorBox "Can not show page", Err.Description
  Exit Function
End Function


'*****************************************************************************
'Description:
'        This function makes all update activities for the page Parameters.
'-----------------------------------------------------------------------------
'Parameters:
'        sIndex          module name (prefix of the parameters)
'        ps              reference to the Peripherals object
'        para            reference to the ParameterClass object
'        Controlname     control name
'        v()             value array
'-----------------------------------------------------------------------------
'Returns:
'        True            on successfull operation
'        False           else
'*****************************************************************************
Function UpdateParameter(sIndex As String, ps As Peripherals, para As ParameterClass, Controlname As String, v() As Variant) As Boolean
  On Error GoTo UpdateParameterError
  UpdateParameter = False

  Dim r As ProjectData
  Dim sName As String
  Dim d As String
  Dim l As Long

  Set r = ps.ProjectData

  ' for each parameter in this ParameterClass object
  For l = 1 To para.ParameterCount
    ' get the parameter name which was insert in 'Include.inc' with 'AddParameter()'
    d = para.ParameterName(l)
    ' get only the third part of the parameter name (e.g "T2")
    sName = Right(d, Len(d) - InStr(d, "PARA") - Len("PARA"))

    '---------------------------------------------------------------------
    ' control: txt_.. (action)
    '---------------------------------------------------------------------
    If (Controlname = "txt_" & sName) Then
      ' action on clicking the selected control
      If (IsInputFormatOK(v(1), FORMAT_PARAMETERS, , , )) Then
        r.s(d) = v(1)
      Else
        Exit Function
      End If
      Exit For
    End If
  Next l

  '---------------------------------------------------------------------
  ' control: txt_HFile (action)
  '---------------------------------------------------------------------
  If (Controlname = "txt_HFile") Then
    ' action on clicking the selected control
    If (IsInputFormatOK(v(1), FORMAT_PARAMETERS, , , )) Then
      r.v(sIndex & "_HFile") = v(1)
    Else
      Exit Function
    End If
  End If

  UpdateParameter = True
  Exit Function

UpdateParameterError:
  ErrorBox "Can not show page", Err.Description
  Exit Function
End Function


'*****************************************************************************
'Description:
'        This parameter makes all show activities for the page Parameters
'-----------------------------------------------------------------------------
'Parameters:
'        sIndex          module name (prefix of the parameters)
'        ps              reference to the Peripherals object
'        para            reference to the ParameterClass object
'        v()             value array
'-----------------------------------------------------------------------------
'Returns:
'        True            on successfull operation
'        False           else
'*****************************************************************************
Function ShowParameter(sIndex As String, ps As Peripherals, para As ParameterClass, v() As Variant) As Boolean
  On Error GoTo ShowParameterError
  ShowParameter = False
    
  Dim r As ProjectData
  Dim c As Collection
  Dim e As Collection
  Dim bDisabled As Boolean
  Dim sName As String
  Dim d As String
  Dim l As Long

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

  ' for each parameter in this 'para' object
  For l = 1 To para.ParameterCount
    ' get the parameter name which was insert in 'Include.inc' with 'AddParameter()'
    d = para.ParameterName(l)

    ' get only the third part of the parameter name (e.g "T2")
    sName = Right(d, Len(d) - InStr(d, "PARA") - Len("PARA"))

    '-------------------------------------------------------------------------
    ' control: txt_... (show)
    '-------------------------------------------------------------------------
    ' action on showing the control
    With c("txt_" & sName)
      ' disable condition
      bDisabled = (r.v(sIndex & "_USE_Init") = 0 Or para.Disabled(d))
      .Enabled = Not (bDisabled)
      ' show condition
      .Text = FormatTextOutput(r.s(d), "S")
    End With

    With e("txt_" & sName)
      If (bDisabled) Then
        ' tooltip if control is disabled
        .DisabledTooltipText = TOOLTIP_DISABLED_PARAMETER
        If (para.Disabled(d) = True) Then
          .DisabledTooltipText = .DisabledTooltipText & para.DisabledTooltip(d)
        End If
        If (r.v(sIndex & "_USE_Init") = 0) Then
          .DisabledTooltipText = .DisabledTooltipText & _
            FormatDisabledToolTip(TOOLTIP_DESELECTED_INIT) & vbCrLf & vbCrLf & _
            FormatParameterToolTip(r, para, d)
        End If
      Else
        ' tooltip if control is enabled
        .EnabledTooltipText = FormatParameterToolTip(r, para, d)
      End If
    End With

  Next l

  '-------------------------------------------------------------------------
  ' control: lbl_HFile (show)
  '-------------------------------------------------------------------------
  ' action on showing the control
  With c("lbl_HFile")
    ' disable condition
    bDisabled = (r.v(sIndex & "_USE_Init") = 0)
    .Enabled = Not (bDisabled)
  End With

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

  '-------------------------------------------------------------------------
  ' control: txt_HFile (show)
  '-------------------------------------------------------------------------
  ' action on showing the control
  With c("txt_HFile")
    ' disable condition
    bDisabled = (r.v(sIndex & "_USE_Init") = 0)
    .Enabled = Not (bDisabled)
    ' show condition
    .Text = FormatTextOutput(r.v(sIndex & "_HFile"), "S")
  End With

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


  ShowParameter = True
  Exit Function

ShowParameterError:
  ErrorBox "Can not show page", Err.Description
  Exit Function
End Function

'*****************************************************************************
'Description:
'        This function checks the paramter v if he is lower as the parameter
'        MinValue or greater as the parameter MaxValue. If so then a massage
'        box is opened.
'-----------------------------------------------------------------------------
'Parameters:
'        v               value which should be checked
'        MinValue        minimum value for the parameter v
'        MaxValue        maximum value for the parameter v
'-----------------------------------------------------------------------------
'Returns:
'        True            the value v is in range
'        False           else
'*****************************************************************************
Function IsInputValueOK(v As Variant, MinValue As Double, MaxValue As Double) As Boolean
  On Error Resume Next

  IsInputValueOK = False

  If (v < MinValue) Then  ' if the value is too low
    msgbox ("The input value must be greater.", vbCritical ,"DAvE")
    Exit Function
  Else
    If (v > MaxValue) Then   ' if the value is too high
      msgbox ("The input value must be lower.", vbCritical ,"DAvE")
      Exit Function
    Else
      IsInputValueOK = True  ' the value is OK
    End If
  End If

End Function


Function SetDefaultPropertiesForAllControlsOnPage(szPagename As String, c As Object, e As Object, bDisabled As Boolean, szDisabledTooltip As String, szEnabledTooltip As String)
  On Error Resume Next

  Dim o As Object
  Dim s As String

  ' for each control
  For Each o In c
    ' on this page
    s = ""
    s = o.Container.Container.Tag
    If (s = szPagename) Then
      o.Enabled = Not bDisabled
      ' determine tooltip
      If (bDisabled) Then
        ' tooltip if control is disabled
        e(o.Tag).DisabledTooltipText = szDisabledTooltip
      Else
        ' tooltip if control is enabled
        e(o.Tag).EnabledTooltipText = szEnabledTooltip
      End If
    End If
  Next
End Function

Function SetDefaultPropertiesForAllControlsOnFrame(szFramename As String, c As Object, e As Object, bDisabled As Boolean, szDisabledTooltip As String, szEnabledTooltip As String)
  On Error Resume Next

  Dim o As Object
  Dim s As String

  ' for each control
  For Each o In c
    ' on this frame
    s = ""
    s = o.Container.Caption
    If (s = szFramename) Then
      o.Enabled = Not bDisabled
      ' determine tooltip
      If (bDisabled) Then
        ' tooltip if control is disabled
        e(o.Tag).DisabledTooltipText = szDisabledTooltip
      Else
        ' tooltip if control is enabled
        e(o.Tag).EnabledTooltipText = szEnabledTooltip
      End If
    End If
  Next
End Function


'*****************************************************************************
'Description:
'         This function sets the PEC channel in to the default.
'         If the channel is linked to or from the other channel and
'         the channel has no interrupt, then the pair channel is set to
'         the default too.
'-----------------------------------------------------------------------------
'Parameters:
'        r               ProjectData
'        iChannel        PEC channel
'-----------------------------------------------------------------------------
'Returns:
'       none
'*****************************************************************************
Sub SetPECChannelToDefault(r As ProjectData, iChannel As Integer)
    On Error Resume Next

    Dim i As Integer
    Dim cp As DAvECollection
    Dim ir As InterruptClass
    Dim iFirstLevel As Integer
    Dim iGroup As Integer
    Dim asISR(0 To 3) As String

    Set cp = ObjectCollection("IRSOURCES")
  

    If (iChannel < 4) Then
        iFirstLevel = 14
        iGroup = iChannel
    ElseIf (iChannel > 3) Then
        iFirstLevel = 15
        iGroup = iChannel - 4
    End If

    For i = 0 To 3
        asISR(i) = "none"
    Next i
    
    For Each ir In cp
        If (r.v(ir.Enabled) = 1) Then
            If (r.v(ir.GroupName) = iGroup And r.v(ir.GPXName) = 0) Then
                If (r.v(ir.LevelName) = iFirstLevel) Then
                    asISR(0) = ir.IntName
                End If
                If (r.v(ir.LevelName) = iFirstLevel - 2) Then
                    asISR(1) = ir.IntName
                End If
                If (r.v(ir.LevelName) = iFirstLevel - 4) Then
                    asISR(2) = ir.IntName
                End If
                If (r.v(ir.LevelName) = iFirstLevel - 6) Then
                    asISR(3) = ir.IntName
                End If
            End If
          
        End If
    Next ir
    
    For i = 0 To 3
        If (asISR(i) = "none" And r.v("PECC" & iChannel & "_PLEV") = i) Then
            
            'set PEC to default
            r.v("PECC" & iChannel & "_COUNT") = 0
            r.v("PEC_SRCP" & iChannel & "_SRCP" & iChannel) = 0
            r.v("PEC_DSTP" & iChannel & "_DSTP" & iChannel) = 0
            r.v("PECC" & iChannel & "_INC") = 0
            r.v("PECC" & iChannel & "_EOPINT") = 0
            r.v("PECISNC_C" & iChannel & "IE") = 0
            r.v("PEC_CHANNEL_" & iChannel) = ""
            r.v("SRCPTR_UD" & iChannel) = 0
            r.v("DSTPTR_UD" & iChannel) = 0

        End If
        r.v("PEC_CHANNEL_" & iChannel & "_" & i) = asISR(i)
    Next i

    

End Sub

'*****************************************************************************
'Description:
'        The "UpdateProjectData" functions of all peripheral modules are
'        called with parameter "lngUpdateDescriptor" being passed down.
'
'        Restrictions:
'        - The first module in collection "MODULENAMES" is the main module
'          (which has no "UpdateProjectData" function implemented)
'        - All other modules in collection "MODULENAMES" contain a file
'          "Ini.dave" with a "UpdateProjectData" function
'-----------------------------------------------------------------------------
'Parameters:
'        r                      reference to the project data
'
'        lngUpdateDescriptor    identification of the update context (input)
'                               - INITIALIZATION for inizialization of the module
'                               - UPDATE_CLOCK for clock update
'                               - SET_MODULE_TO_DEFAULT for setting all modules to
'                                 the default
'
'        bAsk (Optional)        if Ask must be started (true or false)
'
'-----------------------------------------------------------------------------
'Returns:
'        True            on successfull operation
'        False           else
'*****************************************************************************
Function UpdateAllPeripherals(r As ProjectData, lngUpdateDescriptor As Long, Optional bAsk As Variant) As Boolean
  On Error GoTo UpdateAllPeripheralsError

  Dim lngIndex    As Long
  Dim cp          As DAvECollection
  Dim sc          As Script
  Dim sModuleName As String
  Dim sAsk       As String

  UpdateAllPeripherals = False
  Set cp = ObjectCollection("MODULENAMES")


  If (lngUpdateDescriptor = UPDATE_CLOCK) Then
      sAsk = "Any of your settings which are dependent on the system clock" & vbCrLf & _
              "will be adjusted without further notification." & vbCrLf & _
              "Do you really want to continue ?"

  ElseIf (lngUpdateDescriptor = SET_MODULE_TO_DEFAULT) Then
      sAsk = "All modules are set to default. Do you really want to continue ?"
  ElseIf (lngUpdateDescriptor = INITIALIZATION) Then
      sAsk = "All module are neu initialization. Do you really want to continue ?"
  Else
      sAsk = ""
  End If

  If IsMissing(bAsk) Then
      'if no ask
      For lngIndex = 2 To cp.Count  ' skip the first module "main"
          sModuleName = cp(lngIndex).ModuleName
          If (Not UpdateOnePeripherals(r, sModuleName, lngUpdateDescriptor)) Then Exit Function
      Next lngIndex
  
  Else
      If (Ask(sAsk)) Then
          For lngIndex = 2 To cp.Count  ' skip the first module "main"
              sModuleName = cp(lngIndex).ModuleName
              If (Not UpdateOnePeripherals(r, sModuleName, lngUpdateDescriptor)) Then Exit Function
          Next lngIndex
      Else
          Exit Function
      End If
  
  End If
  

  UpdateAllPeripherals = True
  Exit Function

UpdateAllPeripheralsError:
  ErrorBox "Initialize error", Err.Description
  Exit Function
End Function

'*****************************************************************************
'Description:
'        The "UpdateProjectData" functions of all peripheral modules are
'        called with parameter "lngUpdateDescriptor" being passed down.
'
'        Restrictions:
'        - The first module in collection "MODULENAMES" is the main module
'          (which has no "UpdateProjectData" function implemented)
'        - All other modules in collection "MODULENAMES" contain a file
'          "Ini.dave" with a "UpdateProjectData" function
'-----------------------------------------------------------------------------
'Parameters:
'        r                      reference to the project data
'        sModuleName            name of the Module s. Install.inc (e.g. ASC0, CAN1, CC1)
'        lngUpdateDescriptor    identification of the update context (input)
'                               - INITIALIZATION for inizialization of the module
'                               - UPDATE_CLOCK for clock update
'                               - SET_MODULE_TO_DEFAULT for set the module to
'                                 the default
'
'        bAsk (Optional)        if Ask must be started (true or false)
'-----------------------------------------------------------------------------
'Returns:
'        True            on successfull operation
'        False           else
'*****************************************************************************
Function UpdateOnePeripherals(r As ProjectData, sModuleName As String, lngUpdateDescriptor As Long, Optional bAsk As Variant) As Boolean
  On Error GoTo UpdateOnePeripheralsError

  Dim lngIndex  As Long
  Dim cp        As DAvECollection
  Dim sc        As Script
  Dim sAsk      As String

  UpdateOnePeripherals = False
  Set cp = ObjectCollection("MODULENAMES")

  If (lngUpdateDescriptor = UPDATE_CLOCK) Then
      sAsk = "Any of your settings in " & sModuleName & " which are dependent on the system clock" & vbCrLf & _
              "will be adjusted without further notification." & vbCrLf & _
              "Do you really want to continue ?"
  ElseIf (lngUpdateDescriptor = SET_MODULE_TO_DEFAULT) Then
      sAsk = "The module " & sModuleName & " will be set to the default. Do you really want to continue ?"
  ElseIf (lngUpdateDescriptor = INITIALIZATION) Then
      sAsk = "The module " & sModuleName & " will be neu initialization. Do you really want to continue ?"
  Else
      sAsk = ""
  End If

  If IsMissing(bAsk) Then
      'if no ask
      Set sc = New Script
      sc.Load (DefGetPath("Scripts") & cp(sModuleName).ModuleDirectory & "\Ini.dave")
      If (Not sc.Functions.UpdateProjectData(r, cp(sModuleName).ParameterValue, lngUpdateDescriptor)) Then Exit Function
  Else
      If (Ask(sAsk)) Then
          Set sc = New Script
          sc.Load (DefGetPath("Scripts") & cp(sModuleName).ModuleDirectory & "\Ini.dave")
          If (Not sc.Functions.UpdateProjectData(r, cp(sModuleName).ParameterValue, lngUpdateDescriptor)) Then Exit Function
      Else
          Exit Function
      End If
  
  End If

  UpdateOnePeripherals = True
  Exit Function

UpdateOnePeripheralsError:
  ErrorBox "Initialize error", Err.Description
  Exit Function
End Function

Function GetPublisher(r As ProjectData, sIndex As String, sKeyword As String) As String
    On Error GoTo GetPublisherError

    Dim i       As Integer
    Dim cNode   As New DAvECollection
    Dim uNode   As New LinNode
    Dim bFlag   As Boolean
    
    bFlag = False
    If (r.v("LIN_NodeList_EXIST")) Then
    Set cNode = ObjectCollection("LinNodes")
    For Each uNode In cNode
        If (sKeyword = "Signal") Then
            For i = 0 To uNode.SPubCount - 1
                If (uNode.SigPublisher(i) = sIndex) Then
                    GetPublisher = uNode.Name
                    bFlag = True
                End If
            Next i
        ElseIf (sKeyword = "Frame") Then
            For i = 0 To uNode.FPubCount - 1
                If (uNode.FrmPublisher(i) = sIndex) Then
                    GetPublisher = uNode.Name
                    bFlag = True
                End If
            Next i
        End If
        If (bFlag) Then
            Exit For
        End If
    Next
    End If
    Exit Function

GetPublisherError:
    ErrorBox "Error - GetPublisher", Err.Description
    Exit Function
End Function
Function GetSubscriber(nIndex As Integer, sIndex As String, sKeyword As String) As String
    On Error GoTo GetSubscriberError
    Dim i As Integer
    Dim cNode As New DAvECollection
    Dim uNode As New LinNode
    Dim bFlag As Boolean
        
    Set cNode = ObjectCollection("LinNodes")
    bFlag = False
    Set uNode = cNode.Item(nIndex)
        If (sKeyword = "Signal") Then
            For i = 0 To uNode.SSubCount - 1
                If (uNode.SigSubscriber(i) = sIndex) Then
                    GetSubscriber = uNode.Name
                    bFlag = True
                Exit For
                End If
        Next
        ElseIf (sKeyword = "Frame") Then
            For i = 0 To uNode.FSubCount - 1
                If (uNode.FrmSubscriber(i) = sIndex) Then
                    GetSubscriber = uNode.Name
                    bFlag = True
                Exit For
            End If
        Next
        End If
    Exit Function
    
GetSubscriberError:
    ErrorBox "Error", Err.Description
    Exit Function
End Function



