Snap! extensions are a great way to connect to the physical world and send or receive information to and from hardware. In this topic we'll be taking a look at using a Wii Remote (henceforth Wiimote) and connecting it to Snap!. We'll explore the hardware a bit to learn something interesting about the way it behaves. So without further ado... let's begin!
Step 1: Preparing the Files
Using Snap! extensions usually requires two things: a set of blocks which can be used to send information from Snap!, and a local server to receive that information and do something with it. In the case of our Wiimote, both of these pieces are available on the wiisnap repository.
The two wiisnap files we need are
wiisnap.xml. The xml file contains of the Snap! blocks we need to interact with our wiimote. The python file (.py) is what we run to recieve information from Snap! and send it back to the wiimote. The simplest way to download these files is to click "clone or download" and then "download zip", and then extract the downloaded zip into a folder on your computer.
However, if you're a git pro, feel free to clone the repository instead.
Now that you have the files available, it's time to put them to good use.
Step 2: Preparing Snap!
Snap! allows users to import sets of blocks created by others. In our case, we want to use the blocks created by technoboy10 to connect to our Wiimote. One of the files we downloaded in step 1 was
wiisnap.xml. That file contains all of the blocks we need. To use those blocks in Snap!, we need to open a new project and then click File > Import...
A file select dialogue will appear. Find the
wiisnap.xml file you downloaded earlier and open it. Now you'll find some new wiimote-related blocks in the "variables" section.
Now it's time to get these blocks working using
Setting up the Middleman
We're going to use Python as a middleman, taking in information from Snap! and sending it over to our wiimote (and vice-versa). If you haven't already, you'll need to install Python on your computer. This works differently on different platforms, but the Python docs should have you covered.
Next we're going to install snapext using pip. Simply run
pip install snapext in a terminal and, barring any unforeseen errors, you'll be set to go.
The final step is to install cwiid. This is what allows our python file to connect with the wiimote. To install on Ubuntu, run
sudo apt-get install libcwiid1 lswm wmgui wminput on the terminal. If you're on a different linux distribution, this will require some searching around for instructions. If you find out how to install cwiid on non-Ubuntu systems, please respond with an explanation!
Connecting to the Wiimote
Now that we have everything installed and ready, it's time to connect to a Wiimote. Grab a wii remote from your nearest friend and open up a terminal window. cd into the directory where you saved the
wiisnap.py file and run
python wiisnap.py. The program will ask you to connect a Wiimote by pressing the 1 and 2 buttons simultaneously. Do so, wait a few seconds, and you will be connected:
We're on the home straight now. Head back to the Snap! project where you imported the blocks from
wiisnap.xml and try running this script:
If all goes well, the bottom four lights on your Wiimote will all turn on.
Now why would the number 15 result in all of the lights being turned on? To figure it out, let's run a little experiment. We'll loop through every number (starting with 0 and counting up) and keep track of the pattern of lights.
After running the script, we find this pattern of lights:
(After hitting 15, the pattern loops back around. So number 16 is blank just like 0, 17 looks like 1, and so on...)
At first this pattern seems quite random, but upon closer inspection this actually isn't the case. The lights which are activated actually correspond to the binary equivalent of the number we enter!
Here's a decimal to binary table, for comparison:
| 0 | 0000 | | 1 | 0001 | | 2 | 0010 | | 3 | 0011 | | 4 | 0100 | | 5 | 0101 | | 6 | 0110 | | 7 | 0111 | | 8 | 1000 | | 9 | 1001 | | 10 | 1010 | | 11 | 1011 | | 12 | 1100 | | 13 | 1101 | | 14 | 1110 | | 15 | 1111 |
The way the lights are oriented on the remote means they show up backwards, but the lights essentially match the 1s and 0s. A 1 means the light is turned on, and a 0 means the light is turned off.
The biggest question left is why the lights match up perfectly to the binary representation of the numbers. This, however, will be left as an exercise for the reader.