Hardware interface

The PhidgetInterfaceKit 8/8/8 (which we’ll call an 888) connects up to eight zones to a PC through a USB port. The 888 uses the Human Interface Device (HID) standard USB protocol and drivers built into Windows, eliminating any requirement to install system-level software. The HID standard was designed to support mice, keyboards, joysticks, and similar devices. If you think about the 888 in that light, it looks like a very capable joystick having lots of switches (digital inputs), axes (analog inputs), and indicator lights (digital outputs).

Programming to the HID specification is somewhat complex, so we used the phidget20 library, which encapsulates the interface to the HID device into a collection of functions. There are two elements of the phidget20 interface you could use — the PhidgetManager identifies the connected Phidget devices and will notify your code when one is attached or detached from the USB bus, while the PhidgetInterfaceKit operates the device itself.We assumed the 888 was always connected in our code; see the discussion later for how you might extend the code to detect attach and detach operations.

The HID protocol is not interrupt driven — you have to poll the device for status. The PhidgetInterfaceKit application programming interface (API) follows the usual  open/read/write/close pattern; we open the device and keep it open, then read it in a polling loop, sleeping between reads. There are three ways to implement that pattern in MFC:

1. Override the application’s OnIdle method, so your routine can be called when the application message queue is empty.We wanted to control how often we polled the 888, which could be done this way by monitoring the clock, but it’s clumsy.

2. Use a Windows timer (via the WM_TIMER message) to periodically invoke a processing routine. This is a good approach, which we discarded only because we had no data on how long the calls into the phidget20 library might take, and don’t want to keep the main application thread running for any significant time.

3. Create a worker thread using AfxBeginThread whose responsibility is to loop continuously polling the interface. This is the approach we used.