Controlling your Treadmill from Silverlight - Part 2

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.

image

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

Feedback

# re: Controlling your Treadmill from Silverlight - Part 2

Gravatar Page, this is cool stuff.

Even cooler would be to make the speed and incline graphics touch-sensitive, so if you had a tablet or similar device, you could simply touch & drag to the desired speed/incline, rather than clicking a button repeatedly. Hmm...may have to play with something like that for my M700 (pen and touch input-enabled). 7/30/2008 11:52 AM | G. Andrew Duthie

# Silverlight Cream for July 30, 2008 -- #338

Gravatar Mark Monster on cross-comain-scripting issues, Page Brooks on his treadmill, Mike Snow on the Mouse Wheel 7/30/2008 12:17 PM | Community Blogs

# re: Controlling your Treadmill from Silverlight - Part 2

Gravatar Yeah, that would be a cool enhancement. I really like the tablet idea too, it would be nice to be able to lay a tablet pc on the treadmill while running. I'll see what I can do after I finish these other features! :) 7/30/2008 9:12 PM | Page Brooks

# re: Controlling your Treadmill from Silverlight - Part 2

Gravatar It looks like you did this as an exercise, but if it interests you, the guys at netathlon have software that does something similar for many types of machines.

www.riderunrow.com 8/9/2008 2:46 PM | Kevin Joubert

Comments have been closed on this topic.