⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mavisctl.cpp

📁 机器人程序
💻 CPP
📖 第 1 页 / 共 3 页
字号:
		);

	}
	pStatus->update( gStatusMsgBuf );


	return 0;
}


//
//  FUNCTION: FrameCB(BYTE *, int)
//
//  PURPOSE:  Callback that Mavis invokes when a
//            new frame is ready to display.
//
int FrameCB(BYTE * pBuff, int nBytes)
{
	if(nBytes > bufSize)
	{
		bufSize = nBytes;
		if(pBmpBuffer) delete[] pBmpBuffer;
		pBmpBuffer = new BYTE[bufSize];
	}

	memcpy(pBmpBuffer, pBuff, nBytes);

	DisplayBitmap();

	return 0;
}


//
//  FUNCTION: RunMavis(LPVOID)
//
//  PURPOSE:  Run Mavis in a separate thread.
//
DWORD WINAPI RunMavis(LPVOID lpThreadData)
{
	if(pMv) pMv->run();
	return 0;
}


//
//  FUNCTION: DisplayBitmap()
//
//  PURPOSE:  Draws the frame received from Mavis
//
void DisplayBitmap()
{
	if( !pBmpBuffer ) return;

	HDC hdc = GetDC( ghWnd );
	PAINTSTRUCT ps;
	BeginPaint(ghWnd, &ps);

	SetStretchBltMode(hdc, COLORONCOLOR);
	StretchDIBits(
		hdc,
		rcBmp.left, rcBmp.top,
		abs(rcBmp.right-rcBmp.left),
		abs(rcBmp.bottom-rcBmp.top),
		0, 0, bih.biWidth, bih.biHeight,
		pBmpBuffer,
		(BITMAPINFO*) &bih,
		DIB_RGB_COLORS,
		SRCCOPY
	);

	EndPaint(ghWnd, &ps);
	ReleaseDC( ghWnd, hdc );
}


bool LoadMavisDll()
{
	// load the communications dll
	try
	{
		mavisdll = LoadLibrary("mavisclient");

		if(NULL == mavisdll) throw MavisErr( "can't load mavisclient.dll" );

		procLookOnce = (LOOKONCEPROC)GetProcAddress(mavisdll, LOOKONCEPROC_NAME);
		if(NULL == procLookOnce)
			throw MavisErr( "can't access mavisclient.dll method for LOOKONCEPROC" );

		procStartLooking = (STARTLOOKPROC)GetProcAddress(mavisdll, STARTLOOKPROC_NAME);
		if(NULL == procStartLooking)
			throw MavisErr( "can't access mavisclient.dll method for STARTLOOKPROC" );

		procStopLooking = (STOPLOOKPROC)GetProcAddress(mavisdll, STOPLOOKPROC_NAME);
		if(NULL == procStopLooking)
			throw MavisErr( "can't access mavisclient.dll method for STOPLOOKPROC" );

		procLocateHLines = (LOCATEHLINESPROC)GetProcAddress(mavisdll, LOCATEHLINESPROC_NAME);
		if(NULL == procLocateHLines)
			throw MavisErr( "can't access mavisclient.dll method for LOCATEHLINESPROC" );

		procNextLine = (NEXTLINEPROC)GetProcAddress(mavisdll, NEXTLINEPROC_NAME);
		if(NULL == procNextLine)
			throw MavisErr( "can't access mavisclient.dll method for NEXTLINEPROC" );

		procNextFrame = (NEXTFRAMEPROC)GetProcAddress(mavisdll, NEXTFRAMEPROC_NAME);
		if(NULL == procNextFrame)
			throw MavisErr( "can't access mavisclient.dll method for NEXTFRAMEPROC" );
	}
	catch(MavisErr& e)
	{
		string msg = "Error: ";
		msg += e.getMsg();
		MessageBox(NULL, TEXT(msg.c_str()), gszTitle, 0);
		return false;  // failure
	}
	catch(...)
	{
		MessageBox(NULL, TEXT("Can't load Mavis client dll - unknown error"), gszTitle, 0);
		return false;  // failure
	}

	// print a status  msg
	sprintf( gStatusMsgBuf, "%s", "Dll loaded. Use button to send requests to Mavis." );
	pStatus->update( gStatusMsgBuf );

	return true; // success
}

