' Text file showing VB Code for reference info.
' Program:     DataLogger
' Author:      Dan Henne
' Date:        April 8, 2004
' Version:     1.0.0
' Description:
' -This example:
'    o Records the state of all the inputs of a PhidgetInterfaceKit with
'      adjustable timer intervals (approx 10 msec. depending on your PC)
'    o The data is recorded in a simple text file with comma delimited data
'      which can be used by any spreadsheet program
'    o Displays all the information it can about the PhidgetInterfaceKit
'    o The total number of data points can be changed
'    o The sample can be stopped at anytime
'    o The filename can be changed
'    o This is just an example and is intended to provide you with the basic
'      concepts you need to create your own datalogger. Not all errors have been
'      handled in a robust manner to keep the example as simple and intuitive as
'      possible. If you make any enhancements and want to share your work with
'      other Phidget users send an email to Sales@Phidgets.com
'    o Have fun

Option Explicit

'Phidget Library info
Private WithEvents PM As PhidgetManager
Dim WithEvents IFKit As PhidgetInterfaceKit
Dim IFKitAttached As Boolean

'These two functions are used as a fancy way of getting an accurate timing
Private Declare Function QueryPerformanceFrequency Lib "kernel32" (lpFrequency As Currency) As Long
Private Declare Function QueryPerformanceCounter Lib "kernel32" (lpPerformanceCount As Currency) As Long

'Variables used across seperate subroutines
Dim DataPoint As Integer
Dim StartTime As Currency

'Initialize form and turn timer off
Private Sub Form_Load()
    Set PM = New PhidgetManager     'Get the phidget manager going
    Timer1 = False
    lblStatus.Caption = "Phidget Not Attached"
    DisplayProperties False
    
End Sub

'PHIDGET ATTACHING
Private Sub PM_OnAttach(ByVal PHIDGET As PHIDGET.IPhidget)
'Phidget Manager provides info on all Phidget devices attached

If PHIDGET.DeviceType <> "PhidgetInterfaceKit" Then Exit Sub
            'Ignore all Phidget devices except the first Interface kit connected
            'Use the IFKitAttached as a flag that an InterfaceKit has been recognized
        
        If IFKitAttached = False Then
            'Assign the generic Phidget information to the IFKit object
            Set IFKit = PHIDGET
            IFKitAttached = True
            DisplayProperties True
            lblStatus.Caption = "Ready"
      
        Else
            'If more Phidget's attach ignore them by exiting this routine
            Exit Sub
        
        End If

End Sub

'SERVO DETACHING: Clean up.
Private Sub PM_OnDetach(ByVal PHIDGET As PHIDGET.IPhidget)
    
    If PHIDGET.DeviceType = "PhidgetInterfaceKit" Then
            'Check that the PhidgetInterface Kit is the one we are using and not
            ' a Phidget that we ignored
        If IFKit.SerialNumber = PHIDGET.SerialNumber Then
            Close #1                    'Ensures the sample file isn't left open
            Timer1 = False              'Turns Timer events off
            IFKitAttached = False
            DisplayProperties False
            lblStatus.Caption = "Phidget Not Attached"
           
        Else
            Exit Sub
           
        End If
    End If
End Sub

Private Sub Command_Start_Click()
        
    Timer1.Interval = 10 * txtTimeInterval.Text
    'Create a file and create the header line
    Open txtFileName.Text For Output As #1
    Print #1, Timer1.Interval & " msec.,Time sec.,A1,A2,A3,A4,A5,A6,A7,A8,I1,I2,I3,I4,I5,I6,I7,I8"
        
    'Record the precise time that the Timer started at
    QueryPerformanceCounter StartTime
    lblStatus.Caption = "Taking Sample"
    lblStartTime.Caption = Time
    Timer1 = True

End Sub

Private Sub Command_Stop_Click()

    'To interrupt a sample, close file and stop Timer
    Timer1 = False
    Close #1
    lblStatus.Caption = "Sample Stopped"
    lblEndTime.Caption = Time
    DataPoint = 0
    
End Sub

'The Timer event occurs everytime the set interval has elapsed
Private Sub Timer1_Timer()
    Dim i As Long
    Dim Datastring As String
    Dim EndTime As Currency
    Dim Freq As Currency
    
    'Show how many datapoints have been collected
    lblTime.Caption = DataPoint
    
    'The total number of data points to collect is entered on the form as
    '# of Datapoints
    If DataPoint >= txtNumDataPoints.Text Then
        Timer1 = False
        Close #1
        lblStatus.Caption = "Sample Taken"
        lblEndTime.Caption = Time
        DataPoint = 0
        Exit Sub
    End If
    
    'QueryPerforamnce routine uses a frequency counter to calculate time the
    'counter needs to be divided by the actual frequency
    QueryPerformanceCounter EndTime
    QueryPerformanceFrequency Freq
    
    'Writing to the file uses the DataString
    'All inputs are added to the DataString using these two loops
    Datastring = DataPoint & "," & ((EndTime - StartTime) / Freq)
    For i = 1 To IFKit.NumSensors
        Datastring = Datastring & "," & IFKit.SensorRawValue(i)
    Next i
    For i = 1 To IFKit.NumInputs
        Datastring = Datastring & "," & CInt(IFKit.InputState(i))
    Next i
            
    Print #1, Datastring
    DataPoint = DataPoint + 1

End Sub

Private Sub DisplayProperties(State As Boolean)
    
    'List all the info from the Interface Kit in a simple label
    Dim i As Integer
    lblProperties.Caption = ""
    If State = False Then
        Display "Detached"
        Exit Sub
    End If
    
    Display "DeviceType:        " & IFKit.DeviceType
    Display "SerialNumber:      " & IFKit.SerialNumber
    Display "IsAttached:          " & IFKit.IsAttached
    Display "NumInputs:          " & IFKit.NumInputs
    Display "NumOutputs:        " & IFKit.NumOutputs
    Display "NumSensors:        " & IFKit.NumSensors
 
End Sub

Private Sub Display(Line As String)
    lblProperties.Caption = lblProperties.Caption & Line & vbCrLf
End Sub

