' -----[ Title ]-----------------------------------------------------------
' IR Remote for the Boe-Bot - 7BitRemoteBoeBot.bs2

' With an IR remote configured to control a SONY TV, point the remote at
' the Boe-Bot and press and hold the 1-9 keys for different maneuvers.
' You can also use CH+/- and VOL+/-..

' {$STAMP BS2}
' {$PBASIC 2.5}

' -----[ I/O Definitions ]-------------------------------------------------

' SONY TV IR remote declaration - input receives from IR detector

IrDet          PIN     9

' -----[ Constants ]-------------------------------------------------------

' SONY TV IR remote constants for non-keypad buttons

Enter          CON     11
ChUp           CON     16
ChDn           CON     17
VolUp          CON     18
VolDn          CON     19
Power          CON     21

' -----[ Variables ]-------------------------------------------------------

' SONY TV IR remote variables

irPulse        VAR     Word
remoteCode     VAR     Byte

' -----[ Initialization ]--------------------------------------------------

DEBUG "Press and hold a key (1-9 or CH/VOL)..."
FREQOUT 4, 2000, 3000                        ' Start/reset indicator.

' -----[ Main Routine ]----------------------------------------------------

' Boe-Bot button control routine.

DO

  ' Call subroutine that loads the IR message value into the
  ' remoteCode variable.

  GOSUB Get_Ir_Remote_Code

  ' Send PULSOUT durations for the various maneuvers based on
  ' the value of the remoteCode variable.

  SELECT remoteCode
    CASE 2, ChUp                             ' Forward
      PULSOUT 13, 850
      PULSOUT 12, 650
    CASE 4, VolDn                            ' Rotate Left
      PULSOUT 13, 650
      PULSOUT 12, 650
    CASE 6, VolUp                            ' Rotate Right
      PULSOUT 13, 850
      PULSOUT 12, 850
    CASE 8, ChDn                             ' Backward
      PULSOUT 13, 650
      PULSOUT 12, 850
    CASE 1                                   ' Pivot Fwd-left
      PULSOUT 13, 750
      PULSOUT 12, 650
    CASE 3                                   ' Pivot Fwd-right
      PULSOUT 13, 850
      PULSOUT 12, 750
    CASE 7                                   ' Pivot Back-left
      PULSOUT 13, 750
      PULSOUT 12, 850
    CASE 9                                   ' Pivot Back-right
      PULSOUT 13, 650
      PULSOUT 12, 750
    CASE ELSE                                ' Hold Position
      PULSOUT 13, 750
      PULSOUT 12, 750
  ENDSELECT

LOOP

' -----[ Subroutine - Get_Ir_Remote_Code ]---------------------------------

' SONY TV IR remote subroutine loads the remote code into the
' remoteCode variable.

Get_Ir_Remote_Code:

  remoteCode = 0                             ' Clear all bits in remoteCode

  ' Wait for resting state between messages to end.

  DO
    RCTIME IrDet, 1, irPulse
  LOOP UNTIL irPulse > 1000

  ' Measure start pulse.  If out of range, then retry at Get_Ir_Remote_Code.

  RCTIME IrDet, 0, irPulse
  IF irPulse > 1125 OR irPulse < 675 THEN GOTO Get_Ir_Remote_Code

  ' Get data bit pulses.

  RCTIME IrDet, 0, irPulse                   ' Measure pulse
  IF irPulse > 300 THEN remoteCode.BIT0 = 1  ' Set (or leave clear) bit-0
  RCTIME IrDet, 0, irPulse                   ' Measure next pulse
  IF irPulse > 300 THEN remoteCode.BIT1 = 1  ' Set (or leave clear) bit-1
  RCTIME IrDet, 0, irPulse                   ' etc
  IF irPulse > 300 THEN remoteCode.BIT2 = 1
  RCTIME IrDet, 0, irPulse
  IF irPulse > 300 THEN remoteCode.BIT3 = 1
  RCTIME IrDet, 0, irPulse
  IF irPulse > 300 THEN remoteCode.BIT4 = 1
  RCTIME IrDet, 0, irPulse
  IF irPulse > 300 THEN remoteCode.BIT5 = 1
  RCTIME IrDet, 0, irPulse
  IF irPulse > 300 THEN remoteCode.BIT6 = 1

  ' Adjust remoteCode so that keypad keys correspond to the value
  ' it stores.

  IF (remoteCode < 10) THEN remoteCode = remoteCode + 1
  IF (remoteCode = 10) THEN remoteCode = 0

  RETURN