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

📄 usbtherm.cpp

📁 Cypress USB HID code
💻 CPP
📖 第 1 页 / 共 2 页
字号:
/**********************************************************
*
*	File: usbtherm.c
*	Purpose: main file for application
*	Date: 3.24.02
*	Author: Stuart Allman
*
*	Copyright 2002 Cypress Semiconductor
*
*	Note that the project settings assume the Windows 98
*	DDK is installed in C:\98DDK.  If not then you will
*	need to change the Linker input settings and the
*	additional include paths.
*
**********************************************************/

/* Include files */
#include <windows.h>
#include <commctrl.h>
#include <stdlib.h>
#include <stdio.h>
#include <setupapi.h>					
#include <dbt.h>
#include <process.h>
extern "C" {
#include <../../src/hid/inc/hidsdi.h>	/* note: part of Windows DDK */
#include <../../src/hid/inc/hidpi.h>	/* note: also part of Windows DDK */
}
#include "usbtherm.h"
#include "resource.h"


/* global variables */
HINSTANCE		hGInstance	= NULL;		/* handle to application instance */
char	*build_time		= __TIME__;		/* build time string */
char	*build_date		= __DATE__;		/* build date string */
BOOL endthread = TRUE;					/* thread end message */
t_units TempDisplayUnits = CELCIUS;		/* displayed temperature units */
BOOL TMThreadActive = FALSE;



/* begin software listing */

//*********************************************************
//
//	Function: WinMain
//	Purpose: Initial starting function for windows apps
//	Parameters: hInstance - handle to an instance of this application
//				hPrevInstance - unused for Win32 apps
//				lpCmdLine - string containing command line parameters
//				nShowCmd - Command of how to show initial window
//
//*********************************************************

int PASCAL WinMain (HINSTANCE hInstance,
					HINSTANCE hPrevInstance,
					LPSTR lpCmdLine,
					int nShowCmd)
{

INITCOMMONCONTROLSEX	ccex;
	
	
	hGInstance = hInstance;			// store global copy of instance handle

	ccex.dwSize = sizeof(ccex);
	ccex.dwICC = ICC_PROGRESS_CLASS;


	/* Initialize the common controls used in this app */
	if(InitCommonControlsEx(&ccex) == FALSE)
	{
		MessageBox(NULL, "Failed to Initialize Application", "Failure!", MB_ICONSTOP);
		return EXIT_FAILURE;
	}


	if(DialogBox(hInstance, MAKEINTRESOURCE(IDD_MAIN), NULL, (DLGPROC) DlgProc) == -1)
	{
		MessageBox(NULL, "Failed to create main dialog box.","Failure!", MB_ICONSTOP);
		return EXIT_FAILURE;
	}

	return	EXIT_SUCCESS;
	}


//*********************************************************
//
//	Function: bMainDlgProc
//	Purpose: This function gets called to handle the main
//			dialog box
//	Parameters: hDlg - handle to this dialog box
//				message - windows messages 
//				wParam - windows message parameters
//				lParam - windows message parameters
//	Returns:
//
//*********************************************************

