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

Option Explicit
'*****************************************************************************
' Module:
'        CAN
'-----------------------------------------------------------------------------
' Description:
'        This file contains functions which are create controls for the
'        module 'CAN'.
'        Furthermore it contains functions for the event handling of
'        these controls.
'
'-----------------------------------------------------------------------------
' History:
'
'        Rev. Author         Date        Description
'        ---------------------------------------------------------------------
'        1.0  ste            04.04.00    final version
'        1.1  ste            02.11.00    the TC1775B has a new Single Transmission
'                                        Try mode; new FIFO direction (FD) bit;
'                                        new FIFO and Gateway handling
'
'
'        ---------------------------------------------------------------------
'        3.0  ahe            05.11.01    final version for Pegasus
'
'        3.1  ahe            02.07.02    no changes

'        ---------------------------------------------------------------------
'        4.0                 26.08.04    final version for XC164CM Pegasus light
'
'*****************************************************************************

Const TOOLTIP_DISABLED_NOTE_A = "the node A is not enabled"
Const TOOLTIP_DISABLED_NOTE_B = "the node B is not enabled"
Const TOOLTIP_DISABLED_OBJECT = "this message object is not enabled"

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

    OnLoad = False

    '-------------------------------------------------------------------------
    ' page: Object
    '-------------------------------------------------------------------------
    If (Not OnLoadPageObject(ps)) Then Exit Function

    '-------------------------------------------------------------------------
    'page: FIFO_Gateway
    '-------------------------------------------------------------------------
    If (Not OnLoadPageFIFO_Gateway(ps)) Then Exit Function

    '-------------------------------------------------------------------------
    ' page: Interrupt
    '-------------------------------------------------------------------------
    If (Not OnLoadPageInterrupt(ps)) Then Exit Function

    '-------------------------------------------------------------------------
    ' End of pages
    '-------------------------------------------------------------------------
    
    ' call function to initialize the menu
    If (Not InitMenu(ps, "CAN\Objx")) Then Exit Function

    OnLoad = True
    Exit Function

OnLoadError:
    Exit Function
End Function


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

    Dim vIndex As Variant
    Dim sVarPre As String

    vIndex = ps.Parameters.v("x")
    sVarPre = "CAN" & vIndex

    OnChangingPage = False

    '-------------------------------------------------------------------------
    'page: Object
    '-------------------------------------------------------------------------
    If (Not OnChangingPageObject(ps, Pagename)) Then Exit Function

    '-------------------------------------------------------------------------
    'page: FIFO_Gateway
    '-------------------------------------------------------------------------
    If (Not OnChangingPageFIFO_Gateway(ps, Pagename)) Then Exit Function

    '-------------------------------------------------------------------------
    'page: Interrupt
    '-------------------------------------------------------------------------
    If (Not OnChangingPageInterrupt(ps, Pagename)) Then Exit Function


    OnChangingPage = True
    Exit Function

OnChangingPageError:
    Exit Function
End Function


'*****************************************************************************
'Description:
'        This function is called from DAvE in two cases:
'
'        1.
'            The contents of the peripherals window must be refreshed.
'            In this case the 'Controlname' parameter is a empty string
'            and the values array 'v()' contains nothing.
'
'        2.
'            A control on the peripherals has been changed.
'            In this case the 'Controlname' parameter contains the name
'            of the changed control and the values array 'v()' contains the
'            changed value(s).
'
'-----------------------------------------------------------------------------
'Parameters:
'        ps              reference to the Peripherals object
'        Controlname     name of the changed control (for case 2)
'        v()             array with the changed values (for case 2)
'        Page            no. of the page (starts with 1)
'        Pagename        name of the page
'-----------------------------------------------------------------------------
'Returns:
'        True            on successfull operation
'        False           else
'*****************************************************************************
Function OnShow(ps As Peripherals, Controlname As String, v() As Variant, Page As Integer, Pagename As String) As Boolean
    On Error GoTo OnShowError

    Dim vIndex As Variant
    Dim sVarPre As String

    vIndex = ps.Parameters.v("x")
    sVarPre = "CAN" & vIndex

    OnShow = False

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

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

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

    OnShow = True
    Exit Function

OnShowError:
    Exit Function
End Function


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

    OnClick = False

    Dim np As Peripherals


    OnClick = True
    Exit Function

OnClickError:
    Errorbox "Can not process the click", Err.Description
    Exit Function
End Function


'*****************************************************************************
'Description:
'        This function is called from DAvE if the user clicks on a menu entry.
'-----------------------------------------------------------------------------
'Parameters:
'        ps              reference to the Peripherals object
'        Toolname        name of the entry
'        ToolId          id of the entry
'        Tooltext        additional text of the entry
'-----------------------------------------------------------------------------
'Returns:
'        True            on successfull operation
'        False           else
'*****************************************************************************
Function OnMenuClick(ps As Peripherals, Toolname As String, ToolId As Long, Tooltext As String) As Boolean
    On Error GoTo OnMenuClickError

    OnMenuClick = False

    If (Not MenuClick(ps, Toolname, ToolId, Tooltext, "CAN\Objx")) 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 the page 'Control'.
'-----------------------------------------------------------------------------
'Parameters:
'        ps               reference to the Peripherals object
'-----------------------------------------------------------------------------
'Returns:
'        True            on successfull operation
'        False           else
'*****************************************************************************
Public Function OnLoadPageObject(ps As Object) As Boolean
  On Error Resume Next

  OnLoadPageObject = False

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

  OnLoadPageObject = True
End Function



'*****************************************************************************
'Description:
'        This function is called from the OnChangingPage function of the
'        associated module in order load all controls for the requested page
'        if they are not already loaded.
'-----------------------------------------------------------------------------
'Parameters:
'        ps               reference to the Peripherals object
'        Pagename         requested page name
'-----------------------------------------------------------------------------
'Returns:
'        True            on successfull operation
'        False           else
'*****************************************************************************
Function OnChangingPageObject(ps As Object, Pagename As String) As Boolean
  On Error Resume Next
  Dim r As ProjectData
  Dim iNr As Integer
  Dim vIndex As Variant
  Dim sVarPre As String

  vIndex = ps.Parameters.v("x")
  sVarPre = "CAN" & vIndex
  iNr = ps.Parameters.v("Number")

  Set r = ps.ProjectData

  OnChangingPageObject = False
  
  If (Not ps.ExtenderCollection("Object").Loaded) And (Pagename = "Object") Then
    ps.ExtenderCollection("Object").Loaded = True

    '-------------------------------
    ' frame: frmMSGVAL
    '-------------------------------
    If (Not ps.AddFrame("frmMSGVAL", 100, 100, 2850, 1000, "Enable Control")) Then Exit Function
        If (Not ps.AddCheck("chkMSGVAL", 200, 400, 1900, 400, "Enable message object " & iNr & " (MSGVAL)")) Then Exit Function

    '-------------------------------
    ' frame: frmXTD
    '-------------------------------
    If (Not ps.AddFrame("frmXTD", 3100, 100, 2600, 1000, "Identifier Selection (XTD)")) Then Exit Function
        If (Not ps.AddOption("optXTD_0", 200, 300, 2050, 300, "Standard 11-bit identifier")) Then Exit Function
        If (Not ps.AddOption("optXTD_1", 200, 600, 2125, 300, "Extended 29-bit identifier")) Then Exit Function

    '-------------------------------
    ' frame: frmNODE
    '-------------------------------
    If (Not ps.AddFrame("frmNODE", 5850, 100, 2700, 1000, "Node Select (NODE)")) Then Exit Function
        If (Not ps.AddOption("optNODE_0", 200, 250, 2400, 380, "Assign object to CAN node A")) Then Exit Function
        If (Not ps.AddOption("optNODE_1", 200, 550, 2400, 380, "Assign object to CAN node B")) Then Exit Function

    '-------------------------------
    ' frame: frmDIR
    '-------------------------------
    If (Not ps.AddFrame("frmDIR", 100, 1200, 2850, 1350, "Message Direction (DIR)")) Then Exit Function
        If (Not ps.AddOption("optDIR_0", 200, 300, 2425, 380, "Receive ..")) Then Exit Function
        If (Not ps.AddOption("optDIR_1", 200, 800, 2400, 380, "Transmit ..")) Then Exit Function

    '-------------------------------
    ' frame: frmAM
    '-------------------------------
    If (Not ps.AddFrame("frmAM", 3100, 1200, 2600, 1350, "Acceptance Mask")) Then Exit Function
        If (Not ps.AddText("txtAM11", 1300, 350, 975, 315)) Then Exit Function
        If (Not ps.AddLabel("lblAM11", 0, 200, 350, 1050, 390, "Mask 11-bit (AM28-18)")) Then Exit Function
        If (Not ps.AddText("txtAM29", 1300, 850, 1150, 315)) Then Exit Function
        If (Not ps.AddLabel("lblAM29", 0, 200, 850, 1050, 390, "Mask 29-bit (AM28-0)")) Then Exit Function

    '-------------------------------
    ' frame: frmID
    '-------------------------------
    If (Not ps.AddFrame("frmID", 5850, 1200, 2700, 1350, "Arbitration Register")) Then Exit Function
        If (Not ps.AddText("txtID11", 1450, 350, 975, 315)) Then Exit Function
        If (Not ps.AddLabel("lblID11", 0, 200, 350, 1200, 390, "Identifier 11-bit (ID28-18)")) Then Exit Function
        If (Not ps.AddText("txtID29", 1450, 850, 1150, 315)) Then Exit Function
        If (Not ps.AddLabel("lblID29", 0, 200, 850, 1200, 390, "Identifier 29-bit (ID28-0)")) Then Exit Function

    '-------------------------------
    ' frame: frmDLC
    '-------------------------------
    If (Not ps.AddFrame("frmDLC", 100, 2650, 2850, 800, "Data Length Code (DLC)")) Then Exit Function
        If (Not ps.AddCombo("cboDLC", 1150, 300, 1500, 315)) Then Exit Function
        If (Not ps.AddLabel("lblDLC", 0, 200, 350, 950, 400, "Data length")) Then Exit Function

    '-------------------------------
    ' frame: frmRMM
    '-------------------------------
    If (Not ps.AddFrame("frmRMM", 3100, 2650, 5450, 800, "Remote Monitoring")) Then Exit Function
        If (Not ps.AddCheck("chkRMM", 200, 250, 4700, 400, "Copy the identifier and DLC code of a remote frame with matching identifier to this transmit object (RMM)")) Then Exit Function

    '-------------------------------
    ' frame: frmSDT
    '-------------------------------
    If (Not ps.AddFrame("frmSDT", 100, 3550, 2850, 850, "Single Data Transfer Mode (SDT)")) Then Exit Function
        If (Not ps.AddCheck("chkSDT", 200, 300, 2600, 400, "Disable this object automatically after a successful data transfer")) Then Exit Function

    '-------------------------------
    ' frame: frmSTT
    '-------------------------------
    If (Not ps.AddFrame("frmSTT", 100, 4500, 2850, 850, "Single Transmission Try")) Then Exit Function
        If (Not ps.AddCheck("chkSTT", 200, 300, 2250, 400, "Enable single transmission try (STT)")) Then Exit Function

    '-------------------------------
    ' frame: frmDATA
    '-------------------------------
    If (Not ps.AddFrame("frmDATA", 3100, 3550, 5450, 1800, "Data Fields")) Then Exit Function
        If (Not ps.AddText("txtDATA0", 1550, 300, 975, 315)) Then Exit Function
        If (Not ps.AddLabel("lblDATA0", 0, 200, 350, 1335, 355, "Data byte 0")) Then Exit Function
        If (Not ps.AddText("txtDATA1", 4150, 300, 975, 315)) Then Exit Function
        If (Not ps.AddLabel("lblDATA1", 0, 2850, 350, 1245, 355, "Data byte 1")) Then Exit Function
        If (Not ps.AddText("txtDATA2", 1550, 650, 975, 315)) Then Exit Function
        If (Not ps.AddLabel("lblDATA2", 0, 200, 700, 1275, 355, "Data byte 2")) Then Exit Function
        If (Not ps.AddText("txtDATA3", 4150, 650, 975, 315)) Then Exit Function
        If (Not ps.AddLabel("lblDATA3", 0, 2850, 700, 1275, 355, "Data byte 3")) Then Exit Function
        If (Not ps.AddText("txtDATA4", 1550, 1000, 975, 315)) Then Exit Function
        If (Not ps.AddLabel("lblDATA4", 0, 200, 1050, 1335, 355, "Data byte 4")) Then Exit Function
        If (Not ps.AddText("txtDATA5", 4150, 1000, 975, 315)) Then Exit Function
        If (Not ps.AddLabel("lblDATA5", 0, 2850, 1050, 1275, 355, "Data byte 5")) Then Exit Function
        If (Not ps.AddText("txtDATA6", 1550, 1350, 975, 315)) Then Exit Function
        If (Not ps.AddLabel("lblDATA6", 0, 200, 1400, 1305, 355, "Data byte 6")) Then Exit Function
        If (Not ps.AddText("txtDATA7", 4150, 1350, 975, 315)) Then Exit Function
        If (Not ps.AddLabel("lblDATA7", 0, 2850, 1400, 1275, 355, "Data byte 7")) Then Exit Function


  End If
  OnChangingPageObject = True
End Function



