#ifdef LOCAL
#define EXTERN
#else
#define EXTERN extern
#endif
void save_az_rotor(void);

/*
Do not change the definition of TRUE and False. Bit fields use them and can
not hold more than 1 bit of information - 0 or 1 value.
*/
#define TRUE 1
#define FALSE 0
enum limits { CW=1, CCW };

enum sub_mode { INITIAL=0, WAIT_FOR_TO, DRIVE_TO_CW_STOP, DRIVE_TO_CCW_STOP,
                CW_LIMIT, WAIT_FOR_CW_LIMIT, WAIT_FOR_CCW_LIMIT,FIND_TPP,
                CCW_LIMIT, FINISHED };

enum detect_submodes { START, MOVING_CW, MOVING_CCW, WAITING, HOLD_DISPLAY, DONE };

EXTERN  enum { PWR_CW=1, PWR_CCW, PWR_OFF } ctl;

struct rotor {
    unsigned char   type;           //'P' if pulse type rotor detected else zero
    unsigned char   pot_voltage;    // voltage from rotor if type 'R'
    unsigned char   pulses_per_rev; // pulses per 360 deg; of rotation
    unsigned char   pwr_off;        // power off mask
    unsigned char   pwr_cw;         // power clockwise mask
    unsigned char   pwr_ccw;        // power counter clockwise mask
    unsigned char   pulse_count;    //CW count relative to CCW physical limit
    unsigned int   tics_per_pulse;  // # of inerrupts (5ms) between rotor pulses
    unsigned int   tics_per_degree; //# of tics in a degree of rotation
    unsigned int   tic_count;       //# of tics since last pulse
    unsigned int   wait;            // wait for # of tics before continuing.
    enum    sub_mode cal_submode;   // sub modes for calibration process
    enum    detect_submodes detect_submode; // sub modes for detect process
    void  (*pwr_ctl) (struct rotor *, char); // pointer to power control function
    int             bearing;        // current bearing of the rotor
    int             auto_bearing;   // auto bearing - desired rotor bearing
    unsigned char   calibrated;     // 'C' if the rotor has been calibrated
    unsigned char   deg_update;     // # of degrees past the last rotor pulse
    unsigned int    pulse_timer;        // timeout indicator for rotor pulses
    unsigned int    deg_per_pulse:4;//# of degrees between each rotor pulse
    unsigned int    new_pulse:1;    // TRUE if a new rotor pulse is detected
    unsigned int    cw:1;           //TRUE if rotor last moved in a cw direction
    unsigned int    ccw:1;          //TRUE if last moved in a CCW direction
    unsigned int    moving:1;       // TRUE if rotor is powered (either dir.)
    unsigned int    first_pass:1;   // 1st pass in auto_track mode?
    enum limits     limit:2;        // rotor direction when physical limit hit
};

enum modes { INITIALIZE, MANUAL, AUTO, CALIBRATE, DETECT, IDLE };
EXTERN enum modes mode;
EXTERN enum modes old_mode;
EXTERN enum modes  hold_for_mode;



//#define PULSE_TIMEOUT 625
#define PULSE_TIMEOUT 800
#define LIMIT_HOLDOFF 650

/******************************
 0 to a port pin turns power on.
 1 toa port pin turns power off.

AND with PORTB to turn power on.
OR with portB to turn power off.
******************************/
#define AZ_PWR_OFF_MASK  0x03
#define AZ_PWR_CW_MASK   0xfe
#define AZ_PWR_CCW_MASK  0xfd

#define EL_PWR_OFF_MASK  0x0c
#define EL_PWR_CW_MASK   0xfb
#define EL_PWR_CCW_MASK  0xf7

EXTERN struct rotor az_rotor;
EXTERN struct rotor el_rotor;
EXTERN EEMEM struct rotor ee_az_rotor;
EXTERN EEMEM struct rotor ee_el_rotor;
EXTERN unsigned char request_lcd_refresh;

