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.

Committer:
AndyA
Date:
Fri Nov 07 09:38:54 2014 +0000
Revision:
1:46e0549c2bfb
Parent:
0:29155ff9313e
Documentation and example updated

Who changed what in which revision?

UserRevisionLine numberNew contents of line
AndyA 0:29155ff9313e 1 #ifndef __miniFont_h__
AndyA 0:29155ff9313e 2 #define __miniFont_h__
AndyA 0:29155ff9313e 3
AndyA 0:29155ff9313e 4 #include "mbed.h"
AndyA 1:46e0549c2bfb 5
AndyA 0:29155ff9313e 6 /** A basic low resolution font
AndyA 1:46e0549c2bfb 7 ** This font is intened for very low resolution display systems e.g. LED matrix displays.
AndyA 0:29155ff9313e 8 *
AndyA 0:29155ff9313e 9 * All characters are 5 pixels high and 5 or less pixels wide.
AndyA 0:29155ff9313e 10 *
AndyA 0:29155ff9313e 11 * Data for a given character is returned as an array of 5 chars
AndyA 1:46e0549c2bfb 12 * each char represents one row of the image, the first being the top row.
AndyA 1:46e0549c2bfb 13 * The least significant 5 bits of each char represent the pixels on that line.
AndyA 0:29155ff9313e 14 *
AndyA 0:29155ff9313e 15 * e.g. For the letter X the returnd data is
AndyA 0:29155ff9313e 16 * { 0x11, 0x0a, 0x04, 0x0a, 0x11 }
AndyA 1:46e0549c2bfb 17 * As bits this translates to
AndyA 0:29155ff9313e 18 *
AndyA 1:46e0549c2bfb 19 * 10001
AndyA 1:46e0549c2bfb 20 *
AndyA 1:46e0549c2bfb 21 * 01010
AndyA 1:46e0549c2bfb 22 *
AndyA 1:46e0549c2bfb 23 * 00100
AndyA 0:29155ff9313e 24 *
AndyA 1:46e0549c2bfb 25 * 01010
AndyA 1:46e0549c2bfb 26 *
AndyA 1:46e0549c2bfb 27 * 10001
AndyA 1:46e0549c2bfb 28 *
AndyA 1:46e0549c2bfb 29 * example: The following code will output the bit pattern for the selected character.
AndyA 0:29155ff9313e 30 *
AndyA 0:29155ff9313e 31 * @code
AndyA 0:29155ff9313e 32 *
AndyA 0:29155ff9313e 33 * #include "miniFont.h"
AndyA 0:29155ff9313e 34 * miniFont myFont;
AndyA 0:29155ff9313e 35 *
AndyA 0:29155ff9313e 36 * int main() {
AndyA 0:29155ff9313e 37 *
AndyA 0:29155ff9313e 38 * char characterData[5];
AndyA 1:46e0549c2bfb 39 char letter = 'x'
AndyA 1:46e0549c2bfb 40 * if (myFont.getChar(letter,&characterData)) {
AndyA 1:46e0549c2bfb 41 * for (int row = 0; row < myFont.getPixHeight(letter); row++) {
AndyA 1:46e0549c2bfb 42 for (int col = myFont.getPixWidth(letter) - 1; col >= 0; col--) { // start at the left no the right
AndyA 1:46e0549c2bfb 43 if (characterData[row] & (0x01<<col))
AndyA 1:46e0549c2bfb 44 printf("1");
AndyA 1:46e0549c2bfb 45 else
AndyA 1:46e0549c2bfb 46 printf("0");
AndyA 1:46e0549c2bfb 47 }
AndyA 1:46e0549c2bfb 48 printf("\n");
AndyA 1:46e0549c2bfb 49 }
AndyA 0:29155ff9313e 50 * }
AndyA 0:29155ff9313e 51 * }
AndyA 1:46e0549c2bfb 52 * @endcode
AndyA 0:29155ff9313e 53 *
AndyA 0:29155ff9313e 54 * 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.
AndyA 0:29155ff9313e 55 *
AndyA 0:29155ff9313e 56 * There are options to force fixed width characters (narrow characers will be shifted to the middle of a 5 pixel width) and to rotate characters 90 degrees
AndyA 0:29155ff9313e 57 *
AndyA 0:29155ff9313e 58 * Supported characters are A-Z 0-9 . ! ? <space>
AndyA 0:29155ff9313e 59 *
AndyA 0:29155ff9313e 60 * Any lower case letters are automatically capitalised.
AndyA 0:29155ff9313e 61 *
AndyA 0:29155ff9313e 62 * Not tested excessivly, if you find any problems please let me know.
AndyA 0:29155ff9313e 63 */
AndyA 1:46e0549c2bfb 64 class miniFont {
AndyA 0:29155ff9313e 65 public:
AndyA 0:29155ff9313e 66
AndyA 0:29155ff9313e 67 /** Initalise the font
AndyA 0:29155ff9313e 68 */
AndyA 0:29155ff9313e 69 miniFont();
AndyA 0:29155ff9313e 70
AndyA 0:29155ff9313e 71 /** Returns the width of the requested character
AndyA 0:29155ff9313e 72 @param letter The character to measure
AndyA 0:29155ff9313e 73 @return The width in pixels
AndyA 0:29155ff9313e 74 */
AndyA 0:29155ff9313e 75 uint8_t getPixWidth(char letter);
AndyA 0:29155ff9313e 76
AndyA 0:29155ff9313e 77 /** Returns the height of the requested character
AndyA 0:29155ff9313e 78 @param letter The character to measure
AndyA 0:29155ff9313e 79 @return The height in pixels (currently always 5)
AndyA 0:29155ff9313e 80 */
AndyA 0:29155ff9313e 81 uint8_t getPixHeight(char letter);
AndyA 0:29155ff9313e 82
AndyA 0:29155ff9313e 83 /** get the data for a given character
AndyA 0:29155ff9313e 84 @param letter The chatacter to retrieve the data for
AndyA 0:29155ff9313e 85 @param rowArray An array containing the pixel data
AndyA 0:29155ff9313e 86 @return Flag indicating that rowArray is valid.
AndyA 0:29155ff9313e 87 */
AndyA 0:29155ff9313e 88 bool getChar(char letter, const char **rowArray);
AndyA 0:29155ff9313e 89
AndyA 0:29155ff9313e 90 /** get current fixed width mode
AndyA 0:29155ff9313e 91 @return true if fixed width is enabled
AndyA 0:29155ff9313e 92 */
AndyA 0:29155ff9313e 93 bool isFixedWidth() {return fixedWidth;};
AndyA 0:29155ff9313e 94 /** set fixed width mode
AndyA 0:29155ff9313e 95 @param newValue True to enable fixed width, false to disable
AndyA 0:29155ff9313e 96 */
AndyA 0:29155ff9313e 97 void setFixedWidth(bool newValue) {fixedWidth = newValue;};
AndyA 0:29155ff9313e 98
AndyA 0:29155ff9313e 99 /** get current rotation mode
AndyA 0:29155ff9313e 100 @return true if font is rotated
AndyA 0:29155ff9313e 101 */
AndyA 0:29155ff9313e 102 bool isRotated() {return rotate90;};
AndyA 0:29155ff9313e 103
AndyA 0:29155ff9313e 104 /** set rotation mode
AndyA 0:29155ff9313e 105 @param newValue True to rotate text 90 degrees anti-clockwise (except in the USA where it rotates counterclockwise instead)
AndyA 0:29155ff9313e 106 */
AndyA 0:29155ff9313e 107 void setRotated(bool newValue) {rotate90 = newValue;};
AndyA 0:29155ff9313e 108
AndyA 0:29155ff9313e 109 private:
AndyA 0:29155ff9313e 110
AndyA 0:29155ff9313e 111 uint8_t getMinPixWidth(char letter);
AndyA 0:29155ff9313e 112 bool getRawChar(char letter, const char **rowArray);
AndyA 0:29155ff9313e 113 bool getVerticalChar(char letter, const char **rowArray);
AndyA 0:29155ff9313e 114
AndyA 0:29155ff9313e 115 static const uint8_t maxWidth = 5;
AndyA 0:29155ff9313e 116 static const uint8_t maxHeight = 5;
AndyA 0:29155ff9313e 117
AndyA 0:29155ff9313e 118 bool rotate90;
AndyA 0:29155ff9313e 119 bool fixedWidth;
AndyA 0:29155ff9313e 120 char letterBuffer[5];
AndyA 0:29155ff9313e 121 char rotateBuffer[5];
AndyA 0:29155ff9313e 122
AndyA 0:29155ff9313e 123
AndyA 0:29155ff9313e 124 static const char letters[26][5];
AndyA 0:29155ff9313e 125
AndyA 0:29155ff9313e 126 static const char numbers[10][5];
AndyA 0:29155ff9313e 127
AndyA 0:29155ff9313e 128 static const char dot[5];
AndyA 0:29155ff9313e 129 static const char space[5];
AndyA 0:29155ff9313e 130 static const char exclam[5];
AndyA 0:29155ff9313e 131 static const char quest[5];
AndyA 0:29155ff9313e 132
AndyA 0:29155ff9313e 133 };
AndyA 0:29155ff9313e 134
AndyA 0:29155ff9313e 135
AndyA 0:29155ff9313e 136 #endif