Leap Motion - Why is Leap.LeapPINVOKE throwing an exception when I’ve copied the DLLs?

I was contacted the other week by a reader who had followed my Leap Motion tutorial, only to find that their code threw an exception when the SDK called Leap.LeapPINVOKE. Normally, this would suggest that the Leap.dll and LeapCSharp.dll files are not present within the project’s bin folder.

To my surprise, after following the steps I’d provided, I was faced with the same error. I loaded up my original project with an older version of the SDK, and this worked perfectly. Then it hit me; was my Leap Motion’s software out of date?

There was an update available (Leap Motion Control Panel -> General -> Install Update). I installed it, and naturally, everything started working!

EDIT: I’ve just followed my tutorial to double-check my findings, and there seems to be one additional step as of SDK version 1.2; I had to manually target my processor’s architecture. To do this for x64, once I’d included the correct Leap.dll and LeapCSharp.dll libraries in my project, I:

  1. Right-clicked my solution in Solution Explorer and clicked Properties
  2. Selected Configuration Properties on the left
  3. Clicked Configuration Manager
  4. In the resultant dialogue, under the Platform header for the project context, I clicked <New…>
  5. Chose x64 from the first dropdown, and copied the settings from Any CPU
  6. Clicked OK, Close, and OK
  7. Rebuilt and ran my project

Consequently, the program ran with no issues. Hope this helps!

Personality Forge.NET 1.0.1 released!

For those of you who are aware of my Personality Forge.NET library, I released version 1.0.1 yesterday. You can get it by either following the previous link or installing it from NuGet.

So what’s changed?

Apart from refactoring and the addition of unit tests, not much. The only major client-facing change concerns error handling. Previously, the PersonalityForge class exposed an ErrorService property, which in turn had a CustomHandler property:

IPersonalityForge forge = new PersonalityForge();
forge.ErrorService.CustomHandler += (ex, message, fatal) =>
        {
            //do stuff
        };
        
Response response = forge.Send(username, message);

Not only was this approach verbose, but just unnecessary. The only place a user would typically handle any exceptions raised by the library is upon calling the PersonalityForge.Send method. Also, determining if the exception was fatal made no sense, as this could differ with regard to the developer’s context. Therefore, I have replaced this by throwing a custom PersonalityForgeException, which can be handled like this:

try
{
    Response response = forge.Send(username, message);
}
catch (PersonalityForgeException e)
{
    Console.WriteLine(e.Message);
}

Personally, I feel that this is a much cleaner, familiar approach to error handling.

UPDATED: SimpleHttp - a basic HTTP server framework written in C#

I got bored last night so I started playing around with HttpListener and other relevant classes in the System.Net namespace, and I decided to write respective wrapper classes. The result: SimpleHttp.

For now I’ve included the release binary in the GitHub repository, but as the framework matures and becomes more stable, I’ll release it as a NuGet package.

Here’s the most simple usage of the framework:

using JamesWright.SimpleHttp;

namespace JamesWright.SimpleHttp.Example
{
    class Program
    {
        static void Main(string[] args)
        {
            App app = new App();

            app.Get("/", (req, res) =>
            {
                res.Content = "{ \"message\": \"Hello\" }";
                res.ContentType = "application/json";
                res.Send();
            });

            app.Get("/version", (req, res) =>
            {
                res.Content = "{ \"version\": \"0.1\" }";
                res.ContentType = "application/json";
                res.Send();
            });

            app.Start();
        }
    }
}

Given it’s only a day old, responses are not sent asynchronously, which means that a request will block the entire server until a response is sent; this is what I’ll address next. Also, it only works with HTTP GET, but I’m currently adding support for other verbs.

The App class bootstraps an internal Server class:

class Server
{
    private Listener listener;
    //contains four Dictionaries for GET, POST, PUT, and DELETE
    public RouteRepository RouteRepository { get; private set; }

    public Server(Listener listener, RouteRepository routeRepository)
    {
        this.listener = listener;
        RouteRepository = routeRepository;
    }

    public void Start(string port)
    {
        Console.Write("SimpleHttp server 0.2\n\n");
        Console.WriteLine("Initialising server on port {0}...", port);
        this.listener.Start(port, RouteRepository);
    }
}

Here’s the Listener class, used internally by Server to handle incoming requests:

