Snippet - LedFlasher class (in VB)

LedFlasher class (in VB)

I constructed a device using a Cerberus for the entry of iButtons and associated tag numbers. This required feedback to the user via leds turned on and off and flashed in countable patterns. Hence this class, which allows one or more leds to be controlled in various ways.

To use the class, each led port is added, and then calls can refer to leds by an index number. There is an arbitrary maximum of 20 leds. The methods are:

  • Turn on or off
  • Flash repeatedly on-off
  • Flash a number of times, such that the flashes can be counted
  • Flash briefly as an I’m alive indication
  • Flash in a user-specified pattern
  • Invert the sense, ie on becomes off, off becomes on
  • Toggle the current state
  • Chase, ie turn each led on and off in turn.
    The parameter to the methods can be the led index, or if omitted, all leds are used.

The code operates with a timer, by default of 100 milliseconds. At each event, the flashing pattern for each led is shifted right by one, and if bit zero is a one the led is turned on, otherwise it is turned off. When the flashing pattern reaches zero, it is reinitialised with the pattern that the user requested.

To get an idea of the overhead (on a Cerberus), I turned a port on at the start, then off at the end of the timer routine, and viewed the output on an oscilloscope. The results are:

  • Flash 1 led: 180 microseconds
  • Flash 8 leds: 1.2 milliseconds
  • Chase 8 leds: 250 microseconds

The code is written in VB, using Visual Studio 2012. I had a go at running it through a C# converter, but got stuck on what are probably simple fixes. In any case it is straightforward to compile the code as a VB class library, and then reference the dll in C#.

The code is documented by way of XML comments, so the description can be viewed in the Object Browser, and with Intellisense when writing code.