// Scan.h

// Debug Info
#define DEBUG			// comment this out to turn off diagnostics
#ifdef DEBUG
  #define	DBG(X)	X	// template:  DBG( PinSet(1); ) or DBG( cprintf("!"); )
#else
  #define	DBG(X)		// nothing
#endif

#include	<cf1bios.h>		// Persistor CF1 System and I/O Definitions
#include	<cf1pico.h>		// Persistor CF1 PicoDOS Definitions  

#include	<stat.h>		// CF1 POSIX-like File Status Definitions
#include	<fcntl.h>		// CF1 POSIX-like File Access Definitions
#include	<unistd.h>		// CF1 POSIX-like UNIX Function Definitions
#include	<dirent.h>		// CF1 POSIX-like Directory Access Definitions
#include	<termios.h>		// CF1 POSIX-like Terminal I/O Definitions
#include	<dosdrive.h>	// CF1 DOS Drive and Directory Definitions

#include	<assert.h>
#include	<ctype.h>
#include	<errno.h>
#include	<float.h>
#include	<limits.h>
#include	<locale.h>
#include	<math.h>
#include	<setjmp.h>
#include	<signal.h>
#include	<stdarg.h>
#include	<stddef.h>
#include	<stdio.h>
#include	<stdlib.h>
#include	<string.h>
#include	<time.h>


// QPB Slots
#define MAX146_SLOT		8
#define U7_SLOT			9
#define COM2_SLOT		10
#define COM3_SLOT		11
#define U1_SLOT			12
#define U5_SLOT			13
#define U14_SLOT		14


// Misc. Definitions
#define	LPMODE		FullStop	// choose: FullStop or FastStop or CPUStop

// Motor Definitions
#define ON				1
#define OFF				0
#define	X				1
#define	Y				2
#define	Z				3
#define	F				4
#define STOP			0
#define FWD				1
#define BKWD			2
#define TEST_STEPS		80		// About 10 cm
#define UP				1
#define DOWN			2
#define MAX_X_STEPS		1000
#define MAX_Y_STEPS		2800
#define MAX_Z_STEPS		1000
#define X_STEPS_PER_MM	200
#define Y_STEPS_PER_MM	8		// Actually 7.87
#define Z_STEPS_PER_MM	1


// MAX146 Definitions
#define AD0				0
#define AD1				1
#define AD2				2
#define AD3				3
#define AD4				4
#define AD5				5
#define AD6				6
#define	BATTERY_CAL		1192				// 32768/2.5/11
#define BACKUP_BAT_CAL	4935				// 32768/2.5/2.656
#define DEPTH_CAL		13107				// 32768/2.5/1.0

// U1 Definitions
#define ALL_U1_PINS		0xFF
#define OPTODE_ON		0x01
#define FLOOD_ON		0x02
#define VIDEO_ON		0x04
#define GRAB_MOT_ON		0x08
#define GRAB_UP			0x10
#define GRAB_DOWN		0x20

// U5 Definitions
#define ALL_U5_PINS		0xFF
#define BURN_1			0x01
#define BURN_2			0x02
#define BURN_3			0x04
#define BURN_4			0x08
#define BURN_5			0x10
#define BURN_6			0x20
#define ISO_PWR_ON		0x40
#define STIR_5V_ON		0x80

// U7 Definitions
#define ALL_U7_PINS		0xFF
#define COM2_ON			0x01
#define COM3_ON			0x02
#define TRANS_ON		0x04
#define SENSORS_ON		0x08
#define O2_ON			0x10
#define PHOTOMULT_ON	0x20
#define PRESS_ON		0x40

// U14 Definitions
#define ALL_U14_PINS	0xFF
#define X_MOTOR_ON		0x01
#define Y_MOTOR_ON		0x02
#define Z_MOTOR_ON		0x04
#define DIR				0x08
#define STEP_PWR_ON		0x10

// Photomultiplier Definitions
#define FILTER1			0			// Steps from index
#define FILTER2			45
#define FILTER3			90
#define FILTER4			135

// Transceiver Definitions
#define MAX_SAMPLES		4000
#define MAX_SAMPLE_TIME	150			// Microseconds
#define SAMPLES_PER_MM	27			// At 20MHz sample rate
#define ACOUSTIC		1
#define OPTIC			2

// Video Camera Definitions
#define VIDEO_START		1
#define	VIDEO_STOP		0

// External Variables

extern int	g_grab_turns;

// Structures
struct def{
	ushort stir_speed;
	time_t start_time;
	time_t end_time;
	ushort samples;
	ushort start_bin;
	ushort end_bin;
	ushort bin_length;
	ushort x_scan_len;
	ushort y_scan_len;
	ushort cell_size;
	ushort camera_start_depth;
	ushort camera_on_time;
	ushort delay_time;
};


