Saturday, March 30, 2013

Developing for the Leap Motion controller in Scala


I just got a hold of a Leap Motion controller today and I have to admit it's a neat little device with a lot of potential. The first thing I did was figure out how to interface with the device using Scala. Here are some quick pointers on how to get setup.

Before reading further, entire process I outline below is summed up in the following github repo. You can clone it and run ./install then ./run to produce the following results:


Instructions

1- Download the SDK here.

2- Assuming you have maven installed, create a project using the following command
mvn org.apache.maven.plugins:maven-archetype-plugin:2.2:generate \
-DarchetypeGroupId=org.scala-tools.archetypes \
-DarchetypeArtifactId=scala-archetype-simple \
-DarchetypeVersion=1.3 -DgroupId=leap-scala \
-DartifactId=leap-scala -Dversion=1.0.0 -DinteractiveMode=false

3- Install the leap motion sdk jar in your local maven repository (found in the sdk package from step #1 under /LeapSDK/lib/LeapJava.jar). Use the following command:
mvn org.apache.maven.plugins:maven-install-plugin:2.3.1:install-file \
    -Dfile=PATH_TO_JAR \
    -DgroupId=com.leapmotion.leap -DartifactId=leapMotion \
    -Dversion=1.0.0 -Dpackaging=jar

4- Add a reference to this jar file in the leap-scala/pom.xml that was created in step #2.
    
    <dependencies>
        <dependency>
            <groupid>org.scala-lang</groupid>
            <artifactid>scala-library</artifactid>
            <version>${scala.version}</version>
        </dependency>
        <dependency>
            <groupid>com.leapmotion.leap</groupid>
            <artifactid>leapMotion</artifactid>
            <version>1.0.0</version>
        </dependency>
    </dependencies>

5- Write some code for capturing events from the device. Here are some snippets from the leap provided samples I ported to scala:


6- Once you have some code, you can bundle it along with the leap motion library in a "fat" jar using the maven-assembly-plugin. Check out a sample pom.xml that does this here. You can then bundle the app using the following maven command
 
mvn install

This will produce a jar file called "leap-scala-1.0.0-jar-with-dependencies.jar" under the target folder.

7- Once your code is compiling, it's time to run. You'll need to point the jvm runtime to the native leap motion libraries for interfacing with your OS. On OSX, this is a file called libLeapJava.dylib found in the same directory of the SDK as the jar file. Here is how you would run your app once it's compiled:
 
java -Djava.library.path=".:./libs" -jar target/leap-scala-1.0.0-jar-with-dependencies.jar

Using the code above, you should be able to see input directly form the device on your console.

These instructions were generated while developing on OSX. Things might need a little bit of tweaking if running on other platforms.

No comments:

Post a Comment