/*
 *  ltdata.h
 *  lt
 *
 *  Created by Shane Saxon on 4/4/05.
 *  Copyright 2005-2006 Shane Saxon. All rights reserved.
 *
 *  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,			//sssnert
	kChannelTweezStatus,		//sssssnert
	kChannelNull,						//zero's for conveniance, inefficient, who cares
	kChannelCount
};
		
enum {
	kInstrumentReadBufferSize = 262144,
	kInstrumentWriteBufferSize = 4096,
	kCommandBufferSize = 8,
	kWritePacketSize = 31,
	kRecentSize = 262144,
	kCommandQueueSize = 4096,
	kKeyMapSize = 256,
	kRecordArraySize = 1000,		//ssssnert
	kMainPICfrequency = 3800		//ssssnert
};

enum {							//used by OpenGL to initialize specific window types
	kGraphDefault = 0,
	kGraphForceExtension,
	kGraphExtensionTime,
	kGraphForceTime,
	kGraphOscilloscope,
	kGraphFourier,	
	kGraphNetForce,
	kGraphPSD,
	kGraphLever,
	kGraphMap,
	kGraphForceVelocity		

};

//===========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[]
	kRateDivider = 0,
	kForceGain,
	kGotoSpeedX,
	kGotoSpeedY,
	kGotoSpeedZ,
	kUserIntSize
};
					
enum {				// index of userFloat[]
	kMaxForceP1 = 0,
	kMinForceP1,
	kPullRateP1,
	kRefoldTime1,	//ssssnert
	kMaxForceP2,
	kMinForceP2,
	kPullRateP2,
	kRefoldTime2,	//ssssnert
	kconstForce1,	//ssssnert
	kconstForce2,	//ssssnert
	kYourFloat,
	kUserFloatSize
	};
					
enum {					//index of userXYZinteger[]
	kGoto0 = 0,
	kGoto1,
	kGoto2,
	kGoto3,
	kGoto4,
	kGoto5,
	kGoto6,
	kGoto7,
	kGoto8,
	kGoto9,
	kYourXYZint,
	kUserXYZintSize
	};
	
enum {					// index of userXYZfloat
	kYourXYZfloat = 0,
	kUserXYZfloatSize
	};

enum 
	{				// run modes
	kAllOff = 0,		//ssnert
	kAutoPull1,
	kAutoPull2,
	kStiffTest,
	kHopping,
	kLeverAlignment
	};
	
enum{ kNHigh = 0, kNLow, kNWriteStateCount };

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

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

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

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

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

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

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 hopping;
	int zeroingPSDs;
	int zeroingForce;
	int zeroingPSDsCount;
	int zeroingForceCount;
	int movingUpward;
	int positionIndex;
	int functionGeneratorOn;		//ssssnert
	float fakeDistance;			//ssssnert
	int altColor;				//snert
	unsigned short trapATargetX; //ssnert
	unsigned short trapATargetY; //ssnert
	unsigned short trapBTargetX; //ssnert
	unsigned short trapBTargetY; //ssnert
	unsigned short leverATargetX; //ssnert
	unsigned short leverATargetY; //ssnert
	unsigned short leverBTargetX; //ssnert
	unsigned short leverBTargetY; //ssnert

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

	int moveMotors;

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

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

	XYZfloat trapAZeroingPSDsSum;
	XYZfloat trapBZeroingPSDsSum;
	XYZfloat zeroingForceSum;
	float maxForce;
	float minForce;
	float refoldTime;
	float maxPosition;
	float minPosition;
	int maxPositionOn;
	int minPositionOn;

	int runMode;	//ssnert
	
	int smoothingNum;
	int stepSize;

	float motorMicroMeterConversion;
		
	XYZfloat forceOffset;

	XYZfloat trapAForceSensitivity;
	XYZfloat trapALeverSensitivity;
	XYZfloat trapALeverOffset;
	
	XYZfloat trapBForceSensitivity;
	XYZfloat trapBLeverSensitivity;
	XYZfloat trapBLeverOffset;	
	
	float trapATempFudge;
	float trapBTempFudge;
	
	//===========Calibration I/O variables -- ENS
	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;			// snert
	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 {	// to record light-lever values when traps are aligned 
	unsigned short trapAleverX[kRecordArraySize];
	unsigned short  trapAleverY[kRecordArraySize];
	unsigned short  trapBleverX[kRecordArraySize];
	unsigned short  trapBleverY[kRecordArraySize];
	XYZfloat errForce[kRecordArraySize];
} Record;							//ssssnert

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
	Record record;						//records and correlated light lever positions			ssssnert
	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	//ssnert
	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);			//ssnert
void InitData(pData data);
int defaultInteger( void* dataRef, int index);		//ssnert
XYZinteger defaultXYZint( void* dataRef, int index );	//ssnert
float defaultFloat( void* dataRef, int index );		//ssnert
XYZfloat defaultXYZfloat( void* dataRef, int index );		//ssnert

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