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.
Assembly is easy as usual.
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();
multiLedColors.Enqueue(GT.Color.White);
multiLedColors.Enqueue(GT.Color.Blue);
multiLedColors.Enqueue(GT.Color.Red);
multiLedColors.Enqueue(GT.Color.Green);
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:
- Dequeue (or “pop”) the first color out of the queue.
- Set the LED color to the popped color
- Enqueue (or “push”) the color to the back of the queue.
multicolorLed.TurnColor((GT.Color)multiLedColors.Dequeue());
multiLedColors.Enqueue(multicolorLed.GetCurrentColor());
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)
{
display_T35.SimpleGraphics.Clear();
currentHeight = 0;
}
display_T35.SimpleGraphics.DisplayText(text,f,Color.White,0,currentHeight);
currentHeight += fontHeight;
}