/* File: robotics8d.h
   Header file for Version 5 of basic controller routines for Version 8
   of the UE controller board.  Corrected ReadInputPort to return 1 rather
   than non-zero on close.

   Keil C headers are not guarded.  If robotics library is used, its header
   file should be the only included file unless KEIL_C_H is defined first.
*/

#ifndef ROBOTICS_H
#define ROBOTICS_H

#ifndef KEIL_C_H
#define KEIL_C_H
#include<reg51f.h>
#include<absacc.h>
#endif

/*-------------------------------------------------------------------------
GLOBAL CONSTANTS AND STATIC VARIABLES
---------------------------------------------------------------------------*/
/* General constants */
#define BITMASK 0x01

/* Controller indexes */
#define MOTOR_IDX 0x6000
#define ATOD_IDX 0xC000
#define IO_IDX 0xA000

/* Motor constants */
#define ALLMOTORSOFF 0x00  
#define MAXMOTORID 2       /* Max ID for motor */
#define MOTOROFF 0x03      /* Inverse for shifting */
#define MOTORLEFT 0x01
#define MOTORRIGHT 0x02 

/* I/O constants */
#define LOW 0
#define HIGH 1

/* Sound constants */
#define OFF 0
#define ON 1

/* State variables */
extern unsigned char motors;      /* Current state of motor port */
extern unsigned char outputs;     /* Current state of output ports  */
extern unsigned int tenths;       /* Counter for clock in tenths of second */

/* ------------------------------------------------------------------------
INITIALIZE ROUTINE.  Initializes all of the parts of the controller board.
Must be called before any other routines.
---------------------------------------------------------------------------*/
extern void Initialize(void);

/* ------------------------------------------------------------------------
BEACON ROUTINES
Returns 0 (slowest) to 3 (fastest) for beacons oscillating at different 
frequencies.
---------------------------------------------------------------------------*/
unsigned char BeaconType (void);

/* ------------------------------------------------------------------------
DELAY ROUTINE.  With count = 1 this routine provides a delay of 100 msec.
This timer routine blocks.
---------------------------------------------------------------------------*/
extern void Delay(unsigned char count);

/* ------------------------------------------------------------------------
MOTOR ROUTINES 
Must call Initialize before using.
Motors indexed 1-2.
Speed is: -255 to 255, however, |speed| needs to be > 190 to be effective
for most vehicle designs.
Direction is based on sign of speed:
                   < 0 for left/reverse
                   = 0 for stop
                   > 0 for right/forward
---------------------------------------------------------------------------*/
extern void AllStop(void);
extern void SetMotor(unsigned char motorID, int speed);

/* ------------------------------------------------------------------------
I/O PORT ROUTINES 
Input ports indexed 0-7.  (Left to right facing connector.)
Returns 1 when closed, 0 when open
Ouput ports indexed 0-7.  (Left to right facing connector.)
Direction is 0 (LOW) or 1 (HIGH)
Initialize sets to all HIGH
---------------------------------------------------------------------------*/
extern unsigned char ReadInputPort (unsigned char num);
extern void SetOutputPort (unsigned char num, unsigned char direction);

/* ------------------------------------------------------------------------
A TO D CONVERTER ROUTINE
Channels indexed 0 to 7
ScanAnalog scans all eight channels of the A to D converter and sends them 
to the serial port.  Use it with HyperTerm with the following setup:                                           
19.2 kilobits per second, 1 stop bit, no parity, and no flow control                                                       
---------------------------------------------------------------------------*/
extern unsigned char GetAtoD (unsigned char channel);
extern void ScanAnalog();

/* ------------------------------------------------------------------------
SOUND ROUTINES
Sound turns buzzer ON (1) and OFF (0).
Beep oscillates buzzer for duration cycles.
---------------------------------------------------------------------------*/
extern void Sound(unsigned char state);
extern void Beep(unsigned char duration);

#endif  /* ROBOTICS_H */
