/*
 *  ltcontrol.h
 *  Macintosh specific code such as NIB, app event loop,...
 *
 *
 *  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
 */


#ifndef _ltcontrol_h_
#define _ltcontrol_h_

#pragma mark ---- Instrument Command Constants ----

#define kTempSensitivity 0.024444f
// from AD590, 1deg=1uA, from preamp 1uA=15mV, voltage divider 15mV*(10.5k/(10.5k+51.1k)) = 2.5568mV
// from MAX1168 16 counts/mV gives 40.9090 cts, kTempSensitivity = 1/40.9090 degrees/count

//===========================================ENS Prototypes
void disposeCustomUtilWindows( void* dataRef );
//===========================================ENS Prototypes

//This is a copy of the parameters used by the controller firmware

enum{                         //defines the operation mode, used by a[mode]
   kManual = 0,               //no feedback in manual mode, allows host control
   kConstantForce,            //feedback modes may overide host vector commands
   kConstantPosition,
   kSiblingConstantForce,             //uses sibling trap measurements for target
   kSiblingConstantPosition
};

enum{
   kHostPacketID = 42,        //host packet header identifier
   kPicPacketID = 213         //inter-PIC communication packet header ID
};

//we use the following enumerators to index all of our variables, this allows us
//to set any variable from the host and read any variable using the variableID,
//a[] array stores unsigned bytes, the b[] array stores unsigned 16bit words,
//each has its own index starting from 1, index 0 is for cycling through array
//this method is simpler then using pointers and makes it easy to add commands

//----------------index for 8bit(byte) variables in the a[array]----------------
enum{
   byteZero = 0,              //reserved for cycling through array mode

   mode,                      //operation mode of the trap
   feedbackAxisEnabled,       // 1 = X-axis only, 2 = Y-axis only, 3 = both axes enabled

   daActiveChannels,          //output DA channels to use, bitwise
   adActiveChannels,          //input AD channels to scan, bitwise
                              //note, AD can only scan sequential channels
                              //from ch0-N or ch4-N, N can be 0-7, see datasheet

   trapInvertAxes,            //Inverts axes of the DA output channels, bitwise
   trapSwapXY,                //swaps X and Y of the DA output

   adInvertAxes,              //Inverts axes of the AD input channels, bitwise
   psdSwapXY,                 //swaps X and Y of the PSD force
   leverSwapXY,               //swaps X and Y of the Lever position

   constantForceGain,            //used by feedback from local PSD
   constantPositionGain,
   constantForceRateDivider,     //feedback skips n+1 cycles
   constantPositionRateDivider,   //for reducing the frequency of the feedback rate

   siblingForceGain,          //used by feedback from sibling PSD
   siblingPositionGain,
   siblingForceRateDivider,   //feedback skips n+1 cycles
   siblingPositionRateDivider, //for reducing the frequency of the feedback rate

   optionVariableIDA,         //the a[] optional byte to transmit to the host
   optionVariableIDB,         //the b[] optional word to transmit to the host
                              //setting the optionVaribleID to 0 causes cycle
                              //through entire array

   restartPIC,                //if true the PIC will restart the program

   cycleCount,                //counts 0-255 and is transmitted to comPIC

   ARRAYSIZEA                 //size of the byte array a[ARRAYSIZEA]
};


//----------------------index of variables in the b[array]----------------------
//----------------all values are 2 byte words using AD/DA units-----------------
enum{
   piezoVectorX = 1,          //for recieving vector moves from the host and
   piezoVectorY,              //used by the feedback algorithms
   dummyVectorZ,
   laserVector,

   piezoX,                    //values used by the DA to set piezo voltage
   piezoY,
   dummyZ,                    //not currently in use, available for future use
   laserPercent,              //sets the percentage of laser current supplied,
                              //the laser power supply determines how many amps
                              //represent 100%

   piezoLimitLoX,             //limits the range of the DA outputs
   piezoLimitHiX,
   piezoLimitLoY,
   piezoLimitHiY,
   dummyLimitLoZ,
   dummyLimitHiZ,
   laserLimitLo,
   laserLimitHi,

   adPsdX,                    //the X force, raw PSD value read from AD
   adPsdY,                    //the Y force, raw PSD value read from AD
   adIris,                    //the Z detector, raw PSD value read from AD     debug

   adPsdOffsetX,              //callibration offsets added to the raw value
   adPsdOffsetY,
   adIrisOffset,

   psdX,                      //AD value with callibration offset added
   psdY,                      //AD value with callibration offset added
   psdSum,                    //the sum of the X and Y force, no offset used
   iris,                      //AD value with callibration offset added

   adLeverX,                  //the X position, raw PSD value read from AD
   adLeverY,                  //the Y position, raw PSD value read from AD
   adLeverOffsetX,            //callibration offsets added to the raw value
   adLeverOffsetY,

