#ifndef SHUTTER_H
#define SHUTTER_H

/*
    The ticks per second is based on an 8-bit counter over flowing that
    is driven by a 4 Mhz ocsillator prescaled by 4 and postscaled by 64.
    
    TICKS_PER_SEC = (OSC_CLK) / (PRESCALE) / (POSTSCALE) / (8_BIT_OVERFLOW)
                  = 4000000   /     4      /      64     /      256
                  = 61.0351
*/
#define TICKS_PER_SEC   61

/*
    CLOSE_DELAY is the amount of time that close 
    routine waits for shutter to stop moving before 
    clearing motor position
*/
#define CLOSE_DELAY 200 /* milliseconds */

/*
    MOTION_START_DELAY is the amount of time that motion 
    routines wait for shutter to start moving before 
    checking various motion errors such as velocity.
*/
#define MOTION_START_DELAY  (TICKS_PER_SEC / 8)

/*
    MIN_MOTOR_VEL is the minimum motor velocity that
    the shutter must have in order to avoid getting a 
    velocity error.
*/
#define MIN_MOTOR_VEL   200 /* counts per second */

#define CLOSE   0
#define OPEN    1

#define OPEN_COUNTS_PARAM_0   0
#define OPEN_COUNTS_PARAM_1  (OPEN_COUNTS_PARAM_0 + 4)

/* counts per rev is the 246:1 gearhead  with a 16 cont encoder */
#define COUNTS_PER_STROKE  (246 * 16)
#define FULL_SCALE_MOVE 1000

/* relative counts give to move_motor by jog_shutter */
#define JOG_OPEN_COUNTS    100000
#define JOG_CLOSE_COUNTS   -100000

/* h-bridge I/O */
#define H_BRIDGE_0  PIN_C2
#define H_BRIDGE_1  PIN_C1
#define H_BRIDGE_2  PIN_A4
#define H_BRIDGE_3  PIN_A5

/* encoder I/O */
#define ENCODER_RD  PIN_E1
#define ENCODER_CLR PIN_E2

/* sensor I/O */
#define WATER_ALARM     PIN_B1
#define HALL_SENSOR1    PIN_B4
#define HALL_SENSOR2    PIN_B5

/* status bits */
#define WATER_ALARM_SET     0x01
#define HALL_SENSOR1_SET    0x02
#define HALL_SENSOR2_SET    0x04
#define SHUTTER_HOMED_SET   0x08
#define UNUSED_SET_4        0x10
#define UNUSED_SET_5        0x20
#define UNUSED_SET_6        0x40
#define UNUSED_SET_7        0x80

/* error codes */
#define NO_COMMAND_MATCH        0x01

#define PARAM_NOT_NUMBER        0x02
#define PARAM_TOO_MANY_CHARS    0x03
#define PARAM_REQUIRED          0x04
#define PARAM_OUT_OF_RANGE      0x05

#define MOVE_HALTED_RXCHAR      0x06
#define MOVE_HALTED_HALL_1      0x07
#define MOVE_HALTED_HALL_2      0x08
#define MOVE_HALTED_LOW_VEL     0x09

#define SHUTTER_NOT_HOMED       0x0A

#define OPEN_PARAM_CORRUPT      0x0B

#endif
