#if !defined(SonyVisca_H__2EFB8930_0DA4_4B4E_9614_77CC6D44D9FD__INCLUDED_)
#define SonyVisca_H__2EFB8930_0DA4_4B4E_9614_77CC6D44D9FD__INCLUDED_
#endif

#if _MSC_VER > 1000
#pragma once
#endif		// _MSC_VER > 1000

#pragma warning(disable : 4786)

#include "resource.h"

// Global Token Definitions
#define DEEP_DEBUG	0			// Set to 1 to compile extra debug output. Set to zero for release code.
#define MAX_LOADSTRING 100		// Maximum length of a string read from resource file
#define	MAX_BUFFER_STRING 256
#define BBC_MSG_BUFFER 512		// Size of buffer used by BBC 32-bit messaging
#define APP_WIDTH 526			// Window width
#define APP_HEIGHT 560			// Window Height
#define	MAX_LB_ITEMS 100		// Maximum error reports in debug listbox
#define VISCA_MAXMESSAGE 16		// Maximum number of bytes in a VISCA message

// Define some colours used in this App
#define COL_BLACK  RGB(0,0,0)
#define COL_WHITE RGB(255,255,255)
#define COL_DK_RED RGB(127,0,0)
#define COL_DK_GRN RGB(0,127,0)
#define COL_DK_BLUE RGB(0,0,80)
#define COL_DK_ORANGE RGB(128,64,0)
#define COL_LT_RED RGB(255,0,0)
#define COL_LT_GRN RGB(0,255,0)
#define COL_LT_BLUE RGB(100,100,255)
#define COL_LT_YELLOW RGB(255,255,0)
#define COL_YELLOW RGB(255,255,0)
#define COL_DLG_BG RGB(160,180,210)  // This colour is the background colour of the dialog box.

// Timer definitions used in this application.
#define STARTUP_TIMER_ID	1			// Used in the startup processing to allow elements to stabilise before we do more work
#define STARTUP_INIT_DELAY	100			// Initial delay is 100 ms
#define STARTUP_TIMER_INTERVAL	1000
#define COMMSTEST_TIMER_ID	2
#define COMMSTEST_TIMER_INTERVAL 4000
#define COMMS_TIMEOUT_ID	3
#define COMMS_TIMEOUT_INTERVAL	1500	// Allow up to 1.5 second for a message reply, as some completion messages may have a delay.

// User defined messages for private use
#ifndef WM_USER
#ifndef _WIN32
#define WM_APP	0x8000
#endif
#endif
#define RXMSG_PROCESS (WM_APP + 0x0001)

// Define enums needed so we can see logical control case labels rather than just numbers.
enum Misc_Box_Mode :int { MBM_Norm = 0, MBM_Good, MBM_Alert, MBM_Fail };			// Used to control colours used in misc status box.
enum msgReason :int{ mr_Command = 1, mr_Reply, mr_Timeout, mr_ClearAll };			// Reasons that message processor is invoked
enum viscaReplyMode :unsigned int {	// Reply modes for Visca Commands
	vrmAckOnly=1,		// Only returns an ACK message (not yet in use)
	vrmAckComplete,		// Returns ACK, then later returns command complete
	vrmDataOnly,		// Only returns Data
	vrmNone				// No reply
};	

