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: