Attribute VB_Name = "IRcapture"
'-------------------------------------------------------------------------------
Option Explicit

' IRcapture Program
'
' This program will capture up to 16 IR pulses on the input capture pin.
' Pulse widths and spaces between pulses are sent out the serial port on
' Com1.
'
' Pulse widths are in units of microseconds.
'
' This program makes no assumptions about the IR encoding scheme, and can
' be used to view the formatting of an unknown IR source.
'
' Input capture pin number:
'
'    BX-01 => 31
'    BX-24 => 12
'    BX-35 => 20

Private Const MaxPulses As Integer = 32
Private PulseTrain(1 To MaxPulses) As New UnsignedInteger
Private Const OverflowValue As Long = 65535
'-------------------------------------------------------------------------------
Public Sub Main()

    Dim I As Integer
    
    Do
        Debug.Print
        Debug.Print "Ready"
        Debug.Print

        ' Capture IR pulses on the input capture pin.
        Call InputCapture(PulseTrain, MaxPulses, 0)
        
        For I = 1 To MaxPulses

            Debug.Print "Pulse("; CStr(I); ") = ";

            If (PulseTrain(I) = CuInt(OverflowValue)) Then
                Debug.Print "[overflow]";
            Else
                Call PutPulseWidth(PulseTrain(I))
                Debug.Print " us";
            End If

            Debug.Print
        Next

        Call Delay(1.0)

    Loop

End Sub
'-------------------------------------------------------------------------------
Private Sub PutPulseWidth( _
    ByVal Operand As UnsignedInteger)

' Converts the operand to units of microseconds and sends the result to the
' serial port.

    Const UnitConversion As Single = 135.63368E-9   ' => 1.0 / 7372800.0
    Dim TimeInterval As Single, iTimeInterval As Long

    ' Convert to seconds.
    TimeInterval = CSng(Operand) * UnitConversion

    ' Convert to integer microseconds.
    iTimeInterval = CLng(TimeInterval * 1.0E6)

    Debug.Print CStr(iTimeInterval);

End Sub
'-------------------------------------------------------------------------------
