Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?

Detecting swipe movements

by morgon (Curate)
on May 16, 2014 at 12:20 UTC ( #1086294=perlquestion: print w/replies, xml ) Need Help??
morgon has asked for the wisdom of the Perl Monks concerning the following question:


sorry for the following rather vague description but I am just starting to think about it, so I have not yet explored any options.

The background of what I want to do is to use a wireless presenter (a Genius RingPresenter to be precise) that also can transmit mouse-events to remote-control a raspberry pi.

I do not want to use it as a mouse-replacement (the pi runs headless) but I rather want mouse-movements to be translated into actions, e.g. when I swipe upwards I want the volume of mpd to be increased or something along those lines.

For that what I do is to read mouse-events from a /dev/input/event* device and what I get are events that basically give me a timestamp, an axis (either up-down or left-right) and a some relative movement.

What I now would like to do is detect swipe-movements (ie. "swipe-up", "swipe-down", "swipe-left", "swipe-right") from an array of such events.

Now an up-swipe will probably be defined as some motion that "mainly" goes upwards in a certain amount of time.

Evidently when performing an up-swipe there will also be some events of small movements to the right or left and maybe even a small amount of downward movement so I need some sort of "fuzzyness" in my detection.

Can someone point me to some modules that may be of use for such a task?

Many thanks!

Replies are listed 'Best First'.
Re: Detecting swipe movements
by marto (Bishop) on May 16, 2014 at 12:24 UTC

    From your description I'd start off by searching cpan for the modules which provide this sort of functionality for the wiimote.

Re: Detecting swipe movements
by zentara (Archbishop) on May 16, 2014 at 12:59 UTC
    Hi, sorrry I can't be more helpful, but here is a Python script that works. It only needs to be converted to Perl. :-)

    From: python mouse delta

    I put the code below for other monks to quickly see, and possibly help translate to Perl. Be aware you must run this as root to get access to /dev/input/mouse.

    Re: Linux device filehandle roadblock seems to be a starting point for Perl.

    import struct file = open( "/dev/input/mice", "rb" ); def getMouseEvent(): buf =; button = ord( buf[0] ); bLeft = button & 0x1; bMiddle = ( button & 0x4 ) > 0; bRight = ( button & 0x2 ) > 0; x,y = struct.unpack( "bb", buf[1:] ); print ("L:%d, M: %d, R: %d, x: %d, y: %d\n" % (bLeft,bMiddle,bRight, + x, y) ); # return stuffs while( 1 ): getMouseEvent(); file.close();

    I'm not really a human, but I play one on earth.
    Old Perl Programmer Haiku ................... flash japh
      Well my question is not about capturing the events (sorry if I made myself not clear).

      I simply use Linux::Input and that task becomes trivial.

      My question was about how to then detect swipe-movements once you have captured a number of those events.

      So given an array of such events, how do you go about analyzing it and detect swipes (once you formalized that notion in a sensible way which is also part of my question).

Re: Detecting swipe movements
by Lotus1 (Curate) on May 16, 2014 at 13:41 UTC

    I recently posted this similar raspberry pi project to Cool Uses for Perl. read raw mouse data in Linux

    When you say you're running headless you could still be running X on the Raspberry Pi and there are modules for X windows that will detect mouse motion. I didn't want to run X so I needed raw mouse data.

Re: Detecting swipe movements
by RonW (Vicar) on May 16, 2014 at 17:30 UTC

    If it's acceptable to require button press/release, then the user presses the button before starting the swipe, then releases it at the end of the swipe. Then you compare the start and end positions. The axis with the largest change in position, and its direction, is your swipe. Example, from 1,4 to 20,100 is an up swipe.

    This would be like swiping on a virtual touch screen. the button press being like your finger contacting the screen, and the release being like your finger lifting off the screen.

    Otherwise, you could try filtering out "swipes" shorter than some threshold, then "decode" the remaining (large) swipes the same as above.

    You will need to experiment to determine the threshold.

    It is possible to devise an adaptive algorithm for distinguishing random hand movement from intentional swipes by tracking the largest "small" movement over a sliding wondow window of a few minutes, then accepting a movement significantly larger as a control swipe. Again, you will have to experiment.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1086294]
Approved by davies
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (12)
As of 2017-06-26 14:22 GMT
Find Nodes?
    Voting Booth?
    How many monitors do you use while coding?

    Results (581 votes). Check out past polls.