Attribute VB_Name = "General"
'*----------------------------------------------------------------------
'* (c) 2008 Microstrain, Inc.
'*----------------------------------------------------------------------
'* THE PRESENT SOFTWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING
'* CUSTOMERS WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER
'* FOR THEM TO SAVE TIME. AS A RESULT, MICROSTRAIN SHALL NOT BE HELD LIABLE
'* FOR ANY DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY
'* CLAIMS ARISING FROM THE CONTENT OF SUCH SOFTWARE AND/OR THE USE MADE BY
'* CUSTOMERS OF THE CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH
'* THEIR PRODUCTS.
'*-----------------------------------------------------------------------
'MicroStrain, Inc.
'VB6 Sample Code for Inertia-Link and 3DM-GX2
'Demonstrates polling for 0xC5 Orientation Matrix
'Uses Data Communication Protocol 1.15
'Version 1.0.0
'1 November 2008
'Fritz, engineer
'Barry, developer
'Notes:
    'Written with Visual Basic 6.0 Service Pack 5
    'Built at 800 X 600 (32-bit color)
    'Uses native controls in standard IDE; no third party controls
    'Decimal equivalent of hex is used for all protocol commands
    'Non-packet protocol used
    'MSComm1 set at 115200,n,8,1 and InputMode=1(binary)
'*-----------------------------------------------------------------------
'Add revisions here
'*-----------------------------------------------------------------------
'byte array
Public arrBytes() As Byte
'data packet variables
Public MyHeader As Double
Public TimerTick As Double
Public MyCheckSum As Double
Public TheirCheckSum As Double
Public M11 As Double
Public M12 As Double
Public M13 As Double
Public M21 As Double
Public M22 As Double
Public M23 As Double
Public M31 As Double
Public M32 As Double
Public M33 As Double
'elapsed time variable
Public ElapsedTime As Double
'previous time variable
Public PreviousTime As Double
'flag first pass through TimerTickCalc function
Public flgFirstPass As Boolean
'Timer tick variable for display
Public DisplayTimerTickCalc As Double
'declare API call for float function
Public Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)

Public Function FloatConversion(byteStart As Integer) As Single
'dim byte array
Dim bytArray(0 To 3) As Byte
'populate byte array
bytArray(3) = arrBytes(byteStart)
bytArray(2) = arrBytes(byteStart + 1)
bytArray(1) = arrBytes(byteStart + 2)
bytArray(0) = arrBytes(byteStart + 3)
'copy into the float
CopyMemory FloatConversion, bytArray(0), 4
End Function

Public Function TimerTickCalc(byteStart As Integer) As Double
'calc 32 bit unsigned integer
TimerTickCalc = CDbl(CDbl(arrBytes(byteStart)) * 16777216) + _
                            CDbl(CDbl(arrBytes(byteStart) + 1) * 65536) + _
                            CDbl(CDbl(arrBytes(byteStart) + 2) * 256) + _
                           CDbl(CDbl(arrBytes(byteStart) + 3))

'scale and round
TimerTickCalc = TimerTickCalc / 19660800
'handle negative output
If TimerTickCalc < 0 Then
    TimerTickCalc = Round(TimerTickCalc + 217.999999949137, 2)
End If

'flag first pass through function; starts elapsed time at 0
If flgFirstPass = False Then
    'set flag
    flgFirstPass = True
    'jump
    GoTo Jump
End If

'handle roll-over
If TimerTickCalc - PreviousTime < 0 Then
    'increment elapsed time
    ElapsedTime = ElapsedTime + (CDbl((TimerTickCalc + 217.999999949137)) - PreviousTime)
Else
    'increment elapsed time
    ElapsedTime = ElapsedTime + (TimerTickCalc - PreviousTime)
End If

Jump:
'set previous time
PreviousTime = TimerTickCalc
'round elapsed time
ElapsedTime = Round(ElapsedTime, 2)
'round timer tick
DisplayTimerTickCalc = Round(TimerTickCalc, 2)

End Function