enum ViscaCommands :int {
	vc_noCmd = 0,			// A null command that is used when there is no message to send
	vc_AddressSet,			// Broadcast the Address Set message
	vc_IF_clear,			// Clear the interface
	vc_ZoomStop,			// Stop any zoom change that is active
	vc_ZoomTeleStandard,	// Go Telephoto at "standard" rate
	vc_ZoomWideStandard,	// Go Wide angle at "standard" rate
	vc_ZoomTeleVar,			//
	vc_ZoomWideVar,			//
	vc_ZoomSetPosition,		// Set zoom position absolute
	vc_FocusStop,
	vc_FocusFarStd,
	vc_FocusNearStd,
	vc_FocusFarVar,
	vc_FocusNearVar,
	vc_FocusDirect,
	vc_FocusAuto,
	vc_FocusManual,
	vc_WBAuto,
	vc_WBIndoor,
	vc_WBOutdoor,
	vc_WBOnePushMode,		// Wait for trigger to start colour balance
	vc_WBOnePushTrigger,	// Trigger auto white balance acrion
	vc_AEAuto,
	vc_AEManual,
	vc_AEShuttterPriority,
	vc_AEIrisPriority,
	vc_AEBrightMode,
	vc_BrightReset,
	vc_BrightUp,
	vc_BrightDown,
	vc_ShutterReset,
	vc_ShutterUp,
	vc_ShutterDown,
	vc_ShutterDirect,
	vc_IrisReset,
	vc_IrisUp,
	vc_IrisDown,
	vc_IrisDirect,				// Set iris to index code, if in manual
	vc_GainReset,				// Set default gain
	vc_GainUp,
	vc_GainDown,
	vc_GainDirect,				// Set the gain defined by the index code
	vc_BacklightOn,
	vc_BacklightOff,
	vc_MemoryReset,				// Clear shotbox defined in command
	vc_MemorySet,				// Set shotbox memory 0 through 5
	vc_MemoryRecall,			// Recall shotbox memory 0 through 5
	vc_PanTiltUp,				// Tilt up at user set rate
	vc_PanTiltDown,				
	vc_PanTiltLeft,
	vc_PanTiltRight,
	vc_PanTiltUpLeft,			// Up and left can have different speeds
	vc_PanTiltUpRight,
	vc_PanTiltDownLeft,
	vc_PanTiltDownRight,
	vc_PanTiltStop,				// Stop moving the pan tilt head
	vc_PanTiltAbsolute,			// Go to user provided pan and tilt position
	vc_PanTiltHome,				// Go to home position (fastest rate)
	vc_PanTiltReset,
	vc_WBManual,				// Only use with BRC300P
	vc_RedGainReset,			// Valid for BRC300P
	vc_RedGainValue,			// Valid for BRC300P
	vc_BlueGainReset,			// Valid for BRC300P
	vc_BlueGainValue,			// Valid for BRC300P
	vc_TallyLight,				// Valid for BRC300P
	// Status enquiry message ids
	vc_PowerInq,				// Return state of camera power. Used to check comms to camera
	vc_ArraySize				// KEEP AS LAST IN LIST - sets size for command array
};

// Structures needed for camera message queue operation.
struct vCmdIf {
	unsigned int command;	// Command index that creates the final message sent
	unsigned int replymode;	// 1=ACK Only, 2=ACK + command complete, 3=ACK + data, 4 data only. See enum viscaReplyMode
	unsigned int ackRx;		// Flag to show an ACK has been received.
	unsigned int isDone;	// Flag to show actions have been completed and response received.
	unsigned int socket;	// Extracted from the ACK reply. Need to look for command complete from same socket.
	int cmdLength;			// Number of active bytes in the cmdArray
	int replyLength;		// Number of bytes in the reply, including terminating 0xFF.
	BYTE cmdArray[VISCA_MAXMESSAGE];	// The sequence of bytes passed to the serial output system
	BYTE reply[VISCA_MAXMESSAGE];		// The sequence of bytes returned by the command
};

struct RxMessage {
	BYTE response[VISCA_MAXMESSAGE];	// Maximum message packet from camera
};

// ========================== //
// Global Function Prototypes //
// ========================== //

// Functions defined in Main.cpp
void Debug(LPCSTR szFmt, ...);
void forceDebug(LPCSTR szFmt, ...);

// Functions defined in SonyVisca.cpp
void setSingleControllerMode(void);		// Tell infodriver not to support redundancy
bool connectToExt(int driverID);		// Connect to external Infodriver
bool InfoDriverConnected(void);			// Report if the external infodriver is connected
bool disconnectFromExt(void);			// Disconnect from the external infodriver
bool setSlotContent(int index, const char* value, bool sendRevertive);	// Set the slot contents in the infodriver
int openSerialPort(void);
void closeCommPort(void);
BOOL setRxProcessor();
void sendSerial(BYTE *msg, long numChar);
void ViscaMsg(int reason);				// Transmit command or process a received message
void OpenCameraComms(void);				// Initialises comms to the camera.
void updateSerialTxCount(UINT msgCount);
void updateSerialRxCount(UINT msgCount);
void updateInfoTxCount(UINT msgCount);
void updateInfoRxCount(UINT msgCount);
void SendCommsTestMsg(void);			// Inserts the background "Link Alive?" command into command queue
int inputHWM(void);						// Report the maximum used depth of input message queue from camera
int sendmsgHWM(void);					// Report the maximum used depth of command message queue to camera
void clearAllQueues(void);				// Clear any data from queues - used for application closedown.

