Controlling your Treadmill from Silverlight - Part 1
In the first part of this series, I demonstrated a Silverlight application that controls a treadmill. Today, we will go into a little more detail on using various audio signals (aka Chirps) which, in turn, instruct the treadmill to change settings. To keep things simple, we are going to just focus on the speed and incline aspect of the application in this post. In the next post, we will enhance the application by adding a feature that will allow you to create treadmill workouts.
Obtaining the Chirps
In order to control your treadmill (assuming your treadmill supports the iFit audio interface), you need to somehow create the appropriate chirps. These chirps are very short (about 1 second) audio sounds. To the human ear, they all sound almost identical, but if you take a look at the waveform for any of these files, you will see that they do exhibit a pattern. The waveform below represents a speed of 3.0 and an incline of 5.0.
Fortunately for us, the hard work has already been done! After a bit of searching, I was able to find an project on SourceForge called iFit Exercise Equipment Programmer. This command line application will generate the necessary audio signals needed to control your treadmill. For example, the following series of commands would generate a file with speed 3.0 and an incline of 5.0:
ifits.exe 30_50.wav
test2.wav does not exist, creating by default.
Max time: 2
FORM: ifits> [time in seconds] [speed] [incline]
EXIT: ifits> -1 0 0
ifits> 1 3 5
ifits> -1 0 0
Exiting
I'll leave it as an exercise for you to figure out how to generate each of the files for the various combinations of speed and incline. You probably won't want to do this by hand, there are a lot of them. I believe there are a few command line options that will assist with generating the various files. Also, since Silverlight does not have the ability to play *.wav files, you must convert these files to another format. To do this, you will need an application such as Audacity. In my application, I converted the *.wav files to *.mp3 format and it worked splendidly.
Building the Visuals
The UI is fairly simple, I jumped into Expression design to create my visuals and then copied the XAML into my VS project. The speed and incline visuals are simple paths covered by clipping rectangles. As the respective speeds and inclines change, we update the clipping rectangle position to display the appropriate percentage of the visual. After all of the XAML is in place, you are left with something like this:
Wiring up the Logic
As the buttons are clicked, we play the appropriate sound and update the visual display. If the sound files are added to your project with a Build Action of Resource, they will be included in the XAP file and can be called like so:
XAML:
1: <MediaElement x:Name="SoundMedia" AutoPlay="True" Height="0" Width="0" />
1: string fileName = string.Format("{0}_{1}.mp3", m_currentSpeed * 10, m_currentIncline * 10);
2: SoundMedia.Volume = 0.5;
3: StreamResourceInfo sri = Application.GetResourceStream(new Uri("SilverTread" + ";component/sounds/" + fileName, UriKind.Relative));
4: SoundMedia.SetSource(sri.Stream);
Source Code
In the next post, we will make the application a little more useful by adding a workout feature.
You can download the code for this post using the hyperlink below. Enjoy!
Download Source Code