Leap Motion

Using Leap Motion in an Objective-C project

I have started learning about Leap Motion and decided to make a simple app in Objective C in order to test it. The first thing I realise it is that there is no template for Objective C, nor a guide of how to integrate the lib and setup the project. So here I will show how to set up an OS X project, and also I will provide a template.

  1. Once you create your project, drag and drop Leap.h LeapMath.h LeapObjectiveC.h LeapObjectiveC.mm into your project and check “Copy items into destination group’s folder (if needed)”. Also check that files are added to your main target and “Create groups for any added folders” is selected.
  2. Copy libLeap.dylib into your project folder.
  3. Select your project and add libLeap.dylib as a required framework.
  4. Now let’s create a new Build Phase in order to copy libLeap.dylib into the executable. Go to your main target, click on “Build Phases” and add a “Copy Files Build Phase”. The destination must be set to “Executables”.
  5. Last, but not least, go to “Build Settings”, look for “C++ Standard Library” and make sure “libstdc++ (GNU C++ standard library)” is selected.

The class EALeapMotionController implements the interface LeapDelegate where all the events are handled.
Download template project

webOS low level programming – basic app

For Windows and Mac OS, Palm provides a low level SDK called Plug-in Development Kit (PDK) which allows you to develop C and C++ native applications or plug-ins that can be called from a web app. In the OS X version of the PDK, a plug-in is installed in XCode (yes, you can use another IDE, but the installer gives a beautiful wizard for Xcode). So let’s create a simple application!

Inside Xcode, go to ‘File’ and click on ‘New Project’ (or Shift + Command + N). You will see a new template called SDL Application, and that is what we are going to create.

This action will create a project with a simple structure that includes some necessary files in order to run the application on your Mac. Yes, you can (fully) develop an SDL application in your Mac (or PC) and then use nearly the same code inside the Palm Pre. What is interesting about this project wizard is that includes all the header files needed, and a script file to build the project for the device.

I will add ‘-lpdl’ to ‘Other Linker Flags’which will let me do some specific calls to the OS. This has to be handle will care. Due to the fact that this functions are only available in webOS, you should place some define to avoid executing and compiling that code when building the app for the desktop simulator.

Let’s create an empty main.cpp file with it’s header file, and the start building the application step by step

Main.h includes SDL header file and also some Palm’s headers.  What is more important, it will have definitions that will help running the app in the Mac and in the device properly.

Main.cpp  has functions to initialize the screen, setup the device and handles events from the user interface.

The most important part of this tutorial comes now. How it looks like in the emulator? and how to build and deploy this application into the device?

For running this application on the emulator, comment the RUNNING_ON_DEVICE definition and press ‘Build and Run’ or ‘Build and Debug’ in Xcode. You will only see something a blank screen of 320×480 pixels as this application doesn’t do something more instersting.

Running inside the Palm Pre requires going to the terminal and editing a few script files. The wizard generates a file called ‘buildit_for_device.sh’ that gives us a hand with the build process but doesn’t help too much in the deployment process. This file must be editing in order to know where is the source code and how the output file is called. It will looks similar to this

As you may noticed, if you run this script, you will get a binary file called ‘simple’ app. Eventhough you can copy this file to the device and run the code, the recommended way is to generate a package and install that package using palm tools. So the next step involves packaging the application. As in the web application a description file is requiered. I created a STAGING directory and inside that directory created a file called ‘appinfo.json’ with the following content.

As this is a native application it is very important to make our binary fila as executable, using ‘echo filemode.755=simpleapp > package.properties’.

Once this files are created, we can call palm-package with STAGING as argument, and the install the package using palm-install.

This action installs the application and places an icon in the launcher. Clicking the icon runs the full screen application.