'*****************************************************************************
'Description:
'        This function is called from the OnShow function of the associated
'        module. See the description for the OnShow function of the
'        associated module for details.
'-----------------------------------------------------------------------------
'Parameters:
'        ps              reference to the Peripherals object
'        Controlname     control name
'        Pagename        current page name
'        v()             value array
'-----------------------------------------------------------------------------
'Returns:
'        True            on successfull operation
'        False           else
'*****************************************************************************
Public Function OnShowPageObject(ps As Object, Controlname As String, Pagename As String, v() As Variant) As Boolean
  On Error GoTo OnShowPageObjectError

  OnShowPageObject = False

  Dim r As ProjectData
  Dim c As Object
  Dim e As Object
  Dim bDisabled As Boolean
  Dim vIndex As Variant
  Dim sVarPre As String
  Dim sRegPre As String
  Dim d As String
  Dim iNr As Integer
  Dim i As Integer
  Dim bReturn As Boolean

  vIndex = ps.Parameters.v("x")
  iNr = ps.Parameters.v("Number")
  sVarPre = "CAN" & vIndex
  sRegPre = sVarPre & "_"

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

  If (Pagename = "Object") Then
    '-------------------------------------------------------------------------
    ' page: Object (action)
    '-------------------------------------------------------------------------
    Select Case (Controlname)
        '---------------------------------------------------------------------
        ' control: chkMSGVAL (action)
        '---------------------------------------------------------------------
        Case "chkMSGVAL"
              If (v(1) = 1) Then
                r.v(sRegPre & "MSGCTRL" & iNr & "_MSGVAL") = 2
                r.v(sRegPre & "MSGFGCRH" & iNr & "_CANPTR") = iNr
                If (r.v(sRegPre & "ACR_INIT") = 1) Then        ' if the node A is deselected
                  r.v(sRegPre & "MSGCFGL" & iNr & "_NODE") = 1  ' sets node B
                End If
              Else
                bReturn = True
                If (bReturn) Then
                  ' if the last mode was FIFO base mode
                  bReturn = ResetFIFOBaseMode(r, vIndex, iNr)
                End If
                If (bReturn) Then
                  ' if the last mode was normal gateway mode
                  bReturn = ResetNormalGatewayMode(r, vIndex, iNr)
                End If
                If (bReturn) Then
                  'reset object
                  r.sfr(sRegPre & "MSGCTRH" & iNr) = &H0
                  r.sfr(sRegPre & "MSGCTRL" & iNr) = &H5555
                  r.sfr(sRegPre & "MSGCFGH" & iNr) = &H0
                  r.sfr(sRegPre & "MSGCFGL" & iNr) = &H0
                  r.sfr(sRegPre & "MSGFGCRH" & iNr) = &H0
                  r.sfr(sRegPre & "MSGFGCRL" & iNr) = &H0
                End If
              End If

        '---------------------------------------------------------------------
        ' control: optNODE_0 (action)
        '---------------------------------------------------------------------
        Case "optNODE_0"
              r.v(sRegPre & "MSGCFGL" & iNr & "_NODE") = 0
              ChangeFIFOSettings(r, vIndex, iNr, r.v(sRegPre & "MSGFGCRL" & iNr & "_FSIZE"))
              Call ChangeGatewayDestinationSettings(r, vIndex, iNr)
        '---------------------------------------------------------------------
        ' control: optNODE_1 (action)
        '---------------------------------------------------------------------
        Case "optNODE_1"
              r.v(sRegPre & "MSGCFGL" & iNr & "_NODE") = 1
              ChangeFIFOSettings(r, vIndex, iNr, r.v(sRegPre & "MSGFGCRL" & iNr & "_FSIZE"))
              Call ChangeGatewayDestinationSettings(r, vIndex, iNr)

        '---------------------------------------------------------------------
        ' control: chkSDT (action)
        '---------------------------------------------------------------------
        Case "chkSDT"
              r.v(sRegPre & "MSGFGCRL" & iNr & "_SDT") = v(1)

        '---------------------------------------------------------------------
        ' control: chkSTT (action)
        '---------------------------------------------------------------------
        Case "chkSTT"
              r.v(sRegPre & "MSGFGCRL" & iNr & "_STT") = v(1)

        '---------------------------------------------------------------------
        ' control: optXTD_0 (action)
        '---------------------------------------------------------------------
        Case "optXTD_0"
              r.v(sRegPre & "MSGCFGL" & iNr & "_XTD") = 0
              ChangeFIFOSettings(r, vIndex, iNr, r.v(sRegPre & "MSGFGCRL" & iNr & "_FSIZE"))
              Call ChangeGatewayDestinationSettings(r, vIndex, iNr)

        '---------------------------------------------------------------------
        ' control: optXTD_1 (action)
        '---------------------------------------------------------------------
        Case "optXTD_1"
              r.v(sRegPre & "MSGCFGL" & iNr & "_XTD") = 1
              ChangeFIFOSettings(r, vIndex, iNr, r.v(sRegPre & "MSGFGCRL" & iNr & "_FSIZE"))
              Call ChangeGatewayDestinationSettings(r, vIndex, iNr)

        '---------------------------------------------------------------------
        ' control: txtAM11 (action)
        '---------------------------------------------------------------------
        Case "txtAM11"
             If (IsInputFormatOK(v(1), "H3", c("lblAM11").Caption, 0, &H7FF)) Then
               r.v(sRegPre & "MSGAMRH" & iNr & "_AM") = v(1) * 2 ^ 2
               r.v(sRegPre & "MSGAMRL" & iNr & "_AM") = 0
               ChangeFIFOSettings(r, vIndex, iNr, r.v(sRegPre & "MSGFGCRL" & iNr & "_FSIZE"))
             Else
               Exit Function
             End If

        '---------------------------------------------------------------------
        ' control: txtAM29 (action)
        '---------------------------------------------------------------------
        Case "txtAM29"
             If (IsInputFormatOK(v(1), "H8", c("lblAM29").Caption, 0, &H1FFFFFFF)) Then
               r.v(sRegPre & "MSGAMRL" & iNr & "_AM") = v(1) And &HFFFF
               r.v(sRegPre & "MSGAMRH" & iNr & "_AM") = v(1) \ 2 ^ 16
               ChangeFIFOSettings(r, vIndex, iNr, r.v(sRegPre & "MSGFGCRL" & iNr & "_FSIZE"))
             Else
               Exit Function
             End If

        '---------------------------------------------------------------------
        ' control: txtID11 (action)
        '---------------------------------------------------------------------
        Case "txtID11"
             If (IsInputFormatOK(v(1), "H3", c("lblID11").Caption, 0, &H7FF)) Then
               r.v(sRegPre & "MSGARH" & iNr & "_ID") = v(1) * 2 ^ 2
               r.v(sRegPre & "MSGARL" & iNr & "_ID") = 0
               ChangeFIFOSettings(r, vIndex, iNr, r.v(sRegPre & "MSGFGCRL" & iNr & "_FSIZE"))
            Else
               Exit Function
             End If

        '---------------------------------------------------------------------
        ' control: txtID29 (action)
        '---------------------------------------------------------------------
        Case "txtID29"
             If (IsInputFormatOK(v(1), "H8", c("lblID29").Caption, 0, &H1FFFFFFF)) Then
               r.v(sRegPre & "MSGARL" & iNr & "_ID") = v(1) And &HFFFF
               r.v(sRegPre & "MSGARH" & iNr & "_ID") = v(1) \ 2 ^ 16
               ChangeFIFOSettings(r, vIndex, iNr, r.v(sRegPre & "MSGFGCRL" & iNr & "_FSIZE"))
             Else
               Exit Function
             End If

        '---------------------------------------------------------------------
        ' control: optDIR_0 (action)
        '---------------------------------------------------------------------
        Case "optDIR_0"
              r.v(sRegPre & "MSGCFGL" & iNr & "_DIR") = 0
              r.v(sRegPre & "MSGCFGL" & iNr & "_RMM") = 0
              ChangeFIFOSettings(r, vIndex, iNr, r.v(sRegPre & "MSGFGCRL" & iNr & "_FSIZE"))

        '---------------------------------------------------------------------
        ' control: optDIR_1 (action)
        '---------------------------------------------------------------------
        Case "optDIR_1"
              bReturn = True
              If (bReturn) Then
                ' if the last mode was normal gateway mode
                bReturn = ResetNormalGatewayMode(r, vIndex, iNr)
              End If
              If (bReturn) Then
                r.v(sRegPre & "MSGCFGL" & iNr & "_DIR") = 1
              End If
              ChangeFIFOSettings(r, vIndex, iNr, r.v(sRegPre & "MSGFGCRL" & iNr & "_FSIZE"))

        '---------------------------------------------------------------------
        ' control: chkRMM (action)
        '---------------------------------------------------------------------
        Case "chkRMM"
              r.v(sRegPre & "MSGCFGL" & iNr & "_RMM") = v(1)

        '---------------------------------------------------------------------
        ' control: cboDLC (action)
        '---------------------------------------------------------------------
        Case "cboDLC"
            For i = 0 To 8
              If (i & " data bytes" = v(1)) Then
                r.v(sRegPre & "MSGCFGL" & iNr & "_DLC") = i
                Exit For
              End If
            Next i

      Case Else
    End Select

    For i = 0 To 7
      '---------------------------------------------------------------------
      ' control: txtDATAx (action) (x = 0-7)
      '---------------------------------------------------------------------
      If (Controlname = "txtDATA" & i) Then
        If (IsInputFormatOK(v(1), "H2", c("lblDATA" & i).Caption, 0, &HFF)) Then
          If (i = 0 Or i = 1 Or i = 5 Or i = 4) Then
              r.v(sRegPre & "MSGDRL" & iNr & (i \ 4) * 4 & "_DATA" & i) = v(1)
          Else
              r.v(sRegPre & "MSGDRH" & iNr & (i \ 4) * 4 & "_DATA" & i) = v(1)
          End If
          Exit For
        Else
          Exit Function
        End If

      End If
    Next i

    '-------------------------------------------------------------------------
    ' page: Object (show)
    '-------------------------------------------------------------------------

    '-------------------------------------------------------------------------
    ' control: chkMSGVAL (show)
    '-------------------------------------------------------------------------
    With c("chkMSGVAL")
      ' disable condition
      bDisabled = IsModuleClockDisabled(r, vIndex) Or _
                  (r.v(sRegPre & "ACR_INIT") = 1 And r.v(sRegPre & "BCR_INIT") = 1) Or _
                  (IsObjGatewayDestination(r, vIndex, iNr) >= 0) Or _
                  (IsObjFIFOSlave(r, vIndex, iNr) >= 0)
      .Enabled = Not (bDisabled)
      ' show condition
      .Value = -CInt(r.v(sRegPre & "MSGCTRL" & iNr & "_MSGVAL") = 2)
    End With

    With e("chkMSGVAL")
      If (bDisabled) Then
        ' tooltip if control is disabled
        .DisabledTooltipText = TOOLTIP_DISABLED_IF
        .DisabledTooltipText = .DisabledTooltipText & GetTooltipModuleClockDisabled(r, vIndex)
        If (r.v(sRegPre & "ACR_INIT") = 1 And r.v(sRegPre & "BCR_INIT") = 1) Then
          .DisabledTooltipText = .DisabledTooltipText & FormatDisabledTooltip("both " & sVarPre & " nodes (A and B) are deselected")
        End If
        If (IsObjGatewayDestination(r, vIndex, iNr) >= 0) Then
          .DisabledTooltipText = .DisabledTooltipText & FormatDisabledTooltip("this object is a gateway destination object, connected to the source object " & IsObjGatewayDestination(r, vIndex, iNr))
        End If
        If (IsObjFIFOSlave(r, vIndex, iNr) >= 0) Then
          .DisabledTooltipText = .DisabledTooltipText & _
            FormatDisabledTooltip("this object is a FIFO slave object, connected to the base object " & IsObjFIFOSlave(r, vIndex, iNr))
        End If
      Else
        ' tooltip if control is enabled
      End If
    End With

    '-------------------------------------------------------------------------
    ' control: optNODE_0 (show)
    '-------------------------------------------------------------------------
    With c("optNODE_0")
      ' disable condition
      bDisabled = r.v(sRegPre & "ACR_INIT") = 1 Or r.v(sRegPre & "MSGCTRL" & iNr & "_MSGVAL") <> 2 Or (IsObjGatewayDestination(r, vIndex, iNr) >= 0) Or (IsObjFIFOSlave(r, vIndex, iNr) >= 0)
      If ((IsObjGatewayDestination(r, vIndex, iNr) >= 0 Or IsObjFIFOSlave(r, vIndex, iNr) >= 0) And r.v(sRegPre & "MSGCFGL" & iNr & "_NODE") = 0) Then   ' for the selected NODE
        .Enabled = True
      Else
        .Enabled = Not (bDisabled)
      End If
      ' show condition
      .Value = (r.v(sRegPre & "MSGCFGL" & iNr & "_NODE") = 0)
    End With

    With e("optNODE_0")
      If (bDisabled) Then
        ' tooltip if control is disabled
        .DisabledTooltipText = TOOLTIP_DISABLED_IF
        If (r.v(sRegPre & "ACR_INIT") = 1) Then
          .DisabledTooltipText = .DisabledTooltipText & FormatDisabledTooltip(TOOLTIP_DISABLED_NOTE_A)
        End If
        If (r.v(sRegPre & "MSGCTRL" & iNr & "_MSGVAL") <> 2) Then
          .DisabledTooltipText = .DisabledTooltipText & FormatDisabledTooltip(TOOLTIP_DISABLED_OBJECT)
        End If
        If (IsObjGatewayDestination(r, vIndex, iNr) >= 0) Then
          .DisabledTooltipText = .DisabledTooltipText & FormatDisabledTooltip("this object is a gateway destination object, connected to the source object " & IsObjGatewayDestination(r, vIndex, iNr))
        End If
        If (IsObjFIFOSlave(r, vIndex, iNr) >= 0) Then
          .DisabledTooltipText = .DisabledTooltipText & FormatDisabledTooltip("this object is a FIFO slave object, connected to the base object " & IsObjFIFOSlave(r, vIndex, iNr))
        End If
      Else
        ' tooltip if control is enabled
      End If
    End With

    '-------------------------------------------------------------------------
    ' control: optNODE_1 (show)
    '-------------------------------------------------------------------------
    With c("optNODE_1")
      ' disable condition
      bDisabled = r.v(sRegPre & "BCR_INIT") = 1 Or r.v(sRegPre & "MSGCTRL" & iNr & "_MSGVAL") <> 2 Or (IsObjGatewayDestination(r, vIndex, iNr) >= 0) Or (IsObjFIFOSlave(r, vIndex, iNr) >= 0)
      If ((IsObjGatewayDestination(r, vIndex, iNr) >= 0 Or IsObjFIFOSlave(r, vIndex, iNr) >= 0) And r.v(sRegPre & "MSGCFGL" & iNr & "_NODE") = 1) Then  ' for the selected NODE
        .Enabled = True
      Else
        .Enabled = Not (bDisabled)
      End If
      ' show condition
      .Value = (r.v(sRegPre & "MSGCFGL" & iNr & "_NODE") = 1)
    End With

    With e("optNODE_1")
      If (bDisabled) Then
        ' tooltip if control is disabled
        .DisabledTooltipText = TOOLTIP_DISABLED_IF
        If (r.v(sRegPre & "BCR_INIT") = 1) Then
          .DisabledTooltipText = .DisabledTooltipText & FormatDisabledTooltip(TOOLTIP_DISABLED_NOTE_B)
        End If
        If (r.v(sRegPre & "MSGCTRL" & iNr & "_MSGVAL") <> 2) Then
          .DisabledTooltipText = .DisabledTooltipText & FormatDisabledTooltip(TOOLTIP_DISABLED_OBJECT)
        End If
        If (IsObjGatewayDestination(r, vIndex, iNr) >= 0) Then
          .DisabledTooltipText = .DisabledTooltipText & FormatDisabledTooltip("this object is a gateway destination object, connected to the source object " & IsObjGatewayDestination(r, vIndex, iNr))
        End If
        If (IsObjFIFOSlave(r, vIndex, iNr) >= 0) Then
          .DisabledTooltipText = .DisabledTooltipText & FormatDisabledTooltip("this object is a FIFO slave object, connected to the base object " & IsObjFIFOSlave(r, vIndex, iNr))
        End If
      Else
        ' tooltip if control is enabled
      End If
    End With

    For i = 0 To 1
      '-------------------------------------------------------------------------
      ' control: optXTD_x (show) (x = 0,1)
      '-------------------------------------------------------------------------
      With c("optXTD_" & i)
        ' disable condition
        bDisabled = IsModuleClockDisabled(r, vIndex) Or r.v(sRegPre & "MSGCTRL" & iNr & "_MSGVAL") <> 2 Or (IsObjGatewayDestination(r, vIndex, iNr) >= 0) Or (IsObjFIFOSlave(r, vIndex, iNr) >= 0)
        If ((IsObjGatewayDestination(r, vIndex, iNr) >= 0 Or IsObjFIFOSlave(r, vIndex, iNr) >= 0) And r.v(sRegPre & "MSGCFGL" & iNr & "_XTD") = i) Then  ' for the selected XTD
          .Enabled = True
        Else
          .Enabled = Not (bDisabled)
        End If
        ' show condition
        .Value = (r.v(sRegPre & "MSGCFGL" & iNr & "_XTD") = i)
      End With

      With e("optXTD_" & i)
        If (bDisabled) Then
          ' tooltip if control is disabled
          .DisabledTooltipText = TOOLTIP_DISABLED_IF
          .DisabledTooltipText = .DisabledTooltipText & GetTooltipModuleClockDisabled(r, vIndex)
          If (r.v(sRegPre & "MSGCTRL" & iNr & "_MSGVAL") <> 2) Then
            .DisabledTooltipText = .DisabledTooltipText & FormatDisabledTooltip(TOOLTIP_DISABLED_OBJECT)
          End If
          If (IsObjGatewayDestination(r, vIndex, iNr) >= 0) Then
            .DisabledTooltipText = .DisabledTooltipText & FormatDisabledTooltip("this object is a gateway destination object, connected to the source object " & IsObjGatewayDestination(r, vIndex, iNr))
          End If
          If (IsObjFIFOSlave(r, vIndex, iNr) >= 0) Then
            .DisabledTooltipText = .DisabledTooltipText & FormatDisabledTooltip("this object is a FIFO slave object, connected to the base object " & IsObjFIFOSlave(r, vIndex, iNr))
          End If
        Else
          ' tooltip if control is enabled
        End If
      End With
    Next i

    '-------------------------------------------------------------------------
    ' control: optDIR_0 (show)
    '-------------------------------------------------------------------------
    With c("optDIR_0")
      ' disable condition
      bDisabled = r.v(sRegPre & "MSGCTRL" & iNr & "_MSGVAL") <> 2 Or (IsObjGatewayDestination(r, vIndex, iNr) >= 0) Or (IsObjFIFOSlave(r, vIndex, iNr) >= 0)
      If ((IsObjGatewayDestination(r, vIndex, iNr) >= 0 Or IsObjFIFOSlave(r, vIndex, iNr) >= 0) And r.v(sRegPre & "MSGCFGL" & iNr & "_DIR") = 0) Then  ' for the selected DIR
        .Enabled = True
      Else
        .Enabled = Not (bDisabled)
      End If
      ' if the connected node is in analyser mode
      If ((r.v(sRegPre & "MSGCFGL" & iNr & "_NODE") = 0 And r.v(sRegPre & "ACR_CALM") = 1) Or (r.v(sRegPre & "MSGCFGL" & iNr & "_NODE") = 1 And r.v(sRegPre & "BCR_CALM") = 1)) Then
        .Caption = "Receive data frames"
      Else
        .Caption = "Receive data frames, transmit remote frames"
      End If
      ' show condition
      .Value = (r.v(sRegPre & "MSGCFGL" & iNr & "_DIR") = 0)
    End With

    With e("optDIR_0")
      If (bDisabled) Then
        ' tooltip if control is disabled
        .DisabledTooltipText = TOOLTIP_DISABLED_IF
        If (r.v(sRegPre & "MSGCTRL" & iNr & "_MSGVAL") <> 2) Then
          .DisabledTooltipText = .DisabledTooltipText & FormatDisabledTooltip(TOOLTIP_DISABLED_OBJECT)
        End If
        If (IsObjGatewayDestination(r, vIndex, iNr) >= 0) Then
          .DisabledTooltipText = .DisabledTooltipText & FormatDisabledTooltip("this object is a gateway destination object, connected to the source object " & IsObjGatewayDestination(r, vIndex, iNr))
        End If
        If (IsObjFIFOSlave(r, vIndex, iNr) >= 0) Then
          .DisabledTooltipText = .DisabledTooltipText & FormatDisabledTooltip("this object is a FIFO slave object, connected to the base object " & IsObjFIFOSlave(r, vIndex, iNr))
        End If
      Else
        ' tooltip if control is enabled
      End If
    End With

    '-------------------------------------------------------------------------
    ' control: optDIR_1 (show)
    '-------------------------------------------------------------------------
    With c("optDIR_1")
      ' disable condition
      bDisabled = r.v(sRegPre & "MSGCTRL" & iNr & "_MSGVAL") <> 2 Or (IsObjGatewayDestination(r, vIndex, iNr) >= 0) Or (IsObjFIFOSlave(r, vIndex, iNr) >= 0)
      If ((IsObjGatewayDestination(r, vIndex, iNr) >= 0 Or IsObjFIFOSlave(r, vIndex, iNr) >= 0) And r.v(sRegPre & "MSGCFGL" & iNr & "_DIR") = 1) Then  ' for the selected DIR
        .Enabled = True
      Else
        .Enabled = Not (bDisabled)
      End If
      ' if the connected node is in analyser mode
      If ((r.v(sRegPre & "MSGCFGL" & iNr & "_NODE") = 0 And r.v(sRegPre & "ACR_CALM") = 1) Or (r.v(sRegPre & "MSGCFGL" & iNr & "_NODE") = 1 And r.v(sRegPre & "BCR_CALM") = 1)) Then
        .Caption = "Receive remote frames"
      Else
        .Caption = "Transmit data frames, receive and answer remote frames"
      End If
      ' show condition
      .Value = (r.v(sRegPre & "MSGCFGL" & iNr & "_DIR") = 1)
    End With

    With e("optDIR_1")
      If (bDisabled) Then
        ' tooltip if control is disabled
        .DisabledTooltipText = TOOLTIP_DISABLED_IF
        If (r.v(sRegPre & "MSGCTRL" & iNr & "_MSGVAL") <> 2) Then
          .DisabledTooltipText = .DisabledTooltipText & FormatDisabledTooltip(TOOLTIP_DISABLED_OBJECT)
        End If
        If (IsObjGatewayDestination(r, vIndex, iNr) >= 0) Then
          .DisabledTooltipText = .DisabledTooltipText & FormatDisabledTooltip("this object is a gateway destination object, connected to the source object " & IsObjGatewayDestination(r, vIndex, iNr))
        End If
        If (IsObjFIFOSlave(r, vIndex, iNr) >= 0) Then
          .DisabledTooltipText = .DisabledTooltipText & FormatDisabledTooltip("this object is a FIFO slave object, connected to the base object " & IsObjFIFOSlave(r, vIndex, iNr))
        End If
      Else
        ' tooltip if control is enabled
      End If
    End With

    '-------------------------------------------------------------------------
    ' control: txtAM11 (show)
    '-------------------------------------------------------------------------
    With c("txtAM11")
      ' disable condition
      bDisabled = r.v(sRegPre & "MSGCTRL" & iNr & "_MSGVAL") <> 2 Or r.v(sRegPre & "MSGCFGL" & iNr & "_XTD") = 1 Or (IsObjFIFOSlave(r, vIndex, iNr) >= 0)
      .Enabled = Not (bDisabled)
      ' show condition
      .Text = FormatTextOutput(r.v(sRegPre & "MSGAMRH" & iNr & "_AM") \ 2 ^ 2, "H3")
    End With

    With e("txtAM11")
      If (bDisabled) Then
        ' tooltip if control is disabled
        .DisabledTooltipText = TOOLTIP_DISABLED_IF
        If (r.v(sRegPre & "MSGCTRL" & iNr & "_MSGVAL") <> 2) Then
          .DisabledTooltipText = .DisabledTooltipText & FormatDisabledTooltip(TOOLTIP_DISABLED_OBJECT)
        End If
        If (IsObjFIFOSlave(r, vIndex, iNr) >= 0) Then
          .DisabledTooltipText = .DisabledTooltipText & FormatDisabledTooltip("this object is a FIFO slave object, connected to the base object " & IsObjFIFOSlave(r, vIndex, iNr))
        End If
        If (r.v(sRegPre & "MSGCFGL" & iNr & "_XTD") = 1) Then
          .DisabledTooltipText = .DisabledTooltipText & FormatDisabledTooltip("the extended 29-bit identifier is selected")
        End If
      Else
        ' tooltip if control is enabled
      End If
    End With

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

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

    '-------------------------------------------------------------------------
    ' control: txtAM29 (show)
    '-------------------------------------------------------------------------
    With c("txtAM29")
      ' disable condition
      bDisabled = r.v(sRegPre & "MSGCTRL" & iNr & "_MSGVAL") <> 2 Or r.v(sRegPre & "MSGCFGL" & iNr & "_XTD") = 0 Or (IsObjFIFOSlave(r, vIndex, iNr) >= 0)
      .Enabled = Not (bDisabled)
      ' show condition
      .Text = FormatTextOutput(r.v(sRegPre & "MSGAMRH" & iNr & "_AM") * 2 ^ 16 + _
                               r.v(sRegPre & "MSGAMRL" & iNr & "_AM"), "H8")
    End With

    With e("txtAM29")
      If (bDisabled) Then
        ' tooltip if control is disabled
        .DisabledTooltipText = TOOLTIP_DISABLED_IF
        If (r.v(sRegPre & "MSGCTRL" & iNr & "_MSGVAL") <> 2) Then
          .DisabledTooltipText = .DisabledTooltipText & FormatDisabledTooltip(TOOLTIP_DISABLED_OBJECT)
        End If
        If (IsObjFIFOSlave(r, vIndex, iNr) >= 0) Then
          .DisabledTooltipText = .DisabledTooltipText & FormatDisabledTooltip("this object is a FIFO slave object, connected to the base object " & IsObjFIFOSlave(r, vIndex, iNr))
        End If
        If (r.v(sRegPre & "MSGCFGL" & iNr & "_XTD") = 0) Then
          .DisabledTooltipText = .DisabledTooltipText & FormatDisabledTooltip("the standard 11-bit identifier is selected")
        End If
      Else
        ' tooltip if control is enabled
      End If
    End With

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

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

    '-------------------------------------------------------------------------
    ' control: txtID11 (show)
    '-------------------------------------------------------------------------
    With c("txtID11")
      ' disable condition
      bDisabled = r.v(sRegPre & "MSGCTRL" & iNr & "_MSGVAL") <> 2 Or r.v(sRegPre & "MSGCFGL" & iNr & "_XTD") = 1 Or (IsObjFIFOSlave(r, vIndex, iNr) >= 0)
      .Enabled = Not (bDisabled)
      ' show condition
      .Text = FormatTextOutput(r.v(sRegPre & "MSGARH" & iNr & "_ID") \ 2 ^ 2, "H3")
    End With

    With e("txtID11")
      If (bDisabled) Then
        ' tooltip if control is disabled
        .DisabledTooltipText = TOOLTIP_DISABLED_IF
        If (r.v(sRegPre & "MSGCTRL" & iNr & "_MSGVAL") <> 2) Then
          .DisabledTooltipText = .DisabledTooltipText & FormatDisabledTooltip(TOOLTIP_DISABLED_OBJECT)
        End If
        If (IsObjFIFOSlave(r, vIndex, iNr) >= 0) Then
          .DisabledTooltipText = .DisabledTooltipText & FormatDisabledTooltip("this object is a FIFO slave object, connected to the base object " & IsObjFIFOSlave(r, vIndex, iNr))
        End If
        If (r.v(sRegPre & "MSGCFGL" & iNr & "_XTD") = 1) Then
          .DisabledTooltipText = .DisabledTooltipText & FormatDisabledTooltip("the extended 29-bit identifier is selected")
        End If
      Else
        ' tooltip if control is enabled
      End If
    End With

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

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

    '-------------------------------------------------------------------------
    ' control: txtID29 (show)
    '-------------------------------------------------------------------------
    With c("txtID29")
      ' disable condition
      bDisabled = r.v(sRegPre & "MSGCTRL" & iNr & "_MSGVAL") <> 2 Or r.v(sRegPre & "MSGCFGL" & iNr & "_XTD") = 0 Or (IsObjFIFOSlave(r, vIndex, iNr) >= 0)
      .Enabled = Not (bDisabled)
      ' show condition
      .Text = FormatTextOutput(r.v(sRegPre & "MSGARH" & iNr & "_ID") * 2 ^ 16 + _
                               r.v(sRegPre & "MSGARL" & iNr & "_ID"), "H8")
    End With

    With e("txtID29")
      If (bDisabled) Then
        ' tooltip if control is disabled
        .DisabledTooltipText = TOOLTIP_DISABLED_IF
        If (r.v(sRegPre & "MSGCTRL" & iNr & "_MSGVAL") <> 2) Then
          .DisabledTooltipText = .DisabledTooltipText & FormatDisabledTooltip(TOOLTIP_DISABLED_OBJECT)
        End If
        If (IsObjFIFOSlave(r, vIndex, iNr) >= 0) Then
          .DisabledTooltipText = .DisabledTooltipText & FormatDisabledTooltip("this object is a FIFO slave object, connected to the base object " & IsObjFIFOSlave(r, vIndex, iNr))
        End If
        If (r.v(sRegPre & "MSGCFGL" & iNr & "_XTD") = 0) Then
          .DisabledTooltipText = .DisabledTooltipText & FormatDisabledTooltip("the standard 11-bit identifier is selected")
        End If
      Else
        ' tooltip if control is enabled
      End If
    End With

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

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

    '-------------------------------------------------------------------------
    ' control: chkRMM (show)
    '-------------------------------------------------------------------------
    With c("chkRMM")
      ' disable condition
      bDisabled = r.v(sRegPre & "MSGCTRL" & iNr & "_MSGVAL") <> 2 Or r.v(sRegPre & "MSGCFGL" & iNr & "_DIR") = 0
      .Enabled = Not (bDisabled)
      ' show condition
      .Value = -CInt(r.v(sRegPre & "MSGCFGL" & iNr & "_RMM") = 1)
    End With

    With e("chkRMM")
      If (bDisabled) Then
        ' tooltip if control is disabled
        .DisabledTooltipText = TOOLTIP_DISABLED_IF
         If (r.v(sRegPre & "MSGCTRL" & iNr & "_MSGVAL") <> 2) Then
           .DisabledTooltipText = .DisabledTooltipText & FormatDisabledTooltip(TOOLTIP_DISABLED_OBJECT)
         End If
         If (r.v(sRegPre & "MSGCFGL" & iNr & "_DIR") = 0) Then  ' if receive object
           .DisabledTooltipText = .DisabledTooltipText & FormatDisabledTooltip("this object is configured as receive object")
         End If
      Else
        ' tooltip if control is enabled
      End If
    End With

    '-------------------------------------------------------------------------
    ' control: cboDLC (show)
    '-------------------------------------------------------------------------
    With c("cboDLC")
      ' disable condition
      bDisabled = r.v(sRegPre & "MSGCTRL" & iNr & "_MSGVAL") <> 2
      .Enabled = Not (bDisabled)
      ' show condition
      .Clear
      For i = 0 To 8
        .AddItem i & " data bytes"
      Next i
      .ListIndex = r.v(sRegPre & "MSGCFGL" & iNr & "_DLC")
    End With

    With e("cboDLC")
      If (bDisabled) Then
        ' tooltip if control is disabled
        .DisabledTooltipText = TOOLTIP_DISABLED_IF
         If (r.v(sRegPre & "MSGCTRL" & iNr & "_MSGVAL") <> 2) Then
           .DisabledTooltipText = .DisabledTooltipText & FormatDisabledTooltip(TOOLTIP_DISABLED_OBJECT)
         End If
      Else
        ' tooltip if control is enabled
      End If
    End With

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

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

    '-------------------------------------------------------------------------
    ' control: chkSDT (show)
    '-------------------------------------------------------------------------
    With c("chkSDT")
      ' disable condition
      bDisabled = r.v(sRegPre & "MSGCTRL" & iNr & "_MSGVAL") <> 2
      .Enabled = Not (bDisabled)
      ' show condition
      .Value = -CInt(r.v(sRegPre & "MSGFGCRL" & iNr & "_SDT") = 1)
    End With

    With e("chkSDT")
      If (bDisabled) Then
        ' tooltip if control is disabled
        .DisabledTooltipText = TOOLTIP_DISABLED_IF
        If (r.v(sRegPre & "MSGCTRL" & iNr & "_MSGVAL") <> 2) Then
          .DisabledTooltipText = .DisabledTooltipText & FormatDisabledTooltip(TOOLTIP_DISABLED_OBJECT)
        End If
      Else
        ' tooltip if control is enabled
      End If
    End With

    '-------------------------------------------------------------------------
    ' control: chkSTT (show)
    '-------------------------------------------------------------------------
    With c("chkSTT")
      ' disable condition
      bDisabled = r.v(sRegPre & "MSGCTRL" & iNr & "_MSGVAL") <> 2
      .Enabled = Not (bDisabled)
      ' show condition
      .Value = -CInt(r.v(sRegPre & "MSGFGCRL" & iNr & "_STT") = 1)
    End With

    With e("chkSTT")
      If (bDisabled) Then
        ' tooltip if control is disabled
        .DisabledTooltipText = TOOLTIP_DISABLED_IF
         If (r.v(sRegPre & "MSGCTRL" & iNr & "_MSGVAL") <> 2) Then
           .DisabledTooltipText = .DisabledTooltipText & FormatDisabledTooltip(TOOLTIP_DISABLED_OBJECT)
         End If
      Else
        ' tooltip if control is enabled
      End If
    End With


    For i = 0 To 7
      '-------------------------------------------------------------------------
      ' control: txtDATAx  (show) (x = 0-7)
      '-------------------------------------------------------------------------
      With c("txtDATA" & i)
        ' disable condition
        bDisabled = r.v(sRegPre & "MSGCTRL" & iNr & "_MSGVAL") <> 2 Or r.v(sRegPre & "MSGCFGL" & iNr & "_DIR") = 0 Or r.v(sRegPre & "MSGCFGL" & iNr & "_DLC") <= i
        .Enabled = Not (bDisabled)
        ' show condition
        If (i = 0 Or i = 1 Or i = 5 Or i = 4) Then
            .Text = FormatTextOutput(r.v(sRegPre & "MSGDRL" & iNr & (i \ 4) * 4 & "_DATA" & i), "H2")
        Else
            .Text = FormatTextOutput(r.v(sRegPre & "MSGDRH" & iNr & (i \ 4) * 4 & "_DATA" & i), "H2")
        End If
      End With

      With e("txtDATA" & i)
        If (bDisabled) Then
          ' tooltip if control is disabled
          .DisabledTooltipText = TOOLTIP_DISABLED_IF
           If (r.v(sRegPre & "MSGCTRL" & iNr & "_MSGVAL") <> 2) Then
             .DisabledTooltipText = .DisabledTooltipText & FormatDisabledTooltip(TOOLTIP_DISABLED_OBJECT)
           End If
           If (r.v(sRegPre & "MSGCFGL" & iNr & "_DIR") = 0) Then  ' if receive object
             .DisabledTooltipText = .DisabledTooltipText & FormatDisabledTooltip("this object is configured as receive object")
           End If
           If (r.v(sRegPre & "MSGCFGL" & iNr & "_DLC") <= i) Then
             .DisabledTooltipText = .DisabledTooltipText & FormatDisabledTooltip("the data length code is configured only for " & r.v(sRegPre & "MSGCFGL" & iNr & "_DLC") & " data bytes")
           End If
        Else
          ' tooltip if control is enabled
        End If
      End With

      '-------------------------------------------------------------------------
      ' control: lblDATAx (show)  (x = 0-7)
      '-------------------------------------------------------------------------
      With c("lblDATA" & i)
        ' same as for txtDATAx
        .Enabled = Not (bDisabled)
      End With

      With e("lblDATA" & i)
        If (bDisabled) Then
          ' tooltip if control is disabled
          .DisabledTooltipText = e("txtDATA" & i).DisabledTooltipText
        Else
          ' tooltip if control is enabled
        End If
      End With

    Next i
  End If


  OnShowPageObject = True
  Exit Function

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


