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:24:10 2014 +0000
Revision:
0:29155ff9313e
Child:
1:46e0549c2bfb
Added comments/doucmentation

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