Working with LCD glyphs

Reading a diff today I found this piece of code for defining a font for a matrix LCD display. The code is interesting, it lets the developer see what the font looks like, so fixing your alphabet is really easy.

unsigned char font5x7[][8] = 
{
/* z */
 ,{
   ________,
   ________,
   XXXXX___,
   ___X____,
   __X_____,
   _X______,
   XXXXX___,
   ________}

/* s */
 ,{
   ________,
   ________,
   _XXX____,
   X_______,
   _XX_____,
   ___X____,
   XXX_____,
   ________}
}

But something is fishy here, how do the compiler understand ________ as being 0x00, or 0xFF? So I went on to see the included header… and ouch, this is what I found.

#define	_XX_____	0x60
#define	_XX____X	0x61
#define	_XX___X_	0x62
#define	_XX___XX	0x63
#define	_XX__X__	0x64
#define	_XX__X_X	0x65
#define	_XX__XX_	0x66
#define	_XX__XXX	0x67
#define	_XX_X___	0x68
#define	_XX_X__X	0x69
#define	_XX_X_X_	0x6a
#define	_XX_X_XX	0x6b
#define	_XX_XX__	0x6c
#define	_XX_XX_X	0x6d
#define	_XX_XXX_	0x6e
#define	_XX_XXXX	0x6f

This is ugly as code and pretty as ASCII art. When we are coding we want beautiful code, but not pretty ASCII art. Let that to all the artists, they do better art than we do. So, how do we fix the code? Simple, macros to the rescue!

#define _	0
#define X	1
#define b(a,b,c,d,e,f,g,h)	(a << 7| b << 6 | c << 5 | d << 4 | e << 3 | f << 2 | g << 1 | h)
&#91;/sourcecode&#93;

With this we let the compiler do the dirty job of creating all those values. Using the macros above, the code becomes easier to maintain and read. Just remember to undef the macros after using it, as you don't want all your X's, _'s and b's being changed!

&#91;sourcecode lang=c&#93;
unsigned char font5x7&#91;&#93;&#91;8&#93; = {
	/* z */ {
	b(_,_,_,_,_,_,_,_),
	b(_,_,_,_,_,_,_,_),
	b(X,X,X,X,X,_,_,_),
	b(_,_,_,X,_,_,_,_),
	b(_,_,X,_,_,_,_,_),
	b(_,X,_,_,_,_,_,_),
	b(X,X,X,X,X,_,_,_),
	b(_,_,_,_,_,_,_,_)
	},

	/* s */ {
	b(_,_,_,_,_,_,_,_),
	b(_,_,_,_,_,_,_,_),
	b(_,X,X,X,_,_,_,_),
	b(X,_,_,_,_,_,_,_),
	b(_,X,X,_,_,_,_,_),
	b(_,_,_,X,_,_,_,_),
	b(X,X,X,_,_,_,_,_),
	b(_,_,_,_,_,_,_,_)
	},
};
&#91;/sourcecode&#93;

By the way, you can apply this idea for creating small graphics on code. It's easy and self-documenting. Happy hacking!

<strong>Update:</strong>
I just remembered the section <em>Making a Glyph from Bit Patterns</em> from <strong>Expert C Programming</strong> (<a href="http://www.amazon.com/Expert-Programming-Peter-van-Linden/dp/0131774298">buy</a> this book if you don't have it yet!), it gives a solution similar to mine. The macros defined there are:


#define _ )*2
#define X )*2 + 1
#define s ((((((((0

So the code looks like this:

unsigned char font5x7[][8] = {
	/* z */ {
	s _ _ _ _ _ _ _ _,
	s _ _ _ _ _ _ _ _,
	s X X X X X _ _ _,
	s _ _ _ X _ _ _ _,
	s _ _ X _ _ _ _ _,
	s _ X _ _ _ _ _ _,
	s X X X X X _ _ _,
	s _ _ _ _ _ _ _ _,
	},
	/* s */ {
	s _ _ _ _ _ _ _ _,
	s _ _ _ _ _ _ _ _,
	s _ X X X _ _ _ _,
	s X _ _ _ _ _ _ _,
	s _ X X _ _ _ _ _,
	s _ _ _ X _ _ _ _,
	s X X X _ _ _ _ _,
	s _ _ _ _ _ _ _ _,
	}
};

Leave a Reply

Your email address will not be published. Required fields are marked *