HapticDevice

class haptic.HapticDevice(id_=0, dev=None)

Manage calibration and use of a haptic device.

name

System-provided name for the device.

Type

string

id

Index of device in pyglet’s list.

Type

int

effect

Main evdev force effect.

Type

evdev.ff.Effect

effect_id

The ID of the effect uploaded to the device.

Type

int?

offset

How much to offset inputs from the joysticks.

Type

tuple

strongMagnitude

Vibration strength for the strong motor

Type

int

weakMagnitude

Vibration strength for the weak motor

Type

int

absinfo(axis_num)

Return current AbsInfo for input device axis

Parameters

axis_num (int) – EV_ABS keycode (example ecodes.ABS_X)

Example

>>> device.absinfo(ecodes.ABS_X)
AbsInfo(value=1501, min=-32768, max=32767, fuzz=0, flat=128, resolution=0)
active_keys(verbose=False)

Return currently active keys.

Example

>>> device.active_keys()
[1, 42]

If verbose is True, key codes are resolved to their verbose names. Unknown codes are resolved to '?'. For example:

[('KEY_ESC', 1), ('KEY_LEFTSHIFT', 42)]
async_read()

Asyncio coroutine to read multiple input events from device. Return a generator object that yields InputEvent instances.

async_read_loop()

Return an iterator that yields input events. This iterator is compatible with the async for syntax.

async_read_one()

Asyncio coroutine to read and return a single input event as an instance of InputEvent.

calibrateRumble(sMag, wMag)

Interactive calibration of vibration strength

The vibration strength starts at wMag and ticks down by 0x0f00 until the user presses the south button on the controller (e.g., x on dualshock). Pressing the east button progresses the calibration.

Parameters
  • sMag (int) – Strength of strong motor vibration.

  • wMag (int) – Strength of weak motor vibration.

  • display (function) – A callback function to handle display of instructional text. Gets passed the Experiment instance. Only called once.

  • displayArgs (list) – Arguments to be passed to display.

Raises

ValueError – Both motors may not be set to zero.

calibrateStick(win, display=None, displayArgs=[])

Calibrate the stick’s resting position.

Currently, the procedure is to move the right stick around, release,

and press the West button (square on PS, X on MS). Future implementations may support other sticks or specified buttons

Parameters

win (psychopy.visual.Window) – The window being used by the experiment. This is needed because the joystick’s input is only updated on screen flip.

capabilities(verbose=False, absinfo=True)

Return the event types that this device supports as a mapping of supported event types to lists of handled event codes.

Example

>>> device.capabilities()
{ 1: [272, 273, 274],
  2: [0, 1, 6, 8] }

If verbose is True, event codes and types will be resolved to their names.

{ ('EV_KEY', 1): [('BTN_MOUSE', 272),
                  ('BTN_RIGHT', 273),
                  ('BTN_MIDDLE', 273)],
  ('EV_REL', 2): [('REL_X', 0),
                  ('REL_Y', 1),
                  ('REL_HWHEEL', 6),
                  ('REL_WHEEL', 8)] }

Unknown codes or types will be resolved to '?'.

If absinfo is True, the list of capabilities will also include absolute axis information in the form of AbsInfo instances:

{ 3: [ (0, AbsInfo(min=0, max=255, fuzz=0, flat=0)),
       (1, AbsInfo(min=0, max=255, fuzz=0, flat=0)) ]}

Combined with verbose the above becomes:

{ ('EV_ABS', 3): [ (('ABS_X', 0), AbsInfo(min=0, max=255, fuzz=0, flat=0)),
                   (('ABS_Y', 1), AbsInfo(min=0, max=255, fuzz=0, flat=0)) ]}
erase_effect(ff_id)

Erase a force effect from a force feedback device. This also stops the effect.

fileno()

Return the file descriptor to the open event device. This makes it possible to pass instances directly to select.select() and asyncore.file_dispatcher.

getAllAxes()

Get input from all sticks, minus offset if possible

getAllButtons()

Get the state of all buttons as a list.

getAllHats()

Get the current values of all available hats as a list of tuples.

