/******************************************************************************
 * Hydraulic/Vent prototypes for Spray2008 SPI control
 * CS assignment = CS_HYD is done in spi_cmd.h
 * created Dec2008
 * updated: jun15 for auto-degas 
 *****************************************************************************/



void        hyd_flash_menu    ( struct spi_param *ss );
short       init_hyd_iparam   ( void    );
void        display_hydflash  ( void    );
void        display_hyd_query ( short rstat, struct spi_param *ss );
void        display_hyd_pwr   ( struct spi_param *ss );
void        display_pump      ( struct spi_param *ss );
void        hyd_set_default   ( void    );

short       setup_pump        ( void    );
short       go_pump           ( short t, short oil   );
short       hyd_stop_pump     ( void    );
short       hyd_more_pump     ( short t );
short       watch_pump        ( short tmax, short take_dat );

short       get_hyd_ad        ( void );
short       last_hyd          ( void );
short       clear_hyd_pmp     ( void );
void        parse_last_hyd    ( struct spi_param *ss );

//--- vent prototypes ---------------------
short       check_vent        ( void );
short       get_lld           ( void );
short       run_vent          ( short ti, short *d1, short *d2 );

//--- auto-degas prototypes ---------------
short       adg_GetPumpInfo   ( short *, short *, short *, short *, short *, short *);
void        adg_printState    ( int state );
void        adg_PrintPacket   ( void      );
void        adg_vent          ( uchar , ushort, int, int  ); 
void        remote_adg_vent   ( void      ); 
uchar       adg_Loop          ( uchar , uchar , ushort ,  int , int  ) ;

void        adg_sbd_make      ( void      );
short       adg_sbd_send      ( void      );
void        lifo_adg_hdr      ( void      );
void        lifo_adg_dat1     ( short j0, short np,  short id, uchar *b );
void        lifo_adg_vent     ( short j0, short np  );
void        lifo_adg_oil      ( short j0, short nr  );
 
                     
//--- test, menus -------------------------
void        test_hyd          ( void );


//****   specific constants for the Hydraulilc SPI module *********************
//****   These values must agree with the compiled msp430 code !!!

// define the a/d channels

// flash addr locations in msp430
// !!!WARNING!!! THESE MUST AGREE WITH MSP430 VALUES !!!!!
//#define F_SER_NO       0    // serial # of this peripheral
//F_VERSION set at compile to msp430:NOT user-settable
//#define F_VERSION      2    // flash version
// assignment past the version is specific to each type of module
// these are the assignments for the hydraulic pump
// these are the assignments for the hydraulic pump
#define F_TMIN         4    // min time to run the hydraulic pump
#define F_TMAX         6    // max time to run the hydraulic pump
#define F_OIL_MAX      8    // max oil sensor a/d counts allowed
#define F_AMP_MAX     10    // max motor current, a/d counts
#define F_H_C2U_I     12    // convert pump current counts to ma
#define F_H_C2U_V     14    // convert pump voltage counts to V


// status of go_pump function
#define PUMP_WAIT     0  // pump is running...not done yet
#define PUMP_OK       1  // ran normally, stopped due to time
#define PUMP_OIL_END  2  // pump stopped, due to oil sensor
#define PUMP_AMPS     3  // pump stopped, due to high current
#define PUMP_STOP     4  // pump stopped, due to master request
#define PUMP_TMAX     5  // pump at tmax...can't run any more
#define PUMP_OFF      6  // pump is off, just handling an a/d request



//*****************************************************************************
// AUX_PWR Command (spi_cmd.h) passes the aux_sensor_id to control
//		range = 1..127
// If it passes a #>0, it is turned on (set high).
// If it is <0, it will be turned off (set low)
// !!!THIS LIST MUST AGREE WITH MSP430 CODE !!!!!!!!
//*****************************************************************************

// following are used by low-level calls to spi_aux_pwr
#define HYD_ON_AD        1  // turn on needed circuitry to sample a/d value
#define HYD_ON_HYD       2  // turn on hydraulic pump
#define HYD_ON_OIL       3  // turn on the oil sensor
#define HYD_OPEN_VALVE   4  // open the hydraulic valve (0.1s pulse)
#define HYD_CLOSE_VALVE  5  // close the hydraulic valve (0.1s pulse)
#define HYD_ON_LLD       6  // turn on the LLD
#define HYD_ON_VENT      7  // turn on the air vent pump
#define LLD_SAMP         8  // turn on LLD for 100ms, return LLD value


// define the a/d channels
#define AD_HYD_AMPS 0 // chan0 = amps
#define AD_HYD_VOLT 1 // chan1 = volts
#define AD_HYD_OIL  2 // chan2 = oil sensor
#define AD_HYD_ALL  8 // request to measure all hyd pump a/d

// define the oil sense settings
#define OIL_NO      0 // ignore the oil sensor
#define OIL_AFTER_T 1 // pay attention after the defined time
#define OIL_YES     2 // yes, pay attn always

//*** jun15 additions for auto-degas (adg) code *******************************
//=== state definitions ========================= 
#define ADG_ST_IDLE             0   // =setting for the initial entry
#define ADG_ST_PUMPING          1   // the hydraulic pump is ON
#define ADG_ST_PAUSING_PRE_VENT 2   // incr. pump is done: wait before venting.
#define ADG_ST_VENTING          3   // =time to vent
#define ADG_ST_BLEEDING         4   // =wait w/valve open to let oil back in
#define ADG_ST_DONE             5   // =finished all cycles: data can be packed.
//=== max array definitions =======
#define  N_CYCLES_MAX  25  // max outer pump cycles 
#define  N_INCR_MAX    60  // ea outer cycle consists of N_INCR_MAX incr. pumps.
//=== other parameters
#define TM_VENT_WAIT   20L   // sec to wait after last pump & running the vent. 10jun15
#define TM_INCR_PUMP   30    // sec to run the for ea incremental pump
#define TM_BLEED_WAIT  1200  // sec to wait for all of the oil to bleed in.
//=== define ADG array IDs, used to tag SBD data
#define SBD_ADG_VentTm  1    // has the total vent time (0.1s ticks) for ea cycle
#define SBD_ADG_PmpTm   2    // has the total pump time for each cycle (1 LSB = 10s)
#define SBD_ADG_Volts   3    // has the average pump V for ea cycle
#define SBD_ADG_Amps    4    // has the average pump amps for ea cycle
#define SBD_ADG_AMax    5    // has the max amps for ea cycle
#define SBD_ADG_Oil     6    // has the oil value for ea incremental pump. (1 LSB = 10 counts)


//*** end *********************************************************************