   leverX,                    //AD value with callibration offset added
   leverY,                    //AD value with callibration offset added
   leverSum,                  //the sum of the X and Y position, no offset used
   temperature,               //the temperature, for environmental control

   psdTargetX,                //feedback target values
   psdTargetY,
   leverTargetX,
   leverTargetY,

   siblingPsdX,               //for matching sibling force
   siblingPsdY,
   siblingPsdSum,
   siblingIris,

   siblingLeverX,             //for matching sibling position
   siblingLeverY,
   siblingLeverSum,
   siblingTemperature,

   ARRAYSIZEB
};

//motorPIC commands
enum{
	kMotorStop = 101,
	kMotorLED,
	kMotorVelocity,
	kMotorVoltage,
	kMotorJog,
	kMotorGotoX,
	kMotorGotoY,
	kMotorGotoZ,
	kMotorCenterEncoders,
	kMotorSetVelocity,
	kMotorSetAcceleration,
	kMotorSetDuration,
	kMotorPinchVolts
};

//motorPIC data
enum{
	kMotorX = 1,
	kMotorY,
	kMotorZ,
	kMotorLimits,
	kMotorPinch
};

//comPIC commands
enum{
	kCycleTime = 99
};

#pragma mark ---- Keycode Constants ----

enum {	kKeyDown = 1,
			kKeyRepeat,
			kKeyUp,
			kKeyModifier
};

enum {	kKeyShiftOn = 1,
			kKeyShiftOff,
			kKeyAlphaOn,
			kKeyAlphaOff
};


enum {		kKeyCodeA = 0x00,
				kKeyCodeS,
				kKeyCodeD,
				kKeyCodeF,
				kKeyCodeH,
				kKeyCodeG,
				kKeyCodeZ,
				kKeyCodeX,
				kKeyCodeC,
				kKeyCodeV = 0x09,
				kKeyCodeB = 0x0b,
				kKeyCodeQ,
				kKeyCodeW,
				kKeyCodeE,
				kKeyCodeR,
				kKeyCodeY,
				kKeyCodeT,
				kKeyCodeOne,
				kKeyCodeTwo,
				kKeyCodeThree,
				kKeyCodeFour,
				kKeyCodeSix,
				kKeyCodeFive,
				kKeyCodeEqual,
				kKeyCodeNine,
				kKeyCodeSeven,
				kKeyCodeMinus,
				kKeyCodeEight,
				kKeyCodeZero,
				kKeyCodeRightBracket,
				kKeyCodeO,
				kKeyCodeU,
				kKeyCodeLeftBracket,
				kKeyCodeI,
				kKeyCodeP,
				kKeyCodeReturn,
				kKeyCodeL,
				kKeyCodeJ,
				kKeyCodeRightSingleQuote,
				kKeyCodeK,
				kKeyCodeSemiColon,
				kKeyCodeBackSlash,
				kKeyCodeComma,
				kKeyCodeSlash,
				kKeyCodeN,
				kKeyCodeM,
				kKeyCodePeriod,
				kKeyCodeTab,
				kKeyCodeSpace,
				kKeyCodeTilda,
				kKeyCodeDelete = 0x33,
				kKeyCodeEscape = 0x35,
				
				kKeyCodeFunctionOne = 0x7A,
				kKeyCodeFunctionTwo = 0x78,
				kKeyCodeFunctionThree = 0x63,
				kKeyCodeFunctionFour = 0x76,
				kKeyCodeFunctionFive = 0x60,
				kKeyCodeFunctionSix = 0x61,
				kKeyCodeFunctionSeven = 0x62,
				kKeyCodeFunctionEight = 0x64,
				kKeyCodeFunctionThirteen = 0x69,
				kKeyCodeFunctionSixteen = 0x6A,
				
				kKeyCodeHelp = 0x72,
				kKeyCodeHome = 0x73,
				kKeyCodePageUp = 0x74,
				kKeyCodeDeleteRight = 0x75,
				kKeyCodeEnd = 0x77,
				kKeyCodePageDown = 0x79,
				
				kKeyCodeArrowLeft = 0x7B,
				kKeyCodeArrowRight = 0x7C, 
				kKeyCodeArrowDown = 0x7D,
				kKeyCodeArrowUp = 0x7E,
				
				kKeyCodeNumPadPeriod = 0x41,
				kKeyCodeNumPadStar = 0x43,
				kKeyCodeNumPadPlus = 0x45,
				kKeyCodeNumPadClear = 0x47, 
				kKeyCodeNumPadEnter = 0x4C,
				kKeyCodeNumPadSlash = 0x4B,
				kKeyCodeNumPadMinus = 0x4E,
				kKeyCodeNumPadEqual = 0x51,
				kKeyCodeNumPadZero = 0x52,
				kKeyCodeNumPadOne = 0x53,
				kKeyCodeNumPadTwo = 0x54,
				kKeyCodeNumPadThree = 0x55,
				kKeyCodeNumPadFour = 0x56,
				kKeyCodeNumPadFive = 0x57,
				kKeyCodeNumPadSix = 0x58,
				kKeyCodeNumPadSeven = 0x59,
				kKeyCodeNumPadEight = 0x5B,
				kKeyCodeNumPadNine = 0x5C,
};


