Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version

Writing a Sensor Driver for the Wiimote on Windows 7

, 16 Feb 2010 CPOL
How to write a Sensor driver that provides access to the 3-axis accelerometer on a Nintendo Wiimote on Windows 7
amd64.zip
amd64
WiimoteSensor.dll
WiimoteSensor.inf
WiimoteSensor.pdb
WUDFUpdate_01009.dll
i386.zip
i386
WiimoteSensor.dll
WiimoteSensor.inf
WiimoteSensor.pdb
WUDFUpdate_01009.dll
src.zip
src
Driver
buildfre_win7_amd64.wrn
buildfre_win7_x86.wrn
install
makefile
mbld
sources
WiimoteSensor.aps
WiimoteSensor.ctl
WiimoteSensor.def
WiimoteSensor.inx
Wiidiag
bin
lib
Microsoft.WindowsAPICodePack.dll
Microsoft.WindowsAPICodePack.pdb
Microsoft.WindowsAPICodePack.Sensors.dll
Microsoft.WindowsAPICodePack.Sensors.pdb
Properties
Settings.settings
WiiSensor.suo
tools.zip
tools
devcon
amd64
devcon.exe
i386
devcon.exe
PlaneDemo
DemoApp.exe
DemoApp.pdb
Microsoft.WindowsAPICodePack.dll
Microsoft.WindowsAPICodePack.pdb
Microsoft.WindowsAPICodePack.Sensors.dll
Microsoft.WindowsAPICodePack.Sensors.pdb
WiimoteTest
WiimoteLib.dll
WiimoteTest.exe
Wiidiag.zip
Wiidiag
Microsoft.WindowsAPICodePack.dll
Microsoft.WindowsAPICodePack.Sensors.dll
Wiidiag.exe
#pragma once

//
// helper macros to create static property lists
//
#define MAKEPROP(k, v)		(&CDeviceProperty(k, v))
#define DEVPROPS(...)		__VA_ARGS__, NULL

//
// This class manages a collection of values and hands out
// "IPortableDeviceKeyCollection" and "IPortableDeviceValues"
// objects as needed.
//
class CPortableDeviceProps
{
public:
	//
	// key and value collection objects
	//
	CComPtr<IPortableDeviceKeyCollection>	m_pKeys;
	CComPtr<IPortableDeviceValues>			m_pValues;

	//
	// if initialization fails then this HR stores the error
	// code
	//
	HRESULT									m_hrCreate;

protected:
	HRESULT Init();
	HRESULT InternalAddProps(vector<CDeviceProperty *> props);

public:
	//
	// Constructs a "CPortableDeviceProps" object.
	//
	CPortableDeviceProps();

	//
	// Constructs a "CPortableDeviceProps" object.  Allows convenient
	// syntax to build static properties lists like so:
	//
	//		CPortableDeviceProps props(DEVPROPS(
	//			MAKEPROP( SENSOR_PROPERTY_CONNECTION_TYPE, SENSOR_CONNECTION_TYPE_PC_ATTACHED ),
	//			MAKEPROP( SENSOR_PROPERTY_DESCRIPTION, L"Doofus" )
	//		));
	//
	CPortableDeviceProps(CDeviceProperty *prop, ...);

	//
	// Adds a set of properties to the collection.  Allows convenient
	// syntax to build properties lists like so:
	//
	//		props.AddProps(DEVPROPS(
	//			MAKEPROP( SENSOR_PROPERTY_CONNECTION_TYPE, SENSOR_CONNECTION_TYPE_PC_ATTACHED ),
	//			MAKEPROP( SENSOR_PROPERTY_DESCRIPTION, L"Doofus" )
	//		));
	//
	//	If this method returns a failure code the object must be considered
	//	as being in an inconsistent state.  It will be safer to just call "Clear"
	//	and retry.
	//
	HRESULT AddProps(CDeviceProperty *prop, ...);

	//
	// Sets the value for a given key to an error code.
	//
	HRESULT SetError(REFPROPERTYKEY key, HRESULT code);

	//
	// Clears all keys and values.
	//
	HRESULT Clear();

	//
	// Helper routine to set values for properties in a type independant
	// manner.
	//
	template<typename T>
	HRESULT SetValue(REFPROPERTYKEY key, const T& value)
	{
		//
		// if create failed then we can't do anything
		//
		if(FAILED(m_hrCreate))
			return m_hrCreate;

		//
		// load it up into a "CDeviceProperty" so it maps it to
		// the correct PROPVARIANT object
		//
		CDeviceProperty prop(key, value);

		//
		// if this key already exists then just update value; otherwise
		// add the key
		//
		PROPVARIANT pvar;
		PropVariantInit(&pvar);
		HRESULT hr = m_pValues->GetValue(key, &pvar);
		if(hr == HRESULT_FROM_WIN32(ERROR_NOT_FOUND))
			hr = m_pKeys->Add(key);
		if(FAILED(hr))
			return hr;

		return m_pValues->SetValue(key, &prop.m_value);
	}

	//
	// Helper routine to initialize a property with a PROPVARIANT.
	//
	HRESULT SetValue(REFPROPERTYKEY key, const PROPVARIANT& value);

	//
	// Helper routine to get values for properties in a type independant
	// manner.
	///
	template<typename T>
	HRESULT GetValue(REFPROPERTYKEY key, T& val);
};

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Rajasekharan Vengalil
Microsoft
United States United States
No Biography provided
Follow on   Twitter   Google+

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.150123.1 | Last Updated 16 Feb 2010
Article Copyright 2010 by Rajasekharan Vengalil
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid