How to create a description file for an HID device:
Filter:
Tutorials (extension) | Libraries > Modality

How to create a description file for an HID device
ExtensionExtension

How to create a description file to use with the Modality toolkit for a HID device

Introduction

The functionality of MKtl relies on descriptions of the devices to be used. For many controllers, there are already descriptions available, but your preferred device may not be among them.

This Tutorial first shows simple examples of HID interfaces, by emulating them within SuperCollider, and then describes how to create a description file for your interface.

Simple element

An element for an HID device looks in general like this:

hidUsage and hidUsagePage are the keys that identify the control from the device - these keys are crossplatform compatible. The ioType can be \in or \out. Examples of input elements are mouse axes, joystick axes, and buttons. Examples of output elements are LEDs on the device, or forcefeedback or rumble (e.g. on gamepads).

Here is an example of an output element:

Some HID devices that do not use common elements, or have ill-defined HID descriptors built into the hardware, will have a hidUsage and hidUsagePage that is not unique. In that case the automatic generation of a description file will return an element that looks like this:

Instead of hidUsage and hidUsagePage we have an identifier hidElementID. This identifier may not be cross platform compatible, as different operating systems may list the order of elements differently. However, on the same operating system, it should always be the same, and thus usable with caution. See also: Working with HID, HIDUsage.

Write a description file and explore an HID device

For HID devices, you can automatically generate a description file, as the device reports what it's inputs and outputs are.

For our optical mouse this creates a file called "edit and save me" with this content:

This is a fully working description file, with templates for all the info contributors should put into a desc file. Other than that, the main task now is to organize the reported flat list of elements into something semantically clearer, which will be easier to use in code.

NOTE: While the mouse reports its elements also as output elements (at least on OSX), sending values to them does not have any effect, so we ignore them here. Other devices respond when sending to their reported outs, so if yours does, you can add the output to the elementsDesc.

Boiling that text down to the working minimum, idInfo, protocol, and elementsDesc are sufficient for a test description dict.

At the top of the elements array, we see that 4 buttons (on linux, 3 on osx) are detected, so we can give these names, and create an MKtl from that desc with just buttons:

In the posted messages, we can find b1, b2, b3, but not b0. While the HID reports a button 0 (on linux), nothing that we click seems to trigger b0, so it seems this is just a dummy element in the HID device. So we can adapt our description to:

Next we refine the description by grouping the buttons into a group \bt, and giving the indiviudal buttons better names - left, right, middle, and reordering them as left, middle, right:

Then, looking back at the initially created file, we had three more elements that need a further definition, we will guess that they are the X and Y-axis of the mouse, and the mouse wheel.

Note that mouse axes and wheels usually create only relative values; that is, the number value tells you by how much the mouse was moved since its last position scan. You can use this to do a relative set on a process parameter, e.g. with the RelSet class.

With our mouse the resolution does not seem to be very good. So we make our custom specs.

HID device info

In the description file, an HID device needs an idInfo, which is created from the device's productName and vendorName.

For our mouse the vendorName is left blank, apparently the vendor did not bother to list it in its device. Here is a little shortcut to create the string:

HID Initialisation messages

Some devices need initialisation messages to be sent before they can function properly with all their modes activated. You can define these messages in the initalisationMessages field of the device description.

For HID we do not have a usecase yet where this is needed, so we do not know yet how to format these messages. Write us if you have a usecase!