#pragma mark ---- Graph Command Constants ----

enum {
	//global graph commands
	kGraphMenu = 4242,		//
	kGraphDrawIndicators,
	
	kGraphNew,
	kGraphDelete,

	kGraphNext,
	kGraphGrid,
	
	//translate, rotate and scale selected objects
	kGraphSelectAxes,
	
	kGraphXincrease,
	kGraphXincreaseOff,
	kGraphXdecrease,
	kGraphXdecreaseOff,
	
	kGraphYincrease,
	kGraphYincreaseOff,
	kGraphYdecrease,
	kGraphYdecreaseOff,
	
	kGraphZincrease,
	kGraphZincreaseOff,
	kGraphZdecrease,
	kGraphZdecreaseOff,
	
	kGraphRotateIncreaseOn,
	kGraphRotateIncreaseOff,
	kGraphRotateDecreaseOn,
	kGraphRotateDecreaseOff,
	
	kGraphZoomOn,
	kGraphZoomOff,
	
	kGraphClearFrameBuffer, 
	
	//plot specific graph commands
	kGraphSampleInterval,
	kGraphChannelDown,
	kGraphChannelUp,
	kGraphColorDown,
	kGraphColorFade,
	kGraphAltColor,
	kGraphColorUp,
	kGraphColorPallete,
	
	kPotentialDown,
	kPotentialUp,
	
	kGraphFreeze,
	kGraphHide,
	kGraphClear,
	
	kGraphCenter,
	kGraphScroll,

	kGraphPoints,	
	kGraphLines,
	kGraphDrawPullLimits,
	kGraphSurface,
	kGraphPolygons,
	kGraphAlphaUp,
	kGraphAlphaDown,
	kGraphBiasUp,
	kGraphBiasDown,
	kGraphGainUp,
	kGraphGainDown,
};


#pragma mark ---- Control Event Constants ----

enum {
	kEventNullParameter = 0,

	kEventStartUSB,
	kEventStopUSB,
	kEventResetGUI,
	kEventResetUSB,
	kEventResetMainPIC,
	kEventResetPiezoPIC,
	kEventResetMotorPIC,
	kEventResetPrefValues,
	kEventImportPrefValues,
	kEventExportPrefValues,
	kEventResetAll,
	kEventChannels,

	kEventSetCycleTime,

	kEventHoppingTrap,	
	kEventTrapA,
	kEventTrapAB,
	kEventTrapB,
	kEventCenterTrap,
	kEventZeroPSDs,
	kEventZeroForce,
	kEventZeroLevers,
	kEventPullOne,
	kEventPullTwo,
	kEventPullThree,
	kEventPiezoRaster,	
	kEventStiffTest,
	kEventAllOff,
	kEventHoldForce1,	//SBS
	kEventHoldForce2,	//SBS
	kEventHighSetpoint,	
	kEventLowSetpoint,
	kEventAutoAlign,
	kEventConstantForce,
	kEventConstantPosition,
	kEventFeedbackOff,
	kEventMovePiezosXY,
	kEventMovePiezosX,
	kEventMovePiezosY,
	kEventAdjustForceRateDiv,
	kEventAdjustForceGain,	
	kEventAdjustPositionRateDiv,
	kEventAdjustPositionGain,
	kEventAdjustGotoSpeed,
	kEventMoveMotors,
	kEventMotorXaxis,
	kEventMotorYaxis,
	kEventMotorZaxis,
	kEventMotorPaxis,  //SBS
	kEventMotorJogRight,
	kEventMotorJogLeft,
	kEventMotorJogUp,
	kEventMotorJogDown,
	kEventMotorJogIn,
	kEventMotorJogOut,
	kEventManualPause,		//SBS
	kEventManualReverse,	//SBS
	kEventHostPositionFeedback, //SBS
	kEventRecordZero,
	kEventRecordOne,
	kEventRecordTwo,
	kEventRecordThree,
	kEventRecordFour,
	kEventRecordFive,
	kEventRecordSix,
	kEventRecordSeven,
	kEventRecordEight,
	kEventRecordNine,
	kEventGotoZero,
	kEventGotoOne,
	kEventGotoTwo,
	kEventGotoThree,
	kEventGotoFour,
	kEventGotoFive,
	kEventGotoSix,
	kEventGotoSeven,
	kEventGotoEight,
	kEventGotoNine,
	kEventStop,
	kEventToggleLED,
	kEventMotorRaster,
	
