///////////////////////////////////////////////////
///////////////////////////////////////////////////
// File: H2O_Jason.h
// Description: Provides function prototypes, 
//				global variables, and macros
///////////////////////////////////////////////////
///////////////////////////////////////////////////

#include	<cfxbios.h>		// Persistor BIOS and I/O Definitions
#include	<cfxpico.h>		// Persistor PicoDOS 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>

#include	<dirent.h>		// PicoDOS POSIX-like Directory Access Defines
#include	<dosdrive.h>	// PicoDOS DOS Drive and Directory Definitions
#include	<fcntl.h>		// PicoDOS POSIX-like File Access Definitions
#include	<stat.h>		// PicoDOS POSIX-like File Status Definitions
#include	<termios.h>		// PicoDOS POSIX-like Terminal I/O Definitions
#include	<unistd.h>		// PicoDOS POSIX-like UNIX Function Definitions


///////////////////////////////////////////////////
///////////////////////////////////////////////////
// Definitions
///////////////////////////////////////////////////
///////////////////////////////////////////////////
// General Definitions
#define ON				1
#define OFF				0
// PC104 Definitions
#define BOOT			0
#define	PICTURE			1
#define SHUTDOWN		2
#define DATETIME_SIZ	11
// QPB Slots
#define MAX146_SLOT		8
#define SPI4MOT_SLOT	12		// Binary of 4 with PCS3 high
// Misc. Definitions
#define	LPMODE		FullStop	// choose: FullStop or FastStop or CPUStop
// 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	1192	// 32768/2.5/2.656
// SPI4 Definitions (Motors, Camera)
#define ALL_SPI4_PINS	0xFF
#define ALL_SPI4_OFF	0x00
#define AUX_ON			0x01
#define FLOOD_ON		0x02
#define VIDEO_ON		0x04
#define GRAB_MOT_ON		0x08
#define GRAB_UP			0x10
#define GRAB_DOWN		0x20
#define PC104_ON		0x40 	// Used to be Stir On
#define	CAMERA_ON		0x80	// SPECIFIC TO H2O

///////////////////////////////////////////////////
///////////////////////////////////////////////////
// Structures
///////////////////////////////////////////////////
///////////////////////////////////////////////////
struct defStruct {
	ulong start_time;
	ulong end_time;
	ushort arm_time;
	bool use_mag_sensor;
	ushort flood_time;
	ushort flood_wait;
	ushort cycle_time;
	ushort camnum;
	ushort sample_rate;
	bool flood_on;
	ushort max_boottime;
	ushort max_shuttime;
	ushort max_camtime;
};


///////////////////////////////////////////////////
///////////////////////////////////////////////////
// Global Variables
///////////////////////////////////////////////////
///////////////////////////////////////////////////
extern	struct 	defStruct defaults;
// Comports
extern	TUPort	*com1;
// Stepper Motor
extern	short	StepCountTM;
extern	short	StepCount; 
// Grab Motor
extern 	int 	GrabTurnCount;


///////////////////////////////////////////////////
///////////////////////////////////////////////////
// Function Prototypes
///////////////////////////////////////////////////
///////////////////////////////////////////////////
// General TPU
void 	InitializeTPU();
void 	ShutdownTPU();
// SPI Bus Initialization
void 	InitSPI();
// Comports
void 	InitializeComportPins();
void 	CloseComportUARTS();
// PC104 Commands
void	Terminal();
void	DisplayCommands();
void	SyncDateTime();
void 	GetDateAndTime(char *dateTimeStr);
void 	TakePicture();
void	SSHFTP(short ON_OFF);
void	Shutdown();
// Stepper Motor
void 	InitializeStepperMotorPins();
void 	RunStepperMotor(int rpm, bool testMode);
void 	StopStepperMotor(bool testMode);
void 	ToggleStepperTestMode();
void 	ToggleStepper();
// Grab Motor
int 	GrabDownCycle();
int 	GrabUpCycle();
void 	GrabMotor(int dir);
void 	GrabTurnInterrupts(short ON_OFF);
IEV_C_PROTO(GrabInterruptFunc);
// SPI Routines
short 	Max146Sample(ushort chan, bool uni, bool sgl, bool pd);
float 	Max146Battery();
float 	Max146BackupBat();
void 	SPI4MOTControl(ushort device, char on_off);
// Miscellaneous
void 	GetBatteryLevel(char *battStr);
void 	SleepWithBreak();
void 	DelaySecs(int secs, bool allowKBBreak);	// slows RTC clock
bool	DelayPC104(short type, int timeout);	// logs PC104
void 	DelayCycles(long num);					// wastes CPU cycles
void 	Irq4RxISR();



///////////////////////////////////////////////////
///////////////////////////////////////////////////
// Macros
///////////////////////////////////////////////////
///////////////////////////////////////////////////
// PC104
#define mPC104PwrOn()		SPI4MOTControl(PC104_ON, ON)
#define mPC104PwrOff()		SPI4MOTControl(PC104_ON, OFF)
// Flood Light
#define mFloodLightOn()		SPI4MOTControl(FLOOD_ON, ON)		
#define mFloodLightOff()	SPI4MOTControl(FLOOD_ON, OFF)
// Canon Camera
#define mCameraPwrOn()		SPI4MOTControl(CAMERA_ON, ON)
#define mCameraPwrOff()		SPI4MOTControl(CAMERA_ON, OFF)
// Magnetic Sensor
#define mSensorPwrOn()		PinSet(TPU4)
#define mSensorPwrOff()		PinClear(TPU4)
#define mMagneticSensor()	!PinRead(TPU1)


///////////////////////////////////////////////////
///////////////////////////////////////////////////
// Debugging Tools
///////////////////////////////////////////////////
///////////////////////////////////////////////////
#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