Close

The Fount, or Font, of All Good Things

A project log for NTP Clock Based on STM32H735 Discovery Kit

This is an SNTP clock based on the STM32H735 Discovery Kit.

dmoisandmoisan 04/07/2021 at 03:380 Comments

When your project involves a bitmapped display, you will need fonts.  When you've gone beyond the old, reliable 2-line LCD (courtesy of the Hitachi HD44780 that powered my Twatch), you need fonts.  When you want something that looks good, you need fonts!

The ST Micro firmware library has a graphics library with a font importer.  However, the HTTP app I adapted for my clock doesn't use the library.  I'm not sure what ST used to create the fonts in the original app, but I found a good app to create fonts.  The Dot Factory  is a small utility that is used to generate C source from TrueType and bitmapped fonts, and it can also be used to convert bitmaps to C source.

I mentioned previously that I decided to use Droid Sans Mono as a display font, due to its aesthetics, and also due to its permissive open-source licensing.  To use the app, you specify the font, the size and the style you want, and then tweak the code generation settings to match how your library expects to use your font data.

You can accept most of the defaults.  You only need to pay attention to the byte ordering--it is Row Major, MSB First--and the space character, which is always done. Droid Sans Mono is a monospaced font;  this is the best choice for a clock and a good choice for nearly all applications.  The app can also create a separate descriptor array, which provides a fast index into font arrays that might have a few hundred glyphs in them.  Descriptors aren't used in my program.  After configuring the options, you're ready to generate the code.

And there is our font code!  I chose to include all the glyphs of Droid Sans Mono--there's plenty of ROM in the Discovery Kit--a megabyte's worth!  My clock has two different font sizes.  Just copy and paste the code into your text editor.

There are just a few more things to do.  We need to copy the font code file into the project directory, but first we need to put in an include to "fonts.h" and create a struct object that describes the font to the graphics API.

Excerpt of fonts.h:

...
typedef struct _tFont
{
  const uint8_t *table;
  uint16_t Width;
  uint16_t Height;
} sFONT;
...

extern sFONT DroidSansMono28;
extern sFONT DroidSansMono20;
...

And DroidSansMono28:

/* Character bitmaps for Droid Sans Mono 28pt */
const uint8_t droidSansMono_28ptBitmaps[] = 
{
...
	/* @111 '!' (22 pixels wide) */
	0x00, 0x00, 0x00, //                       
	0x00, 0x00, 0x00, //                       
	0x00, 0x78, 0x00, //          ####         
	0x00, 0x78, 0x00, //          ####         
	0x00, 0x78, 0x00, //          ####         
	0x00, 0x78, 0x00, //          ####         
	0x00, 0x38, 0x00, //           ###         
	0x00, 0x38, 0x00, //           ###         
	0x00, 0x38, 0x00, //           ###         
	0x00, 0x38, 0x00, //           ###         
	0x00, 0x38, 0x00, //           ###         
	0x00, 0x38, 0x00, //           ###         
	0x00, 0x38, 0x00, //           ###         
	0x00, 0x38, 0x00, //           ###         
	0x00, 0x38, 0x00, //           ###         
	0x00, 0x38, 0x00, //           ###         
	0x00, 0x38, 0x00, //           ###         
	0x00, 0x38, 0x00, //           ###         
	0x00, 0x38, 0x00, //           ###         
	0x00, 0x30, 0x00, //           ##          
	0x00, 0x30, 0x00, //           ##          
	0x00, 0x00, 0x00, //                       
	0x00, 0x00, 0x00, //                       
	0x00, 0x00, 0x00, //                       
	0x00, 0x38, 0x00, //           ###         
	0x00, 0x78, 0x00, //          ####         
	0x00, 0x7C, 0x00, //          #####        
	0x00, 0x78, 0x00, //          ####         
	0x00, 0x38, 0x00, //           ###         
	0x00, 0x00, 0x00, //                       
	0x00, 0x00, 0x00, //                       
	0x00, 0x00, 0x00, //                       
	0x00, 0x00, 0x00, //                       
	0x00, 0x00, 0x00, //                       
	0x00, 0x00, 0x00, //                       
	0x00, 0x00, 0x00, //                       
	0x00, 0x00, 0x00, //                       
...
};

/* Font information for Droid Sans Mono 28pt */

sFONT DroidSansMono28 = {
		droidSansMono_28ptBitmaps,
  22, /* Width */
  37, /* Height */
};

The only annoying part was counting the width and height in pixels for the sFONT struct.  That's not done automatically.  If you get the height wrong (easy to do), the display will resemble an odometer, or a slot machine as the digits go out of alignment every time they're drawn!

I'm about caught up.  I've done everything I needed to do, and I now have a usable clock, even if it isn't perfectly rendered.  I've tested my code in two different clocks over 11 years, and I can design my next clock half-asleep!

My next, and final entry:  What have I learned from this project?  What do I think of the hardware, the STM32H735 Discovery Kit?  This was my first time developing an ST Micro board--what did I think of it?

All of that, next. 

Discussions