class Listener
{
    private HttpListener httpListener;
    private HttpListenerContext context;

    public Listener()
    {
        this.httpListener = new HttpListener();
    }

    public void Start(string port, RouteRepository routeRepository)
    {
        this.httpListener.Prefixes.Add(string.Format("http://localhost:{0}/", port));
        this.httpListener.Start();

        Console.WriteLine("Listening for requests on port {0}.", port);

        Request request;

        while (TryGetNextRequest(out request))
        {
            Console.WriteLine("{0}: {1}", DateTime.Now, request.Endpoint);

            if (!TryRespond(request, routeRepository))
                Console.WriteLine("No handler specified for endpoint {0}.", request.Endpoint);
        }
    }

    private bool TryRespond(Request request, RouteRepository routeRepository)
    {
        if (!routeRepository.Get.ContainsKey(request.Endpoint))
            return false;

        routeRepository.Get[request.Endpoint](request, new Response(context.Response));
        return true;
    }

    private bool TryGetNextRequest(out Request request)
    {
        try
        {
            this.context = this.httpListener.GetContext();
            HttpListenerRequest httpRequest = this.context.Request;
            request = new Request(httpRequest);
            return true;
        }
        catch (Exception)
        {
            //TODO: output/log exception
            request = null;
            return false;
        }
    }
}

The Request and Response classes are simple wrappers around HttpListenerRequest and HttpListenerResponse. Listener retrieves the next request from the HttpListener's GetContext() method, using the Routes Dictionary to find the appropriate handler and pass the original request, and a new Response instance, to the developer.

I look forward to improving it and ultimately make it as scalable as possible. I’ll post an update soon!

Review - ‘Implementing AppFog’ by Matthew Nohr and Isaac Rabinovitch

image

Implementing AppFog is an invaluable book that details how technologists can deploy software to the cloud in a matter of minutes. As cloud services are becoming popular among developers and DevOps engineers for their cost, ease of use, and flexibility, this publication concisely demonstrates creating and modifying hosted applications and services via the web console and the command line interface. Combined with a detailed command line reference, this book is appropriate for beginners and AppFog veterans alike.

As a user of the service, the book has made me aware of some really powerful features, which I plan to use in my next deployment. I highly recommend it.

Getting started with Leap Motion in C#

image

Update: Is Leap.LeapPINVOKE throwing an exception? Read this.

One of my colleagues lent me his Leap Motion this week, and I was pleased to learn that the sensor has a very good SDK, simplifying the tracking of a user’s hands, digits, and any objects that they are holding.

In this tutorial, I am going to show you how to develop a simple .NET command line program in C# that locates the nearest finger to your screen and outputs its vertical and horizontal position in both a decimal representation and pixels, as well as measuring the speed at which your fingertip is travelling. The code can be found over at GitHub (sans the Leap Motion libraries due to licensing).

Firstly, open Visual Studio and create a new project. From the installed Visual C# templates, choose Console Application and name the project LeapMotionTest.

Before exploring some of Leap Motion’s APIs, we’re going to create two classes - Result (a model object representing the data that we will output to the user) and Reporter (a class that prints the values stored in a Result instance). This is just to make our code more tidy and ultimately maintainable.

Create a new class called Result with the following properties

using System;

namespace LeapMotionTest
{
    public class Result
    {
        public int X { get; set; } //Fingertip X position in pixels
        public int Y { get; set; } //Fingertip Y position in pixels
        public float XIntersect { get; set; } //Decimal of the X position in relation to the screen (e.g. 0.7)
        public float YIntersect { get; set; } //Decimal of the Y position in relation to the screen
        public int ScreenWidth { get; set; } 
        public int ScreenHeight { get; set; }
        public float TipVelocity { get; set; } //Speed at which the fingertip is moving
    }
}

Next, create the Reporter class with a method to print results:

using System;

namespace LeapMotionTest
{
    public class Reporter
    {
        public void Print(Result result)
        {
            Console.WriteLine("Screen intersect x: {0}", result.XIntersect);
            Console.WriteLine("Screen intersect y: {0}", result.YIntersect);
            Console.WriteLine("\n");
            Console.WriteLine("X: {0}/{1}px", result.X, result.ScreenWidth);
            Console.WriteLine("Y: {0}/{1}px", result.Y, result.ScreenHeight);
            Console.WriteLine("\n");
            Console.WriteLine("Tip velocity: {0}", result.TipVelocity);
        }
    }
}

