/* BCSYS.H */

#include	<TT8.h>		/* Tattletale Model 8 Definitions */
#include	<tat332.h>		/* 68332 Tattletale (7,8) Hardware Definitions */
#include	"tt8rmlib.h"

#include	<stdarg.h>
#include	<stddef.h>
#include	<stdio.h>
#include	<stdlib.h>
#include	<string.h>
#include	<time.h>

#ifdef __TURBOC__
#include 	"tt8pclib.h"
#include	<conio.h>
#endif

#ifdef AZTEC_C
#include	<sim332.h>		/* 68332 System Integration Module Definitions */
#include	<qsm332.h>		/* 68332 Queued Serial Module Definitions */
#include	<tpu332.h>		/* 68332 Time Processing Unit Definitions */
#include	<dio332.h>		/* 68332 Digital I/O Port Pin Definitions */
#include	<tt8pic.h>		/* Model 8 PIC Parallel Slave Port Definitions */
#include	<tt8lib.h>		/* definitions and prototypes for Model 8 library */
#include	<assert.h>
#include	<ctype.h>
#include	<errno.h>
#include	<fcntl.h>
#include	<float.h>
#include	<limits.h>
#include	<locale.h>
#include	<math.h>
#include	<setjmp.h>
#include	<sgtty.h>
#include	<signal.h>
#include	<stat.h>
#include	<userio.h>
#endif


/***************** Calibration Constants***************/

#define BATT_CAL		1841
#define	CURR_CAL		32.76
#define ISO_PLUS_CAL            3591
#define ISO_MINUS_CAL           3591

/***************** Definitions ************************/

#define	OFF			0
#define ON			1
#define STOP			0
#define JAM_TIMEOUT		5000  	/* Millisecs */

/* PURGE VALVE */
#define CLOSE			1
#define OPEN			2
#define PURGE_TIMEOUT		3   	/* Mins */
#define PURGE_CLOSED_POS  	164  	/* 1/4 turns */
#define PURGE_LIMIT_CHAN	3    	/* TPU3 */
#define PURGE_TURNS_CHAN	2    	/* TPU2 */

/* SLIDE VALVE */
#define AMBIENT			1
#define CHAMBER			2
#define SLIDE_TIMEOUT		3   	/* Mins */
#define SLIDE_CHAMBER_POS	80      /* 1/4 turns */
#define SLIDE_LIMIT_CHAN	5      	/* TPU5 */
#define SLIDE_TURNS_CHAN	4      	/* TPU4 */

/* STIR MOTOR */
#define STIR_TURNS_CHAN		1      	/* TPU1 */
#define STIR_SPEED_CHAN		0      	/* TPU0 */

/* FLOW CELL */
#define FLOW_PUMP_CHAN		6

/* MAX132 */
#define VOLTAGE_AVERAGE 	4
#define ZERO_AVERAGE 		4

/* FILE SYSTEM */
#ifdef AZTEC_C
#define MEMSIZE		128000
#else
#define MEMSIZE		1000
#endif

/********************* Activity Codes *******************/

#define FLOW_CELL_AMBIENT	100
#define FLOW_CELL_CHAMBER	110
#define CAL_DATA		120

/********************* Error Codes *******************/

#define PURGE_JAM_ERR		1000
#define PURGE_TIMEOUT_ERR 	1001
#define SLIDE_JAM_ERR		1010
#define SLIDE_TIMEOUT_ERR	1011

/********************* Macros ************************/

#define	sensor_pwr_on()		PClear(E,4)
#define sensor_pwr_off()	PSet(E,4)
#define o2a()                   read_max132(0)
#define o2b()			read_max132(1)
#define iso_pwr_on()		TPUSetPin(11,0);
#define iso_pwr_off()		TPUSetPin(11,1);
#define iso_plus_volt()		(float)read_max132(6)/ISO_PLUS_CAL
#define iso_minus_volt()	(float)read_max132(7)/ISO_MINUS_CAL
#define batt_volt()		(float)read_max186(0,8)/BATT_CAL
#define current() 		(float)read_max186(1,64)/CURR_CAL
#define purge_limit()		TPUGetPin(3)
#define slide_limit()		TPUGetPin(5)
#define flow_pump_off()		TPUSetPin(6, 0)

#ifdef AZTEC_C
#define flow_pump_on() 		TPUSetupPWM(FLOW_PUMP_CHAN, g_flow_speed, 80, HighPrior)

#else
#define flow_pump_on()		printf("\nFlow pump on")
#endif



/******************* Global Variables ******************/

extern unsigned g_stir_ticks, g_purge_ticks, g_slide_ticks;
extern ulong g_sample_sets, g_sample_rate, g_flow_cell_delay, g_stir_speed, g_flow_speed;



/***** Function Prototypes *****/

/* file.c */
void file_init(void);
void clear_mem(void);
void offload_data(void);
void store_data(short rec_type);

/* test.c */
void test_func(void);

/* bclib.c */
int move_purge_mot(char direction);
int move_slide_mot(char direction);
int purge_valve(char direction);
int slide_valve(char direction);
int timer(ulong starttime, ulong maxtime);
int timer_min(ulong starttime, ulong maxtime);
void delaysecs(unsigned long delay_time);
short read_max186(short chan, short reads);
short read_max132(short chan);
uchar shift_atod(uchar input_val);
void bc_reset(void);
void SetDateTime(void);
void tpurupt(char chan, char on_off);
void stir_turns(void);
void purge_turns(void);
void slide_turns(void);
void stir_motor(char on_off);