////////////////////////////////////////////////////////////////////////////////
// Global Variables
// ================
// One of the compilation modules defines the EXT token, and hence this module
// will define the storage for the variables. All other modules do not have the
// token defined, and hence the names below become references to the globals so
// that the compiler/linker produce resolved symbols.

#ifndef EXT
#define EXT extern
#endif


// Various handles.
EXT HINSTANCE hInst;						// Handle for the Application
EXT HWND hAppWnd;							// Window handle for the main window
EXT HWND hWndList;							// Window handle for the listbox used to display diagnostic data
EXT HWND hWndDlg;							// Window handle for child dialog

EXT RECT rc;								// Used to compute debug listbox size

// Character arrays.
EXT TCHAR szTitle[MAX_LOADSTRING];			// The title bar text
EXT TCHAR szWindowClass[MAX_LOADSTRING];	// the main window class name
EXT TCHAR fileVersion[MAX_LOADSTRING];		// Holds the extracted file version number - Format similar to 1.3.0.4 
											// where elements are major version, minor version, revision, build id.
EXT TCHAR myBNCS_File_Path[MAX_PATH];		// Folder that holds the BNCS INI files
EXT TCHAR myBNCS_Data_Path[MAX_PATH];		// Folder that holds the BNCS Data files
EXT char szResult[MAX_BUFFER_STRING];		// Used by profile item reads

// Various BOOL variables
EXT BOOL fDebug;				// Debug control flag - when true debug messages written to listbox and Windows
								// Actual value is fetched from dev_xxx.ini file
EXT BOOL fDebugShow;			// Flag that controls the hide/show of the local listbox
EXT BOOL fCameraCommStat;		// Flag to indicate status of comms to the camera. TRUE for COMMS OK
EXT BOOL fExtIdConnected;		// Set true whe external infodriver has connected ok.

// Globally accessible integers.
EXT int m_nCmdShow;				// Stores the window display mode sent in via the command line
EXT int majorFileVersion;		// File version major release
EXT int minorFileVersion;		// File version minor release
EXT int revisionFileVersion;	// File version revision
EXT int buildFileVersion;		// File version build version.
EXT int myDriverId;				// Driver ID extracted from command line
EXT int host_BNCS_system;		// BNCS system type determined by function get_BNCS_paths.
EXT int iChildHeight;			// Holds the height of the user client application window
EXT int myWS_ID;				// Workstation ID that is hosting the driver.
EXT int csi_timeout_counter;	// Used to limit time we wait for a broadcast message from CSI
EXT int driverMode;				// Contains the operational mode of the driver
EXT int miscMsgState;			// Value in this parameter controls the colour of the misc message box.

EXT UINT infoTxCount;			// Holds the number of messages sent to CSI
EXT UINT infoRxCount;			// Holds the number of messages received from CSI
EXT UINT serialTxCount;			// Holds the number of messages sent to serial comms port
EXT UINT serialRxCount;			// Holds the number of messages received from serial comms port

// Comms parameters
EXT int comPort;				// Port number to communicate with VIOM		
EXT int comBaud;				// Baud rate to use
EXT int comDataBit;				// Number of data bits
EXT int comStop;				// Number of stop bits
EXT int comParity;				// Parity 0=none, 1=Even, 2=Odd
EXT BOOL comPortOpen;			// TRUE if the serial comms port is available

// Storage for brush handles
EXT	HBRUSH hbrWindowsGrey;
EXT	HBRUSH hbrBlack;
EXT	HBRUSH hbrWhite;
EXT	HBRUSH hbrDarkRed;
EXT	HBRUSH hbrDarkGreen;
EXT	HBRUSH hbrDarkBlue;
EXT	HBRUSH hbrDarkOrange;
EXT	HBRUSH hbrYellow;
EXT	HBRUSH hbrDlgBgd;
EXT	HBRUSH hbrLightRed;
EXT	HBRUSH hbrLightGreen;
EXT	HBRUSH hbrLightYellow;
EXT	HBRUSH hbrLightBlue;

EXT HBRUSH HBr, HBrOld;
EXT COLORREF iCurrentTextColour;
EXT COLORREF iCurrentBackgroundColour;
EXT HBRUSH iCurrentBrush;
