AnyEvent-LeapMotion

 view release on metacpan or  search on metacpan

README.md  view on Meta::CPAN


AnyEvent::LeapMotion - Perl interface to the Leap Motion Controller (via WebSocket)

# SYNOPSIS

    use AnyEvent;
    use AnyEvent::LeapMotion;

    my $leap = AnyEvent::LeapMotion->new(
        enable_gesture => 1,
        on_frame => sub {
            my $frame = shift;

            ...
        },
    );
    $leap->run;

    AE::cv->recv;

# DESCRIPTION

AnyEvent::LeapMotion is a simple interface to the Leap Motion controller. It receives tracking data through a WebSocket server.

# METHODS

- `my $leap = AnyEvent::LeapMotion->new()`

    Create an instance of AnyEvent::LeapMotion.

    - on\_frame : Sub
    - on\_error : Sub
    - host => '127.0.0.1' : Str
    - port => 6437 : Num
    - enable\_gesture => 0 : Bool

- `$leap->run()`

    Running an event loop.

# MOTIVATION

lib/AnyEvent/LeapMotion.pm  view on Meta::CPAN


our $VERSION = "0.01";

sub new {
    my ($class, %args) = @_;
    return bless {
        host           => '127.0.0.1',
        port           => 6437,
        enable_gesture => 0,
        %args,
        frame => Protocol::WebSocket::Frame->new(),
    }, $class;
}

sub run {
    my ($self, $code) = @_;
    AnyEvent::Socket::tcp_connect $self->{host}, $self->{port}, sub {
        my ($fh, $host, $port) = @_ or die "connection failed: $!";
        $self->{handle} = AnyEvent::Handle->new(
            fh => $fh,
            on_eof => sub {

lib/AnyEvent/LeapMotion.pm  view on Meta::CPAN

                $hs->parse(delete $_[0]->{rbuf});
                unless ($hs->is_done) {
                    $self->call(on_error => 'Handshake failed');
                }
            });
            if ($self->{enable_gesture}) {
                $self->send({enableGestures => \1}); # true
            }
            $self->{handle}->on_read(sub {
                $_[0]->push_read(sub {
                    $self->{frame}->append(delete $_[0]->{rbuf});
                    if (my $message = $self->{frame}->next_bytes) {
                        my $data = JSON::decode_json($message);
                        if (exists $data->{id} && exists $data->{timestamp}) {
                            $self->call(on_frame => $data);
                        }
                    }
                });
            });
        });
    };
}

sub send {
    my ($self, $data) = @_;
    my $message = JSON::encode_json($data);
    my $frame = Protocol::WebSocket::Frame->new($message);
    $self->{handle}->push_write($frame->to_bytes);
}

sub call {
    my ($self, $name, @args) = @_;
    if ($self->{$name}) {
        $self->{$name}->(@args);
    }
}

1;

lib/AnyEvent/LeapMotion.pm  view on Meta::CPAN


AnyEvent::LeapMotion - Perl interface to the Leap Motion Controller (via WebSocket)

=head1 SYNOPSIS

    use AnyEvent;
    use AnyEvent::LeapMotion;

    my $leap = AnyEvent::LeapMotion->new(
        enable_gesture => 1,
        on_frame => sub {
            my $frame = shift;

            ...
        },
    );
    $leap->run;

    AE::cv->recv;

=head1 DESCRIPTION

lib/AnyEvent/LeapMotion.pm  view on Meta::CPAN

=head1 METHODS

=over 4

=item C<< my $leap = AnyEvent::LeapMotion->new() >>

Create an instance of AnyEvent::LeapMotion.

=over 4

=item on_frame : Sub

=item on_error : Sub

=item host => '127.0.0.1' : Str

=item port => 6437 : Num

=item enable_gesture => 0 : Bool

=back



( run in 1.244 second using v1.01-cache-2.11-cpan-cdf2f3d4e48 )