'*****************************************************************************
'Description:
'        This function is called from the OnLoad function of the associated
'        module in order to add the page 'FIFO_Gateway'.
'-----------------------------------------------------------------------------
'Parameters:
'        ps               reference to the Peripherals object
'-----------------------------------------------------------------------------
'Returns:
'        True            on successfull operation
'        False           else
'*****************************************************************************
Public Function OnLoadPageFIFO_Gateway(ps As Object) As Boolean
  On Error Resume Next

  OnLoadPageFIFO_Gateway = False

  If (Not ps.AddPage("FIFO/Gateway")) Then Exit Function

  OnLoadPageFIFO_Gateway = True
End Function



'*****************************************************************************
'Description:
'        This function is called from the OnChangingPage function of the
'        associated module in order load all controls for the requested page
'        if they are not already loaded.
'-----------------------------------------------------------------------------
'Parameters:
'        ps               reference to the Peripherals object
'        Pagename         requested page name
'-----------------------------------------------------------------------------
'Returns:
'        True            on successfull operation
'        False           else
'*****************************************************************************
Function OnChangingPageFIFO_Gateway(ps As Object, Pagename As String) As Boolean
  On Error Resume Next
  Dim r As ProjectData
  Dim iNr As Integer
  Dim vIndex As Variant
  Dim sVarPre As String

  OnChangingPageFIFO_Gateway = False

  vIndex = ps.Parameters.v("x")
  sVarPre = "CAN" & vIndex
  iNr = ps.Parameters.v("Number")

  Set r = ps.ProjectData
  
  If (Not ps.ExtenderCollection("FIFO/Gateway").Loaded) And (Pagename = "FIFO/Gateway") Then
    ps.ExtenderCollection("FIFO/Gateway").Loaded = True



    '-------------------------------
    ' frame: frmMMC
    '-------------------------------
    If (Not ps.AddFrame("frmMMC", 100, 100, 4000, 2300, "Message Object Mode Control (MMC)")) Then Exit Function
        If (Not ps.AddOption("optMMC_0", 200, 300, 3650, 300, "Functionality of standard message object")) Then Exit Function
        If (Not ps.AddOption("optMMC_1", 200, 6000, 200, 200, "")) Then Exit Function  ' dummy control
        If (Not ps.AddOption("optMMC_2", 200, 650, 3650, 300, "FIFO functionality; this is a FIFO base object")) Then Exit Function
        If (Not ps.AddOption("optMMC_3", 200, 1000, 3650, 300, "FIFO functionality; this is a FIFO slave object")) Then Exit Function
        If (iNr = 0 Or iNr = 31) Then  ' obj 0 can not have a gateway source; obj 31 can not have a gatway destintion
          If (Not ps.AddOption("optMMC_4", 200, 1350, 3700, 400, "Normal gateway mode")) Then Exit Function
        Else
          If ((iNr Mod 2) = 0) Then
            If (Not ps.AddOption("optMMC_4", 200, 1350, 3700, 400, "Normal gateway mode (gateway destination); object " & iNr - 1 & " is used as gateway source")) Then Exit Function
          Else
            If (Not ps.AddOption("optMMC_4", 200, 1350, 3700, 400, "Normal gateway mode (gateway source); object " & iNr + 1 & " is used as gateway destination")) Then Exit Function
          End If
        End If
