Creating Your Own Module for The KAI

Creating Your Own Module for The KAI

aditi-shah

If you’ve jumped here from the blog that introduces you to the Kai SDK, then you're halfway to creating your first module for the Kai. If you haven’t given it a read, you may want to take a look at that blog first. Here’s the link.

Now that you’re here, let’s get you started. This blog contains a starter module that we have created for you. You can choose to start with this module just to try things out.

This module is a simple example where gestures correspond to actions taken on your computer. The Kai sends across multiple types of data on a subscription basis. Modules that are to integrate with the SDK must explicitly mention the capabilities it wants to receive. Therefore, by “subscribing” to specific Kai data, you are choosing the information that you want to receive from the Kai as and when events occur. This data could be anything from Gesture Data to Gyroscope, Accelerometer, Magnetometer or PYR data.

For the extent of this example, we’re creating a module that plays various sound clips when gestures are performed. Therefore, the data we will be subscribing to is GestureData.

You can start off by picking Visual Studio or JetBrains Rider.

Here are the steps to follow.

  • Create a new c# project.

  • Add a new New Nuget Dependency ( kai.WebSocketModule )

  • In your main function, add the following code.

using System.Threading;

using System.Media;

namespace Kai.Module.Boilerplate

{
internal static class DefaultDataHandler
{

private const KaiCapabilities ModuleCapabilities = KaiCapabilities.GestureData;

public static void Main(string[] args)

{

KaiSDK.Initialise("<example-module-id>", "<example-module-secret>");</example-module-secret></example-module-id>

InitialiseEvents();

KaiSDK.DefaultKai.SetCapabilities(ModuleCapabilities);

KaiSDK.Connect();

Thread.Sleep(Timeout.Infinite); // prevent program from exiting

}

public static void InitialiseEvents()

{

// default kai event handler callbacks

KaiSDK.DefaultKai.Gesture += GestureHandler;

// universal error handler

KaiSDK.Error += ErrorHandler;

}

private static void GestureHandler(object e, GestureEventArgs gestureArg)

{
// Checkout: https://github.com/vicara-hq/kai-dotnet/blob/develop/Kai.BaseModule/Delegates.cs

// For specs of the eventArg classes

Log.Info($"Gesture Event Triggered: {gestureArg.Gesture}");

SoundPlayer player;

switch(gestureArg.Gesture)

{

case Gesture.SwipeUp:

case Gesture.SideSwipeUp:

player = new SoundPlayer(@"c:\one.wav");

break;

case Gesture.SwipeDown:

case Gesture.SideSwipeDown:

player = new SoundPlayer(@"c:\two.wav");

break;

case Gesture.SwipeLeft:

case Gesture.SideSwipeLeft:

player = new

SoundPlayer(@"c:\three.wav");

break;

case Gesture.SwipeRight

:case Gesture.SideSwipeRight:

player = new SoundPlayer(@"c:\four.wav");

break;

default:

return;

}

player.Play();

}

private static void ErrorHandler(object e, ErrorEventArgs errorArg){

Log.Error($"Error: {errorArg.Error}");

}}}

Voila! You’re done. You have now given the process of creating modules a ‘first try’. Once you’ve tried this out, you could then work on your own module. We hope this helps to kick things off!

For those of you who need more information, We have our trusty SDK documentation here. It covers pretty much everything you would need to know.

If you still need help, you can reach out to our development team at drift.me/vicara. We’re always around to help you get started!

If you’ve jumped here from the blog that introduces you to the Kai SDK, then you're halfway to creating your first module for the Kai. If you haven’t given it a read, you may want to take a look at that blog first. Here’s the link.

Now that you’re here, let’s get you started. This blog contains a starter module that we have created for you. You can choose to start with this module just to try things out.

This module is a simple example where gestures correspond to actions taken on your computer. The Kai sends across multiple types of data on a subscription basis. Modules that are to integrate with the SDK must explicitly mention the capabilities it wants to receive. Therefore, by “subscribing” to specific Kai data, you are choosing the information that you want to receive from the Kai as and when events occur. This data could be anything from Gesture Data to Gyroscope, Accelerometer, Magnetometer or PYR data.

For the extent of this example, we’re creating a module that plays various sound clips when gestures are performed. Therefore, the data we will be subscribing to is GestureData.

You can start off by picking Visual Studio or JetBrains Rider.

Here are the steps to follow.

  • Create a new c# project.

  • Add a new New Nuget Dependency ( kai.WebSocketModule )

  • In your main function, add the following code.

using System.Threading;

using System.Media;

namespace Kai.Module.Boilerplate

{
internal static class DefaultDataHandler
{

private const KaiCapabilities ModuleCapabilities = KaiCapabilities.GestureData;

public static void Main(string[] args)

{

KaiSDK.Initialise("<example-module-id>", "<example-module-secret>");</example-module-secret></example-module-id>

InitialiseEvents();

KaiSDK.DefaultKai.SetCapabilities(ModuleCapabilities);

KaiSDK.Connect();

Thread.Sleep(Timeout.Infinite); // prevent program from exiting

}

public static void InitialiseEvents()

{

// default kai event handler callbacks

KaiSDK.DefaultKai.Gesture += GestureHandler;

// universal error handler

KaiSDK.Error += ErrorHandler;

}

private static void GestureHandler(object e, GestureEventArgs gestureArg)

{
// Checkout: https://github.com/vicara-hq/kai-dotnet/blob/develop/Kai.BaseModule/Delegates.cs

// For specs of the eventArg classes

Log.Info($"Gesture Event Triggered: {gestureArg.Gesture}");

SoundPlayer player;

switch(gestureArg.Gesture)

{

case Gesture.SwipeUp:

case Gesture.SideSwipeUp:

player = new SoundPlayer(@"c:\one.wav");

break;

case Gesture.SwipeDown:

case Gesture.SideSwipeDown:

player = new SoundPlayer(@"c:\two.wav");

break;

case Gesture.SwipeLeft:

case Gesture.SideSwipeLeft:

player = new

SoundPlayer(@"c:\three.wav");

break;

case Gesture.SwipeRight

:case Gesture.SideSwipeRight:

player = new SoundPlayer(@"c:\four.wav");

break;

default:

return;

}

player.Play();

}

private static void ErrorHandler(object e, ErrorEventArgs errorArg){

Log.Error($"Error: {errorArg.Error}");

}}}

Voila! You’re done. You have now given the process of creating modules a ‘first try’. Once you’ve tried this out, you could then work on your own module. We hope this helps to kick things off!

For those of you who need more information, We have our trusty SDK documentation here. It covers pretty much everything you would need to know.

If you still need help, you can reach out to our development team at drift.me/vicara. We’re always around to help you get started!