BOOL CALLBACK DlgProc(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
static HWND hListBox;							/* handle to the GUI list box */
static HWND hTempGraph;							/* handle to the GUI bar graph */
static HWND hTempText;							/* handle to the GUI temperature read out */
char msgstring[256];							/* generic message string for the list box */
static HANDLE HidDevHandle;						/* Handle to HID device */
static BOOLEAN HidAttached;
static HDEVNOTIFY *hDevNotify;					/* notification results */



	/* parse windows messages sent to the dialog box */
	switch(uMsg)	{
	
		/* dialog box initialization */
		case WM_INITDIALOG:
			{
				/* get a handle to the dialog items */
				hListBox = GetDlgItem(hwndDlg, IDC_TESTMSG);
				hTempGraph = GetDlgItem(hwndDlg, IDC_TEMPGRAPH);

				/* Initial startup messaging in list window */
				sprintf(msgstring, "Cypress USB Thermometer - build %s %s", build_time, build_date);
				SendMessage( hListBox, LB_INSERTSTRING, 0, (LPARAM) msgstring);

				/* Initial temperature graph setting */
				SendMessage(hTempGraph, PBM_SETPOS, 0, 0); 

				/* Set the Initial Temperature Setting */
				SetDlgItemText(hwndDlg, IDC_TEMPERATURE, "**癈");

				/* Check to see if the HID Thermometer is attached to the system */
				HidAttached = bOpenHidDevice(&HidDevHandle, VID, PID);

				if(HidAttached == TRUE)
				{
					sprintf(msgstring, "ATTACH: Cypress USB Thermometer found!");
					SendMessage( hListBox, LB_INSERTSTRING, 0, (LPARAM) msgstring);
					CloseHandle(&HidDevHandle);
					endthread = FALSE;
					_beginthread(TMeasure, 0, hwndDlg);
				}

				/* Set up notifications of HID device attach and detach events */
				if( bHidDeviceNotify(hwndDlg, hDevNotify) == FALSE)
					MessageBox(hwndDlg, "Failed to Register Device Notifications", "ERROR", MB_ICONSTOP);				
				
				
				break;
			}
			
		/* these are the various controls in the dialog window */
		case WM_COMMAND:
		{
			break;
		}


		/* When a (meaning ANY) HID device is attached or detached
		the application will receive a WM_DEVICECHANGE message.  The
		application then needs to try to open a handle to the device.
		If it succeeds, then the device is attached.  If it fails, then
		the device was detached.*/
		case WM_DEVICECHANGE:
		{

			/* try to open a handle to the device */
			HidAttached = bOpenHidDevice(&HidDevHandle, VID, PID);
			CloseHandle(HidDevHandle);			

			
			/* take the appropriate action for the message */
			switch(LOWORD(wParam)) {
						
				/* HID device arrival */
				case DBT_DEVICEARRIVAL:
				{
					/* if attached then start temperature measurements */					
					if(endthread == TRUE && HidAttached == TRUE)
					{
						endthread = FALSE;
						_beginthread(TMeasure, 0, hwndDlg);
						sprintf(msgstring, "HID Thermometer attached detected.");					
						SendMessage( hListBox, LB_INSERTSTRING, 0, (LPARAM) msgstring);
					}
					break;
				}

				/* HID device removal */
				case DBT_DEVICEREMOVECOMPLETE:
				{
					/* if failed, then stop temperature measurements */
					if(endthread == FALSE && HidAttached == FALSE)
					{
						/* end the IN endpoint thread */
						endthread = TRUE;
						sprintf(msgstring, "HID Thermometer detach detected.");
						SendMessage( hListBox, LB_INSERTSTRING, 0, (LPARAM) msgstring);
						/* reset the temperature graph */
						SendMessage(hTempGraph, PBM_SETPOS, 0, 0); 
						/* reset the temperature text readout */
						if(TempDisplayUnits == CELCIUS)
							SetDlgItemText(hwndDlg, IDC_TEMPERATURE, "**癈");
						else
							SetDlgItemText(hwndDlg, IDC_TEMPERATURE, "**癋");
					}

					break;
				}

				
				/* HID Device Change - Note that Win2k tends to use this messages
				instead of the previous two. */
				case DBT_DEVNODES_CHANGED:
				{
				
					/* if success then start temperature measurements */					
					if(endthread == TRUE && HidAttached == TRUE)
					{
						endthread = FALSE;
						_beginthread(TMeasure, 0, hwndDlg);
						sprintf(msgstring, "HID Thermometer attached detected.");					
						SendMessage( hListBox, LB_INSERTSTRING, 0, (LPARAM) msgstring);
					}
					
					/* if failed, then stop temperature measurements */
					else if(endthread == FALSE && HidAttached == FALSE)
					{
						/* end the IN endpoint thread */
						endthread = TRUE;
						sprintf(msgstring, "HID Thermometer detach detected.");
						SendMessage( hListBox, LB_INSERTSTRING, 0, (LPARAM) msgstring);
						/* reset the temperature graph */
						SendMessage(hTempGraph, PBM_SETPOS, 0, 0); 
						/* reset the temperature text readout */
						if(TempDisplayUnits == CELCIUS)
							SetDlgItemText(hwndDlg, IDC_TEMPERATURE, "**癈");
						else
							SetDlgItemText(hwndDlg, IDC_TEMPERATURE, "**癋");
					}					
					
					break;
				}



				default:
					break;
			}
			
			break;
		}


		/* little "X" in upper right of window */
		case WM_CLOSE:
			{
				/* stop the temperature measurement thread */
				endthread = TRUE;

				/* wait for temperature measurement thread to stop */
				if(TMThreadActive == TRUE)
				{
					PostMessage(hwndDlg, WM_CLOSE, 0, 0);
					break;
				}
				/* end HID device plug/unplug notifications */
				UnregisterDeviceNotification( hDevNotify );

				/* end the dialog box */
				EndDialog(hwndDlg, 0);
				break;
			}
	
		default:
			break;
		}

	
	return	FALSE;
	}


/**********************************************************
*
*	Function TMeasure
*	Purpose: This thread takes temperature measurements from
*		the IN endpoint and reports them to the main window
*	Parameters: none
*	Returns: none
*
**********************************************************/

void TMeasure(LPVOID lpParameter)
{

	HWND			hDlg = (HWND) lpParameter;
	HWND			hOutputBox;
	HWND			hTempGraph;
	HANDLE			hDevice;
	unsigned long	numBytesReturned;
	unsigned char	inbuffer[9];		/* input buffer*/
	unsigned char   outbuffer[9];		/* output buffer */
	BOOL			bResult;
	HIDP_CAPS		Capabilities;
	PHIDP_PREPARSED_DATA		HidParsedData;
	OVERLAPPED		HidOverlapped;
	HANDLE			ReportEvent;
	short			temperature = 0;	/* temperature measurement */

	char msgstring[64];


	TMThreadActive = TRUE;

	/* get a handle to the dialog box controls */
	hOutputBox = GetDlgItem (hDlg, IDC_TESTMSG);
	hTempGraph = GetDlgItem (hDlg, IDC_TEMPGRAPH);
	
	
	SendMessage( hOutputBox, LB_INSERTSTRING, 0, (LPARAM) "Started Temperature monitor");

	

	/* Open the HID device handle */
	if(bOpenHidDevice( &hDevice, VID, PID) == TRUE)
	{
		/* get a handle to a buffer that describes the device's capabilities.  This
		line plus the following two lines of code extract the report length the
		device is claiming to support */
		HidD_GetPreparsedData(hDevice, &HidParsedData);
		

⌨️ 快捷键说明

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