<?xml version='1.0' encoding='UTF-8'?>

<?xml-stylesheet href="./_c74_tut.xsl" type="text/xsl"?>

<chapter name="Max Comm Tutorial 1: Human-Interface Devices">

<setdocpatch name="01cHIDCommunication" patch="01cHIDCommunication.maxpat"/>
<previous name="datachapter05">List Processing</previous>
<next name="communicationschapter02">Serial Communication</next>
<parent name="00_maxindex">Max Tutorials</parent>

<indexinfo category="Communications" title="Human-Interface Devices">Working with game controllers</indexinfo>

<h1>Communications Tutorial 1: Human-Interface Devices</h1>

<h2>Introduction</h2>

<p>This tutorial covers the use of the Max <o>hi</o> object, an interface with HID devices connected to your computer.  We will see how to discover, select and process data from these common devices.</p>

<p>The HID (Human Interface Device) protocol allows a physical controller to connect to a computer without device-specific drivers.  The computer is able to query the device for information about itself, and can present this information to the user.  HID-compliant controllers include joysticks, gamepads, some mice and graphics tablets, touchscreens, and a wide variety of other interfaces designed to communicate with the computer for real-time control.</p>

<p>While HID devices are typically designed for use by the gaming community, they are often used by Max performers, since they tend to be inexpensive and flexible; the most common HID device is a game-style joystick, which can provide an extraordinary number of interface tools in a small, portable package.</p>

<p>To open the tutorial patch, click on the <b>Open Tutorial</b> button in the upper right-hand corner of the documentation window.</p>

<h2>Understanding the <o>hi</o> interface</h2>

<p>Take a look at the tutorial.  This entire patch is based around the <o>hi</o> object – Max’s interface with HID devices. This object provides all of the information we need to interface with joysticks, drawing pads and other controllers. In order to see the devices that are available to the <o>hi</o> object, click on the <m>info</m> <o>message</o> box.  The <o>hi</o> object will respond by listing all of your available HID devices (as well as some information on them) in the Max Console.</p>

<p>The list begins with version information and a count of all the available HID devices.  You will probably be surprised to see that your computer already has a number of devices available.  Each of these devices is listed by name, followed by a notation of the number of <i>elements</i> for that device.  Elements are the individual control parameters available from the device; for example, a simple game controller might include four joystick elements (two joysticks with two axes each), six or more button elements and other assorted controls. There are often more elements than physical controls – these might be for device feedback, or for communication with the connected computer.</p>

<p>We will want to have the <o>hi</o> object focus on a single device (in our tests, we used a <i>Logitech Dual Action</i> game controller).  You can send a message to the object with the name of the HID device, but it is easier to let the <o>hi</o> object propagate a <o>umenu</o> with the available devices, and use that to select a controller.  This is what the <m>menu</m> message does – when you click on that <o>message</o> box, the right outlet of the <o>hi</o> object outputs the messages necessary to load a <o>umenu</o> with the names of the devices.  Taking the second output of the <o>umenu</o> and sending it back to the <o>hi</o> object will allow you to select a device.  When you select a menu option, you will notice that the Max Consoles says “<o>hi</o>: focusing on your device name”.</p>

<h2>Polling and data</h2>

<p>The <o>hi</o> object is unlike MIDI interfaces – rather than producing a stream of messages, it waits to be <i>polled</i> (by sending it a <m>bang</m> message), then outputs any changed values. In the second section of the tutorial patch, you can see that we’ve set up a <o>metro</o> with a <m>20</m> ms interval to poll the <o>hi</o> object. If you’ve selected a device, you should now be able to move your controller and see the results displayed in the <o>number</o> boxes below the <o>hi</o> object.  The output is a pair of numbers: the first number is the <m>element</m> index, while the second number contains the last <m>value</m> received from that element.</p>

<p>Another option for polling the <o>hi</o> object is to use its built-in timer; to do this, you send it a <m>poll</m> message with an argument for the polling interval.  This is a convenient way to set up the <o>hi</o> object with as few additional objects as necessary.  Turn off the <o>metro</o>, then click on the <m>poll 20</m> message – you will see that the values are still output, based on the object’s ability to self-poll.</p>

<p>While the <o>hi</o> object is running, let’s use the output to determine the elements that are available with the device you are using. The patch has a <o>comment</o> box that lists some common elements available with a game controller: in this case, a two-stick control with a four-button game pad. Identifying the available elements is the first step to making a useful Max program for your HID device.  Operate the controls on your HID controller, and watch the element/value pair.  Each move you make, whether a continuous control or a button push, should produce output.  If you are having problems seeing all of the element/value pairs, you could connect a <o>print</o> object to the out of the <o>hi</o> object to view all of the output.  If your HID device does not match the parameters listed in the tutorial patch, you will need to determine which controls are available and useful to you.</p>

<h2>Routing data for the applications</h2>

<p>Once you have a list of controls that work for your HID device, you can use them to control the rest of your application. In our tutorial patcher, the <o>route</o> object is the perfect tool: it routes messages by the first item in a list (in this case, the element index), and conveniently strips it off for us as well.</p>

<p>In this case, we use two elements (<m>20</m> and <m>21</m>, the joystick X and Y) to conduct the drawing, two elements (<m>6</m> and <m>8</m>, the top and bottom gamepad buttons) for sizing the drawn circle, and two more elements (<m>5</m> and <m>7</m>, the left and right gamepad buttons) for clearing and randomizing the color.  If you look at the contents of the subpatchers, you will find that the programming is very similar to what we’ve used in previous drawing patches.  Since the <o>hi</o> object has turned complex game controller movements into a simple stream of numbers, we can use it for any processing that Max can perform.</p>

<h2>Summary</h2>

<p>The HID protocol allows a standardized interface between a computer and relatively inexpensive hardware.  The <o>hi</o> object give a Max programmer the ability to use these devices for controlling and manipulating their performance patches. Since there are useful HID devices for almost any task, <o>hi</o> provides an easy interface into physical control of your system.</p>

<seealsolist>
<seealso name="hi">Human Interface (gaming) device input</seealso>
</seealsolist>

</chapter>
