Module 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.
    'VB 2005 Sample Code for Inertia-Link and 3DM-GX2
    'Demonstrates wireless polling for 0xCE Euler Angles
    'Uses Data Communication Protocol 1.15
    'Version 1.0.0
    '1 November 2008
    'Fritz, engineer
    'Barry, developer
    'Notes:
    'Written with Visual Basic 2005 Service Pack 1
    'Built at 1024 X 768 (Highest 32-bit)
    'Uses native controls in standard IDE; no third party controls
    'Decimal equivalent of hex is used for all protocol commands
    'Non-packet protocol used
    'SerialPort1 set at 115200,n,8,1
    '*-----------------------------------------------------------------------
    'Add revisions here
    '*-----------------------------------------------------------------------
    'byte array
    Public arrBytes() As Byte
    'byte array
    Public arrCommand() As Byte
    'array upper limit
    Public UpperLimitOfArray As Double
    'data packet variables
    Public MyHeader As Double
    Public TimerTick As Double
    Public MyCheckSum As Double
    Public TheirCheckSum As Double
    Public Roll As Double
    Public Pitch As Double
    Public Yaw As Double
    Public BaseStationACK As Double
    Public SOP As Double
    Public DeliveryFlags As Double
    Public AppDataType As Double
    Public NodeAddress As Double
    Public ReplyLength As Double
    Public ExtraByte As Double
    Public Command As Double
    Public LQI As Double
    Public RSSI 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
    'temporary array to send 4 bytes to bit converter; convert to float
    Public arrTemp(0 To 3) As Byte
    Public Function TimerTickCalc(ByVal 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 = Math.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 = Math.Round(ElapsedTime, 2)
        'round timer tick
        DisplayTimerTickCalc = Math.Round(TimerTickCalc, 2)

    End Function
End Module
