Skip to content

Commit 3c6a12e

Browse files
committed
Add examples of working with FF-effects
1 parent 1fb9e4c commit 3c6a12e

1 file changed

Lines changed: 90 additions & 0 deletions

File tree

‎docs/tutorial.rst‎

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -362,3 +362,93 @@ Create ``uinput`` device with capabilities of another device
362362

363363

364364
.. _`async/await`: https://docs.python.org/3/library/asyncio-task.html
365+
366+
Create ``uinput`` device capable of recieving FF-effects
367+
========================================================
368+
369+
::
370+
import asyncio
371+
from evdev import UInput, categorize, ecodes as e
372+
373+
cap = {
374+
e.EV_FF : [ e.FF_RUMBLE ],
375+
e.EV_KEY : [e.KEY_A, e.KEY_B],
376+
}
377+
378+
try:
379+
ui = UInput(cap, name='test-controller', version=0x3)
380+
except UInputError:
381+
print('Please make sure you have appropriate permissions to access /dev/uinput and the "uinput" kernel module is loaded')
382+
raise
383+
384+
async def print_events(device):
385+
async for event in device.async_read_loop():
386+
print(categorize(event))
387+
#
388+
# Wait for EV_UINPUT event that will signal us that effect
389+
# upload/erase operation is in progress
390+
#
391+
if event.type != e.EV_UINPUT:
392+
pass
393+
394+
if event.code == e.UI_FF_UPLOAD:
395+
upload = device.begin_upload(event.value)
396+
upload.retval = 0
397+
398+
print('[upload] effect_id: {}, type: {}'
399+
.format(upload.effect_id, upload.effect.type))
400+
device.end_upload(upload)
401+
402+
elif event.code == e.UI_FF_ERASE:
403+
erase = device.begin_erase(event.value)
404+
print('[erase] effect_id {}'.format(erase.effect_id))
405+
406+
erase.retval = 0
407+
device.end_erase(erase)
408+
409+
asyncio.ensure_future(print_events(ui))
410+
loop = asyncio.get_event_loop()
411+
loop.run_forever()
412+
413+
414+
Injecting FF-events into first FF-capable device found
415+
======================================================
416+
417+
::
418+
from evdev import ecodes as e
419+
from glob import glob
420+
421+
dev = None
422+
423+
#
424+
# Find first EV_FF capable event device (that we have permissions to
425+
# use)
426+
#
427+
for name in glob('/dev/input/event*'):
428+
try:
429+
dev = InputDevice(name)
430+
431+
if e.EV_FF in dev.capabilities():
432+
break
433+
except PermissionError as e:
434+
print(e)
435+
continue
436+
437+
if dev:
438+
duration_ms = 1000
439+
440+
rumble = ff.Rumble(strong_magnitude=0x0000,
441+
weak_magnitude=0xffff)
442+
effect_type = ff.EffectType(ff_rumble_effect=rumble)
443+
effect = ff.Effect(e.FF_RUMBLE, -1, 0,
444+
ff.Trigger(0, 0),
445+
ff.Replay(duration_ms, 0),
446+
effect_type)
447+
448+
repeat_count = 1
449+
effect_id = dev.upload_effect(effect)
450+
dev.write(e.EV_FF, effect_id, repeat_count)
451+
dev.erase_effect(effect_id)
452+
else:
453+
print('No FF capable device was found!')
454+

0 commit comments

Comments
 (0)