void UnloadMavisDll()
{
	if( LOOK_MODE == gAppState ) procStopLooking();

	// unload the communications dll
	if(mavisdll) FreeLibrary(mavisdll);
	mavisdll = NULL;
	procLookOnce     = NULL;
	procStartLooking = NULL;
	procStopLooking  = NULL;
	procLocateHLines = NULL;
	procNextLine     = NULL;
	procNextFrame    = NULL;

	// print a status  msg
	sprintf( gStatusMsgBuf, "%s", "Dll unloaded. Mavis is ready to accept outside requests." );
	pStatus->update( gStatusMsgBuf );
}

void SetAppState(int state)
{
	// update file menu
	switch(state)
	{
		case WAIT_MODE:
			EnableMenuItem(GetMenu(ghWnd), MENU_LOAD_DLL,   MF_GRAYED);
			EnableMenuItem(GetMenu(ghWnd), MENU_UNLOAD_DLL, MF_ENABLED);
			sprintf( gStatusMsgBuf, "Ready" );
			break;

		case REMOTE_MODE:
			EnableMenuItem(GetMenu(ghWnd), MENU_UNLOAD_DLL, MF_GRAYED);
			EnableMenuItem(GetMenu(ghWnd), MENU_LOAD_DLL,   MF_ENABLED);
			sprintf( gStatusMsgBuf, "dll is unloaded" );
			break;

		case CALIBRATE_MODE:
			EnableMenuItem(GetMenu(ghWnd), MENU_UNLOAD_DLL, MF_GRAYED);
			EnableMenuItem(GetMenu(ghWnd), MENU_LOAD_DLL,   MF_GRAYED);
			sprintf( gStatusMsgBuf, "Ready to calibrate camera" );
			break;

		case HLINES_MODE:
			EnableMenuItem(GetMenu(ghWnd), MENU_UNLOAD_DLL, MF_GRAYED);
			EnableMenuItem(GetMenu(ghWnd), MENU_LOAD_DLL,   MF_GRAYED);
			sprintf( gStatusMsgBuf, "HLines series in progress" );
			break;

		case LOOK_MODE:
			EnableMenuItem(GetMenu(ghWnd), MENU_LOAD_DLL,   MF_GRAYED);
			EnableMenuItem(GetMenu(ghWnd), MENU_UNLOAD_DLL, MF_GRAYED);
			sprintf(gStatusMsgBuf, "Looking for %s", gObjNameBuf);
			break;
	}
	// print a status  msg
	pStatus->update( gStatusMsgBuf );


	// Set visibility for high-level controls that let user
	// choose an activity.
	int visibility;
	switch(state)
	{
		case WAIT_MODE:
			visibility = SW_SHOW;
			break;

		case REMOTE_MODE:
		case CALIBRATE_MODE:
		case HLINES_MODE:
			visibility = SW_HIDE;
			break;
	}

	if( LOOK_MODE == state)
	{
		ShowWindow(ghwndLookButton,      SW_HIDE);
		ShowWindow(ghwndCalibrateButton, SW_HIDE);
		ShowWindow(ghwndHLinesButton,    SW_HIDE);
		ShowWindow(ghwndLookContButton,  SW_SHOW);

		// show the currently-selected list item
		LRESULT currInd = SendMessage(ghWndObjList, LB_GETCURSEL, (WPARAM)0, (LPARAM)0);
		SendMessage(ghWndObjList, LB_SETCURSEL, (WPARAM)currInd, (LPARAM)0);

		// then disable the select list
		EnableWindow(ghWndObjList, FALSE);
	}
	else
	{
		EnableWindow(ghWndObjList, TRUE);
		ShowWindow(ghWndObjList,         visibility);
		ShowWindow(ghwndLookButton,      visibility);
		ShowWindow(ghwndLookContButton,  visibility);
		ShowWindow(ghwndCalibrateButton, visibility);
		ShowWindow(ghwndHLinesButton,    visibility);
	}


	// Set visibility for Next and Stop buttons.
	switch(state)
	{
		case CALIBRATE_MODE:
		case HLINES_MODE:
			visibility = SW_SHOW;
			break;

		case REMOTE_MODE:
		case WAIT_MODE:
		case LOOK_MODE:
			visibility = SW_HIDE;
			break;
	}
	ShowWindow(ghwndNextButton, visibility);
	ShowWindow(ghwndStopButton, visibility);


	// Set visibility for the calibration instructions
	if(CALIBRATE_MODE == state)
		ShowWindow(ghwndInstrText,  SW_SHOW);
	else
		ShowWindow(ghwndInstrText,  SW_HIDE);

	// Set visibility for the HLine controls
	if(HLINES_MODE == state)
	{
		ShowWindow(ghwndHLinesText,  SW_SHOW);
		ShowWindow(ghwndTravelDist,  SW_SHOW);
		ShowWindow(ghwndMMText,      SW_SHOW);
		// put cursor into the edit control
		SetFocus(ghwndTravelDist);
	}
	else
	{
		ShowWindow(ghwndHLinesText,  SW_HIDE);
		ShowWindow(ghwndTravelDist,  SW_HIDE);
		ShowWindow(ghwndMMText,      SW_HIDE);
	}


	gAppState = state;
}