'        If (Not ps.AddOption("optMMC_5", 200, 1800, 3700, 400, "Shared gateway mode; this object is initialized as transmit object on the destination side")) Then Exit Function
        If (Not ps.AddOption("optMMC_5", 200, 1800, 3700, 400, "Shared gateway mode")) Then Exit Function

    '-------------------------------
    ' frame: frmFSIZE
    '-------------------------------
    If (Not ps.AddFrame("frmFSIZE", 4300, 100, 1650, 2300, "FIFO Size (FSIZE)")) Then Exit Function
        If (Not ps.AddOption("optFSIZE_1", 200, 300, 1300, 300, "2-stage FIF0")) Then Exit Function
        If (Not ps.AddOption("optFSIZE_2", 200, 650, 1300, 300, "4-stage FIF0")) Then Exit Function
        If (Not ps.AddOption("optFSIZE_3", 200, 1000, 1300, 300, "8-stage FIF0")) Then Exit Function
        If (Not ps.AddOption("optFSIZE_4", 200, 1350, 1350, 300, "16-stage FIF0")) Then Exit Function
        If (Not ps.AddOption("optFSIZE_5", 200, 1700, 1350, 300, "32-stage FIF0")) Then Exit Function

    '-------------------------------
    ' frame: frmFD
    '-------------------------------
    If (Not ps.AddFrame("frmFD", 6100, 100, 2500, 2300, "FIFO Direction (FD)")) Then Exit Function
        If (Not ps.AddOption("optFD_0", 200, 350, 2150, 850, "FIFO reception:               the CANPTR is updated after a correct reception of a frame")) Then Exit Function
        If (Not ps.AddOption("optFD_1", 200, 1350, 2250, 850, "FIFO transmission:              the CANPTR is updated after a correct transmission of a frame")) Then Exit Function

    '-------------------------------
    ' frame: frmDLCC
    '-------------------------------
    If (Not ps.AddFrame("frmDLCC", 100, 2550, 4000, 1300, "Data Length Code Copy (DLCC)")) Then Exit Function
        If ((iNr Mod 2) = 0) Then  ' Destination
          If (Not ps.AddOption("optDLCC_0", 200, 300, 3750, 400, "The received destination DLC information is not copied to the source object")) Then Exit Function
          If (Not ps.AddOption("optDLCC_1", 200, 800, 3750, 400, "The received destination DLC information is copied to the source object")) Then Exit Function
        Else
          If (Not ps.AddOption("optDLCC_0", 200, 300, 3750, 400, "The received source DLC information is not copied to the destination object")) Then Exit Function
          If (Not ps.AddOption("optDLCC_1", 200, 800, 3750, 400, "The received source DLC information is copied to the destination object")) Then Exit Function
        End If

    '-------------------------------
    ' frame: frmIDC
    '-------------------------------
    If (Not ps.AddFrame("frmIDC", 4300, 2550, 4300, 1300, "Identifier Copy (IDC)")) Then Exit Function
        If ((iNr Mod 2) = 0) Then  ' Destination
          If (Not ps.AddOption("optIDC_0", 200, 300, 3800, 400, "A received destination identifier is not automatically copied to the source object")) Then Exit Function
          If (Not ps.AddOption("optIDC_1", 200, 800, 4075, 400, "The received destination identifier is automatically copied to the source object to replace the identifier")) Then Exit Function
        Else
          If (Not ps.AddOption("optIDC_0", 200, 300, 3800, 400, "A received source identifier is not automatically copied to the destination object")) Then Exit Function
          If (Not ps.AddOption("optIDC_1", 200, 800, 4075, 400, "The received source identifier is automatically copied to the destination object to replace the identifier")) Then Exit Function
        End If

    '-------------------------------
    ' frame: frmGDFS
    '-------------------------------
    If (Not ps.AddFrame("frmGDFS", 100, 4000, 4000, 1300, "Gateway Data Frame Send (GDFS)")) Then Exit Function
        If (Not ps.AddOption("optGDFS_0", 200, 300, 3750, 400, "A data frame received by the source object  is not automatically sent by the destination object")) Then Exit Function
        If (Not ps.AddOption("optGDFS_1", 200, 800, 3750, 400, "A data frame received by the source object  is automatically sent by the destination object")) Then Exit Function

    '-------------------------------
    ' frame: frmSRREN
    '-------------------------------
    If (Not ps.AddFrame("frmSRREN", 4300, 4000, 4300, 1300, "Source Remote Request Enable (SRREN)")) Then Exit Function
        If (Not ps.AddOption("optSRREN_0", 200, 300, 3950, 400, "A matching remote frame on a destination CAN bus is answered by a data frame from destination object")) Then Exit Function
        If (Not ps.AddOption("optSRREN_1", 200, 800, 3950, 400, "A matching remote frame on a destination CAN bus generate a remote frame on the source CAN bus")) Then Exit Function

  End If
  OnChangingPageFIFO_Gateway = True
End Function