Now we will write the code for reading data from the Leap Motion. Head over to the Leap Motion Developer site and register, then download the Leap Motion SDK for Windows. Once you have the files, you’ll need to add a reference to the correct .NET library (the project you created is most likely targeted at .NET 4/4.5/4.5.1 unless you’re using an older version of Windows):

image

image

image

image

The Leap Motion .NET library is dependent upon two other assemblies being present in the program’s build directory: Leap.dll and LeapCSharp.dll. I was unable to set these dependencies as project references, so I had to import these files and update their build properties:

image

image

Note: you’ll most likely have spotted the x86 and x64 folders within the lib directory. If you are unsure of whether you are targeting a 32-bit or 64-bit machine, chose the DLL files from the x86 folder.

For each DLL file, update the properties as shown in the following screenshot:

image

Now we are going to read some data from the device. Before we get stuck in, here are the SDK classes that we are going to use (found in the Leap namespace):

  • Controller - represents the main interface to the device. Can be queried directly for data, or can pass data to associated Listener instances
  • Listener - an inheritable class containing a set of virtual (overridable) methods, invoked when the device’s status changes
  • Frame - a snapshot of data concerning the user’s interaction with the device at a given time
  • Finger - represents a user’s finger
  • Vector - details the position of a finger or object
  • Screen - the screen through which the user is viewing the program

Create a new class called ReportingListener, inheriting from the Listener class:

using System;
using System.Linq;
using Leap;

namespace LeapMotionTest
{
    public class ReportingListener : Listener
    {
        private long currentTime,
                     previousTime,
                     timeChange;

        private Reporter reporter;

        public ReportingListener(Reporter reporter)
        {
            this.reporter = reporter;
        }

        public override void OnInit(Controller ctrl)
        {
            Console.WriteLine("Device initialised");
        }

        public override void OnConnect(Controller ctrl)
        {
            Console.WriteLine("Connected");
        }

        public override void OnDisconnect(Controller ctrl)
        {
            Console.WriteLine("Disconnected");
        }

        public override void OnExit(Controller ctrl)
        {
            Console.WriteLine("Exited");
        }

        public override void OnFrame(Controller ctrl)
        {
            
        }
    }
}

The OnFrame method is of most interest to us, as this will be invoked every time the device has received new tracking data. Add the following code to said method:

    public override void OnFrame(Controller ctrl)
    {
        //Get the current frame
        Frame currentFrame = ctrl.Frame();

        this.currentTime = currentFrame.Timestamp;
        this.timeChange = currentTime - previousTime;

        if (this.timeChange < 1000 || currentFrame.Hands.IsEmpty)
            return;
            
        //Get the first finger
        Finger finger = currentFrame.Fingers[0];

        //Get the closest screen that intercepts a ray projected from the finger
        Screen screen = ctrl.LocatedScreens.ClosestScreenHit(finger);

        //Get finger tip's velocity
        float tipVelocity = (int)finger.TipVelocity.Magnitude;

        /*only print info when velocity exceeds a certain threshold,
          so that the slightest movements won't continuously output
          data to the screen*/
        if (screen == null || !screen.IsValid || tipVelocity <= 25)
            return;

        Vector vector = screen.Intersect(finger, true);

        //x and y positions are sometimes returned as NaN (e.g. if hand is partly obscured by screen)
        if (float.IsNaN(vector.x) || float.IsNaN(vector.y))
            return;

        float xScreenIntersect = vector.x;
        float yScreenIntersect = vector.y;

        float x = xScreenIntersect * screen.WidthPixels;
        float y = screen.HeightPixels - (yScreenIntersect * screen.HeightPixels);

        Result result = new Result
        {
            X = (int)x,
            Y = (int)y,
            XIntersect = xScreenIntersect,
            YIntersect = yScreenIntersect,
            ScreenWidth = screen.WidthPixels,
            ScreenHeight = screen.HeightPixels,
            TipVelocity = tipVelocity
        };

        this.reporter.Print(result);
        previousTime = currentTime;
    }

The comments and previous remarks should explain how the code and APIs are working, but here a few points of interest:

Frame currentFrame = ctrl.Frame();

