Imports Microsoft.Kinect
Imports System.Threading
Imports System.Drawing



Module modIF_Kinect
    Public boolKinectDriversInstalled As Boolean = True
    Dim boolKinectInitializationInProgress As Boolean = False

    Public Function ftnKinectInitializeTop() As Boolean
        Try ' Look through all sensors and start the first connected one.
            ftnKinectInitializeTop = False
            If boolKinectDriversInstalled Then  ' Not elegant
                If Not boolKinectInitializationInProgress And Not boolShutDown Then '
                    boolKinectInitializationInProgress = True
                    subConnectKinect()
                    boolKinectConnected = ftnKinectRun()
                    If boolKinectConnected Then
                        subEnableDepthStream()
                        sngScanFOV = 58.5
                        subRecalculateVariables()
                        subCalculateScanBeamAngles()
                        subCalculateScanCalCoefficients()
                        intScanVectorsMaxRangeInches = 177
                        intScanVectorsMinRangeInches = 20
                    End If
                    If boolDebugKinect Then
                        frmDebug.subWriteLogs(Now() & " ====================ftnKinectInitializeTop==================================")
                        frmDebug.subWriteLogs(" ftnKinectInitializeTop post Run: Kinect Sensor Status: " & K_Sensor.Status.ToString)
                        frmDebug.subWriteLogs(" ftnKinectInitializeTop End: Kinect Sensor Status: " & K_Sensor.Status.ToString)
                    End If
                    boolKinectInitializationInProgress = False
                End If
            End If
            ftnKinectInitializeTop = boolKinectConnected
        Catch ex As Exception
            boolKinectDriversInstalled = False
            frmDebug.subWriteLogs(" EXCEPTION in ftnKinectInitializeTop :  " & ex.ToString)
        End Try
    End Function

    Public Sub subConnectKinect()
        Try
            If boolKinectDriversInstalled Then  ' Not elegant
                ftnKinectStop()
                For Each potentialSensor In KinectSensor.KinectSensors
                    If potentialSensor.Status = KinectStatus.Connected Then
                        K_Sensor = potentialSensor
                        Exit For
                    Else
                        K_Sensor = Nothing
                    End If
                Next potentialSensor
                ftnKinectWaitInitializing()
                If (K_Sensor IsNot Nothing) Then
                    If boolDebugKinect Then frmDebug.subWriteLogs(" subConnectKinect: Kinect Sensor Status: " & K_Sensor.Status.ToString)
                    boolKinectConnected = (K_Sensor.Status = KinectStatus.Connected)
                    If boolKinectConnected Then
                        If boolDebugKinect Then frmDebug.subWriteLogs(" subConnectKinect: Kinect connected")
                    Else
                        If boolDebugKinect Then frmDebug.subWriteLogs(" subConnectKinect: Kinect Sensor Status: " & K_Sensor.Status.ToString)
                    End If
                Else
                    boolKinectConnected = False
                    If boolDebugKinect Then frmDebug.subWriteLogs(" subConnectKinect: Kinect Sensor not detected. ")
                End If
            End If
        Catch ex As Exception
            frmDebug.subWriteLogs(" EXCEPTION in subConnectKinect :  " & ex.ToString)
            boolKinectDriversInstalled = False
        End Try
    End Sub

    Public Function ftnKinectStop() As Boolean
        Try
            ftnKinectStop = True
            If boolKinectDriversInstalled Then  ' Not elegant
                If (K_Sensor IsNot Nothing) Then
                    ftnKinectStop = Not K_Sensor.IsRunning
                    If (K_Sensor.IsRunning) Then
                        K_Sensor.Stop()
                    End If
                End If
            End If
        Catch ex As Exception
            frmDebug.subWriteLogs(" EXCEPTION in ftnKinectStop :  " & ex.ToString)
        End Try
    End Function


    Function ftnKinectWaitInitializing() As Boolean
        Try
            If boolKinectDriversInstalled Then  ' Not elegant
                ftnKinectWaitInitializing = False
                If K_Sensor IsNot Nothing Then
                    If boolDebugKinect Then frmDebug.subWriteLogs(" ftnKinectWaitInitializing: Start!")
                    Dim timeOut As DateTime = Now().AddMilliseconds(1000)
                    Do Until (K_Sensor.Status <> KinectStatus.Initializing) Or boolShutDown Or (Now() > timeOut)
                        Thread.Sleep(50)
                    Loop
                    ftnKinectWaitInitializing = (K_Sensor.Status <> KinectStatus.Initializing)
                End If
                If boolDebugKinect Then frmDebug.subWriteLogs(" ftnKinectWaitInitializing: Kinect Sensor Status: " & K_Sensor.Status.ToString)
            End If
        Catch ex As Exception
            frmDebug.subWriteLogs(" EXCEPTION in ftnKinectWaitInitializing :  " & ex.ToString)
        End Try
    End Function


    Public Function ftnKinectRun() As Boolean
        Try
            ftnKinectRun = False
            If boolDebugKinect Then frmDebug.subWriteLogs(" ftnKinectRun Start: " & K_Sensor.Status.ToString)
            If boolKinectDriversInstalled Then  ' Not elegant
                If K_Sensor IsNot Nothing Then
                    K_Sensor.ForceInfraredEmitterOff = False
                    Dim timeOut As DateTime = Now().AddMilliseconds(1000)
                    Do Until K_Sensor.IsRunning Or boolShutDown Or (Now() > timeOut)
                        K_Sensor.Start()
                        Thread.Sleep(50)
                    Loop
                    If boolDebugKinect Then frmDebug.subWriteLogs(" ftnKinectRun End: " & K_Sensor.Status.ToString)
                    ftnKinectRun = K_Sensor.IsRunning
                End If
            End If
        Catch ex As Exception
            frmDebug.subWriteLogs(" EXCEPTION in ftnKinectRun :  " & ex.ToString)
        End Try
    End Function



    Sub subEnableDepthStream()
        Try
            If boolKinectDriversInstalled Then  ' Not elegant
                If (K_Sensor IsNot Nothing) Then
                    If boolDebugKinect Then frmDebug.subWriteLogs(" subEnableDepthStream Start ")
                    K_Sensor.ForceInfraredEmitterOff = False
                    K_Sensor.ColorStream.Disable()
                    K_Sensor.SkeletonStream.Disable()
                    K_Sensor.DepthStream.Enable(DepthImageFormat.Resolution640x480Fps30)
                    K_Sensor.DepthStream.Range = DepthRange.Default
                    K_Sensor.ElevationAngle() = intDefaultElevation
                End If
            End If
        Catch ex As Exception
            frmDebug.subWriteLogs(" EXCEPTION in subEnableDepthStream :  " & ex.ToString)
        End Try
    End Sub

 

End Module