'*****************************************************************************
'Description:
'        This function is called from the OnShow function of the associated
'        module. See the description for the OnShow function of the
'        associated module for details.
'-----------------------------------------------------------------------------
'Parameters:
'        ps              reference to the Peripherals object
'        Controlname     control name
'        Pagename        current page name
'        v()             value array
'-----------------------------------------------------------------------------
'Returns:
'        True            on successfull operation
'        False           else
'*****************************************************************************
Public Function OnShowPageFIFO_Gateway(ps As Object, Controlname As String, Pagename As String, v() As Variant) As Boolean
  On Error GoTo OnShowPageFIFO_GatewayError

  OnShowPageFIFO_Gateway = False

  Dim r As ProjectData
  Dim c As Object
  Dim e As Object
  Dim bDisabled As Boolean
  Dim vIndex As Variant
  Dim sVarPre As String
  Dim sRegPre As String
  Dim d As String
  Dim iNr As Integer
  Dim i As Integer

  vIndex = ps.Parameters.v("x")
  iNr = ps.Parameters.v("Number")
  sVarPre = "CAN" & vIndex
  sRegPre = sVarPre & "_"

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

  If (Pagename = "FIFO/Gateway") Then
    '-------------------------------------------------------------------------
    ' page: FIFO_Gateway (action)
    '-------------------------------------------------------------------------

    For i = 1 To 5
      '---------------------------------------------------------------------
      ' control: optFSIZE_x (action)
      '---------------------------------------------------------------------
      If (Controlname = "optFSIZE_" & i) Then
        ChangeFIFOSettings(r, vIndex, iNr, (2^i - 1))
        Exit For
      End If
    Next i

    Select Case (Controlname)
        '---------------------------------------------------------------------
        ' control: optMMC_0 (action)
        '---------------------------------------------------------------------
        Case "optMMC_0"  ' normal mode
              SetStandardMode(r, vIndex, iNr)

        '---------------------------------------------------------------------
        ' control: optMMC_1 (action)
        '---------------------------------------------------------------------
        Case "optMMC_1"  ' Dummy control

        '---------------------------------------------------------------------
        ' control: optMMC_2 (action)
        '---------------------------------------------------------------------
        Case "optMMC_2"  ' FIFO base mode
              SetFIFOBaseMode(r, vIndex, iNr)

        '---------------------------------------------------------------------
        ' control: optMMC_3 (action)
        '---------------------------------------------------------------------
        Case "optMMC_3"  ' FIFO slave mode
              ' will be set by DAvE automatically

        '---------------------------------------------------------------------
        ' control: optMMC_4 (action)
        '---------------------------------------------------------------------
        Case "optMMC_4"  ' normal gateway mode
              SetNormalGatewayMode(r, vIndex, iNr)

        '---------------------------------------------------------------------
        ' control: optMMC_5 (action)
        '---------------------------------------------------------------------
        Case "optMMC_5"  ' shared gateway mode
              SetSharedGatewayMode(r, vIndex, iNr)

        '---------------------------------------------------------------------
        ' control: optFD_0 (action)
        '---------------------------------------------------------------------
        Case "optFD_0"
              r.v(sRegPre & "MSGFGCRL" & iNr & "_FD") = 0
              ChangeFIFOSettings(r, vIndex, iNr, r.v(sRegPre & "MSGFGCRL" & iNr & "_FSIZE"))

        '---------------------------------------------------------------------
        ' control: optFD_1 (action)
        '---------------------------------------------------------------------
        Case "optFD_1"
              r.v(sRegPre & "MSGFGCRL" & iNr & "_FD") = 1
              ChangeFIFOSettings(r, vIndex, iNr, r.v(sRegPre & "MSGFGCRL" & iNr & "_FSIZE"))

        '---------------------------------------------------------------------
        ' control: optDLCC_0 (action)
        '---------------------------------------------------------------------
        Case "optDLCC_0"
              r.v(sRegPre & "MSGFGCRL" & iNr & "_DLCC") = 0

        '---------------------------------------------------------------------
        ' control: optDLCC_1 (action)
        '---------------------------------------------------------------------
        Case "optDLCC_1"
              r.v(sRegPre & "MSGFGCRL" & iNr & "_DLCC") = 1

        '---------------------------------------------------------------------
        ' control: optIDC_0 (action)
        '---------------------------------------------------------------------
        Case "optIDC_0"
              r.v(sRegPre & "MSGFGCRL" & iNr & "_IDC") = 0

        '---------------------------------------------------------------------
        ' control: optIDC_1 (action)
        '---------------------------------------------------------------------
        Case "optIDC_1"
              r.v(sRegPre & "MSGFGCRL" & iNr & "_IDC") = 1

        '---------------------------------------------------------------------
        ' control: optSRREN_0 (action)
        '---------------------------------------------------------------------
        Case "optSRREN_0"
              r.v(sRegPre & "MSGFGCRL" & iNr & "_SRREN") = 0

        '---------------------------------------------------------------------
        ' control: optSRREN_1 (action)
        '---------------------------------------------------------------------
        Case "optSRREN_1"
              r.v(sRegPre & "MSGFGCRL" & iNr & "_SRREN") = 1

        '---------------------------------------------------------------------
        ' control: optGDFS_0 (action)
        '---------------------------------------------------------------------
        Case "optGDFS_0"
              r.v(sRegPre & "MSGFGCRL" & iNr & "_GDFS") = 0

        '---------------------------------------------------------------------
        ' control: optGDFS_1 (action)
        '---------------------------------------------------------------------
        Case "optGDFS_1"
              r.v(sRegPre & "MSGFGCRL" & iNr & "_GDFS") = 1

      Case Else
    End Select

    '-------------------------------------------------------------------------
    ' page: FIFO_Gateway (show)
    '-------------------------------------------------------------------------


    '-------------------------------------------------------------------------
    ' frame: frmMMC and frmFSIZE (show)
    '-------------------------------------------------------------------------
    ' if a FIFO slave object is automatically configured by DAvE then this disable
    ' condition and tooltips are used
    If (IsObjFIFOSlave(r, vIndex, iNr) >= 0) Then
      bDisabled = True
      ' tooltip if control is disabled
      d = TOOLTIP_DISABLED_IF
      d = d & FormatDisabledTooltip("this object is a FIFO slave object, connected to the base object " & IsObjFIFOSlave(r, vIndex, iNr))
      Call SetDefaultPropertiesForAllControlsOnFrame(c("frmMMC").Caption, c, e, bDisabled, d, "")
      Call SetDefaultPropertiesForAllControlsOnFrame(c("frmFSIZE").Caption, c, e, bDisabled, d, "")
      Call SetDefaultPropertiesForAllControlsOnFrame(c("frmFD").Caption, c, e, bDisabled, d, "")

      For i = 0 To 5
        '-------------------------------------------------------------------------
        ' control: optMMC_x (show)
        '-------------------------------------------------------------------------
        With c("optMMC_" & i)
          If (i = 3) Then  ' for FIFO mode
            .Enabled = True
          End If
          ' show condition
          .Value = (r.v(sRegPre & "MSGFGCRH" & iNr & "_MMC") = i)
        End With
      Next i

      For i = 1 To 5
        '-------------------------------------------------------------------------
        ' control: optFSIZE_x (show)
        '-------------------------------------------------------------------------
        With c("optFSIZE_" & i)
          If ((2 ^ i - 1) = r.v(sRegPre & "MSGFGCRL" & iNr & "_FSIZE")) Then ' for the selected stage
            .Enabled = True
          End If
          .Value = (r.v(sRegPre & "MSGFGCRL" & iNr & "_FSIZE") = (2 ^ i - 1))
        End With
      Next i

      For i = 0 To 1
        '-------------------------------------------------------------------------
        ' control: optFD_x (show)
        '-------------------------------------------------------------------------
        With c("optFD_" & i)
          If (i = r.v(sRegPre & "MSGFGCRL" & iNr & "_FD")) Then  ' for the selected direction
            .Enabled = True
          End If
          .Value = (r.v(sRegPre & "MSGFGCRL" & iNr & "_FD") = i)
        End With
      Next i

    Else      ' the object is not configured as FIFO slave

      '-------------------------------------------------------------------------
      ' control: optMMC_0 (show)
      '-------------------------------------------------------------------------
      With c("optMMC_0")
        ' disable condition
        bDisabled = r.v(sRegPre & "MSGCTRL" & iNr & "_MSGVAL") <> 2
        .Enabled = Not (bDisabled)
        ' show condition
        .Value = (r.v(sRegPre & "MSGFGCRH" & iNr & "_MMC") = 0)
      End With

      With e("optMMC_0")
        If (bDisabled) Then
          ' tooltip if control is disabled
          .DisabledTooltipText = TOOLTIP_DISABLED_IF
           If (r.v(sRegPre & "MSGCTRL" & iNr & "_MSGVAL") <> 2) Then
             .DisabledTooltipText = .DisabledTooltipText & FormatDisabledTooltip(TOOLTIP_DISABLED_OBJECT)
           End If
        Else
          ' tooltip if control is enabled
        End If
      End With

      '-------------------------------------------------------------------------
      ' control: optMMC_1 (show)
      '-------------------------------------------------------------------------
      ' dummy control

      '-------------------------------------------------------------------------
      ' control: optMMC_2 (show)  FIFO base mode
      '-------------------------------------------------------------------------
      With c("optMMC_2")
        ' disable condition
        bDisabled = r.v(sRegPre & "MSGCTRL" & iNr & "_MSGVAL") <> 2 Or (iNr Mod 2) = 1
        .Enabled = Not (bDisabled)
        ' show condition
        .Value = (r.v(sRegPre & "MSGFGCRH" & iNr & "_MMC") = 2)
      End With

      With e("optMMC_2")
        If (bDisabled) Then
          ' tooltip if control is disabled
          .DisabledTooltipText = TOOLTIP_DISABLED_IF
           If (r.v(sRegPre & "MSGCTRL" & iNr & "_MSGVAL") <> 2) Then
             .DisabledTooltipText = .DisabledTooltipText & FormatDisabledTooltip(TOOLTIP_DISABLED_OBJECT)
           End If
           If ((iNr Mod 2) = 1) Then
             .DisabledTooltipText = .DisabledTooltipText & FormatDisabledTooltip("only even numbered message objects can be configured for FIFO base objects")
           End If
        Else
          ' tooltip if control is enabled
        End If
      End With

      '-------------------------------------------------------------------------
      ' control: optMMC_3 (show)  FIFO slave mode
      '-------------------------------------------------------------------------
      With c("optMMC_3")
        ' disable condition
        bDisabled = True
        .Enabled = Not (bDisabled)
        ' show condition
        .Value = (r.v(sRegPre & "MSGFGCRH" & iNr & "_MMC") = 3)
      End With

      With e("optMMC_3")
        If (bDisabled) Then
          ' tooltip if control is disabled
          .DisabledTooltipText = FormatNormalTooltip("This setting will be configured by DAvE automatically")
        Else
          ' tooltip if control is enabled
        End If
      End With

      '-------------------------------------------------------------------------
      ' control: optMMC_4 (show)       normal gateway mode
      '-------------------------------------------------------------------------
      With c("optMMC_4")
        ' disable condition                                           ' even number
        If (iNr = 0 Or iNr = 31) Then ' object can not be gateway destination/source
          bDisabled = True
        Else
          If ((iNr Mod 2) = 0) Then   ' object can be gateway destination
            bDisabled = (IsObjGatewayDestination(r, vIndex, iNr) = -1) Or r.v(sRegPre & "MSGCTRL" & iNr & "_MSGVAL") <> 2
          Else                        ' object can be gateway source
            bDisabled = r.v(sRegPre & "MSGCTRL" & iNr & "_MSGVAL") <> 2 Or r.v(sRegPre & "MSGCFGL" & iNr & "_DIR") <> 0 Or Not IfNodesConfiguredForGatewayMode(r, vIndex)
          End If
        End If
        .Enabled = Not (bDisabled)
        ' show condition
        .Value = (r.v(sRegPre & "MSGFGCRH" & iNr & "_MMC") = 4)
      End With

      With e("optMMC_4")
        If (bDisabled) Then
          ' tooltip if control is disabled
          .DisabledTooltipText = TOOLTIP_DISABLED_IF
          If (iNr = 0 Or iNr = 31) Then  ' object can not be gateway destination/source
            .DisabledTooltipText = .DisabledTooltipText & FormatDisabledTooltip("this object can not be configured to normal gateway mode")
          Else
            If ((iNr Mod 2) = 0) Then    ' object can be gateway destination
              If (IsObjGatewayDestination(r, vIndex, iNr) = -1) Then  ' if obj Nr-1 not the gateway source
                 .DisabledTooltipText = .DisabledTooltipText & FormatDisabledTooltip("the object " & iNr - 1 & " is not configured as gateway source, so this object can not configured as gateway destination")
              End If
              If (r.v(sRegPre & "MSGCTRL" & iNr & "_MSGVAL") <> 2) Then
                .DisabledTooltipText = .DisabledTooltipText & FormatDisabledTooltip(TOOLTIP_DISABLED_OBJECT)
              End If
            Else                         ' object can be gateway source
              .DisabledTooltipText = .DisabledTooltipText & DisabledTooltipIfNodesNotConfiguredForGatewayMode(r, vIndex)
              If (r.v(sRegPre & "MSGCTRL" & iNr & "_MSGVAL") <> 2) Then
                .DisabledTooltipText = .DisabledTooltipText & FormatDisabledTooltip(TOOLTIP_DISABLED_OBJECT)
              End If
              If (r.v(sRegPre & "MSGCFGL" & iNr & "_DIR") <> 0) Then
                .DisabledTooltipText = .DisabledTooltipText & FormatDisabledTooltip("this object is not configured as receive object")
              End If
            End If
          End If
        Else
          ' tooltip if control is enabled
        End If
      End With

      '-------------------------------------------------------------------------
      ' control: optMMC_5 (show)  shared gateway mode
      '-------------------------------------------------------------------------
      With c("optMMC_5")
        ' disable condition
        bDisabled = r.v(sRegPre & "MSGCTRL" & iNr & "_MSGVAL") <> 2 Or (IsObjGatewayDestination(r, vIndex, iNr) >= 0) Or Not IfNodesConfiguredForGatewayMode(r, vIndex)
        .Enabled = Not (bDisabled)
        ' show condition
        .Value = (r.v(sRegPre & "MSGFGCRH" & iNr & "_MMC") = 5)
        If (r.v(sRegPre & "MSGCFGL" & iNr & "_DIR") = 1) Then
          .Caption = "Shared gateway mode; this object is initialized as transmit object on the destination side"
        Else
          .Caption = "Shared gateway mode; this object is initialized as receive object on the source side"
        End If
      End With

      With e("optMMC_5")
        If (bDisabled) Then
          ' tooltip if control is disabled
          .DisabledTooltipText = TOOLTIP_DISABLED_IF
          .DisabledTooltipText = .DisabledTooltipText & DisabledTooltipIfNodesNotConfiguredForGatewayMode(r, vIndex)
          If (r.v(sRegPre & "MSGCTRL" & iNr & "_MSGVAL") <> 2) Then
            .DisabledTooltipText = .DisabledTooltipText & FormatDisabledTooltip(TOOLTIP_DISABLED_OBJECT)
          End If
          If (IsObjGatewayDestination(r, vIndex, iNr) >= 0) Then
            .DisabledTooltipText = .DisabledTooltipText & FormatDisabledTooltip("this object is a gateway destination object, connected to the source object " & IsObjGatewayDestination(r, vIndex, iNr))
          End If
        Else
          ' tooltip if control is enabled
        End If
      End With

      For i = 1 To 5
        '-------------------------------------------------------------------------
        ' control: optFSIZE_x (show)
        '-------------------------------------------------------------------------
        With c("optFSIZE_" & i)
          ' disable condition
          bDisabled = r.v(sRegPre & "MSGCTRL" & iNr & "_MSGVAL") <> 2 Or (iNr Mod 2 ^ i) <> 0 Or r.v(sRegPre & "MSGFGCRH" & iNr & "_MMC") <> 2
          .Enabled = Not (bDisabled)
          ' show condition
          .Value = (r.v(sRegPre & "MSGFGCRL" & iNr & "_FSIZE") = (2 ^ i - 1))
        End With

        With e("optFSIZE_" & i)
          If (bDisabled) Then
            ' tooltip if control is disabled
            .DisabledTooltipText = TOOLTIP_DISABLED_IF
             If ((iNr Mod 2 ^ i) <> 0) Then
               .DisabledTooltipText = .DisabledTooltipText & FormatDisabledTooltip("this FIFO stage is not provided by this object")
             Else
               If (r.v(sRegPre & "MSGCTRL" & iNr & "_MSGVAL") <> 2) Then
                 .DisabledTooltipText = .DisabledTooltipText & FormatDisabledTooltip(TOOLTIP_DISABLED_OBJECT)
               End If
               If (r.v(sRegPre & "MSGFGCRH" & iNr & "_MMC") <> 2) Then
                 .DisabledTooltipText = .DisabledTooltipText & FormatDisabledTooltip("the FIFO mode is not selected")
               End If
             End If
          Else
            ' tooltip if control is enabled
          End If
        End With
      Next i

      For i = 0 To 1
        '-------------------------------------------------------------------------
        ' control: optFD_x (show)
        '-------------------------------------------------------------------------
        With c("optFD_" & i)
          ' disable condition
          bDisabled = r.v(sRegPre & "MSGCTRL" & iNr & "_MSGVAL") <> 2 Or r.v(sRegPre & "MSGFGCRH" & iNr & "_MMC") <> 2
          .Enabled = Not (bDisabled)
          ' show condition
          .Value = (r.v(sRegPre & "MSGFGCRL" & iNr & "_FD") = i)
        End With

        With e("optFD_" & i)
          If (bDisabled) Then
            ' tooltip if control is disabled
            .DisabledTooltipText = TOOLTIP_DISABLED_IF
             If (r.v(sRegPre & "MSGCTRL" & iNr & "_MSGVAL") <> 2) Then
               .DisabledTooltipText = .DisabledTooltipText & FormatDisabledTooltip(TOOLTIP_DISABLED_OBJECT)
             End If
             If (r.v(sRegPre & "MSGFGCRH" & iNr & "_MMC") <> 2) Then
               .DisabledTooltipText = .DisabledTooltipText & FormatDisabledTooltip("the FIFO mode is not selected")
             End If
          Else
            ' tooltip if control is enabled
          End If
        End With
      Next i

    End If

      '-------------------------------------------------------------------------
      ' frame: frmDLCC, frmIDC (show)
      '-------------------------------------------------------------------------
