/* File: robotics7a.h
   Header file for Version 1a of basic controller routines for Version 7
   of the UE controller board  (corrected motor controller routines)

   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 ROBOTICS7A_H
#define ROBOTICS7A_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 LCD_CTRL_IDX 0xf000
#define LCD_DATA_IDX 0xf100
#define MOTOR_IDX 0x6000
#define ATOD_IDX 0xC000
#define IO_IDX 0xA000

/* Motor constants */
#define ALLMOTORSOFF 0x01  /* LSB set to 1 to leave IR sensors on */
#define MAXMOTORID 3       /* Max ID for motor; 4 is used for IR sensor power */
#define MOTOROFF 0x03      /* Inverse for shifting */
#define MOTORLEFT 0x01
#define MOTORRIGHT 0x02
#define PWM_ENABLE 0x42  

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

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

/* State variables */
static unsigned char motors = 0x01;      /* Current state of motor port */
                                         /* LSB set to 1 to turn on IR sensors */
static unsigned char outputs = 0xFF;     /* Current state of output ports  */

/* ------------------------------------------------------------------------
DELAY ROUTINE.  With count = 1 this routine provides a delay 
of about 200 usec.
---------------------------------------------------------------------------*/
extern void Delay(unsigned char count);

/* ------------------------------------------------------------------------
DISPLAY ROUTINES
Must call InitializeDisplay before using.  
Currently all three Write routines appends to the current position
Currently wraps around if more characters than available to end of display
Writing character '/' clears the display
---------------------------------------------------------------------------*/
extern void InitializeDisplay(void);
extern void WriteString (unsigned char *str);
extern void WriteChar (unsigned char ch);
extern void WriteInt (int number);

/* ------------------------------------------------------------------------
MOTOR ROUTINES 
Must call InitializeMotors before using.
Motors indexed 1-3.
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 InitializeMotors(void);
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 (switch closing causes 0, negated for positive result)
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 InitializeOutputPorts(void);
extern void SetOutputPort (unsigned char num, unsigned char direction);

/* ------------------------------------------------------------------------
A TO D CONVERTER ROUTINE
Channels indexed 0 to 7
---------------------------------------------------------------------------*/
extern unsigned char GetAtoD (unsigned char channel);

/* ------------------------------------------------------------------------
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  /* ROBOTICS6B_H */
