Attribute VB_Name = "Module1"
'#Uses "..\classes\FunctionClass.cls"
'#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:
'        CAPCOMx
'-----------------------------------------------------------------------------
' Description:
'        This file contains functions which are create controls for the
'        module 'CAPCOMx'.
'        Furthermore it contains functions for the event handling of
'        these controls.
'
'-----------------------------------------------------------------------------
' History:
'
'        Rev. Author         Date        Description
'        ---------------------------------------------------------------------
'        1.0   ahe         18.05.01     final version for XC164CS
'
'        1.1   ahe         05.06.02     no changes
'        1.2   ste         18.12.02     stelling correction
'                                       final version for XC167CI

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


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

    '-------------------------------------------------------------------------
    ' end of pages
    '-------------------------------------------------------------------------
    
    '-------------------------------------------------------------------------
    ' call function to initialize the menu
    '-------------------------------------------------------------------------
    If (Not InitMenu(ps, "CC6\CC6_CHANNEL_3")) 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
    Dim sVarPre As String
    Dim vIndex As Variant

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

    
    '-------------------------------------------------------------------------
    'page: OnChangingPageControl
    '-------------------------------------------------------------------------
    If (Not OnChangingPageControl(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
    
    Dim sVarPre As String
    Dim vIndex As Variant

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


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


    OnShow = True
    Exit Function

OnShowError:
    Errorbox "Can not show page.", Err.Description
    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 Text in OnClick.", 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, "CC6\CC6_CHANNEL_3")) 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 OnLoadPageControl(ps As Object) As Boolean
  On Error Resume Next

  OnLoadPageControl = False

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

  OnLoadPageControl = 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 OnChangingPageControl(ps As Object, Pagename As String) As Boolean
  On Error Resume Next
  Dim iChannel As Integer

  iChannel = ps.Parameters.v("Channel")

  OnChangingPageControl = False
  
  If (Not ps.ExtenderCollection("Control").Loaded) And (Pagename = "Control") Then
    ps.ExtenderCollection("Control").Loaded = True
  

    '-------------------------------
    ' frame: frmECT13O
    '-------------------------------
    If (Not ps.AddFrame("frmECT13O", 100, 100, 4150, 875, _
      "Compare Timer 13 Output Control")) Then Exit Function
        If (Not ps.AddCheck("chkECT13O", 200, 300, 3750, 400, _
          "Enable alternate output function COUT63 for the PWM signal generated by T13 (ECT13O)")) Then Exit Function

    '-------------------------------
    ' frame: frmTRPEN13
    '-------------------------------
    If (Not ps.AddFrame("frmTRPEN13", 100, 1025, 4150, 875, _
      "Trap Control for COUT63")) Then Exit Function
        If (Not ps.AddCheck("chkTRPEN13", 200, 300, 3750, 400, _
          "Enable the trap functionality of the output pin COUT63 (TRPEN13)")) Then Exit Function

    '-------------------------------
    ' frame: frmCC6xSR
    '-------------------------------
    If (Not ps.AddFrame("frmCC6xSR", 4400, 100, 4150, 1800, "Duty Cycle")) Then Exit Function
        If (Not ps.AddLabel("lblReq", 0, 275, 350, 1750, 300, "Required duty cycle [%]")) Then Exit Function
        If (Not ps.AddText("txtReq", 2650, 300, 975, 315)) Then Exit Function

        If (Not ps.AddLabel("lblReal", 0, 275, 850, 1775, 300, "Real duty cycle [%]")) Then Exit Function
        If (Not ps.AddLabel("txtReal", 1, 2650, 800, 975, 315, "50")) Then Exit Function

        If (Not ps.AddLabel("lblCC6xSR", 0, 275, 1275, 1575, 400, "Compare register (CCU6_CC63SR)")) Then Exit Function
        If (Not ps.AddText("txtCC6xSR", 2650, 1300, 975, 315)) Then Exit Function

    '-------------------------------
    ' frame: frm" & sPin & "6xPS
    '-------------------------------
    If (Not ps.AddFrame("frmCOUT6xPS", 100, 1950, 4150, 1300, _
      "Passive State Select (COUT63PS)")) Then Exit Function
        If (Not ps.AddOption("optCOUT6xPS_0", 200, 250, 3900, 380, _
          "The compare output COUT63 drives passive level while CC63ST is '0'")) Then Exit Function
        If (Not ps.AddOption("optCOUT6xPS_1", 200, 750, 3900, 380, _
          "The compare output COUT63 drives passive level while CC63ST is '1'")) Then Exit Function

    '-------------------------------
    ' frame: frmPSL63
    '-------------------------------
    If (Not ps.AddFrame("frmPSL63", 100, 3300, 4150, 1300, _
      "Compare Output Control for COUT63 (PSL63)")) Then Exit Function
        If (Not ps.AddOption("optPSL63_0", 200, 250, 3900, 380, _
          "The passive level of COUT63 output is '0'")) Then Exit Function
        If (Not ps.AddOption("optPSL63_1", 200, 750, 3900, 380, _
          "The passive level of COUT63 output is '1'")) Then Exit Function

    '-------------------------------
    ' frame: frmT13IM
    '-------------------------------
    If (Not ps.AddFrame("frmT13IM", 4400, 1950, 4150, 1300, _
      "T13 Modulation Control (T13IM)")) Then Exit Function
        If (Not ps.AddOption("optT13IM_0", 200, 250, 3900, 380, _
          "T13 output is not inverted")) Then Exit Function
        If (Not ps.AddOption("optT13IM_1", 200, 750, 3900, 380, _
          "T13 output is inverted for further modulation")) Then Exit Function


  End If
  OnChangingPageControl = 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 OnShowPageControl(ps As Object, Controlname As String, Pagename As String, v() As Variant) As Boolean
  On Error GoTo OnShowPageControlError

  OnShowPageControl = False

  Dim r As ProjectData
  Dim c As Object
  Dim e As Object
  Dim iChannel As Integer
  Dim sRegPre As String
  Dim sVarPre As String
  Dim vIndex As Variant
  Dim bDisabled As Boolean
  Dim sDisabledTooltip As String

  vIndex = ps.Parameters.v("x")
  iChannel = ps.Parameters.v("Channel")

  sVarPre = "CCU" & vIndex
  sRegPre = sVarPre & "_"

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

  If (Pagename = "Control") Then
    '-------------------------------------------------------------------------
    ' page: Control (action)
    '-------------------------------------------------------------------------
    Select Case (Controlname)
        '---------------------------------------------------------------------
        ' control: chkECT13O (action)
        '---------------------------------------------------------------------
        Case "chkECT13O"
            r.v(sRegPre & "MODCTR_ECT13O") = v(1)

        '---------------------------------------------------------------------
        ' control: chkTRPEN13 (action)
        '---------------------------------------------------------------------
        Case "chkTRPEN13"
            r.v(sRegPre & "TRPCTR_TRPEN13") = v(1)

        '---------------------------------------------------------------------
        ' control: txt0 (action)
        '---------------------------------------------------------------------
        Case "txtReq"
            If (IsInputFormatOK(v(1), "F", c("lblReq").Caption, 0, 100)) Then
                r.v(sVarPre & "_REQ_DUTY_CYCLE_" & iChannel) = v(1)
                Call UpdateDutyCycle(r, vIndex, iChannel)
            Else
                Exit Function
            End If

        '---------------------------------------------------------------------
        ' control: txtCC6xSR (action)
        '---------------------------------------------------------------------
        Case "txtCC6xSR"
            If (IsInputFormatOK(v(1), "H4", c("lblCC6xSR").Caption, 0, 65535)) Then
                r.sfr(sRegPre & "CC6" & iChannel & "SR") = v(1)
                r.v(sVarPre & "_REQ_DUTY_CYCLE_" & iChannel) = _
                GetRealDutyCecle(r, vIndex, iChannel)
            Else
                Exit Function
            End If

        '---------------------------------------------------------------------
        ' control: optCOUT6xPS_0 (action)
        '---------------------------------------------------------------------
        Case "optCOUT6xPS_0"
            r.v(sRegPre & "CMPSTAT_COUT63PS") = 0

        '---------------------------------------------------------------------
        ' control: optCOUT6xPS_1 (action)
        '---------------------------------------------------------------------
        Case "optCOUT6xPS_1"
            r.v(sRegPre & "CMPSTAT_COUT63PS") = 1

        '---------------------------------------------------------------------
        ' control: optPSL63_0 (action)
        '---------------------------------------------------------------------
        Case "optPSL63_0"
            r.v(sRegPre & "PSLR_PSL63") = 0

        '---------------------------------------------------------------------
        ' control: optPSL63_1 (action)
        '---------------------------------------------------------------------
        Case "optPSL63_1"
            r.v(sRegPre & "PSLR_PSL63") = 1

        '---------------------------------------------------------------------
        ' control: optT13IM_0 (action)
        '---------------------------------------------------------------------
        Case "optT13IM_0"
            r.v(sRegPre & "CMPSTAT_T13IM") = 0

        '---------------------------------------------------------------------
        ' control: optT13IM_1 (action)
        '---------------------------------------------------------------------
        Case "optT13IM_1"
            r.v(sRegPre & "CMPSTAT_T13IM") = 1


      Case Else
    End Select

    '-------------------------------------------------------------------------
    ' page: Control (show)
    '-------------------------------------------------------------------------
    bDisabled = (IsModuleClockDisabled(r, vIndex))
    
    sDisabledTooltip = ""
    sDisabledTooltip = TOOLTIP_DISABLED_IF
    
    If (IsModuleClockDisabled(r, vIndex)) Then
        sDisabledTooltip = sDisabledTooltip & GetTooltipModuleClockDisabled(r, vIndex)
    End If
    
    Call SetDefaultPropertiesForAllControlsOnFrame(c("frmCC6xSR").Caption, c, e, bDisabled, sDisabledTooltip, "")
    Call SetDefaultPropertiesForAllControlsOnFrame(c("frmT13IM").Caption, c, e, bDisabled, sDisabledTooltip, "")
   
    '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    bDisabled = (IsModuleClockDisabled(r, vIndex) Or _
                 Not (GetPinFunction(r, sVarPre & "_COUT63_O") = 2))
    
    sDisabledTooltip = ""
    sDisabledTooltip = TOOLTIP_DISABLED_IF
    
    If (IsModuleClockDisabled(r, vIndex)) Then
        sDisabledTooltip = sDisabledTooltip & GetTooltipModuleClockDisabled(r, vIndex)
    End If
    If (Not (GetPinFunction(r, sVarPre & "_COUT63_O") = 2)) Then
        sDisabledTooltip = sDisabledTooltip & _
          FormatDisabledTooltip("the pin COUT63 is not used as output")
    End If

    Call SetDefaultPropertiesForAllControlsOnFrame(c("frmECT13O").Caption, c, e, bDisabled, sDisabledTooltip, "")
    Call SetDefaultPropertiesForAllControlsOnFrame(c("frmTRPEN13").Caption, c, e, bDisabled, sDisabledTooltip, "")
    Call SetDefaultPropertiesForAllControlsOnFrame(c("frmCOUT6xPS").Caption, c, e, bDisabled, sDisabledTooltip, "")
    Call SetDefaultPropertiesForAllControlsOnFrame(c("frmPSL63").Caption, c, e, bDisabled, sDisabledTooltip, "")

    '-------------------------------------------------------------------------
    ' control: chkECT13O (show)
    '-------------------------------------------------------------------------
    With c("chkECT13O")
        .Value = -CInt(r.v(sRegPre & "MODCTR_ECT13O") = 1)
    End With

    '-------------------------------------------------------------------------
    ' control: chkTRPEN13 (show)
    '-------------------------------------------------------------------------
    With c("chkTRPEN13")
        .Value = -CInt(r.v(sRegPre & "TRPCTR_TRPEN13") = 1)
    End With

    '-------------------------------------------------------------------------
    ' control: lblReq (show)
    '-------------------------------------------------------------------------
    With c("lblReq")
    End With

    '-------------------------------------------------------------------------
    ' control: txt0 (show)
    '-------------------------------------------------------------------------
    With c("txtReq")
        .Text = Format(r.v(sVarPre & "_REQ_DUTY_CYCLE_" & iChannel), "0.00")
    End With

    '-------------------------------------------------------------------------
    ' control: lblReal (show)
    '-------------------------------------------------------------------------
    With c("lblReal")
    End With

    '-------------------------------------------------------------------------
    ' control: lblCC6xSR (show)
    '-------------------------------------------------------------------------
    With c("lblCC6xSR")
    End With

    '-------------------------------------------------------------------------
    ' control: txtCC6xSR (show)
    '-------------------------------------------------------------------------
    With c("txtCC6xSR")
        If (IsModuleClockDisabled(r, vIndex)) Then
            .Text = CALCULATION_NOT_POSSIBLE
        Else
            .Text = FormatTextOutput(r.sfr(sRegPre & "CC6" & iChannel & "SR"), "H4")
        End If
    End With

    '-------------------------------------------------------------------------
    ' control: txtReal (show)
    '-------------------------------------------------------------------------
    With c("txtReal")
        .Caption = Format(GetRealDutyCecle(r, vIndex, iChannel), "0.00")
    End With

    '-------------------------------------------------------------------------
    ' control: optCOUT6xPS_0 (show)
    '-------------------------------------------------------------------------
    With c("optCOUT6xPS_0")
        .Value = -CInt(r.v(sRegPre & "CMPSTAT_COUT63PS") = 0)
    End With

    '-------------------------------------------------------------------------
    ' control: optCOUT6xPS_1 (show)
    '-------------------------------------------------------------------------
    With c("optCOUT6xPS_1")
        .Value = -CInt(r.v(sRegPre & "CMPSTAT_COUT63PS") = 1)
    End With

    '-------------------------------------------------------------------------
    ' control: optPSL63_0 (show)
    '-------------------------------------------------------------------------
    With c("optPSL63_0")
        .Value = -CInt(r.v(sRegPre & "PSLR_PSL63") = 0)
    End With

    '-------------------------------------------------------------------------
    ' control: optPSL63_1 (show)
    '-------------------------------------------------------------------------
    With c("optPSL63_1")
        .Value = -CInt(r.v(sRegPre & "PSLR_PSL63") = 1)
    End With

    '-------------------------------------------------------------------------
    ' control: optT13IM_0 (show)
    '-------------------------------------------------------------------------
    With c("optT13IM_0")
        .Value = -CInt(r.v(sRegPre & "CMPSTAT_T13IM") = 0)
    End With

    '-------------------------------------------------------------------------
    ' control: optT13IM_1 (show)
    '-------------------------------------------------------------------------
    With c("optT13IM_1")
        .Value = -CInt(r.v(sRegPre & "CMPSTAT_T13IM") = 1)
    End With


  End If

  OnShowPageControl = True
  Exit Function

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