//
//  FUNCTION: OnClose()
//
//  PURPOSE: Release application resources.
//
void OnClose()
{
	// unload the communications dll
	UnloadMavisDll();

	if(hMavisThread) CloseHandle(hMavisThread);
	if(pBmpBuffer) delete [] pBmpBuffer;
	if(pStatus) delete pStatus;

	if(pMv) delete pMv;

	mavisdll = NULL;
	pMv = NULL;
	pBmpBuffer = NULL;
	bufSize = 0;
	pStatus = NULL;
	hMavisThread = NULL;
}


//
//  FUNCTION: MyRegisterClass()
//
//  PURPOSE: Registers the window class.
//
//  COMMENTS:
//
//    This function and its usage is only necessary if you want this code
//    to be compatible with Win32 systems prior to the 'RegisterClassEx'
//    function that was added to Windows 95. It is important to call this function
//    so that the application will get 'well formed' small icons associated
//    with it.
//
ATOM MyRegisterClass(HINSTANCE hInstance)
{
	WNDCLASSEX wcex;

	wcex.cbSize = sizeof(WNDCLASSEX);

	wcex.style			= CS_HREDRAW | CS_VREDRAW;
	wcex.lpfnWndProc	= (WNDPROC)WndProc;
	wcex.cbClsExtra		= 0;
	wcex.cbWndExtra		= 0;
	wcex.hInstance		= hInstance;
	wcex.hIcon			= LoadIcon(hInstance, (LPCTSTR)IDI_MAVISCTL);
	wcex.hCursor		= LoadCursor(NULL, IDC_ARROW);
	wcex.hbrBackground	= (HBRUSH)(COLOR_WINDOW+1);
	wcex.lpszMenuName	= (LPCSTR)IDC_MAVISCTL;
	wcex.lpszClassName	= gszWindowClass;
	wcex.hIconSm		= LoadIcon(wcex.hInstance, (LPCTSTR)IDI_SMALL);

	return RegisterClassEx(&wcex);
}


//
//  FUNCTION: About(HWND, UINT, WPARAM, LPARAM)
//
//  PURPOSE: Mesage handler for about box.
//
LRESULT CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
	switch (message)
	{
		case WM_INITDIALOG:
				return TRUE;

		case WM_COMMAND:
			if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
			{
				EndDialog(hDlg, LOWORD(wParam));
				return TRUE;
			}
			break;
	}
	return FALSE;
}

//
//  These two functions forward messages to the status bar
//
LRESULT CALLBACK StatusProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
	return pStatus->wndProc(hwnd, msg, wParam, lParam);
}

LRESULT CALLBACK StatusText(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
	return pStatus->textProc(hwnd, msg, wParam, lParam);
}

///////////////////////////////////////////////////////////////////////////////////////
// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
//
// By downloading, copying, installing or using the software you agree to this
// license. If you do not agree to this license, do not download, install, copy or
// use the software.
//
//
//                        Mavis License Agreement
//
// Copyright (c) 2004-2005, Robin Hewitt (http://www.robin-hewitt.com).
// Third party copyrights are property of their respective owners.
//
// Redistribution and use in source and binary forms, with or without modification,
// are permitted provided that the following conditions are met:
//
//   * Redistributions of source code must retain the above copyright notice,
//     this list of conditions and the following disclaimer.
//
//   * Redistributions in binary form must reproduce the above copyright notice,
//     this list of conditions and the following disclaimer in the documentation
//     and/or other materials provided with the distribution.
//
// This software is provided "as is" and any express or implied warranties, including,
// but not limited to, the implied warranties of merchantability and fitness for a
// particular purpose are disclaimed. In no event shall the authors or contributors be
// liable for any direct, indirect, incidental, special, exemplary, or consequential
// damages (including, but not limited to, procurement of substitute goods or services;
// loss of use, data, or profits; or business interruption) however caused and on any
// theory of liability, whether in contract, strict liability, or tort (including
// negligence or otherwise) arising in any way out of the use of this software, even
// if advised of the possibility of such damage.
///////////////////////////////////////////////////////////////////////////////////////

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -