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

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!

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 :)");    
    }
}

C# - Delaying code execution

I was working on a WPF quiz game and I wanted to display a message for a number of seconds before posing the next question. To achieve this I had to write a helper class:

public static class DelayedInvocation
{
    public static void After(TimeSpan delay, Action action)
    {
        TimerCallback callback = OnDelay;
        new Timer(callback, action, (int) delay.TotalMilliseconds, Timeout.Infinite);
    }

    private static void OnDelay(object action)
    {
        Action callback = action as Action;

        if (callback != null)
            callback();
    }
}

Passing Timeout.Infinite to the Timer's constructor specifies an infinite waiting period, or in other words, ensures that the timer's interval occurs only once.

Here’s how to use the After method to the delay the execution of an anonymous method:

DelayedInvocation.After(TimeSpan.FromSeconds(3), () => State = GameState.Asking);

Some similar code snippets tend to surround the creation of the TimerCallback and Timer object instances with a lock statement, but this isn’t necessary as the callback is executed on a separate thread.

The art of using custom fonts in Windows 8 Store Apps

I was attempting to use pulling my hair out over custom fonts in a Windows 8 Store app last weekend. I must have spent around 3 or 4 hours trying to render a TrueType font, and a number of thoughts as to why it didn’t work circled my mind: Is the file path correct? Is the font file invalid? Is there an issue with the framework?

As a last resort, I used an OpenType font. Interestingly, it worked straight away. This, as well as Microsoft’s documentation, could suggest that OpenType is the best font format to use with the framework:

For XAML, advanced OpenType capabilities that were supported in WPF and Silverlight are also supported in the Windows Runtime XAML framework.

However, I have seen various examples of developers successfully embedding TrueType fonts. Therefore, my assumption is that TrueType fonts aren’t fully supported by Windows 8 Store apps. More experienced developers: would you care to agree?


Anyway, my app (source code can be found here) has a ResourceDictionary which contains the below style. I have removed the leading slash from the path as my views are stored in the app’s start up project, which additionally contains the common assets (thanks COM):

<Style x:Key="BaseFont"
           TargetType="TextBlock">
    <Setter Property="FontFamily"
                Value="Assets/Quicksand_Book.otf#Quicksand" />
    <Setter Property="Foreground"
                Value="Black" />
</Style>
 
This can then be inherited using the BasedOn attribute. Although my styles need further refactoring, this ensures that all text styles are updated when the app’s font is changed:
 

<Style x:Key="PostUsername"
           TargetType="TextBlock"
           BasedOn="{StaticResource BaseFont}">
    <Setter Property="FontSize"
                Value="18" />
</Style>
 
The styles can be used via the StaticResource markup extension:

<TextBlock Text="{Binding Username}"
                  Style="{StaticResource PostUsername}"
                  Grid.Row="2"
                  Grid.Column="0"
                  HorizontalAlignment="Center" />

I finally did it. After almost 5 years of using the same ageing laptop with a load of issues (dodgy speakers, broken trackpad, I could go on), I bought a new one. It’s an Acer Aspire V5-571P with an i7 processor and 8GB RAM. I would have saved for something even better if my old Windows laptop had lasted a bit longer.

Load more posts