'      bDisabled = r.v(sRegPre & "MSGCTRL" & iNr & "_MSGVAL") <> 2 Or r.v(sRegPre & "MSGFGCRH" & iNr & "_MMC") <> 4 Or (iNr Mod 2) = 0
      bDisabled = r.v(sRegPre & "MSGCTRL" & iNr & "_MSGVAL") <> 2 Or r.v(sRegPre & "MSGFGCRH" & iNr & "_MMC") <> 4
      ' tooltip if control is disabled
       d = TOOLTIP_DISABLED_IF
'       If ((iNr Mod 2) = 0 Or iNr = 31) Then
'         d = d & FormatDisabledTooltip("this setting is not provided by this object")
'       Else
         If (r.v(sRegPre & "MSGCTRL" & iNr & "_MSGVAL") <> 2) Then
           d = d & FormatDisabledTooltip(TOOLTIP_DISABLED_OBJECT)
         End If
         If (r.v(sRegPre & "MSGFGCRH" & iNr & "_MMC") <> 4) Then
           d = d & FormatDisabledTooltip("this object is not configured for normal gateway mode")
         End If
'       End If
      Call SetDefaultPropertiesForAllControlsOnFrame(c("frmDLCC").Caption, c, e, bDisabled, d, "")
      Call SetDefaultPropertiesForAllControlsOnFrame(c("frmIDC").Caption, c, e, bDisabled, d, "")

      For i = 0 To 1
        '-------------------------------------------------------------------------
        ' control: optDLCC_x (show)
        '-------------------------------------------------------------------------
        With c("optDLCC_" & i)
          ' show condition
          .Value = (r.v(sRegPre & "MSGFGCRL" & iNr & "_DLCC") = i)
        End With
      Next i

      For i = 0 To 1
        '-------------------------------------------------------------------------
        ' control: optIDC_x (show)
        '-------------------------------------------------------------------------
        With c("optIDC_" & i)
          ' show condition
          .Value = (r.v(sRegPre & "MSGFGCRL" & iNr & "_IDC") = i)
        End With
      Next i

      For i = 0 To 1
        '-------------------------------------------------------------------------
        ' control: optSRREN_x (show)
        '-------------------------------------------------------------------------
        With c("optSRREN_" & i)
          ' disable condition
          ' only for shared gateway mode and destination objects in normal gateway mode
          bDisabled = ((IsObjGatewayDestination(r, vIndex, iNr) = -1) And r.v(sRegPre & "MSGFGCRH" & iNr & "_MMC") = 4) Or r.v(sRegPre & "MSGFGCRH" & iNr & "_MMC") < 4
          .Enabled = Not (bDisabled)
          ' show condition
          .Value = (r.v(sRegPre & "MSGFGCRL" & iNr & "_SRREN") = i)
        End With

        With e("optSRREN_" & i)
          If (bDisabled) Then
            ' tooltip if control is disabled
            .DisabledTooltipText = TOOLTIP_DISABLED_IF
            If ((IsObjGatewayDestination(r, vIndex, iNr) = -1) And r.v(sRegPre & "MSGFGCRH" & iNr & "_MMC") = 4) Then
              .DisabledTooltipText = .DisabledTooltipText & FormatDisabledTooltip("this object is not configured for shared gateway mode")
            End If
            If (iNr = 0 Or iNr = 31 Or (iNr Mod 2) = 1) Then  ' object can not be gateway destination
              If (r.v(sRegPre & "MSGFGCRH" & iNr & "_MMC") < 4) Then
                .DisabledTooltipText = .DisabledTooltipText & FormatDisabledTooltip("this object is not configured for shared gateway mode")
              End If
            Else
              If (r.v(sRegPre & "MSGFGCRH" & iNr & "_MMC") < 4) Then
                .DisabledTooltipText = .DisabledTooltipText & FormatDisabledTooltip("this object is not configured for gateway mode")
              End If
            End If
          Else
            ' tooltip if control is enabled
          End If
        End With

      Next i

      '-------------------------------------------------------------------------
      ' frame: frmGDFS (show) for shared Gateway and Source im normal Gateway mode
      '-------------------------------------------------------------------------
       d = TOOLTIP_DISABLED_IF
       bDisabled = False
       If ((IsObjGatewayDestination(r, vIndex, iNr) <> -1)) Then  ' obj can be gateway destination
         If (r.v(sRegPre & "MSGCTRL" & iNr & "_MSGVAL") <> 2) Then
           d = d & FormatDisabledTooltip(TOOLTIP_DISABLED_OBJECT)
           bDisabled = True
         End If
         If (r.v(sRegPre & "MSGFGCRH" & iNr & "_MMC") <> 5) Then
           d = d & FormatDisabledTooltip("this object is not configured for shared gateway mode")
           bDisabled = True
         End If
       Else
         If (r.v(sRegPre & "MSGCTRL" & iNr & "_MSGVAL") <> 2) Then
           d = d & FormatDisabledTooltip(TOOLTIP_DISABLED_OBJECT)
           bDisabled = True
         End If
         If (r.v(sRegPre & "MSGFGCRH" & iNr & "_MMC") < 4) Then
           d = d & FormatDisabledTooltip("this object is not configured for gateway mode")
           bDisabled = True
         End If
       End If
      Call SetDefaultPropertiesForAllControlsOnFrame(c("frmGDFS").Caption, c, e, bDisabled, d, "")

      For i = 0 To 1
        '-------------------------------------------------------------------------
        ' control: optGDFS_x (show)
        '-------------------------------------------------------------------------
        With c("optGDFS_" & i)
          ' show condition
          .Value = (r.v(sRegPre & "MSGFGCRL" & iNr & "_GDFS") = i)
        End With
      Next i

  End If

  OnShowPageFIFO_Gateway = True
  Exit Function

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


'*****************************************************************************
'Description:
'        This function is called from the OnLoad function of the associated
'        module in order to add the page 'Interrupt'.
'-----------------------------------------------------------------------------
'Parameters:
'        ps               reference to the Peripherals object
'-----------------------------------------------------------------------------
'Returns:
'        True            on successfull operation
'        False           else
'*****************************************************************************
Public Function OnLoadPageInterrupt(ps As Object) As Boolean
  On Error Resume Next

  OnLoadPageInterrupt = False

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

  OnLoadPageInterrupt = True
End Function


'*****************************************************************************
'Description:
'        This function is called from the OnChangingPage function of the
'        associated module in order load all controls for the requested page
'        if they are not already loaded.
'-----------------------------------------------------------------------------
'Parameters:
'        ps               reference to the Peripherals object
'        Pagename         requested page name
'-----------------------------------------------------------------------------
'Returns:
'        True            on successfull operation
'        False           else
'*****************************************************************************
Function OnChangingPageInterrupt(ps As Object, Pagename As String) As Boolean
  On Error Resume Next
  Dim r As ProjectData
  Dim iNr As Integer
  Dim vIndex As Variant
  Dim sVarPre As String

  OnChangingPageInterrupt = False

  vIndex = ps.Parameters.v("x")
  sVarPre = "CAN" & vIndex
  iNr = ps.Parameters.v("Number")

  Set r = ps.ProjectData

  
  If (Not ps.ExtenderCollection("Interrupt").Loaded) And (Pagename = "Interrupt") Then
    ps.ExtenderCollection("Interrupt").Loaded = True
  
    '-------------------------------
    ' frame: frmInterrupt
    '-------------------------------
    If (Not ps.AddFrame("frmInterrupt", 100, 100, 8400, 1400, "Interrupt Control")) Then Exit Function
        If (Not ps.AddCheck("chkRXIE", 200, 350, 6900, 380, "Enable receive interrupt; bit INTPND is set after successfull reception of a frame (RXIE)")) Then Exit Function
        If (Not ps.AddCheck("chkTXIE", 200, 800, 7100, 380, "Enable transmit interrupt; bit INTPND is set after successfull transmission of a frame (TXIE)")) Then Exit Function

    '-------------------------------
    ' frame: frmINP
    '-------------------------------
    If (Not ps.AddFrame("frmINP", 100, 1650, 8400, 1600, "Interrupt Node Pointer")) Then Exit Function
        If (Not ps.AddCombo("cboRXINP", 3600, 400, 2100, 315)) Then Exit Function
        If (Not ps.AddLabel("lblRXINP", 0, 200, 450, 3100, 400, "Receive interrupt node pointer (RXINP)")) Then Exit Function
        If (Not ps.AddCombo("cboTXINP", 3600, 1000, 2100, 315)) Then Exit Function
        If (Not ps.AddLabel("lblTXINP", 0, 200, 1050, 3100, 400, "Transmit interrupt node pointer (TXINP)")) Then Exit Function

  End If
  OnChangingPageInterrupt = True
End Function



'*****************************************************************************
'Description:
'        This function is called from the OnShow function of the associated
'        module. See the description for the OnShow function of the
'        associated module for details.
'-----------------------------------------------------------------------------
'Parameters:
'        ps              reference to the Peripherals object
'        Controlname     control name
'        Pagename        current page name
'        v()             value array
'-----------------------------------------------------------------------------
'Returns:
'        True            on successfull operation
'        False           else
'*****************************************************************************
Public Function OnShowPageInterrupt(ps As Object, Controlname As String, Pagename As String, v() As Variant) As Boolean
  On Error GoTo OnShowPageInterruptError

  OnShowPageInterrupt = False

  Dim r As ProjectData
  Dim c As Object
  Dim e As Object
  Dim bDisabled As Boolean
  Dim vIndex As Variant
  Dim sVarPre As String
  Dim sRegPre As String
  Dim d As String
  Dim iNr As Integer
  Dim i As Integer

  vIndex = ps.Parameters.v("x")
  iNr = ps.Parameters.v("Number")
  sVarPre = "CAN" & vIndex
  sRegPre = sVarPre & "_"

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

  If (Pagename = "Interrupt") Then
    '-------------------------------------------------------------------------
    ' page: Interrupt (action)
    '-------------------------------------------------------------------------
    Select Case (Controlname)
        '---------------------------------------------------------------------
        ' control: chkTXIE (action)
        '---------------------------------------------------------------------
        Case "chkTXIE"
                r.v(sRegPre & "MSGCTRL" & iNr & "_TXIE") = 1 + v(1)

        '---------------------------------------------------------------------
        ' control: chkRXIE (action)
        '---------------------------------------------------------------------
        Case "chkRXIE"
                r.v(sRegPre & "MSGCTRL" & iNr & "_RXIE") = 1 + v(1)

        '---------------------------------------------------------------------
        ' control: cboTXINP (action)
        '---------------------------------------------------------------------
        Case "cboTXINP"
            For i = 0 To 7
              If ("Use TwinCAN SRN " & i = v(1)) Then
                r.v(sRegPre & "MSGCFGH" & iNr & "_TXINP") = i
                Exit For
              End If
            Next i

        '---------------------------------------------------------------------
        ' control: cboRXINP (action)
        '---------------------------------------------------------------------
        Case "cboRXINP"
            For i = 0 To 7
              If ("Use TwinCAN SRN " & i = v(1)) Then
                r.v(sRegPre & "MSGCFGH" & iNr & "_RXINP") = i
                Exit For
              End If
            Next i

      Case Else
    End Select

    '-------------------------------------------------------------------------
    ' page: Interrupt (show)
    '-------------------------------------------------------------------------
    '-------------------------------------------------------------------------
    ' frame: frmInterrupt (show)
    '-------------------------------------------------------------------------
    bDisabled = r.v(sRegPre & "MSGCTRL" & iNr & "_MSGVAL") <> 2
    ' tooltip if control is disabled
    d = TOOLTIP_DISABLED_IF
    If (r.v(sRegPre & "MSGCTRL" & iNr & "_MSGVAL") <> 2) Then
      d = d & FormatDisabledTooltip(TOOLTIP_DISABLED_OBJECT)
    End If
    Call SetDefaultPropertiesForAllControlsOnFrame(c("frmInterrupt").Caption, c, e, bDisabled, d, "")

    '-------------------------------------------------------------------------
    ' control: chkTXIE (show)
    '-------------------------------------------------------------------------
    With c("chkTXIE")
      .Value = -CInt(r.v(sRegPre & "MSGCTRL" & iNr & "_TXIE") = 2)
    End With

    '-------------------------------------------------------------------------
    ' control: chkRXIE (show)
    '-------------------------------------------------------------------------
    With c("chkRXIE")
      .Value = -CInt(r.v(sRegPre & "MSGCTRL" & iNr & "_RXIE") = 2)
    End With

    '-------------------------------------------------------------------------
    ' control: cboTXINP (show)
    '-------------------------------------------------------------------------
    With c("cboTXINP")
      ' disable condition
      bDisabled = r.v(sRegPre & "MSGCTRL" & iNr & "_MSGVAL") <> 2 Or r.v(sRegPre & "MSGCTRL" & iNr & "_TXIE") <> 2
      .Enabled = Not (bDisabled)
      ' show condition
      .Clear
      For i = 0 To 7
        .AddItem "Use TwinCAN SRN " & i
      Next i
      .ListIndex = r.v(sRegPre & "MSGCFGH" & iNr & "_TXINP")
    End With

    With e("cboTXINP")
      If (bDisabled) Then
        ' tooltip if control is disabled
        .DisabledTooltipText = TOOLTIP_DISABLED_IF
        If (r.v(sRegPre & "MSGCTRL" & iNr & "_MSGVAL") <> 2) Then
          .DisabledTooltipText = .DisabledTooltipText & FormatDisabledTooltip(TOOLTIP_DISABLED_OBJECT)
        End If
        If (r.v(sRegPre & "MSGCTRL" & iNr & "_TXIE") <> 2) Then
          .DisabledTooltipText = .DisabledTooltipText & FormatDisabledTooltip("the transmit interrupt is not enabled")
        End If
      Else
        ' tooltip if control is enabled
      End If
    End With

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

    With e("lblTXINP")
      If (bDisabled) Then
        .DisabledTooltipText = e("cboTXINP").DisabledTooltipText
      Else
        ' tooltip if control is enabled
      End If
    End With

    '-------------------------------------------------------------------------
    ' control: cboRXINP (show)
    '-------------------------------------------------------------------------
    With c("cboRXINP")
      ' disable condition
      bDisabled = r.v(sRegPre & "MSGCTRL" & iNr & "_MSGVAL") <> 2 Or r.v(sRegPre & "MSGCTRL" & iNr & "_RXIE") <> 2
      .Enabled = Not (bDisabled)
      ' show condition
      .Clear
      For i = 0 To 7
        .AddItem "Use TwinCAN SRN " & i
      Next i
      .ListIndex = r.v(sRegPre & "MSGCFGH" & iNr & "_RXINP")
    End With

    With e("cboRXINP")
      If (bDisabled) Then
        ' tooltip if control is disabled
        .DisabledTooltipText = TOOLTIP_DISABLED_IF
        If (r.v(sRegPre & "MSGCTRL" & iNr & "_MSGVAL") <> 2) Then
          .DisabledTooltipText = .DisabledTooltipText & FormatDisabledTooltip(TOOLTIP_DISABLED_OBJECT)
        End If
        If (r.v(sRegPre & "MSGCTRL" & iNr & "_RXIE") <> 2) Then
          .DisabledTooltipText = .DisabledTooltipText & FormatDisabledTooltip("the receive interrupt is not enabled")
        End If
      Else
        ' tooltip if control is enabled
      End If
    End With

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

    With e("lblRXINP")
      If (bDisabled) Then
        .DisabledTooltipText = e("cboRXINP").DisabledTooltipText
      Else
        ' tooltip if control is enabled
      End If
    End With

  End If

  OnShowPageInterrupt = True
  Exit Function

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


'*****************************************************************************
'Description:
'        This function changes the settings of the gateway destination objects when
'        they are changed in the source object.
'-----------------------------------------------------------------------------
'Parameters:
'        r               reference to the ProjectData object
'        vIndex          parameter
'        iNr             number of the object
'-----------------------------------------------------------------------------
'Returns:
'*****************************************************************************
Sub ChangeGatewayDestinationSettings(r As ProjectData, vIndex As Variant, iNr As Integer)
  On Error Resume Next
  Dim s As String
  Dim sVarPre As String
  Dim sRegPre As String
  Dim i As Integer
  Dim iBaseNr As Integer
  Dim iFirstNr As Integer
  Dim iLastNr As Integer

  sVarPre = "CAN" & vIndex
  sRegPre = sVarPre & "_"

  If (r.v(sRegPre & "MSGFGCRH" & iNr & "_MMC") = 4) Then  ' only if the normal gateway mode is selected
    ' the destination object is connected to the consecutive node
    r.v(sRegPre & "MSGCFGL" & iNr + 1 & "_NODE") = 1 - r.v(sRegPre & "MSGCFGL" & iNr & "_NODE")
    ' the gateway destination object must be configured as transmit object
    r.v(sRegPre & "MSGCFGL" & iNr + 1 & "_XTD") = r.v(sRegPre & "MSGCFGL" & iNr & "_XTD")

    ' check if the gateway destination is configured for FIFO mode, if so then
    ' all FIFO slaves must be changed
    ChangeFIFOSettings(r, vIndex, iNr + 1, r.v(sRegPre & "MSGFGCRL" & iNr & "_FSIZE"))
  End If

End Sub


'*****************************************************************************
'Description:
'        This function checks if both CAN nodes are enabled and not in
'        analyser mode.
'-----------------------------------------------------------------------------
'Parameters:
'        r               reference to the ProjectData object
'        vIndex          parameter
'-----------------------------------------------------------------------------
'Returns: True if the nodes are configured for gateway mode, else False
'*****************************************************************************
Function IfNodesConfiguredForGatewayMode(r As ProjectData, vIndex As Variant) As Boolean
  On Error Resume Next
  Dim sVarPre As String
  Dim sRegPre As String

  IfNodesConfiguredForGatewayMode = False

  sVarPre = "CAN" & vIndex
  sRegPre = sVarPre & "_"

  If ((r.v(sRegPre & "ACR_INIT") = 0 And r.v(sRegPre & "ACR_CALM") = 0) And (r.v(sRegPre & "BCR_INIT") = 0 And r.v(sRegPre & "BCR_CALM") = 0)) Then
    IfNodesConfiguredForGatewayMode = True
  End If

End Function


'*****************************************************************************
'Description:
'        This function returns the disabled tooltip if both CAN nodes are not
'        enabled and they are in analyser mode.
'-----------------------------------------------------------------------------
'Parameters:
'        r               reference to the ProjectData object
'        vIndex          parameter
'-----------------------------------------------------------------------------
'Returns: formatted string
'*****************************************************************************
Function DisabledTooltipIfNodesNotConfiguredForGatewayMode(r As ProjectData, vIndex As Variant) As String
  On Error Resume Next
  Dim sVarPre As String
  Dim sRegPre As String
  Dim v As String

  sVarPre = "CAN" & vIndex
  sRegPre = sVarPre & "_"

  v = ""
  If (r.v(sRegPre & "ACR_INIT") = 1) Then
    v = v & FormatDisabledTooltip("the " & sVarPre & " node A is disabled")
  ElseIf (r.v(sRegPre & "ACR_CALM") = 1) Then
    v = v & FormatDisabledTooltip("the " & sVarPre & " node A is in analyzer mode")
  End If

  If (r.v(sRegPre & "BCR_INIT") = 1) Then
    v = v & FormatDisabledTooltip("the " & sVarPre & " node B is disabled")
  ElseIf (r.v(sRegPre & "BCR_CALM") = 1) Then
    v = v & FormatDisabledTooltip("the " & sVarPre & " node B is in analyzer mode")
  End If

  DisabledTooltipIfNodesNotConfiguredForGatewayMode = v

End Function


'*****************************************************************************
'Description:
'        This function sets the standard mode for the selected object.
'-----------------------------------------------------------------------------
'Parameters:
'        r               reference to the ProjectData object
'        vIndex          parameter
'        iNr             number of the gateway object
'-----------------------------------------------------------------------------
'Returns:  true if all settings are made, else false
'
'*****************************************************************************
Function SetStandardMode(r As ProjectData, vIndex As Variant, iNr As Integer) As Boolean
  On Error Resume Next
  Dim s As String
  Dim sVarPre As String
  Dim sRegPre As String
  Dim bReturn As Boolean

  SetStandardMode = False

  sVarPre = "CAN" & vIndex
  sRegPre = sVarPre & "_"

  bReturn = True
  If (bReturn) Then
    bReturn = ResetFIFOBaseMode(r, vIndex, iNr)
  End If
  If (bReturn) Then
    bReturn = ResetNormalGatewayMode(r, vIndex, iNr)
  End If
  If (bReturn) Then
    bReturn = ResetSharedGatewayMode(r, vIndex, iNr)
  End If

  If (bReturn) Then
    '---------------------------------
    ' settings for the standard mode
    '---------------------------------
    r.v(sRegPre & "MSGFGCRH" & iNr & "_MMC") = 0
    r.v(sRegPre & "MSGFGCRH" & iNr & "_CANPTR") = iNr
    SetStandardMode = True
  End If

End Function


'*****************************************************************************
'Description:
'        This function makes all settings for the FIFO base mode.
'-----------------------------------------------------------------------------
'Parameters:
'        r               reference to the ProjectData object
'        vIndex          parameter
'        iNr             number of the FIFO base object
'-----------------------------------------------------------------------------
'Returns:  true if all settings are made, else false
'
'*****************************************************************************
Function SetFIFOBaseMode(r As ProjectData, vIndex As Variant, iNr As Integer) As Boolean
  On Error Resume Next
  Dim sVarPre As String
  Dim sRegPre As String
  Dim iOldMMC As Integer
  Dim bReturn As Boolean

  SetFIFOBaseMode = False

  sVarPre = "CAN" & vIndex
  sRegPre = sVarPre & "_"

  bReturn = True
  If (bReturn) Then
    bReturn = ResetNormalGatewayMode(r, vIndex, iNr)
  End If
  If (bReturn) Then
    bReturn = ResetSharedGatewayMode(r, vIndex, iNr)
  End If

  If (bReturn) Then
    ' save the current value of MMC for the case that the function
    ' ChangeFIFOSettings returns a False
    iOldMMC = r.v(sRegPre & "MSGFGCRH" & iNr & "_MMC")
    r.v(sRegPre & "MSGFGCRH" & iNr & "_MMC") = 2
    ' set FSIZE to default 1 (2-stage FIFO)
    bReturn = ChangeFIFOSettings(r, vIndex, iNr, 1)

    If (bReturn) Then
      ' settings for the FIFO base object
      r.v(sRegPre & "MSGFGCRH" & iNr & "_CANPTR") = iNr
      SetFIFOBaseMode = True
    Else
      ' restore old value of MMC
      r.v(sRegPre & "MSGFGCRH" & iNr & "_MMC") = iOldMMC
    End If
  End If

End Function


'*****************************************************************************
'Description:
'        This function resets all slave objects for a FIFO which are no
'        longer part of the FIFO.
'-----------------------------------------------------------------------------
'Parameters:
'        r               reference to the ProjectData object
'        vIndex          parameter
'        iNr             number of the FIFO base object
'-----------------------------------------------------------------------------
'Returns:  true if all settings are made or no FIFO mode selected, else
'          false
'*****************************************************************************
Function ResetFIFOBaseMode(r As ProjectData, vIndex As Variant, iNr As Integer) As Boolean
  On Error Resume Next
  Dim sVarPre As String
  Dim sRegPre As String
  Dim bReturn As Boolean

  ResetFIFOBaseMode = True

  sVarPre = "CAN" & vIndex
  sRegPre = sVarPre & "_"

  If (r.v(sRegPre & "MSGFGCRH" & iNr & "_MMC") = 2) Then  ' only if the FIFO mode is selected
    ' set FSIZE to 0 (no FIFO)
    bReturn = ChangeFIFOSettings(r, vIndex, iNr, 0)

    If (bReturn) Then
      ' default settings for the FIFO base object
      r.v(sRegPre & "MSGFGCRL" & iNr & "_FD") = 0
    Else
      ResetFIFOBaseMode = False
    End If
  End If
End Function


'*****************************************************************************
'Description:
'        This function sets the normal gateway mode for the selected object.
'        The dissicion if the object can be used as gateway source or
'        destination is made in this function.
'-----------------------------------------------------------------------------
'Parameters:
'        r               reference to the ProjectData object
'        vIndex          parameter
'        iNr             number of the gateway object
'-----------------------------------------------------------------------------
'Returns:  true if all settings are made, else false
'
'*****************************************************************************
Function SetNormalGatewayMode(r As ProjectData, vIndex As Variant, iNr As Integer) As Boolean
  On Error Resume Next
  Dim s As String
  Dim sVarPre As String
  Dim sRegPre As String
  Dim bReturn As Boolean

  SetNormalGatewayMode = False

  sVarPre = "CAN" & vIndex
  sRegPre = sVarPre & "_"

  bReturn = True
  If (bReturn) Then
    bReturn = ResetFIFOBaseMode(r, vIndex, iNr)
  End If
  If (bReturn) Then
    bReturn = ResetSharedGatewayMode(r, vIndex, iNr)
  End If

  If (bReturn) Then
    s = "Message object " & iNr + 1 & " must be part of the gateway. This object will " & vbCrLf & _
        "be configured as gateway destination object." & vbCrLf & _
        "Do you really want to continue ?"

    If (iNr = 0 Or iNr = 31) Then   ' object can not be gateway destination/source
      ' is alway disabled
    Else
      If ((iNr Mod 2) = 0) Then
        '------------------------------------------
        ' the object can be used as gateway destination
        '------------------------------------------
        r.v(sRegPre & "MSGFGCRH" & iNr & "_MMC") = 4
        r.v(sRegPre & "MSGFGCRH" & iNr & "_CANPTR") = iNr - 1
        SetNormalGatewayMode = True
      Else
        '------------------------------------------
        ' the object can be used as gateway source
        '------------------------------------------
        If (Ask(s)) Then
          '---------------------------------
          ' settings for the gateway source
          '---------------------------------
          r.v(sRegPre & "MSGFGCRH" & iNr & "_MMC") = 4
          r.v(sRegPre & "MSGFGCRH" & iNr & "_CANPTR") = iNr + 1
          ' if the gateway destination is configured as FIFO so the
          ' gateway source must be have the same FSIZE value
          If (r.v(sRegPre & "MSGFGCRH" & iNr + 1 & "_MMC") = 2) Then
            r.v(sRegPre & "MSGFGCRL" & iNr & "_FSIZE") = r.v(sRegPre & "MSGFGCRL" & iNr + 1 & "_FSIZE")
          End If
          ' if the gateway destination is configured as shared gateway so
          ' it is set to standard object
          If (r.v(sRegPre & "MSGFGCRH" & iNr + 1 & "_MMC") = 5) Then
            ResetSharedGatewayMode(r, vIndex, iNr + 1)
            r.v(sRegPre & "MSGFGCRH" & iNr + 1 & "_MMC") = 0
          End If

          '--------------------------------------
          ' settings for the gateway destination
          '--------------------------------------
          ' the destination object is connected to the consecutive node
          r.v(sRegPre & "MSGCFGL" & iNr + 1 & "_NODE") = 1 - r.v(sRegPre & "MSGCFGL" & iNr & "_NODE")
          ' the gateway destination object must be configured as transmit object
          r.v(sRegPre & "MSGCFGL" & iNr + 1 & "_DIR") = 1
          r.v(sRegPre & "MSGCFGL" & iNr + 1 & "_XTD") = r.v(sRegPre & "MSGCFGL" & iNr & "_XTD")
          r.v(sRegPre & "MSGCTRL" & iNr + 1 & "_MSGVAL") = r.v(sRegPre & "MSGCTRL" & iNr & "_MSGVAL")
          r.v(sRegPre & "MSGFGCRH" & iNr + 1 & "_CANPTR") = iNr + 1
          ' if the gateway destination is currently configured for FIFO mode
          ' so all FIFO slaves must be adjusted
          Call ChangeGatewayDestinationSettings(r, vIndex, iNr)
          SetNormalGatewayMode = True
        End If
      End If
    End If
  End If

