Search code examples
pythonevdev

How to send a rumble effect to a device using python evdev


I like to send a rumble effect to a device using python evdev. This should be achieved with the upload_effect() function, which requires a buffer object as input.

This is what capabilities() reveals:

('EV_FF', 21L): [
    (['FF_EFFECT_MIN', 'FF_RUMBLE'], 80L),
    ('FF_PERIODIC', 81L),
    (['FF_SQUARE', 'FF_WAVEFORM_MIN'], 88L),
    ('FF_TRIANGLE', 89L),
    ('FF_SINE', 90L),
    ('FF_GAIN', 96L),
    ],

How do I create that buffer?


Solution

  • Python-evdev 1.1.0 supports force-feedback effect uploads. Here's an example from the documentation:

    from evdev import ecodes, InputDevice, ff
    
    # Find first EV_FF capable event device (that we have permissions
    # to use).
    for name in evdev.list_devices():
        dev = InputDevice(name)
        if ecodes.EV_FF in dev.capabilities():
            break
    
    rumble = ff.Rumble(strong_magnitude=0x0000, weak_magnitude=0xffff)
    effect_type = ff.EffectType(ff_rumble_effect=rumble)
    duration_ms = 1000
    
    effect = ff.Effect(
        ecodes.FF_RUMBLE, -1, 0,
        ff.Trigger(0, 0),
        ff.Replay(duration_ms, 0),
        effect_type
    )
    
    repeat_count = 1
    effect_id = dev.upload_effect(effect)
    dev.write(ecodes.EV_FF, effect_id, repeat_count)
    dev.erase_effect(effect_id)