Attribute VB_Name = "BX24_byork"

''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'    TAB Electronics Build Your Own Robot Kit BX24 Interface Module
'        by Dr. J (Juliano@csuChico.edu)
'        California State University, Chico
'        Intelligent Systems Lab
'        http://isl.ecst.csuchico.edu
'
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

Option Explicit

' This module is used to control a TAB Electronics Build Your Own Robot Kit 
' robot with a NetMedia BasicX-24 microcontroller.  The code in this module
' is based on the BS2 programming template by Myke Predko to facilitate any
' BS2 to BX24 code migration.

' Predefined Robot Commands

Public  Const RobotStop     As Byte =  0  '  Stop the Robot
Public  Const Behavior1     As Byte =  1  '  Random Movement
Public  Const Behavior2     As Byte =  2  '  Photovore
Public  Const Behavior3     As Byte =  3  '  Photophobe
Public  Const Behavior4     As Byte =  4  '  Wall Hugger/Maze Solver
Public  Const RobotForward  As Byte =  5  '  Move Forward for 200 msecs
Public  Const RobotReverse  As Byte =  6  '  Move Reverse for 200 msecs
Public  Const RobotLeft     As Byte =  7  '  Turn Left for 200 msecs
Public  Const RobotRight    As Byte =  8  '  Turn Right for 200 msecs
Public  Const RobotLEDOn    As Byte =  9  '  Turn on the Robot's LED
Public  Const RobotLEDOff   As Byte = 10  '  Turn off the Robot's LED
Public  Const RobotPWM0     As Byte = 11  '  PWM = 0% Duty Cycle
Public  Const RobotPWM1     As Byte = 12  '  PWM = 1st "Notch"
Public  Const RobotPWM2     As Byte = 13  '  PWM = 2nd "Notch"
Public  Const RobotPWM3     As Byte = 14  '  PWM = 3rd "Notch"
Public  Const RobotPWM4     As Byte = 15  '  PWM = 100% Duty Cycle
Public  Const RobotPWM      As Byte = 16  '  Return the Current PWM Value
Public  Const RobotState    As Byte = 17  '  Return the Executing State
Public  Const RobotWhiskers As Byte = 18  '  Return State of the "Whiskers"
					  '   Bit 0 - Left "Whisker"
					  '   Bit 1 - Right "Whisker"
Public  Const RobotCDSL     As Byte = 19  '  Return Value of Left CDS Cell
Public  Const RobotCDSR     As Byte = 20  '  Return Value of Right CDS Cell
Public  Const RobotButton   As Byte = 21  '  Return the Last Remote Button Press
					  '   0 - No Buttons Pressed
					  '   1 - Leftmost Button Pressed
					  '   2 - Middle Button Pressed
					  '   3 - Rightmost Button Pressed
					  '  After "RobotButton" Operation,  
					  '   Button Save is Cleared
'  Robot Serial Interface Pins

Public  Const SC As Byte = 14		  '  Default BS2 I/O Pins
Public  Const SD As Byte = 15

'  BX-24 constants (for system calls)

Private Const NumberOfBits  As Byte =  8  '  for ShiftIn() and ShiftOut()

'  BX-24 private serial interface variables

Private ClockPin As Byte
Private Data_Pin As Byte

''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

Public Sub SetSerialPins( _
    ByVal SC As Byte, _
    ByVal SD As Byte    )

' Sets the robot serial interface pin numbers

    ClockPin = SC
    Data_Pin = SD

End Sub

''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

Public Sub GetSerialPins( _
    ByRef SC As Byte, _
    ByRef SD As Byte    )

' Gets the robot serial interface pin numbers

    SC = ClockPin
    SD = Data_Pin

End Sub

''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

Public Sub InitSerialPins()

' Sets the robot interface pins as output and high
'   Note: Called right before any application code

    Call PutPin(ClockPin, bxOutputHigh)
    Call PutPin(Data_Pin, bxOutputHigh)

End Sub

''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

Public Sub RobotSend( ByVal DataByte As Byte )

' Sends the byte in "DataByte" to the robot where
'   "Data_Pin" = I/O pin connected to synchronous serial device's data input
'   "ClockPin" = I/O pin connected to synchronous serial device's clock input

'   Hold ClockPin low for 1 msec before shifting in data
    Call PutPin(ClockPin, bxOutputLow)
    Call Sleep(0.001)

'   Shift out data bits to the robot LSB-first ...
'        Note: ShiftOut is MSB-first; hence, the need for FlipBits()
    Call ShiftOut(Data_Pin, ClockPin, NumberOfBits, FlipBits(DataByte))
    Call PutPin(ClockPin, bxOutputHigh)

End Sub

''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

Public Sub RobotSendReceive( ByRef DataByte As Byte )

' Sends the byte in "DataByte" to the robot, using it for return value, where
'   "Data_Pin" = I/O pin connected to synchronous serial device's data input
'   "ClockPin" = I/O pin connected to synchronous serial device's clock input

'   Hold ClockPin low for 1 msec before shifting in data
    Call PutPin(ClockPin, bxOutputLow)
    Call Sleep(0.001)

'   Shift out data bits to the robot LSB-first and wait for completion ...
'        Note: ShiftOut is MSB-first; hence, the need for FlipBits()
    Call ShiftOut(Data_Pin, ClockPin, NumberOfBits, FlipBits(DataByte))
    Call Sleep(0.001)

'   Read in result/status of requested operation
'        Note: ShiftIn returns MSB-first; hence, the need for FlipBits()
    DataByte = FlipBits(ShiftIn(Data_Pin, ClockPin, NumberOfBits))
    Call PutPin(ClockPin, bxOutputHigh)

End Sub

''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
