Good Vibrations on Windows Phone

I thought I’d do something foolish today and decided to play with a Windows Phone API I never used before – the VibrateController. How about playing back a rhythm? I coded up a very simple API to play back sequences of vibrations. You just build a VibrationSequence object with a list of Vibrations that specify vibration duration and a delay between the vibration start and the next vibration.

This is the code that plays it back:

var seq = new VibrationSequence(
    new Vibration(250, 500), 
    new Vibration(250, 1250), 
    new Vibration(200, 250), 
    new Vibration(250, 500), 
    new Vibration(250, 1500));
Play(seq);

And here is the VibrationSequence API:

namespace VibrationPlayground
{
    using System;

    /// <summary>
    /// The vibration.
    /// </summary>
    public class Vibration
    {
        /// <summary>
        /// Gets the vibration duration.
        /// </summary>
        public TimeSpan Duration { get; private set; }

        /// <summary>
        /// Gets the total duration from the vibration start to the next <see cref="Vibration"/> object in <see cref="VibrationSequence"/>.
        /// </summary>
        public TimeSpan TotalDuration { get; private set; }

        /// <summary>
        /// Initializes a new instance of the <see cref="Vibration"/> class.
        /// </summary>
        /// <param name="durationMs">
        /// The duration ms.
        /// </param>
        /// <param name="totalDurationMs">
        /// The total duration ms.
        /// </param>
        public Vibration(double durationMs, double totalDurationMs)
        {
            this.Duration = TimeSpan.FromMilliseconds(durationMs);
            this.TotalDuration = TimeSpan.FromMilliseconds(totalDurationMs);
        }
    }
}
namespace VibrationPlayground
{
    using System;
    using System.Collections.ObjectModel;
    using System.Threading;

    using Microsoft.Devices;

    /// <summary>
    /// The vibration sequence.
    /// </summary>
    public class VibrationSequence
    {
        /// <summary>
        /// Gets the sequence of <see cref="Vibration"/> objects.
        /// </summary>
        public ReadOnlyCollection<Vibration> Vibrations { get; private set; }

        /// <summary>
        /// Initializes a new instance of the <see cref="VibrationSequence"/> class.
        /// </summary>
        /// <param name="vibrations">
        /// The vibrations.
        /// </param>
        public VibrationSequence(params Vibration[] vibrations)
        {
            this.Vibrations = new ReadOnlyCollection<Vibration>(vibrations);
        }

        /// <summary>
        /// Starts playing the vibration sequence.
        /// </summary>
        /// <param name="onComplete">
        /// The action to invoke when sequence playback is complete.
        /// </param>
        public void Start(Action onComplete)
        {
            ThreadPool.QueueUserWorkItem(
                s =>
                {
                    foreach (var vibration in Vibrations)
                    {
                        VibrateController.Default.Start(vibration.Duration);
                        Thread.Sleep(vibration.TotalDuration);
                    }

                    onComplete.Invoke();
                });
        }
    }
}

The project is available here. Does not seem to work on the emulator, or maybe my laptop lacks a vibrator? Smile

Advertisements
Tagged , , ,

4 thoughts on “Good Vibrations on Windows Phone

  1. […] Good Vibrations on Windows Phone Share this:FacebookTwitterRedditDiggStumbleUponLinkedInTumblrEmailPrintLike this:LikeBe the first to like this post. […]

  2. Graham Smith says:

    I don’t see where you are getting Play() from…

  3. xyzzer says:

    It’s there in the code download: private static void Play(VibrationSequence seq){seq.Start(() => Play(seq));}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: