/*
 *  ltdata.h
 *  lt
 *
 *  Created by Shane Saxon on 4/4/05.
 *  Shane Saxon has waived all copyright and related or neighboring rights to
 *  "miniTweezers GUI". Published from the United States as Public Domain.
 *  Creative Commons License CC0 1.0 Universal.
 *
 *  Created by Shane Saxon with contributions by Steven Smith, Evan Sperber,
 *  and Josep-Maria Huguet.  Code base started in 2004.
 *
 *  To view the full license with a list of additional authors and to download or
 *  contribute code goto http://tweezerslab.unipr.it/license.html
 *
 *
 *  All shared data is located here 
 */
 
#ifndef _ltdata_h_
#define _ltdata_h_

#define TRUE 1
#define FALSE 0

enum {
	kChannelOscilloscope = -2,		//not actual channels
	kChannelTime = -1,
	kChannelCycleCount = 0,			//start of real channels
	kChannelTrapAPsdX,
	kChannelTrapAPsdY,
	kChannelTrapAPsdSum,
	kChannelTrapAIris,
	kChannelTrapALeverX,
	kChannelTrapALeverY,
	kChannelTrapALeverSum,
	kChannelTrapBPsdX,
	kChannelTrapBPsdY,
	kChannelTrapBPsdSum,
	kChannelTrapBIris,
	kChannelTrapBLeverX,
	kChannelTrapBLeverY,
	kChannelTrapBLeverSum,
	kChannelTrapATemp,
	kChannelTrapBTemp,
	kChannelNetForceX,
	kChannelNetForceY,
	kChannelNetForceZ,
	kChannelTension,
	kChannelTrapANormLeverX,
	kChannelTrapANormLeverY,
	kChannelTrapBNormLeverX,
	kChannelTrapBNormLeverY,
	kChannelMotorX,
	kChannelMotorY,
	kChannelMotorZ,
	kChannelMotorVelocityX,		
	kChannelMotorVelocityY,		
	kChannelMotorVelocityZ,		
	kChannelErrorTrapA,
	kChannelErrorTrapB,
	kChannelErrorComPIC,
	kChannelTrapACycleCount,
	kChannelTrapBCycleCount,
	kChannelMotorCycleCount,
	kChannelPICtime,			//SBS
	kChannelTweezStatus,		//SBS
	kChannelNull,						//zero's for conveniance, inefficient, who cares
	kChannelCount
};
		
enum {
	kInstrumentReadBufferSize = 262144,
	kInstrumentWriteBufferSize = 4096,
	kCommandBufferSize = 8,
	kWritePacketSize = 31,
	kRecentSize = 262144,
	kCommandQueueSize = 4096,
	kKeyMapSize = 256,
	kMainPICfrequency = 4000		//SBS
};

enum {							//used by OpenGL to initialize specific window types
	kGraphDefault = 0,
	kGraphForcePosition,
	kGraphExtensionTime,
	kGraphForceTime,
	kGraphOscilloscope,
	kGraphFourier,	
	kGraphNetForce,
	kGraphPSD,
	kGraphLever,
	kGraphMap,
	kGraphForceVelocity,
	kGraphMotorLever,			//SBS
	kGraphLeverLever,			//SBS
	kGraphHopping,				//SBS
	kGraphIntensityMap,			//SBS
	kGraphDualForce				//SBS
};

//===========ENS for calibration input
enum{		// index of calibFloat[]
kTrapATempFudge = 0,
kTrapBTempFudge,
kcalibFloatSize
};

//===========ENS for calibration input
enum{		// index of calibXYZfloat[]
kTrapAForceSens = 0,
kTrapBForceSens,
kTrapALevSens,
kTrapBLevSens,
kcalibXYZfloatSize
};

enum{		// index of calibXYZinteger[]
kMotorThresh = 0, 
kcalibXYZintSize
};
//===========ENS for calibration input
						
enum {					//index of userInteger[]
	kForceRateDivider = 0,
	kForceGain,
	kPositionRateDivA,
	kPositionRateDivB,
	kPositionGainA,
	kPositionGainB,
	kGotoSpeedX,
	kGotoSpeedY,
	kGotoSpeedZ,
	kMaxDataFileSize,
	kHopDistance,
	kUserIntSize
};
					
enum {				// index of userFloat[]
	kMaxForceP1 = 0,
	kMinForceP1,
	kPullRateP1,
	kRefoldTime1,	//SBS
	kMaxForceP2,
	kMinForceP2,
	kPullRateP2,
	kRefoldTime2,	//SBS
	kMaxForceP3,
	kMinForceP3,
	kPullRateP3,
	kRefoldTime3,	//SBS
	kconstForce1,	//SBS
	kconstForce2,	//SBS
	kYourFloat,
	kUserFloatSize
	};
					
