/* File: robotics6a.h
   Header file for Version 2 of basic controller routines for Version 6
   of the UE controller board 

   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 ROBOTICS6A_H
#define ROBOTICS6A_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 0x9000
#define ATOD_IDX 0x8000
#define PORT_A_IDX 0xa000
#define PORT_B_IDX 0xa100
#define PORT_C_IDX 0xa200
#define COM_PORT_IDX 0xa300

/* Motor constants */
#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 = 0x00;      /* Current state of motor port */
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-4.
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 
Must call InitializeIO before using. 
Input ports indexed 1-8, 17-24.  Corresponding to the outside 8 pins
on each side
Returns 1 when closed (switch closing causes 0, negated for positive result)
Ouput ports indexed 9-16.  Corresponding to the middle 8 pins
Direction is 0 (LOW) or 1 (HIGH)
---------------------------------------------------------------------------*/
extern void InitializeIO(void);
extern unsigned char ReadInputPort (unsigned char num);
extern void SetOutputPort (unsigned char num, unsigned char direction);
/* -------------------------------------------------------------------------
This routine is for reading Bit 1 or Bit 2 of Port 1
These inputs do not require a pull-up resistor, so will work with
the standard LEGO bump switches.
----------------------------------------------------------------------------*/
extern unsigned char ReadP1 (unsigned char num);

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