Part One: Setup
The Myo armband is a new device for a new computing ecosystem, but that doesn’t mean you can’t Myo-enable the existing Windows and Mac apps you use every day. In fact, we’ve made it ridiculously easy with Myo Scripts, and it’s built right into Myo Connect! I’m Paul, Developer Evangelist at Thalmic Labs, and I’m going to walk you through building one, step by step.
Let’s get the basics set up. First thing you need is a Myo armband and Myo Connect. If you haven’t done it, Myo Connect has a getting started walkthrough where you can practice the various gestures, lets you manage the Myo armbands paired with your computer, and even ping a Myo so you know which is which. There are a few other neat features we’ll be using today. One is the Pose Window that will show you when the Myo thinks you are making a pose (which is invaluable for debugging). You can launch this from the Manage Myo Armbands screen. Do this now and keep it in view. The other is the Application Manager.
Any connector you download and install from the Myo Market will go here. This is also where you’ll add and update any scripts you write or find yourself. To actually write a script you need a text editor. You’ll probably want something a little more powerful than Notepad. I’d recommend either Notepad++ or Sublime Text, both of which have syntax highlighting for Lua (super useful). I’ll be using Notepad++. Once you have an editor, create and open a new file called “MyFirstScript.lua”. Inside, we are going to build a basic script.
The first thing you need to do is set your
scriptId variable. This is what’s used to identify your script, and should be unique. You will typically want to use the “reverse domain name” convention. eg:
>com.<your domain>.<maybe a subdomain>.<your script name>. It’s worth noting at this point that when I put something in angle brackets like that I mean replace it and the brackets with a real value. For example, this is what I’m using:
scriptId = 'com.thalmic.examples.myfirstscript'
For me, that looks like this:
You can use that for our examples, but it does need to be unique for any script you actually release. In Lua, you can use single or double quotes to enclose strings. I usually use single quotes for my
scriptId since Notepad++ doesn’t do spell checking in them, but it doesn’t matter.
Now, this is technically the minimum you need for a Myo Script. You could save it out and load it in the script manager like this but it wouldn’t do anything. Let’s add just a little bit more so we can actually see that it’s working.
First, add a
scriptTitle variable, and one called
scriptDetailsUrl. We’ll leave
scriptDetailsUrl as an empty string for now, but when you submit your script to the Myo Market you’ll want to come back and fill it in with the URL for your script so users have easy access to the script’s instructions. Fill in
scriptTitle with the name that you want to actually show up in the Application Manager (otherwise it will be the file name). For example, I did this:
scriptId = 'com.thalmic.examples.myfirstscript' scriptTitle = "My First Script" scriptDetailsUrl = "" -- Fill this in when you submit to Myo Market!`
Now, there are a few different predefined “callbacks” you can implement in Myo Script. These are special functions that will get called for you in response to certain events. In Myo scripts there is no “main” method or anything like that. Technically any code written outside of a function will be executed when the script is first loaded by the Myo Script Manager (that’s how
scriptId is getting set), but all of our actual work will be done from these callbacks.