Hello!
People usually say that someone is mature enough when you start to be more concerned by the nobey to be spend in the dentist rather the minutes of pain and suffering. In our profession mature usually is associated to set aside the lines of code and start doing more management tasks. To many people this seems an aberration, when in reality don’t realize that it is an excellent opportunity to work with friends, learn on how to work as a team and try to have a good time with one common goal. This is team management
Well, when you spend traveling from here to there, and you don’t open Visual Studio in a week, you have to get back the lost time in the weekend (also with family and friends!). Why is that today, already with several ideas and personal projects in my head, I start with a new one: Leap Motion time.
The truth is that the device is €99 , but personally I think it worth more than this amout: is a great device!. I will not explain what does the Leap do, thinks in Minority Report, or better to watch this video of Engadget.
I what I will do is to download the SDK and then:
1 Open Visual Studio 2013
2. Create a console application
3. Add the following references to the application
- \LeapDeveloperKit\LeapSDK\lib\LeapCSharp.NET4.0.dll
4 I can now start to put some code to see the really power of the Leap Motion sensor! As always with these devices, I have to configure the app to be compiled for a specific target, in this case X86.
5, In the Main add the following code and already have something running. In this section, what we do is create a listener for events that Leap Motion sends and initialize it with a Controller.
1: using System;
2: using Leap;
3:
4: namespace LeapConsole1
5: {
6: class Program
7: {
8: static void Main(string[] args)
9: {
10: var listener = new ElBrunoListener();
11: var controller = new Controller();
12: controller.AddListener(listener);
13: Console.WriteLine("Press Enter to quit...");
14: Console.ReadLine();
15: controller.RemoveListener(listener);
16: controller.Dispose();
17: }
18: }
19: }
6. The ElBrunoListener class has a little more code, since it is that is responsible for processing information that sends us the Leap. We see that you have inside
1: using System;
2: using System.Linq;
3: using Leap;
4:
5: namespace LeapConsole1
6: {
7: class ElBrunoListener : Listener
8: {
9: private readonly Object _thisLock = new Object();
10:
11: private void SafeWriteLine(String line)
12: {
13: lock (_thisLock)
14: {
15: Console.WriteLine(line);
16: }
17: }
18:
19: public override void OnInit(Controller controller)
20: {
21: SafeWriteLine("Initialized");
22: }
23:
24: public override void OnConnect(Controller controller)
25: {
26: SafeWriteLine("Connected");
27: controller.EnableGesture(Gesture.GestureType.TYPESWIPE);
28: }
29:
30: public override void OnDisconnect(Controller controller)
31: {
32: SafeWriteLine("Disconnected");
33: }
34:
35: public override void OnExit(Controller controller)
36: {
37: SafeWriteLine("Exited");
38: }
39:
40: public override void OnFrame(Controller controller)
41: {
42: var frame = controller.Frame();
43: var gestures = frame.Gestures();
44: foreach (var swipe in from gesture in gestures where gesture.Type == Gesture.GestureType.TYPESWIPE select new SwipeGesture(gesture))
45: {
46: var gestureName = GetGestureNameFromSwipe(swipe);
47: SafeWriteLine(string.Format("Swipe id: {0}, name: {1}", swipe.Id, gestureName));
48: break;
49: }
50: }
51:
52: private string GetGestureNameFromSwipe(SwipeGesture swipe)
53: {
54: var gestureName = "undefined";
55: var direction = swipe.Direction;
56: if (direction.Yaw > 0)
57: {
58: gestureName = "left to right";
59: }
60: if (direction.Yaw < 0)
61: {
62: gestureName = "right to left";
63: }
64: return gestureName;
65: }
66: }
67: }
7. In the first place, we can see that we can do an override of OnInit, OnConnect, OnDisconnect and OnExit to initialize the sensor. In this case, I just want that the sensor registers the SWIPE gesture.
8 Then the OnFrame method, we can process the information that returns us a FRAME. The truth is that it is impressive what has this object. In this example, we access the collection of gestures and in it, we get SWYPE type.
9. Then we have to process all the information from this SWIPE. On the one hand we know that it is a SWIPE, but we need to find out the direction of the same. To do this, I’ll work with the SwipeGesture object and I’ll try to identify if the swipe has been left to right processing the Direction property.
Note: Direction is a vector. A vector has a lot of information, such as the start position and end, always associated with the axes, X, and y Z. With this information and thinking of the vector in the 3 axes, we could identify swipes from left to right, top to bottom, bottom-left, etc.
In this case, and to summarize much we can do with the LEAP, I’ve decided to process the YAW property. It gives us an integer value with the difference in angles from the negative Z axis and the positive X axis.
Thus if the angle is greater that 90 degrees will be a SWIPE LEFT TO RIGHT and if it is less than 90 degrees is a RIGHT TO LEFT SWIPE.
Note: This always thinking that you’re not Terminator and make gestures of swipe in a direction straight unchanged.
10 As well, now already we can launch the app and see how the console shows the gestures that we are doing on the Leap Motion!
By the way, I will have to get some Google Glases or a sports camera because the film stock in 1st person with the mobile phone… because it is not well
Greetings @ Home
El Bruno
Leave a comment