MultiLED control using Queue data scructure

Here is an example of using the queue collection to control the GHI multi-color LED module.  I’ve seen several examples of the same code but using some variant of an arraylist with a tracking variable.  Using or queue accomplishes the same thing but in a more obvious way.  I’m also showing a method I use for showing simple text on T35 display module.

This solution can be found on my Gadgeteer GitHub repository.

This is a very basic hardware setup.

LED Queue Rotation Setup

Assembly is easy as usual.

LED Queue Rotation Assembly

First, let’s cover the queueing code.  .NET has several basic data structures (Stack, Dictionary, List, etc… ). A Queue is a first-in-first-out data structure. The first element you add will be the first one removed.

We first need to load the queue with the LED colors in the Program method.

            multiLedColors = new Queue();

I setup a simple 1 second timer which is started/stopped when the button is pressed.  The timer is responsible for rotating the LED color using the queue. The timer performs the following:

  1. Dequeue (or “pop”) the first color out of the queue.
  2. Set the LED color to the popped color
  3. Enqueue (or “push”) the color to the back of the queue.

This is all done in two lines of code without the need for using or needing to reset any tracking variables.

Lastly, I have a simple utility method I use for simple text display for the T35.  I want a way to display simple text in rows, then clear the screen when I reach the bottom.  To do this, I need the height of the font used, and the height of the screen.  

            f = Resources.GetFont(Resources.FontResources.NinaB);
            fontHeight = (uint)f.Height;
            displayHeight = display_T35.Height;
            currentHeight = 0;

When I display text, current height position is updated for the line of text added. The screen and current height are reset if the height of all text is greater than the display height.

        private void DisplaySimpleText(String text)
            if (currentHeight >= displayHeight)
                currentHeight = 0;

            currentHeight += fontHeight;