Each value is a tuple (x, y) where x and y can be -1, 0, +1

getAxis(axisId)

Get the value of an axis by an integer id.

(from 0 to number of axes - 1)

getButton(buttonId)

Get the state of a given button.

buttonId should be a value from 0 to the number of buttons-1

getHat(n)

Get the d-pad input. Not currently used

getName()

Return the manufacturer-defined name describing the device.

getNumAxes()

Return the number of joystick axes found.

getNumButtons()

Return the number of digital buttons on the device.

getNumHats()

Get the number of hats on this joystick.

The GLFW backend makes no distinction between hats and buttons. Calling ‘getNumHats()’ will return 0.

getX()

Return the X axis value (equivalent to joystick.getAxis(0)).

getY()

Return the Y axis value (equivalent to joystick.getAxis(1)).

getZ()

Return the Z axis value (equivalent to joystick.getAxis(2)).

grab()

Grab input device using EVIOCGRAB - other applications will be unable to receive events until the device is released. Only one process can hold a EVIOCGRAB on a device.

Warning

Grabbing an already grabbed device will raise an IOError.

grab_context()

A context manager for the duration of which only the current process will be able to receive events from the device.

input_props(verbose=False)

Get device properties and quirks.

Example

>>> device.input_props()
[0, 5]

If verbose is True, input properties are resolved to their names. Unknown codes are resolved to '?':

[('INPUT_PROP_POINTER', 0), ('INPUT_PROP_POINTING_STICK', 5)]
leds(verbose=False)

Return currently set LED keys.

Example

>>> device.leds()
[0, 1, 8, 9]

If verbose is True, event codes are resolved to their names. Unknown codes are resolved to '?':

[('LED_NUML', 0), ('LED_CAPSL', 1), ('LED_MISC', 8), ('LED_MAIL', 9)]
need_write()

Decorator that raises EvdevError if there is no write access to the input device.

read()

Read multiple input events from device. Return a generator object that yields InputEvent instances. Raises BlockingIOError if there are no available events at the moment.

read_loop()

Enter an endless select.select() loop that yields input events.

read_one()

Read and return a single input event as an instance of InputEvent.

Return None if there are no pending input events.

property repeat

Get or set the keyboard repeat rate (in characters per minute) and delay (in milliseconds).

rumble(repeat=1)

Cause the device to rumble.

Keyword Arguments

repeat (int) – How long the device should rumble for, in seconds

set_absinfo(axis_num, value=None, min=None, max=None, fuzz=None, flat=None, resolution=None)

Update AbsInfo values. Only specified values will be overwritten.

Parameters

axis_num (int) – EV_ABS keycode (example ecodes.ABS_X)

Example

>>> device.set_absinfo(ecodes.ABS_X, min=-2000, max=2000)

You can also unpack AbsInfo tuple that will overwrite all values

>>> device.set_absinfo(ecodes.ABS_Y, *AbsInfo(0, -2000, 2000, 0, 15, 0))
set_led(led_num, value)

Set the state of the selected LED.

Example

>>> device.set_led(ecodes.LED_NUML, 1)
ungrab()

Release device if it has been already grabbed (uses EVIOCGRAB).

Warning

Releasing an already released device will raise an IOError('Invalid argument').

upload_effect(effect)

Upload a force feedback effect to a force feedback device.

write(etype, code, value)

Inject an input event into the input subsystem. Events are queued until a synchronization event is received.

Parameters
  • etype – event type (e.g. EV_KEY).

  • code – event code (e.g. KEY_A).

  • value – event value (e.g. 0 1 2 - depends on event type).

Example

>>> ui.write(e.EV_KEY, e.KEY_A, 1) # key A - down
>>> ui.write(e.EV_KEY, e.KEY_A, 0) # key A - up
write_event(event)

Inject an input event into the input subsystem. Events are queued until a synchronization event is received.

Parameters

event (InputEvent) – InputEvent instance or an object with an event attribute (KeyEvent, RelEvent etc).

Example

>>> ev = InputEvent(1334414993, 274296, ecodes.EV_KEY, ecodes.KEY_A, 1)
>>> ui.write_event(ev)