enum {					//index of userXYZinteger[]
	kGoto0 = 0,
	kGoto1,
	kGoto2,
	kGoto3,
	kGoto4,
	kGoto5,
	kGoto6,
	kGoto7,
	kGoto8,
	kGoto9,
	kYourXYZint,	//unused example variable
	kUserXYZintSize
	};
	
enum {					// index of userXYZfloat
	kYourXYZfloat = 0,
	kUserXYZfloatSize
	};

enum 
	{				// run modes
	kAllOff = 0,		//SBS
	kAutoPull1,
	kAutoPull2,
	kAutoPull3,   //SBS
	kStiffTest,
	kHopping,
	kRaster,
	kCForce1,
	kCForce2
	};
	
enum{ kNHigh = 0, kNLow, kNWriteStateCount };

typedef struct {
	int x, y, z;
} XYZinteger;

typedef struct {
	float x, y, z;
} XYZfloat;

typedef struct{			//SBS
	char* label;
	int value;
} userIntegerType;

typedef struct{			//SBS
	char* label;
	float value;
} userFloatType;

typedef struct{			//SBS
	char* label;
	XYZinteger value;
} userXYZIntType;

typedef struct{			//SBS
	char* label;
	XYZfloat value;
} userXYZfloatType;

typedef struct{		//ENS
	char* label;
	float value;
} calibFloatType;

typedef struct{		//ENS
	char* label;
	XYZfloat value;
} calibXYZfloatType;

typedef struct{		//ENS
	char* label;
	XYZinteger value;
} calibXYZintType;

typedef struct {
	unsigned int keyMap[6][kKeyMapSize];	//6 is the number of key event types
	int commandQueue[kCommandQueueSize];
	int commandQueueIndex;
	int upperCase;									//!(alphaLock == shift)
	int alphaLockModifier;
	int shiftModifier;
	int altModifier;
	int controlModifier;
	int commandModifier;
	int mouseTrapSensitivity;
	int mouseMotorSensitivity;
	int altColor;
} Control;

struct Instrument {
	int name;

	unsigned char readBuffer[kInstrumentReadBufferSize];					//make private shared buffers for the update timer function, debug
	unsigned char writeBuffer[kInstrumentWriteBufferSize];
	unsigned char commandBuffer[kCommandBufferSize][kInstrumentWriteBufferSize];
	int readBufferIndex;
	int readBufferEndIndex;	
	int writeBufferIndex;
	int writeBufferEndIndex;
	int commandBufferIndex;
	int commandBufferSentIndex;
	int sendCommand;
	int fileDescriptor;
	int threadRef;
	int stopThread;
	int LED;
	int rasteringMotors;  //SBS
	int hopping;
	int zeroingPSDs;
	int zeroingForce;
	int manualPausing;		//SBS
	int hostPositionFeedbackOn; //SBS
	int zeroingPSDsCount;
	int zeroingForceCount;
	int movingUpward;
	int stiffTestHighA;
	int previouslyUpwards;	//SBS
	int positionIndex;
	int functionGeneratorOn;		//SBS
	float fakeDistance;			//SBS
	unsigned short trapATargetX; //SBS
	unsigned short trapATargetY; //SBS
	unsigned short trapBTargetX; //SBS
	unsigned short trapBTargetY; //SBS
	unsigned short leverATargetX; //SBS
	unsigned short leverATargetY; //SBS
	unsigned short leverBTargetX; //SBS
	unsigned short leverBTargetY; //SBS    

	int moveTrap;
	int trapA;
	int trapAB;
	int trapB;
	XYZinteger trapAxes;

	int moveMotors;

	userXYZIntType userXYZinteger[kUserXYZintSize];	//SBS
	userIntegerType userInteger [kUserIntSize + kChannelCount + kNWriteStateCount];		//SBS
	userFloatType userFloat [kUserFloatSize];		//SBS
	userXYZfloatType userXYZfloat [ kUserXYZfloatSize];		//SBS
	
	long start;		//ENS

	XYZinteger motorAxes;
	int pinchAxis;
	XYZfloat motorGain;
	XYZinteger motorThreshold;
	int jogDuration;

	XYZfloat trapAZeroingPSDsSum;
	XYZfloat trapBZeroingPSDsSum;
	XYZfloat zeroingForceSum;
	float maxForce;
	float minForce;
	float absForceY;			//SBS
	float absForceX;			//SBS
	float refoldTime;
	float pullRate;
	float maxPosition;
	float minPosition;
	float pathLength;		//SBS
	int maxPositionOn;
	int minPositionOn;
	int absPositionX;		//SBS
	int absPositionY;		//SBS
	int psdSumEmptyA;		//SBS
	int psdSumEmptyB;		//SBS
	int irisEmptyA;		//SBS
	int irisEmptyB;		//SBS
	int psdSumFullA;		//SBS
	int psdSumFullB;		//SBS
	int irisFullA;		//SBS
	int irisFullB;		//SBS


	int runMode;	//SBS
	
	int smoothingNum;

	float motorMicroMeterConversion;
		
	XYZfloat forceOffset;

	XYZfloat trapAForceSensitivity;
	XYZfloat trapALeverSensitivity;
	XYZfloat trapALeverOffset;
	
	XYZfloat trapBForceSensitivity;
	XYZfloat trapBLeverSensitivity;
	XYZfloat trapBLeverOffset;	
	
	float trapATempFudge;		//SBS
	float trapBTempFudge;		//SBS
	float tempTime;		//SBS
	float trapAdistanceSetpointX;		//SBS
	float trapAdistanceSetpointY;		//SBS
	float trapBdistanceSetpointX;		//SBS
	float trapBdistanceSetpointY;		//SBS
	
	
	//===========Calibration I/O variables -- ENS
	calibFloatType calibFloat[kcalibFloatSize];		//SBS
	calibXYZfloatType calibXYZfloat[kcalibXYZfloatSize];
	calibXYZintType calibXYZint[kcalibXYZintSize];
	char* deviceName;
	
	int nHigh;		//ENS
	int nLow;		//ENS
};

typedef struct Instrument Instrument;
typedef struct Instrument * pInstrument;

typedef struct {
	unsigned char byteFromTrapA[256];				//parameters read from the traps
	unsigned short wordFromTrapA[256];
	unsigned char byteFromTrapB[256];
	unsigned short wordFromTrapB[256];
	XYZinteger motorPosition;
	unsigned char motorLimit, blueLED, motorsBusy;			//SBS
	float recent[kRecentSize][kChannelCount];
	float latest[kChannelCount];
	float smoothed[kChannelCount];
	int recentIndex;
	char *channelString[kChannelCount];
	char *channelUnits[kChannelCount];
	int mainFrequency;
	int printCount;
} QuickTime;


typedef struct {
	char status[80];				//status text
} NIB;

typedef struct {
	int temp;
} HID;

typedef struct {
	int threadRef;
} OpenGL;

//ENS
typedef struct {
	char* dFilePath;
	int dFileActiveChannels[kChannelCount];
	int dFileHighestActiveChannel;
	const char* dFileChannelString[kChannelCount];
	const char* dFileChannelFormat[kChannelCount];
	
	int dFileIsOpen;
	int n;
	
	int ccNumRollover;
	float ccOffset;
	int ccOffsetIsSet;
	
	char posFileCycle;
	int currentWriteState;
	int setWriteSpeedFastOnNextCycle;
	int setWriteSpeedSlowOnNextCycle;
	
	char* prefFilePath;
} dataFileData;

//enum{ kNHigh = 400 };													//ENS
//enum{ kNLow = 20 };														//ENS
enum{ kNotWriting = 0, kWritingFast, kWritingSlow };			//ENS

//ENS
typedef struct{
	char* commentsString;
	char* commentsPath;
	float localCommentsTime;
	int updateCommentsFile;	//use as a flag
	//char* customEventString;
} commentsFileData;


struct Data {
	Control control;					//control manager data
	Instrument instrument;			//io buffer for instrument
	QuickTime quicktime;				//stores and retrieves all experimental data
	NIB nib;								//NIB reference, events, and display
	HID hid;								//HID events and values from joystick or other
	OpenGL gl;							//parameters that effect all openGL windows
	dataFileData dFileData;			//data file parameters					//ENS
	userIntegerType userInteger;		//user modifiable preference integers	//SBS
	commentsFileData cFileData;		//comments file parameters			//ENS
};
typedef struct Data Data;
typedef struct Data * pData;

struct ThreadData{
	pData data;
	int threadID;
};
typedef struct ThreadData ThreadData;
typedef struct ThreadData * pThreadData;
 
void InitUserVariables( void* dataRef);			//SBS
void InitData(pData data);
int defaultInteger( void* dataRef, int index);		//SBS
XYZinteger defaultXYZint( void* dataRef, int index );	//SBS
float defaultFloat( void* dataRef, int index );		//SBS
XYZfloat defaultXYZfloat( void* dataRef, int index );		//SBS

void InitCalibVariables( void* dataRef );		//ENS
void loadDefaultCalibVariables( void* dataRef );
#endif									//compiler directive