//Function Prototypes
void 	test_func(void);
void 	deploy(void);
void 	setup(void);
	
void 	init(void);
float 	battery(void);
float 	backup_bat(void);

void 	COM2Init(void);
int 	COM2ByteAvail(void);
void	COM2Flush(void);
int 	COM2GetChar(void);
bool 	COM2PutChar(ushort c);
int 	COM3ByteAvail(void);
void 	COM3Init(void);
void	COM3Flush(void);
int 	COM3GetChar(void);
bool 	COM3PutChar(ushort c);

void 	delay_secs(int secs);
float 	depth(void);
int 	filter_motor(int position);
short 	getchlp(void);
void 	grab_motor(int dir);
void 	Irq4RxISR(void);
short 	Max146Sample(ushort chan, bool uni, bool sgl, bool pd);
void 	ping_cycle(ushort *buffer);
int 	scan_cycle(int type, ushort *buffer);
void	scan_motor(int motor, int dir, int steps);
void 	stir_motor(ushort speed, int on_off);
void 	test_scan_motor(int motor);

void 	U1_control(ushort device, char on_off);
void 	U5_control(ushort device, char on_off);
void 	U7_control(ushort device, char on_off);
void 	U14_control(ushort device, char on_off);

void 	video(short function);
void 	video_send_packet(unsigned char *packet);

IEV_C_PROTO(COM2Interrupt);
IEV_C_PROTO(grab_turns_interrupt);


// Macros
#define burnwire(wire, on_off)		U5_control(wire, !on_off)
#define com2_on()			U7_control(COM2_ON, ON)
#define com2_off()			U7_control(COM2_ON, OFF)
#define dir_on()			U14_control(DIR, ON)
#define dir_off()			U14_control(DIR, OFF)
#define flood_on()			U1_control(FLOOD_ON, ON)		
#define flood_off()			U1_control(FLOOD_ON, OFF)		
#define iso_pwr_on()		U5_control(ISO_PWR_ON, ON)
#define iso_pwr_off()		U5_control(ISO_PWR_ON, OFF)
#define optode_on()			U1_control(OPTODE_ON, ON)
#define optode_off()		U1_control(OPTODE_ON, OFF)
#define o2_pwr_on()			U7_control(O2_ON, ON)
#define o2_pwr_off()		U7_control(O2_ON, OFF)
#define photomult_pwr_on()	U7_control(PHOTOMULT_ON, ON)
#define photomult_pwr_off()	U7_control(PHOTOMULT_ON, OFF)
#define press_pwr_on()		U7_control(PRESS_ON, ON)
#define press_pwr_off()		U7_control(PRESS_ON, OFF)
#define sensor_pwr_on()		U7_control(SENSORS_ON, ON)
#define sensor_pwr_off()	U7_control(SENSORS_ON, OFF)
#define step_pwr_on()		U14_control(STEP_PWR_ON, ON)
#define step_pwr_off()		U14_control(STEP_PWR_ON, OFF)
#define stir_5V_on()		U5_control(STIR_5V_ON, ON)
#define stir_5V_off()		U5_control(STIR_5V_ON, OFF)
#define trans_pwr_on()		U7_control(TRANS_ON, ON)
#define trans_pwr_off()		U7_control(TRANS_ON, OFF)
#define video_pwr_on()		U1_control(VIDEO_ON, ON)
#define video_pwr_off()		U1_control(VIDEO_ON, OFF)
#define x_motor_on()		U14_control(X_MOTOR_ON, ON)
#define x_motor_off()		U14_control(X_MOTOR_ON, OFF)
#define y_motor_on()		U14_control(Y_MOTOR_ON, ON)
#define y_motor_off()		U14_control(Y_MOTOR_ON, OFF)
#define z_motor_on()		U14_control(Z_MOTOR_ON, ON)
#define z_motor_off()		U14_control(Z_MOTOR_ON, OFF)

#define grab_limit()		!PinRead(29)
#define footpad()			!PinRead(27)
#define	filter_index()		!PinRead(26)
#define x_limit()			!PinRead(30)
#define y_limit()			!PinRead(28)
#define z_limit()			!PinRead(31)


#define ping_on()			PinWrite(25, 1)
#define ping_off()			PinWrite(25, 0)
#define clear_fifo_on()		PinWrite(22, 1)
#define clear_fifo_off()	PinWrite(22, 0)
#define read_fifo_on()		PinWrite(23, 1)
#define read_fifo_off()		PinWrite(23, 0)

