A low resolution font for LED matrix displays e.g. neopixels

Dependents:   WS2812Text

A basic low resolution font

This font is intend for very low resolution display systems e.g. LED matrix displays. All characters are 5 pixels high and 5 or less pixels wide.

Data for a given character is returned as an array of 5 chars each char represents one row of the image reading from the top down. The least significant 5 bits of each char represent the pixels on that line.

e.g. For the letter X the returned data is { 0x11, 0x0a, 0x04, 0x0a, 0x11 } As bits this translates to

  • 10001
  • 01010
  • 00100
  • 01010
  • 10001

The following code will output the bit pattern for the selected character.

#include "miniFont.h"
miniFont myFont;

int main()
{

    const char **charData = NULL;
    char letter = 'x';

    if (myFont.getChar(letter,charData)) {
        for (int row = 0; row < myFont.getPixHeight(letter); row++) {
            for (int col = myFont.getPixWidth(letter) - 1; col >= 0; col--) { // start at the left not the right
                if ( *(*charData + row) & (0x01<<col))
                    printf("1");
                else
                    printf("0");
            }
            printf("\n");
        }
    }
}

Note - In order to avoid pointless copying of data getChar modifies the value in the supplied char to point to the start of the character array. However in some situations this means that the data will only be valid until the next time getChar is called. If you need to keep the data longer than that make a copy.

Characters are right justified, e.g. if getWidth() returns a value of 3 then only the 3 least significant bits of each row contain data. There are options to force fixed width characters (narrow characters will be shifted to the middle of a 5 pixel width) and to rotate characters 90 degrees Supported characters are A-Z 0-9 . ! ? <space> Any lower case letters are automatically capitalized. Not tested excessively, if you find any problems please let me know.

Files at this revision

API Documentation at this revision

Comitter:
AndyA
Date:
Fri Nov 07 09:38:54 2014 +0000
Parent:
0:29155ff9313e
Commit message:
Documentation and example updated

Changed in this revision

miniFont.h Show annotated file Show diff for this revision Revisions of this file
diff -r 29155ff9313e -r 46e0549c2bfb miniFont.h
--- a/miniFont.h	Fri Nov 07 09:24:10 2014 +0000
+++ b/miniFont.h	Fri Nov 07 09:38:54 2014 +0000
@@ -2,28 +2,31 @@
 #define __miniFont_h__
 
 #include "mbed.h"
-
+ 
 /** A basic low resolution font
-*
-* This font is intened for very low resolution display systems e.g. LED matrix displays.
+** This font is intened for very low resolution display systems e.g. LED matrix displays.
 *
 * All characters are 5 pixels high and 5 or less pixels wide.
 *
 * Data for a given character is returned as an array of 5 chars
-* each char represents one row of the image. The least significant
-* 5 bits of each char represent the pixels on that line.
+* each char represents one row of the image, the first being the top row.
+* The least significant 5 bits of each char represent the pixels on that line.
 *
 * e.g. For the letter X the returnd data is
 * { 0x11, 0x0a, 0x04, 0x0a, 0x11 }
+* As bits this translates to
 *
-* As bits this translates to
-* > 10001
-* > 01010
-* > 00100
-* > 01010
-* > 10001
+* 10001
+*
+* 01010
+*
+* 00100
 *
-* example use
+* 01010
+*
+* 10001
+*
+* example: The following code will output the bit pattern for the selected character.
 *
 * @code
 *
@@ -33,12 +36,20 @@
 * int main() {
 *    
 *    char characterData[5];
-*    if (getChar('x',&characterData)) {
-*        // display letter
+     char letter = 'x'
+*    if (myFont.getChar(letter,&characterData)) {
+*        for (int row = 0; row < myFont.getPixHeight(letter); row++) {
+           for (int col = myFont.getPixWidth(letter) - 1; col >= 0; col--) { // start at the left no the right
+             if (characterData[row] & (0x01<<col)) 
+               printf("1");
+             else
+               printf("0");
+             }
+             printf("\n");
+           }
 *        }
-*    
 *    }
-* @code
+* @endcode
 *
 * Characters are right justified, e.g. if getWidth() returns a value of 3 then only the 3 least significant bits of each row contain data.
 *
@@ -50,8 +61,7 @@
 *
 * Not tested excessivly, if you find any problems please let me know.
 */
-class miniFont
-{
+class miniFont {
 public:
 
 /** Initalise the font
@@ -98,7 +108,6 @@
 
 private:
 
-
     uint8_t getMinPixWidth(char letter);
     bool getRawChar(char letter, const char **rowArray);
     bool getVerticalChar(char letter, const char **rowArray);