	kEventWindowTraps,
	kEventWindowMotors,
	kEventWindowGraph,
	kEventWindowForceVsPosition,
	kEventWindowExtensionVsTime,
	kEventWindowForceVsTime,
	kEventWindowLever,
	kEventWindowPSD,
	kEventWindowNetForce,
	kEventWindowOscilloscope,
	kEventWindowFourier,
	kEventWindowMap,
	kEventWindowChannels,
	kEventWindowStokesLawTest,		//JMH
	kEventWindowMotorLever,		//SBS
	kEventWindowLeverLever,		//SBS
	kEventWindowHopping,		//SBS
	kEventWindowIntensityMap,	//SBS
	kEventWindowDualForce,		//SBS
	kEventWindowP1,
	kEventWindowP2,
	kEventWindowCF1,
	kEventWindowCF2,
	kEventFunctGen,				//SBS
	
	kEventChangeWriteStatus,	//ENS
	kEventChangeWriteSpeed,		//ENS
	kEventOpenHelpWindow,		//ENS
	kEventShowTheTime,	//SBS
	kEventYFeedbackOnly	//SBS
};

enum {
	kData = 1,
	kControl,
	kOsx,
	kPosix,
	kQt,
	kHid,
	kOpenGL,
	kPosixInstrumentIO,							//uses VCP driver from FTDI
	kOsxFileIO,
};

//constants used for array of pointers for thread functions
enum {
	kTaskNullParameter = 0,
	kInstrumentTask,
	kTaskFunctionCount
};

enum {
	kTrapA = 0,
	kTrapB,
	kTrapAB
};

void InitControl(pData data);
void ControlTimer(void* userData);
int ControlTimerUpdate(int (*pFunction)(void*), void* data);
int ControlEvent(int event, void* data);
void CommandInstrument (	
			unsigned char trapAbyteID,			unsigned char trapAbyteParam, 
			unsigned char trapAfirstWordID,	unsigned short trapAfirstWordParam,
			unsigned char trapASecondWordID,	unsigned short trapASecondWordParam,
							
			unsigned char trapBbyteID,			unsigned char trapBbyteParam, 
			unsigned char trapBfirstWordID,	unsigned short trapBfirstWordParam,
			unsigned char trapBSecondWordID,	unsigned short trapBSecondWordParam,
							
			unsigned char motorA, unsigned char motorB, 
			unsigned char motorC, unsigned char motorD, 
			unsigned char motorE,
							
			unsigned char comPICbyteID,		unsigned char comPICbyteParam, 
			unsigned char comPICfirstWordID,	unsigned short comPICfirstWordParam,
							
			void* data);

void CommandTrapAB (	
			unsigned char trapAbyteID,			unsigned char trapAbyteParam, 
			unsigned char trapAfirstWordID,	unsigned short trapAfirstWordParam,
			unsigned char trapASecondWordID,	unsigned short trapASecondWordParam,
							
			unsigned char trapBbyteID,			unsigned char trapBbyteParam, 
			unsigned char trapBfirstWordID,	unsigned short trapBfirstWordParam,
			unsigned char trapBSecondWordID,	unsigned short trapBSecondWordParam,
							
			void* data);

void CommandTrap (unsigned char byteID,			unsigned char byteParam, 
						unsigned char firstWordID,		unsigned short firstWordParam,
						unsigned char SecondWordID,	unsigned short SecondWordParam,
						int trapID,							
						void* data);

void CommandMotor (	unsigned char motorA,	unsigned char motorB, 
							unsigned char motorC,	unsigned char motorD,
							unsigned char motorE,							
							void* data);
						
void MotorGoto (int gotoIndex, void* data);

void ControlMouseMoved (int x, int y, int stop, void* data);
							
int ControlThread(int taskFunctionID, void* data);
int ControlThreadCurrentID();
int ControlThreadKillThread(int threadID);
void ControlThreadLockCurrentID(int lockCurrentID);
void CheckPullLimits (void* data);
void SetCycleTime(float cycleTime, void* data);
void stopTimerTask (void* data);
void alignLightLevers (void* data);
void writeComment (char[], void* data);
void rasterPiezos (int size, void* data) ;
void setPositions (int aX, int aY, int bX, int bY, void* data);
void LogTemperature(void* data);
void rasterMotorsXY(int lines, void* dataRef);

//============ENS**Command_Parameters for ltFileIO
enum{
kDataOpen = 0,
kDataWrite,
kDataClose,
kPrefWrite,
kPrefRead,
kComm,
kCalibRead,
kSetWriteSpeedSlow,
kSetWriteSpeedFast
};
//============ENS**Command_Parameters for ltFileIO


#endif _ltcontrol_h_

