Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Perl meets Leap Motion

akiym
August 31, 2013

Perl meets Leap Motion

Hokkaido.pm #10

akiym

August 31, 2013
Tweet

More Decks by akiym

Other Decks in Technology

Transcript

  1. my($LEAP_HANDLE); # After connection, this contains our websocket handle my($SEND);

    # Data to send, when other end is ready for it my($BUFF)=''; # Buffer in case calling script misses a few events sub Leap { my $ret; if($BUFF eq '') { # Nothing in the buffer - try to get some more unless($LEAP_HANDLE) { # Establish a new connection # Make the socket socket($LEAP_HANDLE, PF_INET, SOCK_STREAM, getprotobyname('tcp')) || return (\{'error' => "socket: $!"}); # Don't let stuff like UTF-style characters get screwed up binmode($LEAP_HANDLE) || return (\{'error' =>,"binmode(socket handle): $!"}); # Allow more than one connection setsockopt($LEAP_HANDLE, SOL_SOCKET, SO_REUSEADDR, 1) || return (\{'error' =>,"SO_REUSEADDR: $!"}); # Don't block on close (Remember not to close it until we've sent everything we want):- if($^O =~/Win32/i) { setsockopt($LEAP_HANDLE, SOL_SOCKET, SO_DONTLINGER, 1) || return (\{'error' =>,"SO_DONTLINGER: $!"}); } vec($bitsr,fileno($LEAP_HANDLE),1)=1; # This tells select() which socket we want to query vec($null,fileno($LEAP_HANDLE),1)=0; if(defined $SEND) {$bitsw=$bitsr} else {$bitsw=$null} # We only care about write-status if we've got data to write
  2. use Device::Leap; while(1) { $d=&Leap(); next unless(ref $d); # no

    new data print $d->{hands}->[0]->{sphereRadius} . "\n"; # print some }
  3. 

  4. my $leap = AnyEvent::LeapMotion->new( enable_gesture => 1, on_frame => sub

    { my $frame = shift; ... }, ); $leap->run; AE::cv->recv;