This will get the frame associated with the controller at the time of invocation.

    this.currentTime = currentFrame.Timestamp;
    this.timeChange = currentTime - previousTime;

    if (this.timeChange < 1000 || currentFrame.Hands.IsEmpty)
        return;
        
    [...]
    previousTime = currentTime;

By checking the time passed between receiving the previous frame and the next frame, we can control the frequency at which data is output to the console. In the case of our program, printing every frame of data is unnecessary.

    if (screen == null || !screen.IsValid || tipVelocity <= 25)
        return;

Returning from OnFrame if the fingertip’s velocity falls below a certain threshold means that the slightest finger jitter won’t bombard the console with output. Try removing the check later and see what happens.

    if (float.IsNaN(vector.x) || float.IsNaN(vector.y))
        return;

This condition is important as a Vector’s co-ordinates can sometimes return NaN (e.g. when one places one’s hand behind the screen).

Finally, modify your program’s Main method (usually located in Program.cs) to hook everything up!

using System;
using Leap;

namespace LeapMotionTest
{
    class Program
    {
        static void Main(string[] args)
        {
            ReportingListener listener = new ReportingListener(new Reporter());
            Controller controller = new Controller();
            controller.AddListener(listener);

            Console.WriteLine("Listening! Press any key when you're finished to exit.");
            Console.ReadKey();

            controller.RemoveListener(listener);
            controller.Dispose();
        }
    }
}

You should always remove any Listeners from the Controller and dispose of it when you no longer need it; this allows the garbage collector to free up memory for other objects to use. It is a very important step to take in more complex solutions (e.g. in WPF, where you may be navigating between instances of different pages) and is a best practice.

Hit Ctrl + F5 to run the program and wave one of your fingers in front of your monitor. Have fun!

image

I have also been exploring the LeapMotion SDK in WPF, drawing the position of each finger as an ellipse on screen. A tutorial on this will be coming soon.

Feel free to get in touch if you have any questions or issues :)

My first NuGet package

A few months ago, I published a post on my first .NET library, Personality Forge.NET (The Personality Forge is a chatbot service with a <sarcasm>JSON API</sarcasm>). After some refactoring, adding asynchronous methods, and writing some unit tests, I decided to publish it as a NuGet package.

It can be installed into your project by searching for ‘chatbot’ in the NuGet Package Manager window (my package is the sole result for this query - sick!) or by running the following command in the Package Manager Console:

Install-Package JamesWright.PersonalityForge

Norton products causing scrolling and focus issues on Windows 8

I was browsing a web page with Google Chrome today, and once it had loaded I was unable to scroll or right-click. I thought this was an issue with my favourite browser, but I was able to reproduce it with IE10. 

After some searching, it was suggested that the Norton family of products could interfere with scrolling and other UI operations. Uninstalling Norton Online Backup (thanks Acer, because I really needed it, right?) solved the problem.

To conclude, I’d like to state two things:

  1. Norton, the fact that your software can prevent the most basic operations is ridiculous. Sort it out!
  2. Do people still use Norton software out of choice? Years ago I ditched Norton AntiVirus for AVG Free, and had a much better experience

Stealthfeed Updates (aka my first Node.js post)

Taking a recruitment ad too seriously

So I came across this really cool recruitment ad on LinkedIn…

image

…and I wrote a little program to solve it because I have nothing better to do with my lunch hour:

using System;

namespace PhoneNumber
{
    class Program
    {
        private const int X = 24, Y = 30, Ten = 10;			  
        private const double Square = 2;
		
        public static void Main(string[] args)
        {
            Console.WriteLine("The number is 044.{0}.{1}.", ResolveFirstGroup(), ResolveSecondGroup());
            Console.ReadKey();
        }
		
        private static int ResolveFirstGroup()
        {
            return (int)Math.Pow(Y, Square) - X;
        }
		
        private static int ResolveSecondGroup()
        {
            return (int)(Math.Pow(Y, Square) - Math.Pow(Ten, Square)) * Ten;
        }
    }
}
Output: The number is 044.876.8000.

Syntax highlighting with Google Prettify

I have introduced Google Prettify to my blog in order to highlight the syntax of my code snippets. I attempted to use a few other libraries to no avail.

Here it is in action:

class PrettifyTest
{
    static void Main()
    {
        Console.WriteLine("Prettified :)");    
    }
}
Load more posts