HID:
Filter:
Classes | External Control > HID

HID : Object

This class provides access to human input devices, or in short HID, such as joysticks, gamepads, mice, keyboard, and so on.
Source: HID_API.sc

Description

Human input devices can be used as controllers for making music. This class provides you with access to them in a simple and transparent way.

The development of this SuperCollider implementation of HID access was funded by the SuperCollider community and BEK, Bergen Elektronisk Kunst, Bergen, Norway, http://www.bek.no

Introduction

In general using an Human Input Device follows this scheme:

Find available devices:
HID.findAvailable;
Print a readable list of available devices:
HID.postAvailable;
Open a specific device:
~myhid = HID.open( 1103, 53251 );
Set actions for specific elements:
~myhid.elements[1].action = { |...args| args.postln; };

See Working with HID for a full introduction.

Class Methods

Finding devices

HID.findAvailable

queries the operating system which HID devices are attached to the system and can be accessed. When using HID this is the first method you need to execute, before you can access any device.

Returns:

an IdentityDictionary of available devices

HID.available

A dictionary of available devices, or rather info about them in an instance of HIDInfo, populated by the method findAvailable

Returns:

an IdentityDictionary

HID.postAvailable

posts a human readable list of available HID devices and their properties (see also HIDInfo)

HID.findBy(vendorID, productID, path, serial, releaseNumber)

Find devices in the available device dictionary by specifying one or more characteristics of the device

Arguments:

vendorID

The vendor ID of the device, this is a number encoded by the device itself, and the same across platforms.

productID

The product ID of the device, this is a number encoded by the device itself, and the same across platforms.

path

The path of the device, this is a path defined by the operating system, and thus not the same across platforms, but essential to distinguish devices with the same vendor and product ID from each other.

serial

The serial number of the device. This is dependent on the operating system, e.g. on Linux it is not set.

releaseNumber

The release number of the device, this is a number encoded by the device itself, and the same across platforms.

Returns:

an IdentityDictionary of devices the match the search query, or nil if no arguments are given

HID.availableUsages

A dictionary of available usages from all HIDs, populated automatically when devices are opened and closed.

Returns:

an IdentityDictionary

HID.postAvailableUsages

posts a human readable list of available HID usages and their properties (see also HIDElement and HIDUsage)

Opening devices

HID.open(vendorID, productID, path)

Open a device with a given vendorID, product ID and optionally a path.

Arguments:

vendorID

The vendor ID of the device

productID

The product ID of the device

path

(optional) The path in the operating system, e.g. "/dev/hidraw0" on Linux. If not specified, the method will look for a matching device in the device list, and open the first match it finds.

Returns:

The HID device - an instance of HID.

HID.new( ... args)

Same as HID.open

HID.openPath(path)

Open a device using its path in the operating system

Arguments:

path

The path in the operating system, e.g. "/dev/hidraw0" on Linux

Returns:

The HID device - an instance of HID.

HID.openAt(index)

Open a device using its index in the dictionary of available devices

Arguments:

index

The index into the dictionary of available devices

Returns:

The HID device - an instance of HID.

HID.openDevices

A dictionary of the opened devices

Returns:

an IdentityDictionary

Adding functions to HID events

Whenever data comes in from an opened HID device, there are two types of actions fired. An action for the incoming element data and an action for the device, indicating that there has been a change in one of the elements. In most cases you will want to use the first action; only in cases where the order of parsing the element data is important, you may want to use the second type - e.g. when dealing with very accurately timed button press combinations.

There are three levels where you can set actions:

Alternately, you can also use the HIDFunc interface.

HID.debug

HID.debug = value

When set to true, the incoming data from any opened HID device will be printed to the post window.

HID.action

HID.action = function

Set or get the action to be performed upon receiving element data from the device. The function will be passed the following arguments: the value (mapped between 0 and 1), the raw value, element usage page, the element usage, the element id, the device id, the device (an instance of HID).

Arguments:

function

The function to be performed upon receiving element data from the device

HID.addRecvFunc(function)

add a function to the internal FunctionList that will be evaluated whenever element data comes in from an open device. The arguments passed to the function are as defined above. Use this method if you want to add actions to HID functions from classes you write, so that you still keep the option to add an action on the fly from user code.

Arguments:

function

The function to be added to the list.

HID.removeRecvFunc(function)

remove a function to the internal FunctionList that will be evaluated whenever data comes in from a device.

Arguments:

function

The function to remove from the list, this must be a reference to the Function that was originally added to the list

