' Declare some variables that will make the program easier to read

Dim isON, isOFF, Red, Black As Long



Private Sub Form_Load()

' Initialize the module's variables

isON = 3720: isOFF = 3960: Red = RGB(255, 0, 0): Black = RGB(0, 0, 0)

' Put SoftButtons in their default position

For i% = 0 To 7: SoftButton(i%).Top = isOFF: Next i% ' All Soft Buttons Off

SoftButton(1).Top = isON: SoftButton(4).Top = isON  ' Pattern used on Front Cover!

SelectFunction.ListIndex = 0 ' Select "A only"

RealLEDValue.Visible = False ' Turn off debug window

End Sub



Sub SoftButton_Click(index%)

' Toggle the SoftButton on a click

If SoftButton(index%).Top = isON Then

    SoftButton(index%).Top = isOFF

    Else: SoftButton(index%).Top = isON

    End If

End Sub



Private Sub Timer1_Timer()

' Check for button changes every 200msec

' NOTE: only flip the "RealButtons" and "LED(i%)" if necessary, otherwise there is flicker

Dim RealButtons As Byte: Dim RealLEDS As Byte: Dim LEDon As Boolean

' What is the value of the Real Buttons

Call ReadUSBdevice(AddressFor(RealButtons), 1)

For i% = 0 To 7

    If ((RealButtons And 1) = 1) And (RealButton(i%).Top = isOFF) Then RealButton(i%).Top = isON

    If ((RealButtons And 1) = 0) And (RealButton(i%).Top = isON) Then RealButton(i%).Top = isOFF

    RealButtons = ((RealButtons And &HFE) / 2) ' Clear Bit 0 before divide

    Next i%

' Now update the LEDs, first the Display LEDs then the Real LEDs

RealLEDS = 0

For i% = 0 To 7

    LEDon = False ' Let's assume

    RealLEDS = RealLEDS * 2

    Select Case SelectFunction.ListIndex

    Case 0: If SoftButton(i%).Top = isON Then LEDon = True  ' A only

    Case 1: If ((SoftButton(i%).Top = isON) Or (RealButton(i%).Top = isON)) Then LEDon = True ' A OR B

    Case 2: If ((SoftButton(i%).Top = isON) And (RealButton(i%).Top = isON)) Then LEDon = True ' A AND B

    Case 3: If ((SoftButton(i%).Top = isON) Xor (RealButton(i%).Top = isON)) Then LEDon = True ' A XOR B

    Case 4: If RealButton(i%).Top = isON Then LEDon = True ' B only

    End Select

    If LEDon Then ' Turn it on IFF it's not already on

        If LED1(i%).FillColor = Black Then LED1(i%).FillColor = Red

        RealLEDS = RealLEDS + 1

    Else: If LED1(i%).FillColor = Red Then LED1(i%).FillColor = Black

    End If

    Next i%

    RealLEDValue.Text = TwoHexCharacters$(RealLEDS)

End Sub