I was asked to show the source behind the example shown in the .NET Gadgeteer LED Matrix blog post.
(as is, no warranty, and I am sure there are bugs etc blah blah)
The code is supplied below, it has a display buffer that you fill with text and it will scroll based on the timer ( turn off if annoying) . This is a class that you can just add to your project and call the methods to display text.
1) Add code to project (new class)
2) Create instance, supplying an array of your hardware modules to the constructor
- LedDisplayArray ledArray;
- ledArray = new LedDisplayArray(new LEDMatrix[] { lEDMatrix, lEDMatrix1 ,lEDMatrix2,lEDMatrix3});
3) Call DrawString with the text you need to display.
- string displayText = " 1 2 3 4 5 6 7 8 9 0 : ! o ";//Example text
- ledArray.clear();//Reset/clear any previous text
- ledArray.DrawString(displayText);//Display the text
- ledArray.Scroll(); //Start scrolling
NOTE: You will have to create a lookup table for each character/digit you want to display. I have only added a few.
- using System;
- using System.Collections;
- using Gadgeteer;
- using Gadgeteer.Modules.GHIElectronics;
- using Microsoft.SPOT;
- using Microsoft.SPOT.Hardware;
- namespace LedArray
- {
- class LedDisplayArray
- {
- //The array with the chars that we will display
- private byte[] displayArray = new byte[] { };
- //An array of the physical matrix modules, so we can add more
- private LEDMatrix[] displays;
- int destinationIndex = 0;
- //A list of all possible characters (you have to make your own, examples below)
- Hashtable AsciiLookup = new Hashtable();
- Timer timer = new Timer(400);
- private int arrayOffset = 1;
- //Constructor, give it all your physical displays
- public LedDisplayArray(LEDMatrix[] displays)
- {
- //Setup characters/digits – add more
- AsciiLookup.Add((int)'1', new byte[] { 0×02, 0xFE, 0×42 });
- AsciiLookup.Add((int)'2', new byte[] { 0xF2, 0×92, 0x9E });
- AsciiLookup.Add((int)'3', new byte[] { 0xFE, 0×92, 0×92 });
- AsciiLookup.Add((int)'4', new byte[] { 0xFE, 0×10, 0xF0 });
- AsciiLookup.Add((int)'5', new byte[] { 0x9E, 0×92, 0xF2 });
- AsciiLookup.Add((int)'6', new byte[] { 0x9E, 0×92, 0xFE });
- AsciiLookup.Add((int)'7', new byte[] { 0xFE, 0×80, 0×80 });
- AsciiLookup.Add((int)'8', new byte[] { 0xFE, 0×92, 0xFE });
- AsciiLookup.Add((int)'9', new byte[] { 0xFE, 0×90, 0xF0 });
- AsciiLookup.Add((int)'0', new byte[] { 0xFE, 0×82, 0xFE });
- AsciiLookup.Add((int)' ', new byte[] { 0×00 });
- AsciiLookup.Add((int)':', new byte[] { 0×28 });
- AsciiLookup.Add((int)'!', new byte[] { 0×20, 0×10, 0xD7, 0xF8, 0xD7, 0×10, 0×20 });
- AsciiLookup.Add((int)'o', new byte[] { 0×06, 0×63, 0×63, 0x0B, 0×63, 0×63, 0×06 });
- //All hardware
- this.displays = displays;
- displayArray = new byte[displays.Length * 8];
- //scroller
- timer.Tick += new Timer.TickEventHandler(timer_Tick);
- }
- void timer_Tick(Timer timer)
- {
- //move array
- displayArray = Utility.CombineArrays(displayArray, displayArray.Length – arrayOffset, arrayOffset, displayArray, 0, displayArray.Length – arrayOffset);
- Draw();//redraw
- }
- public void Scroll()
- {
- //start timer
- timer.Start();
- }
- private void Draw()
- {
- //pad array to display size – minimum
- if (displayArray.Length < displays.Length * 8)
- {
- displayArray = Utility.CombineArrays(displayArray, new byte[(displays.Length * 8) - displayArray.Length]);
- }
- //For each matrix.
- for (int ctr = 0; ctr < displays.Length; ctr++)
- {
- //The text can be bigger than we can display, get the bit we want to display
- byte[] currentDisplayData = new byte[] { };
- //if the text is small, just display it.
- if ((displayArray.Length >= (ctr + 1) * 8))
- {
- //display all text
- currentDisplayData = Utility.ExtractRangeFromArray(displayArray, ctr * 8, 8);
- }
- else
- {
- //Start of text
- currentDisplayData = Utility.ExtractRangeFromArray(displayArray, ctr * 8,
- displayArray.Length – (ctr * 8));
- //wrap round and get beginning text if at end.
- currentDisplayData = Utility.CombineArrays(currentDisplayData,
- new byte[8 - currentDisplayData.Length]);
- }
- //Display the text that we have decided should be shown
- displays[ctr].DrawBitmap(currentDisplayData);
- }
- }
- public void DrawString(string str)
- {
- //Given a string , put it in the draw buffer
- char[] characterArray = str.ToCharArray();
- //get each char and look it up – be sure to add all the chars you will need
- foreach (char c in characterArray)
- {
- this.AddCharacter((byte[])this.AsciiLookup[(int)c]);
- }
- //Draw it
- this.Draw();
- }
- private void AddCharacter(byte[] characterBytes)
- {
- //add characters to the diaplay array
- displayArray = Utility.CombineArrays(characterBytes, displayArray);
- }
- internal void clear()
- {
- //remove all text
- displayArray = new byte[] { };
- }
- }
- }