'#Uses "..\include\Const.inc"
'#Uses "..\include\Format.inc"
'#Uses "..\include\Global.inc"
'#Uses "..\include\Pages.inc"
'#Uses "..\ASCx\include.inc"

Option Explicit
'*****************************************************************************
' Module:
'        BAUDRATETABLE.INC
'-----------------------------------------------------------------------------
' Description:
'        This include file contains functions which are create controls for 
'        the page 'BaudRateTable'.
'        Furthermore it contains functions for the event handling of
'        these controls.
'
'-----------------------------------------------------------------------------
' History:
'                 
'        Rev. Author         Date        Description
'        ----------------------------------------------------------------------
'        1.0  ahe            27.04.00     final version for C165UTAH
'        3.0  ste            01.12.00     final version for S-GOLDv1
'                                         changes for more flexibillity
'
'
'        ---------------------------------------------------------------------
'        4.0  ahe            14.02.01    final version for Pegasus
'
'        4.1  ahe            08.08.02     no changes
'
'*****************************************************************************


'*****************************************************************************
'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: OnChangingPageBaudRateTable
    '-------------------------------------------------------------------------
    If (Not OnLoadPageBaudRateTable(ps)) Then Exit Function

    '-------------------------------------------------------------------------
    ' call function to initialize the menu
    '-------------------------------------------------------------------------
    If (Not InitMenu(ps, "ASCx\BaudRateTable")) 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

    OnChangingPage = False
    
    '-------------------------------------------------------------------------
    'page: OnChangingPageBaudRateTable
    '-------------------------------------------------------------------------
    If (Not OnChangingPageBaudRateTable (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

    OnShow = False

    '-------------------------------------------------------------------------
    ' page: OnShowPageBaudRateTable
    '-------------------------------------------------------------------------
    If (Not OnShowPageBaudRateTable(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:
    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, "ASCx\BaudRateTable")) Then Exit Function

    OnMenuClick = True
    Exit Function

OnMenuClickError:
    Exit Function
End Function


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

  OnLoadPageBaudRateTable = False

  If (Not ps.AddPage("Baud Rate Table")) Then Exit Function

  OnLoadPageBaudRateTable = 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 OnChangingPageBaudRateTable(ps As Object, Pagename As String) As Boolean
  On Error Resume Next

  OnChangingPageBaudRateTable = False
  
  If (Not ps.ExtenderCollection("Baud Rate Table").Loaded) And (Pagename = "Baud Rate Table") Then
    ps.ExtenderCollection("Baud Rate Table").Loaded = True
  
    '-------------------------------
    ' frame: frm0
    '-------------------------------
    If (Not ps.AddFrame("frm0",150,100, 8400, 5000, "Autobaud Detection using Baud Rates")) Then Exit Function
        If (Not ps.AddText("txtFD_VALUE1", 2400, 3400, 1000, 300)) Then Exit Function
        If (Not ps.AddLabel("lblFD_VALUE1", 0, 200, 3450, 3000, 300, "Fractional divider (n/512)")) Then Exit Function
        If (Not ps.AddFlexGrid("grd", 200, 300, 5450, 2725)) Then Exit Function
          With(ps.ControlCollection("grd"))
          .Rows = 10
          .Cols = 4
          .FixedCols = 0
          .WordWrap = True
          .RowHeight(0) = .RowHeight(0) * 2
          .ColWidth(0) = 1100
          .ColWidth(1) = 2000
          .ColWidth(2) = 800
          .ColWidth(3) = 1500
          .TextMatrix(0, 0) = "Baud Rate" & vbcrlf & "Numbering"
          .TextMatrix(0, 1) = "Detectable" & vbcrlf & "Baud Rates"
          .TextMatrix(0, 2) = "Divide" & vbcrlf & "Factor"
          .TextMatrix(0, 3) = "BG Value After Detection"
        End With
  End If
  OnChangingPageBaudRateTable = 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 OnShowPageBaudRateTable(ps As Object, Controlname As String, Pagename As String, v() As Variant) As Boolean
  On Error GoTo OnShowPageBaudRateTableError

  OnShowPageBaudRateTable = False

  Dim r As ProjectData
  Dim c As Object
  Dim e As Object
  Dim bDisabled As Boolean
  Dim i As Integer

  Dim sRegPre As String
  Dim sVarPre As String
  Dim vIndex As Variant
 
  vIndex = ps.Parameters.v("x")
  sVarPre = "ASC" & vIndex
  sRegPre = "ASC" & vIndex & "_"

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

  If (Pagename = "Baud Rate Table") Then
    '-------------------------------------------------------------------------
    ' page: BaudRateTable (action)
    '-------------------------------------------------------------------------
    Select Case (Controlname)
           '-------------------------------------------------------------------------
           ' control: txtFD_Val (action)
           '-------------------------------------------------------------------------
        Case "txtFD_VALUE1"
             If (IsInputFormatOK(v(1), "D", c("lblFD_VALUE1").Caption, 1, 511)) Then
               r.v(sRegPre & "FDV_FD_VALUE") = v(1)
             Else
               Exit Function
             End If                

      Case Else
    End Select

    '-------------------------------------------------------------------------
    ' page: BaudRateTable (show)
    '-------------------------------------------------------------------------

    '-------------------------------------------------------------------------
    ' control: grd (show)
    '-------------------------------------------------------------------------
    With c("grd")
      For i = 1 To 9
        .TextMatrix(i, 0) = "Baud rate " & i - 1
      Next i
      For i = 1 To 9
       .TextMatrix(i, 1) = Format(1000 * r.v(sRegPre & "FDV_FD_VALUE") * GetASCxModuleClock(r, vIndex) _
        / (512 * GetDivideFactor(i - 1)), "0.000000") & "    kbaud "
      Next i
      For i = 1 To 9
        .TextMatrix(i, 2) = GetDivideFactor(i - 1) & " "
        .TextMatrix(i, 3) = GetBGFactor(i - 1) & " = " & FormatTextOutput(GetBGFactor(i - 1), "H3") & " "
      Next i
    End With

    '-------------------------------------------------------------------------
    ' control: txtFD_Val (show)
    '-------------------------------------------------------------------------
    With c("txtFD_VALUE1")
      bDisabled = r.v(sRegPre & "ABCON_ABEN") = 0
      .Enabled = Not(bDisabled)
      .Text = r.v(sRegPre & "FDV_FD_VALUE")
    End With

    With e("txtFD_VALUE1")
      If (bDisabled) Then
        .DisabledTooltipText = TOOLTIP_DISABLED_IF
        If (r.v(sRegPre & "ABCON_ABEN") = 0) Then
          .DisabledTooltipText = .DisabledTooltipText & FormatDisabledTooltip("autobaud detection mode is not enabled")
        End If
      End If
    End With

    '-------------------------------------------------------------------------
    ' control: lblFD_VALUE1 (show)
    '-------------------------------------------------------------------------
    With c("lblFD_VALUE1")
      ' same as from txtFD_VALUE1
      .Enabled = Not(bDisabled)
    End With

    With e("lblFD_VALUE1")
      If (bDisabled) Then
        .DisabledTooltipText = e("txtFD_VALUE1").DisabledTooltipText
      End If
    End With

  End If

  OnShowPageBaudRateTable = True
  Exit Function

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


'*****************************************************************************
'Description:
'        This function return the value of BG
'
'-----------------------------------------------------------------------------
'Parameters:
'        i              As Integer
'-----------------------------------------------------------------------------
'Returns:
'        BG    
'*****************************************************************************
Function GetBGFactor(i As Integer)
  On Error Resume Next
  Dim BG(8) As Integer

  BG(0) = 2
  BG(1) = 5
  BG(2) = 11
  BG(3) = 17
  BG(4) = 35
  BG(5) = 71
  BG(6) = 143
  BG(7) = 287
  BG(8) = 575
  
  GetBGFactor = BG(i)

End Function

