@@ -367,41 +367,36 @@ Create ``uinput`` device capable of recieving FF-effects
367367========================================================
368368
369369::
370+
370371 import asyncio
371- from evdev import UInput, categorize, ecodes as e
372+ from evdev import UInput, categorize, ecodes
372373
373374 cap = {
374- e .EV_FF : [ e .FF_RUMBLE ],
375- e .EV_KEY : [e .KEY_A, e .KEY_B],
375+ ecodes .EV_FF: [ecodes .FF_RUMBLE ],
376+ ecodes .EV_KEY: [ecodes .KEY_A, ecodes .KEY_B]
376377 }
377378
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
379+ ui = UInput(cap, name='test-controller', version=0x3)
383380
384381 async def print_events(device):
385382 async for event in device.async_read_loop():
386383 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:
384+
385+ # Wait for an EV_UINPUT event that will signal us that an
386+ # effect upload/erase operation is in progress.
387+ if event.type != ecodes.EV_UINPUT:
392388 pass
393389
394- if event.code == e .UI_FF_UPLOAD:
390+ if event.code == ecodes .UI_FF_UPLOAD:
395391 upload = device.begin_upload(event.value)
396392 upload.retval = 0
397393
398- print('[upload] effect_id: {}, type: {}'
399- .format(upload.effect_id, upload.effect.type))
394+ print(f'[upload] effect_id: {upload.effect_id}, type: {upload.effect.type}')
400395 device.end_upload(upload)
401396
402- elif event.code == e .UI_FF_ERASE:
397+ elif event.code == ecodes .UI_FF_ERASE:
403398 erase = device.begin_erase(event.value)
404- print('[erase] effect_id {}'.format( erase.effect_id) )
399+ print(f '[erase] effect_id {erase.effect_id}' )
405400
406401 erase.retval = 0
407402 device.end_erase(erase)
@@ -411,44 +406,31 @@ Create ``uinput`` device capable of recieving FF-effects
411406 loop.run_forever()
412407
413408
414- Injecting FF-events into first FF-capable device found
415- ======================================================
409+ Injecting an FF-event into first FF-capable device found
410+ ========================================================
416411
417412::
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!')
454413
414+ from evdev import ecodes, InputDevice, ff
415+
416+ # Find first EV_FF capable event device (that we have permissions to use).
417+ for name in evdev.list_devices():
418+ dev = InputDevice(name)
419+ if ecodes.EV_FF in dev.capabilities():
420+ break
421+
422+ rumble = ff.Rumble(strong_magnitude=0x0000, weak_magnitude=0xffff)
423+ effect_type = ff.EffectType(ff_rumble_effect=rumble)
424+ duration_ms = 1000
425+
426+ effect = ff.Effect(
427+ ecodes.FF_RUMBLE, -1, 0,
428+ ff.Trigger(0, 0),
429+ ff.Replay(duration_ms, 0),
430+ ff.EffectType(ff_rumble_effect=rumble)
431+ )
432+
433+ repeat_count = 1
434+ effect_id = dev.upload_effect(effect)
435+ dev.write(e.EV_FF, effect_id, repeat_count)
436+ dev.erase_effect(effect_id)
0 commit comments