Snippet - LED Matrix Module - Simple Alphabet Library

LED Matrix Module - Simple Alphabet Library

The LED Matrix Gadgeteer Module provides an 8x8 matrix of red LEDs for display. At the time of this writing, the module driver is in beta, and only supports a single API, DrawBitmap. The DrawBitmap method takes as an argument a byte array representing the on/off pattern for the LEDs. The structure of this byte array is discussed and illustrated on the Wiki here:

Since this API is relatively low-level, I decided to make life a little easier on myself by creating a relatively simple higher-level API, which currently includes uppercase English letters A-Z, Space, and numbers 0-9. The class, named Alphabet, exposes a static property for each letter/number, making it more intuitive to quickly display letters and numbers on the LED Matrix module.

To generate the glyphs, I created a quick and dirty visual editor in HTML and JavaScript, which you can try out here:

With each letter, I used my editor to generate the glyph, then copied the generated string into the ā€œgetā€ accessor for the relevant property.

This has been tested on .NET MF / Gadgeteer 4.2 with the GHI 4.2 SDK beta, which is required for the LED Matrix module at the time of this writing.

2 Likes

This is sweet!

@ Gus - Thanks, Gus!

The alphanumeric glyphs could possibly be enhancedā€¦this was just my first pass, so I have no doubt they could probably be made more consistent and/or readable. Also might be fun to add some code to scroll the letters instead of flashing them one at a time, but my brain is a little fried from lack of sleep so thatā€™s probably beyond me today.

If you think itā€™s a useful enough addition, Iā€™d be happy to see the alphabet helper added to the driver for the module, or point me in the right direction, and Iā€™d be happy to add it myself.

Nice one! I havenā€™t seen jsfiddle. Very helpfull!

Yeah, itā€™s a very useful site for playing around with HTML/JS/jQuery/etc.

As I started writing the Alphabet class, I realized that I needed some way of generating the glyphs to make it faster, and when I thought about what the fastest route to a simple utility might be, HTML/JavaScript came out on top. Fast to develop, easy to share via jsfiddle, and pretty lightweight. Iā€™m sure I could have done something prettier with WPF or a richer client framework, but for something quick and dirty like this, itā€™s hard to beat HTML and JavaScript.

PS - Legend? Guess they had to invent a new title after you exceeded the range for King. :smiley:

Agree. I will definitely be using jsFiddle. Thanks for pointing it out.

That rank was there from the beginning. There was actually one more level - 100K+ (FEZ MVP).

http://web.archive.org/web/20100929175724/http://www.tinyclr.com/forum/users

Cool!! And with the perfect demo textā€¦ :slight_smile:

Iā€™m a GC snob wannabe and am curious: Why thisā€¦


public static byte[] Alpha { get { return new byte[] { 0x18, 0x24, 0x42, 0x42, 0x7e, 0x42, 0x42, 0x42 }; } }

ā€¦instead ofā€¦


public static readonly byte[] Alpha = new byte[] { 0x18, 0x24, 0x42, 0x42, 0x7e, 0x42, 0x42, 0x42 };

Interesting. Do any of those benefits still apply or was that program dropped and thatā€™s why the page no longer exists?

@ ddurant - I started off by using the propfull shortcut to generate most of the property code, and then realized that I didnā€™t need the setters, nor the private backing variables. Youā€™re probably correct that your version is more efficient. :slight_smile:

Bear in mind, Iā€™m still learning about embedded stuff, so my GC-avoidance fu is almost certainly inferiorā€¦

LOLā€¦ Not sure Iā€™d say thatā€¦ Iā€™ve just been thinking about it a lot lately due to a couple projects Iā€™ve got goingā€¦

public static byte[] Alpha { get { return new byte[] { 0x18, 0x24, 0x42, 0x42, 0x7e, 0x42, 0x42, 0x42 }; } }

This results in an allocation on every call, since it uses the ā€˜newā€™ keyword and is a propertyā€¦ Sending ā€˜HELLOā€™ to your display is 5 allocations.

public static readonly byte[] Alpha = new byte[] { 0x18, 0x24, 0x42, 0x42, 0x7e, 0x42, 0x42, 0x42 };

This results in an allocation when the containing class is first referenced. If you pulled the ā€˜staticā€™ out, itā€™d be allocated when a new instance is created. The ā€˜readonlyā€™ says that it can only be assigned a value in the constructor or (like the line above) in member initialization. The downside is that all members declared this way are always allocated (once the class is referenced) so if the user isnā€™t using them, they have to eat the RAM anyway.

A static constructor, BTW, if you havenā€™t seen it before, is just like a regular, non-static constructor but with the word static in there. This are run the first time (and only the first time) the class is referenced. Useful if youā€™ve got complicated things to do that donā€™t work well in member initialization. If youā€™ve got a class called Program, itā€™d be like this:


class Program
{
	static Program()
	{
		// do stuff here
	}

Another option is to use const, like this:

public const byte[] Alpha = new byte[] { 0x18, 0x24, 0x42, 0x42, 0x7e, 0x42, 0x42, 0x42 };

Since itā€™s const, itā€™s read-only and can only be set in member initializtion - not in a constructor like ā€˜readonlyā€™ allows. I think the deal with const is that it allocates the memory at compile time, not run time, so itā€™s stored in flash instead of RAM. If youā€™re reeeeally trying to save RAM, itā€™s worth looking at.

:smiley: