' ===============================================================================
'
'   File....... dc16_test.bsx
'   Purpose.... DEFCON 16: Infrared (IR) System Level Test Unit
'   Author..... Kingpin (Joe Grand), http://www.kingpinempire.com
'
' {$STAMP BS2sx}
'   {$PBASIC 2.5}
'
' ===============================================================================


' -----[ Program Description ]---------------------------------------------

' This program is for the test unit for the DEFCON 16 Badge. Its sole
' purpose is to verify the IR transmission portion of the badge by receiving
' the standard Sony IR "Power Off" code, which is sent by the badge on
' power up. If no IR code is received, the RED LED will turn on. If a
' valid code is received, the test is good, and the GREEN LED will turn on.
'
' This code is a slight modification of IR_SCAN12.BSX IR Remote Scanner/
' Reporter written by Jon Williams for Nuts & Volts Column #76 (2001):
'
'    This program monitors an IR detector module and decodes the 12-bit Sony
'    IR protocol (SIRCS). When a key is detected, its 12-bit code is
'    displayed on the DEBUG screen and separated into device and command
'    codes.
'
'    No key pressed is indicated by code $FFF.
'
'    Change the KeyDelay value to change the auto-repeat response.  The
'    larger this value, the longer the delay repeats of the same key.
'


' -----[ I/O Definitions ]-------------------------------------------------

IR_pin      PIN      7      ' input from IR receiver module (38kHz)
LED_red     PIN      14
LED_green   PIN      15


' -----[ Constants ]-------------------------------------------------------

IsLow     CON 0
IsHigh    CON 1

NoKey     CON $FFF  ' no IR key
KeyDelay  CON 5     ' loops for "new" key ( >0 )

StartWidth    CON 2700  ' width of IR start bit (BS2sx)
Bit0Width     CON 750   ' width of IR zero bit (BS2sx)
Bit1Width     CON 1500  ' width of IR one bit (BS2sx)

BitTest   CON Bit0Width * 3 / 2 ' test width -- look for 1's


' -----[ IR Codes ]-------------------------------------------------------
'
' Generic Sony IR remote codes (not a complete list)
'
IR_1      CON $080
IR_2      CON $081
IR_3      CON $082
IR_4      CON $083
IR_5      CON $084
IR_6      CON $085
IR_7      CON $086
IR_8      CON $087
IR_9      CON $088
IR_0      CON $089
IR_Enter  CON $08B

IR_ChUp   CON $090
IR_ChDn   CON $091
IR_VolUp  CON $092
IR_VolDn  CON $093
IR_Mute   CON $094
IR_Power  CON $095


' -----[ Variables ]-------------------------------------------------------

irCode    VAR Word        ' returned code
lastCode  VAR Word        ' last returned code
irStart   VAR Word        ' width or IR start bit
irBit     VAR irStart     ' width of IR bit
keyRpts   VAR Byte        ' repeats of current key

device    VAR Byte        ' upper 5 bits of irCode
command   VAR Byte        ' lower 7 bits of irCode


' -----[ Initialization ]--------------------------------------------------

Intialize:
  PAUSE 500
  DEBUG "DEFCON 16 Badge IR Transmission Test", CR, CR
  DEBUG "Raw  Device  Command  Rpts", CR
  DEBUG "---  ------  -------  ----", CR

  HIGH LED_red
  LOW LED_green

' -----[ Main Code ]------------------------------------------------------
'
Main:
  GOSUB Scan_IR    ' check for IR key
  IF (irCode = lastCode) THEN Key_Timer ' key is being held
  keyRpts = 0     ' not held, reset timer
  GOTO Show_Key

Key_Timer:
  IF (irCode = NoKey) THEN Show_Key    ' no key, skip timer
  keyRpts = keyRpts + 1 // KeyDelay    ' update the repeats timer

Show_Key:
  lastCode = irCode    ' save last key

  device = irCode >> 7     ' extract device code
  command = irCode & $7F   ' extract command

  DEBUG HOME, LF, LF, LF, LF
  DEBUG HEX3 irCode,"   "
  DEBUG BIN5 device,"  ", BIN7 command, "   "
  DEBUG DEC2 keyRpts, CR
  DEBUG "         ", HEX2 device, "       ", HEX2 command

  IF (irCode = IR_Power) THEN  ' if we've received the valid IR code from the badge
    LOW LED_red  ' then turn on the green LED
    HIGH LED_green
    PAUSE 1500 ' delay to make sure the green LED is recognized
  ELSE   ' otherwise, we've received some other code
    HIGH LED_red
    LOW LED_green
  ENDIF

Loop_Pad:
  PAUSE 50     ' pad loop timing
  GOTO Main

END


' -----[ Subroutines ]---------------------------------------------------
'
' Receive and decode Sony IR command
'
Scan_IR:
  irCode = NoKey   ' flag value

Wait_For_Start:    ' wait for start bit
  PULSIN IR_pin,IsLow,irStart
  BRANCH irStart,[IR_Exit]   ' exit if no key down
  BRANCH irStart/StartWidth,[Wait_For_Start]

  ' This code MUST stay inline
  ' -- will NOT work in a loop
  PULSIN IR_pin,IsLow,irBit   ' decode 12 bits
  irCode.BIT0 = irBit/BitTest
  PULSIN IR_pin,IsLow,irBit
  irCode.BIT1 = irBit/BitTest
  PULSIN IR_pin,IsLow,irBit
  irCode.BIT2 = irBit/BitTest
  PULSIN IR_pin,IsLow,irBit
  irCode.BIT3 = irBit/BitTest
  PULSIN IR_pin,IsLow,irBit
  irCode.BIT4 = irBit/BitTest
  PULSIN IR_pin,IsLow,irBit
  irCode.BIT5 = irBit/BitTest
  PULSIN IR_pin,IsLow,irBit
  irCode.BIT6 = irBit/BitTest
  PULSIN IR_pin,IsLow,irBit
  irCode.BIT7 = irBit/BitTest
  PULSIN IR_pin,IsLow,irBit
  irCode.BIT8 = irBit/BitTest
  PULSIN IR_pin,IsLow,irBit
  irCode.BIT9 = irBit/BitTest
  PULSIN IR_pin,IsLow,irBit
  irCode.BIT10 = irBit/BitTest
  PULSIN IR_pin,IsLow,irBit
  irCode.BIT11 = irBit/BitTest

IR_Exit:
  RETURN