End Function


'*****************************************************************************
'Description:
'        This function resets the normal gateway mode for the selected object.
'        The dissicion if the object is as gateway source or
'        destination is made in this function.
'-----------------------------------------------------------------------------
'Parameters:
'        r               reference to the ProjectData object
'        vIndex          parameter
'        iNr             number of the gateway source object
'-----------------------------------------------------------------------------
'Returns:  true if all settings are made, else false
'
'*****************************************************************************
Function ResetNormalGatewayMode(r As ProjectData, vIndex As Variant, iNr As Integer) As Boolean
  On Error Resume Next
  Dim s As String
  Dim sVarPre As String
  Dim sRegPre As String
  Dim i As Integer
  Dim bReturn As Boolean

  ResetNormalGatewayMode = True

  sVarPre = "CAN" & vIndex
  sRegPre = sVarPre & "_"

  s = "Message objects " & iNr + 1 & " is no longer part of the gateway. " & vbCrLf & _
      "This objects will be disabled. " & vbCrLf & _
      "Do you really want to continue ?"


  If (iNr = 0 Or iNr = 31) Then   ' object can not be gateway destination/source
    ' is alway disabled
  Else
    If ((iNr Mod 2) = 0) Then
      '-----------------------------------------------
      ' the object can be used as gateway destination
      '-----------------------------------------------
      If (r.v(sRegPre & "MSGFGCRH" & iNr & "_MMC") = 4) Then   ' if normal gateway mode
        ' set gateway source object to default settings
        r.v(sRegPre & "MSGFGCRH" & iNr & "_MMC") = 0
        r.v(sRegPre & "MSGFGCRL" & iNr & "_SRREN") = 0
        ResetNormalGatewayMode = True
      End If
    Else
      '------------------------------------------
      ' the object can be used as gateway source
      '------------------------------------------
      If (r.v(sRegPre & "MSGFGCRH" & iNr & "_MMC") = 4) Then   ' if normal gateway mode
        If (Ask(s)) Then

          i = iNr + 1 ' number of destination object
          ' if the gateway destination is used with FIFO
          bReturn = ResetFIFOBaseMode(r, vIndex, i)

          If (bReturn) Then
            ' set gateway source object to default settings
            r.v(sRegPre & "MSGFGCRH" & iNr & "_MMC") = 0
            r.v(sRegPre & "MSGFGCRL" & iNr & "_DLCC") = 0
            r.v(sRegPre & "MSGFGCRL" & iNr & "_IDC") = 0
            r.v(sRegPre & "MSGFGCRL" & iNr & "_GDFS") = 0

            ' set gateway destination object to default settings
            r.sfr(sRegPre & "MSGCTRH" & i) = &H0
            r.sfr(sRegPre & "MSGCTRL" & i) = &H5555
            r.sfr(sRegPre & "MSGCFGH" & i) = &H0
            r.sfr(sRegPre & "MSGCFGL" & i) = &H0
            r.sfr(sRegPre & "MSGFGCRH" & i) = &H0
            r.sfr(sRegPre & "MSGFGCRL" & i) = &H0
            ResetNormalGatewayMode = True
          Else
            ResetNormalGatewayMode = False
          End If
        Else
          ResetNormalGatewayMode = False
        End If
      End If
    End If
  End If
End Function


'*****************************************************************************
'Description:
'        This function sets the shared gateway mode for the selected object.
'-----------------------------------------------------------------------------
'Parameters:
'        r               reference to the ProjectData object
'        vIndex          parameter
'        iNr             number of the gateway object
'-----------------------------------------------------------------------------
'Returns:  true if all settings are made, else false
'
'*****************************************************************************
Function SetSharedGatewayMode(r As ProjectData, vIndex As Variant, iNr As Integer) As Boolean
  On Error Resume Next
  Dim s As String
  Dim sVarPre As String
  Dim sRegPre As String
  Dim bReturn As Boolean

  SetSharedGatewayMode = False

  sVarPre = "CAN" & vIndex
  sRegPre = sVarPre & "_"

  bReturn = True
  If (bReturn) Then
    bReturn = ResetFIFOBaseMode(r, vIndex, iNr)
  End If
  If (bReturn) Then
    bReturn = ResetNormalGatewayMode(r, vIndex, iNr)
  End If

  If (bReturn) Then
    '---------------------------------
    ' settings for the shared gateway
    '---------------------------------
    r.v(sRegPre & "MSGFGCRH" & iNr & "_MMC") = 5
    r.v(sRegPre & "MSGFGCRH" & iNr & "_CANPTR") = iNr
    SetSharedGatewayMode = True
  End If

End Function


'*****************************************************************************
'Description:
'        This function resets all slave objects for a FIFO which are no
'        longer part of the FIFO.
'-----------------------------------------------------------------------------
'Parameters:
'        r               reference to the ProjectData object
'        vIndex          parameter
'        iNr             number of the FIFO base object
'-----------------------------------------------------------------------------
'Returns:  true if all settings are made or no shared gateway mode selected, else
'          false
'*****************************************************************************
Function ResetSharedGatewayMode(r As ProjectData, vIndex As Variant, iNr As Integer) As Boolean
  On Error Resume Next
  Dim s As String
  Dim sVarPre As String
  Dim sRegPre As String
  Dim iFirstNr As Integer
  Dim iLastNr As Integer
  Dim i As Integer

  ResetSharedGatewayMode = True

  sVarPre = "CAN" & vIndex
  sRegPre = sVarPre & "_"

  If (r.v(sRegPre & "MSGFGCRH" & iNr & "_MMC") = 5) Then  ' only if the shared gateway mode is selected
    r.v(sRegPre & "MSGFGCRL" & iNr & "_SRREN") = 0
    r.v(sRegPre & "MSGFGCRL" & iNr & "_GDFS") = 0
    ResetSharedGatewayMode = True
  End If
End Function


'*****************************************************************************
'Description:
'        This function changes the settings of the slave FIFO objects when
'        they are changed in the base object.
'-----------------------------------------------------------------------------
'Parameters:
'        r               reference to the ProjectData object
'        vIndex          parameter
'        iNr             number of the FIFO base object
'        iFSIZE          new value of the FSIZE bit from the FIFO base object
'-----------------------------------------------------------------------------
'Returns:  true if all settings are made or no FIFO mode selected, else
'          false
'*****************************************************************************
Function ChangeFIFOSettings(r As ProjectData, vIndex As Variant, iNr As Integer, iFSIZE As Integer) As Boolean
  On Error Resume Next
  Dim s As String
  Dim sVarPre As String
  Dim sRegPre As String
  Dim i As Integer
  Dim iFirstNr As Integer
  Dim iLastNr As Integer

  ChangeFIFOSettings = True

  sVarPre = "CAN" & vIndex
  sRegPre = sVarPre & "_"

  If (r.v(sRegPre & "MSGFGCRH" & iNr & "_MMC") = 2) Then  ' only if the FIFO mode is selected

    If (r.v(sRegPre & "MSGFGCRL" & iNr & "_FSIZE") <> iFSIZE) Then
      If (r.v(sRegPre & "MSGFGCRL" & iNr & "_FSIZE") > iFSIZE) Then
        ' --------------------------------------------------------
        ' the current FSIZE is greater then to the required FSIZE
        ' so same FIFO slave objects must be deselected
        ' --------------------------------------------------------
        ' number of the first slave object which should be deselected
        iFirstNr = iNr + iFSIZE + 1
        ' number of the last slave object which should be deselected
        iLastNr = iNr + r.v(sRegPre & "MSGFGCRL" & iNr & "_FSIZE")

        s = "Message objects " & iFirstNr & " - " & iLastNr & " are no longer part of the FIFO. " & vbCrLf & _
            "This objects will be disabled. " & vbCrLf & _
            "Do you really want to continue ?"

        If (Ask(s)) Then

          ' all slave objects must be the same value for FSIZE
          For i = iNr + 1 To iFirstNr - 1
            r.v(sRegPre & "MSGFGCRL" & i & "_FSIZE") = iFSIZE
          Next i

          ' deselect all slave objects which are no longer part of the FIFO
          For i = iFirstNr To iLastNr
            r.sfr(sRegPre & "MSGCTRH" & i) = &H0
            r.sfr(sRegPre & "MSGCTRL" & i) = &H5555
            r.sfr(sRegPre & "MSGCFGH" & i) = &H0
            r.sfr(sRegPre & "MSGCFGL" & i) = &H0
            r.sfr(sRegPre & "MSGFGCRH" & i) = &H0
            r.sfr(sRegPre & "MSGFGCRL" & i) = &H0
          Next i
        Else
          ChangeFIFOSettings = False
        End If

      Else
        ' --------------------------------------------------------
        ' the current FSIZE is lower then to the required FSIZE
        ' so same more objects must be configured as FIFO slaves
        ' --------------------------------------------------------
        iFirstNr = iNr + 1
'        iLastNr = iNr + 2^ iFSIZE - 1
        iLastNr = iNr + iFSIZE

        s = "Message objects " & iFirstNr & " - " & iLastNr & " are part of the FIFO. This objects will " & vbCrLf & _
            "be configured as FIFO slave objects of the base object " & iNr & "." & vbCrLf & _
            "Do you really want to continue ?"

        If (Ask(s)) Then
          For i = iFirstNr To iLastNr
            r.v(sRegPre & "MSGFGCRH" & i & "_CANPTR") = iNr
            r.v(sRegPre & "MSGFGCRL" & i & "_FSIZE") = iFSIZE
            r.v(sRegPre & "MSGFGCRL" & i & "_FD") = r.v(sRegPre & "MSGFGCRL" & iNr & "_FD")
            r.v(sRegPre & "MSGFGCRH" & i & "_MMC") = 3

            r.v(sRegPre & "MSGCFGL" & i & "_NODE") = r.v(sRegPre & "MSGCFGL" & iNr & "_NODE")
            r.v(sRegPre & "MSGCFGL" & i & "_DIR") = r.v(sRegPre & "MSGCFGL" & iNr & "_DIR")

            'ahe 16.05.01
            r.v(sRegPre & "MSGCFGL" & i & "_RMM") = r.v(sRegPre & "MSGCFGL" & iNr & "_RMM")

            r.v(sRegPre & "MSGCFGL" & i & "_XTD") = r.v(sRegPre & "MSGCFGL" & iNr & "_XTD")

            r.v(sRegPre & "MSGCTRL" & i & "_MSGVAL") = r.v(sRegPre & "MSGCTRL" & iNr & "_MSGVAL")
            If (r.v(sRegPre & "MSGCFGL" & iNr & "_DIR") = 0) Then ' if receive FIFO
              r.v(sRegPre & "MSGARH" & i & "_ID") = r.v(sRegPre & "MSGARH" & iNr & "_ID")
              r.v(sRegPre & "MSGARL" & i & "_ID") = r.v(sRegPre & "MSGARL" & iNr & "_ID")
            End If
            r.v(sRegPre & "MSGAMRH" & i & "_AM") = r.v(sRegPre & "MSGAMRH" & iNr & "_AM")
            r.v(sRegPre & "MSGAMRL" & i & "_AM") = r.v(sRegPre & "MSGAMRL" & iNr & "_AM")
          Next i
        Else
          ChangeFIFOSettings = False
        End If

      End If

      If (ChangeFIFOSettings) Then
        ' if the FIFO is part of a gateway destination then the gateway source
        ' must have the same FSIZE value
        If (iNr > 0) Then
          If (r.v(sRegPre & "MSGFGCRH" & iNr - 1 & "_MMC") = 4) Then
            r.v(sRegPre & "MSGFGCRL" & iNr - 1 & "_FSIZE") = iFSIZE
          End If
        End If
        ' set the new value of FSIZE for the FIFO base object
        r.v(sRegPre & "MSGFGCRL" & iNr & "_FSIZE") = iFSIZE
      End If

    Else
     ' ---------------------------------------------------
     ' the current FSIZE is equal to the required FSIZE
     ' so only same settings must be adjusted
     ' ---------------------------------------------------
      iFirstNr = iNr + 1
'      iLastNr = iNr + 2^iFSIZE - 1
      iLastNr = iNr + iFSIZE
      For i = iFirstNr To iLastNr
        r.v(sRegPre & "MSGFGCRL" & i & "_FD") = r.v(sRegPre & "MSGFGCRL" & iNr & "_FD")

        r.v(sRegPre & "MSGCFGL" & i & "_NODE") = r.v(sRegPre & "MSGCFGL" & iNr & "_NODE")
        r.v(sRegPre & "MSGCFGL" & i & "_DIR") = r.v(sRegPre & "MSGCFGL" & iNr & "_DIR")
        r.v(sRegPre & "MSGCFGL" & i & "_XTD") = r.v(sRegPre & "MSGCFGL" & iNr & "_XTD")

        'ahe 16.05.01
        r.v(sRegPre & "MSGCFGL" & i & "_RMM") = r.v(sRegPre & "MSGCFGL" & iNr & "_RMM")

        If (r.v(sRegPre & "MSGCFGL" & iNr & "_DIR") = 0) Then ' if receive FIFO
          r.v(sRegPre & "MSGARH" & i & "_ID") = r.v(sRegPre & "MSGARH" & iNr & "_ID")
          r.v(sRegPre & "MSGARL" & i & "_ID") = r.v(sRegPre & "MSGARL" & iNr & "_ID")
        End If
        r.v(sRegPre & "MSGAMRH" & i & "_AM") = r.v(sRegPre & "MSGAMRH" & iNr & "_AM")
        r.v(sRegPre & "MSGAMRL" & i & "_AM") = r.v(sRegPre & "MSGAMRL" & iNr & "_AM")
      Next i
    End If

  End If  ' if FIFO mode

End Function