HID.deviceAction = function

set an action or function to be performed whenever there is an update to any device's elements.

Arguments:

function

The function to be performed upon receiving data from the device

HID.addDevFunc(function)

add a function to the internal FunctionList that will be evaluated whenever data comes in from a device.

Arguments:

function

The function to be performed upon receiving data from the device

HID.removeDevFunc(function)

remove a function to the internal FunctionList that will be evaluated whenever data comes in from a device.

Arguments:

function

The function to remove from the list, this must be a reference to the Function that was originally added to the list

Managing the HID subsystem

The following methods are used internally to initialize and finalize the HID subsystem, but in rare cases you may wish to manage these methods manually.

HID.initializeHID

Initialize the HID subsystem, this method is called automatically when calling the method findAvailable.

HID.running

Indicates whether or not the HID subsystem is running.

HID.closeAll

This method is called automatically upon Shutdown, if the HID subsystem was initialized. It can be stopped manually, in order to save system resources. This method will close all opened HID devices.

Inherited class methods

Instance Methods

.elements

An IdentityDictionary holding all the elements, i.e. controls, of the device

.findElementWithUsage(elUsage, elUsagePage)

Find all elements with a certain usage and usagePage.

Arguments:

elUsage

The usage index of the element

elUsagePage

The usage page of the element

Returns:

an Array with the found elements

.getElementWithID(elid)

Get the element with the given index

Arguments:

elid

The index of the element

Returns:

the HIDElement

.close

Close the HID device, closing a device is asynchronous. You can set a closeAction (see below), which will be performed when the device closes.

.isOpen

Returns true if the device is open, false if the device was closed.

returns: a Boolean

.collections

An IdentityDictionary holding all the collections, i.e. groups of controls, of the device

Adding functionality to the device

.debug

.debug = value

When set to true, the incoming data from this HID device will be printed to the post window.

.closeAction

.closeAction = value

Function to be performed when device is closed.

.action

.action = value

Set or get the action to be performed upon receiving element data from the device. The function will be passed the following arguments: the value (mapped between 0 and 1), the raw value, element usage page, the element usage, the element id

Arguments:

(function)

The function to be performed upon receiving element data from the device

.deviceAction

.deviceAction = value

set an action or function to be performed whenever there is an update to any of the device's elements.

Arguments:

(function)

The function to be performed upon receiving data from the device

Posting human readable information about the device

.postInfo

Post the HIDInfo of this device in a human readable format

.postCollections

Post information about all the collections of this device in a human readable format

.postElements

Post information about all the elements of this device in a human readable format

.postInputElements

Post information about all the input elements of this device in a human readable format

.postOutputElements

Post information about all the output elements of this device in a human readable format

.postFeatureElements

Post information about all the feature elements of this device in a human readable format

.postUsages

Post information about all the usages of this device in a human readable format

Properties of the device

.info

Retrieve the HIDInfo of this device

Returns:

an instance of HIDInfo

.usage(collectionID: 0)

Retrieve the usage index of a collection of this device, or the main usage of the device (if called without an argument).

Arguments:

collectionID

The collection of which to retrieve the usage. Default is 0, which should define the primary usage of the device.

Returns:

the usage index of this device

.usageName(collectionID: 0)

Retrieve the usage name of a collection of this device, or the main usage of the device (if called without an argument). The name is looked up from the standardized HID usage tables using the usage index.

Arguments:

collectionID

The collection of which to retrieve the usage. Default is 0, which should define the primary usage of the device.

Returns:

the usage name of this device

.usagePage(collectionID: 0)

Retrieve the usage page index of a collection of this device, or the main page of the device (if called without an argument). The name is looked up from the standardized HID usage tables using the usage page index.

Arguments:

collectionID

The collection of which to retrieve the usage page. Default is 0, which should define the primary usage of the device.

Returns:

the usage page index of this device

.pageName(collectionID: 0)

Retrieve the page name of a collection of this device, or the main page of the device (if called without an argument). The name is looked up from the standardized HID usage tables using the usage page index.

Arguments:

collectionID

The collection of which to retrieve the usage page name. Default is 0, which should define the primary usage of the device.

Returns:

the usage page name of this device

.vendor

Retrieve the vendor id of this device

Returns:

the vendor id

.product

Retrieve the product id of this device

Returns:

the product id

.usages

Retrieve the usages of the elements of this device.

Returns:

an IdentityDictionary with usages as keys and lists of elements as corresponding elements

Inherited instance methods

Examples