Attribute VB_Name = "Module1"
'#Uses "..\include\CC6Format.inc"
'#Uses "..\include\Const.inc"
'#Uses "..\include\Global.inc"
'#Uses "..\include\Port.inc"
'#Uses "..\classes\CC6FunctionClass.cls"
'#Uses "..\classes\CC6ParameterClass.cls"
'#Uses "..\classes\Port.cls"
'#Uses "..\classes\PortPin.cls"

'*****************************************************************************
' In this file functions are, with those the CC6-Modul to those
' Environment for 8-bit derivatives is adapted.
'*****************************************************************************
'-----------------------------------------------------------------------------
' History:
'
'        Rev. Author         Date        Description
'        ---------------------------------------------------------------------
'        0.1   als          20.11.04     initial version for XC866
'
'*****************************************************************************

Option Explicit

Type UnitType
  dValue As Double   ' value of the time / frequency
  sUnit As String    ' unit of the time / frequency
  dDivider As Double    ' divider factor referring to usec / MHz
End Type


'*****************************************************************************
'Description:
'        This function returns the current function of the selected pin
'
'        0 : no function
'        1 : pin is used as input
'        2 : pin is used as output
'-----------------------------------------------------------------------------
'Parameters:
'        r                     reference to this object
'        sPinName              the name of the pin (specified with function
'                              'AddPin' or 'AddAlternateName' in install.inc)
'-----------------------------------------------------------------------------
'Returns:
'        The number of the pin function.
'        0 : no function
'        1 : pin is used as input
'        2 : pin is used as output
'*****************************************************************************
''Function GetPinFunction(r As ProjectData, sPinName As String) As Integer
''  If (r.exist("MuxFor_" & sPinName) = True) Then
''      GetPinFunction = r.v("MuxFor_" & sPinName)
''  Else
''      GetPinFunction = 0
''  End If
''End Function

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

  dHelp = dValue
  sHelp = "usec"
  dDivider = 1

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

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

  Exit Function

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

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

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

  Set r = ps.ProjectData

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

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

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

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

  UpdateFunction = True
  Exit Function

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


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

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

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

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

    With e("chk_" & sName)
      If (bDisabled) Then
        ' tooltip if control is disabled
        .DisabledTooltipText = func.DisabledTooltip(d)
      Else
        ' tooltip if control is enabled
      End If
    End With

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

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


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

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

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

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

  ShowFunction = True
  Exit Function

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


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

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

  Set r = ps.ProjectData

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

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

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

  UpdateParameter = True
  Exit Function

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

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

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

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

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

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

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

  Next l

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

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

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

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


  ShowParameter = True
  Exit Function

ShowParameterError:
  Errorbox "Can not show page", Err.Description
  Exit Function
End Function
'*****************************************************************************

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

  Dim o As Object
  Dim s As String

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

