Attribute VB_Name = "IRLights"
'-------------------------------------------------------------------------------
Option Explicit

Private Const InputBufferSize As Integer = 30
Private InputBuffer(1 To InputBufferSize) As Byte

Private Const OutputBufferSize As Integer = 10
Private OuputBuffer(1 To OutputBufferSize) As Byte
'-------------------------------------------------------------------------------
Sub Main()

' This program allows you to control both LEDs on a BX-24 in response to
' IR signals. The IR remote should be set to TV code 0032. An IR sensor
' should be connected to pin 13.
'
' You can toggle the green LED by pressing the Volume +/- key. The red
' LED can be similarly toggled by pressing the Channel +/- key.
        
    Dim IrData As Byte

    Call OpenIRchannel

    Do        
        'If data present in the serial queue then get it
        If StatusQueue(InputBuffer) Then
        
            Call GetQueue(InputBuffer, IrData, 1)

            Call Action(IrData)
        End If
            
    Loop

End Sub
'-------------------------------------------------------------------------------
Sub Action( _
    ByVal IrData As Byte)

' Turn on LEDs depending on the IR keypress.
    
    Const GreenLEDpin As Byte = 26
    Const RedLEDpin As Byte = 25
    
    Const LEDon As Byte = 0
    Const LEDoff As Byte = 1

    Const VolumeUp As Byte = 153
    Const VolumeDown As Byte = 157

    Const ChannelUp As Byte = 145
    Const ChannelDown As Byte = 149
        
    Select Case IrData
        Case VolumeUp
            Call PutPin(GreenLEDpin, LEDon)
        Case VolumeDown
            Call PutPin(GreenLEDpin, LEDoff)
        Case ChannelUp
            Call PutPin(RedLEDpin, LEDon)
        Case ChannelDown
            Call PutPin(RedLEDpin, LEDoff)
        Case Else
            ' null
    End Select

End Sub
'-------------------------------------------------------------------------------
Sub OpenIRchannel()

' Open the IR communications channel.

    ' Open input and output queues.
    Call OpenQueue(InputBuffer, InputBufferSize)
    Call OpenQueue(OuputBuffer, OutputBufferSize)

    ' Configure Com3 to receive on I/O pin 13. We're not interested in
    ' transmitting anything, so we'll use pin 0 for the transmit pin
    ' (pin 0 is a dummy pin).
    Call DefineCom3(13, 0, bx0000_1000) ' non-inverted, no parity, 8 bits.
    
    ' Open Com3 at 893 baud.
    Call OpenCom(3, 893, InputBuffer, OuputBuffer)
    
    Call Delay(0.5)

End Sub
'-------------------------------------------------------------------------------
