Monday, September 12, 2011

Introduction to the Siren of Shame Device API

So you don't want to monitor a build but still want to use that cool device you picked up from SirenofShame.com. This post will get you started.

First step is downloading the DLLs from github. If you want examples and the latest code I would recommend grabbing the code via Git from here. Don't have Git and want to look at a simple example go here (this example is pretty much what is covered in this blog post).

OK, lets write some code. Start by creating a new WinForm project (it must be .NET 4 non-client profile) and open up the code behind.

We're going to need access to the device. This is where the class SirenOfShameDevice comes it. SirenOfShameDevice wraps all the functionality of the device in one nice clean wrapper. ISirenOfShameDevice is just an interface allowing you to mock out the device for unit testing which I know all of you are doing, Right? So lets create one. Add the following to your form.
private readonly ISirenOfShameDevice _sirenOfShameDevice
   = new SirenOfShameDevice();

Next we're going to need to know when the device gets plugged in so we can do something cool with it. Windows will send messages to all the open windows on the system when a USB device is plugged in. Well we are going to take advantage of that and listen for those events. Good thing for you SirenOfShameDevice takes care of most of that for you but it does need a little help and that's where this code comes in. Add this to your form.
protected override void WndProc(ref Message m) {
   if (_sirenOfShameDevice != null) {
      _sirenOfShameDevice.WndProc(ref m);
   }
   base.WndProc(ref m);
}

Great, but how do we know when it connects. Well we need to hook up some events to, you guessed it, the SirenOfShameDevice class. Add the following lines to your form.
_sirenOfShameDevice.Connected += SirenOfShameDeviceConnected;
_sirenOfShameDevice.Disconnected += SirenOfShameDeviceDisconnected;

void SirenOfShameDeviceConnected(object sender, EventArgs e) {
   // do something fun, enable some buttons.
}

void SirenOfShameDeviceDisconnected(object sender, EventArgs e) {
   // do something fun, disable some buttons.
}

This tutorial sucks, I'm running the program and I still don't hear the siren going off.

Hold on you're almost done I promise. You need to add a button to make it go. Add the following code to the button's click event.
_sirenOfShameDevice.PlayAudioPattern(
   _sirenOfShameDevice.AudioPatterns.First(),
   new TimeSpan(0, 0, 0, 10));
_sirenOfShameDevice.PlayLightPattern(
   _sirenOfShameDevice.LedPatterns.First(),
   new TimeSpan(0, 0, 0, 10));

This will play the first audio pattern and the first LED pattern on the device for 10 seconds.

Want to know what other audio patterns are on the device or what cool light patterns you can annoy your friends with, check out the SimpleSirenOfShameDeviceExample on how to do it. Or, wait for the next tutorial blog post which may come out never (I'm lazy what can I say).

Now go forth and create the next cool Siren of Shame application. If it's cool we'll put it on our site and promote it for you.

1 comment:

  1. This comment has been removed by a blog administrator.

    ReplyDelete