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

Dependents:   WS2812Text

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers miniFont.h Source File

miniFont.h

00001 #ifndef __miniFont_h__
00002 #define __miniFont_h__
00003 
00004 #include "mbed.h"
00005  
00006 /** A basic low resolution font
00007 ** This font is intened for very low resolution display systems e.g. LED matrix displays.
00008 *
00009 * All characters are 5 pixels high and 5 or less pixels wide.
00010 *
00011 * Data for a given character is returned as an array of 5 chars
00012 * each char represents one row of the image, the first being the top row.
00013 * The least significant 5 bits of each char represent the pixels on that line.
00014 *
00015 * e.g. For the letter X the returnd data is
00016 * { 0x11, 0x0a, 0x04, 0x0a, 0x11 }
00017 * As bits this translates to
00018 *
00019 * 10001
00020 *
00021 * 01010
00022 *
00023 * 00100
00024 *
00025 * 01010
00026 *
00027 * 10001
00028 *
00029 * example: The following code will output the bit pattern for the selected character.
00030 *
00031 * @code
00032 *
00033 * #include "miniFont.h"
00034 * miniFont myFont;
00035 *
00036 * int main() {
00037 *    
00038 *    char characterData[5];
00039      char letter = 'x'
00040 *    if (myFont.getChar(letter,&characterData)) {
00041 *        for (int row = 0; row < myFont.getPixHeight(letter); row++) {
00042            for (int col = myFont.getPixWidth(letter) - 1; col >= 0; col--) { // start at the left no the right
00043              if (characterData[row] & (0x01<<col)) 
00044                printf("1");
00045              else
00046                printf("0");
00047              }
00048              printf("\n");
00049            }
00050 *        }
00051 *    }
00052 * @endcode
00053 *
00054 * 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.
00055 *
00056 * 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
00057 *
00058 * Supported characters are A-Z 0-9 . ! ? <space>
00059 *
00060 * Any lower case letters are automatically capitalised.
00061 *
00062 * Not tested excessivly, if you find any problems please let me know.
00063 */
00064 class miniFont {
00065 public:
00066 
00067 /** Initalise the font
00068 */
00069     miniFont();
00070 
00071 /** Returns the width of the requested character
00072 @param letter The character to measure
00073 @return The width in pixels
00074 */    
00075     uint8_t getPixWidth(char letter);
00076 
00077 /** Returns the height of the requested character
00078 @param letter The character to measure
00079 @return The height in pixels (currently always 5)
00080 */    
00081     uint8_t getPixHeight(char letter);
00082 
00083 /** get the data for a given character
00084 @param letter The chatacter to retrieve the data for
00085 @param rowArray An array containing the pixel data
00086 @return Flag indicating that rowArray is valid.
00087 */
00088     bool getChar(char letter, const char **rowArray);
00089     
00090 /** get current fixed width mode
00091 @return true if fixed width is enabled
00092 */
00093     bool isFixedWidth() {return fixedWidth;};
00094 /** set fixed width mode
00095 @param newValue True to enable fixed width, false to disable
00096 */
00097     void setFixedWidth(bool newValue) {fixedWidth = newValue;};
00098     
00099 /** get current rotation mode
00100 @return true if font is rotated
00101 */
00102     bool isRotated() {return rotate90;};
00103 
00104 /** set rotation mode
00105 @param newValue True to rotate text 90 degrees anti-clockwise (except in the USA where it rotates counterclockwise instead)
00106 */
00107     void setRotated(bool newValue) {rotate90 = newValue;};
00108 
00109 private:
00110 
00111     uint8_t getMinPixWidth(char letter);
00112     bool getRawChar(char letter, const char **rowArray);
00113     bool getVerticalChar(char letter, const char **rowArray);
00114 
00115     static const uint8_t maxWidth = 5;
00116     static const uint8_t maxHeight = 5;
00117 
00118     bool rotate90;
00119     bool fixedWidth;
00120     char letterBuffer[5];
00121     char rotateBuffer[5];
00122 
00123 
00124     static const char letters[26][5];
00125 
00126     static const char numbers[10][5];
00127 
00128     static const char dot[5];
00129     static const char space[5];
00130     static const char exclam[5];
00131     static const char quest[5];
00132 
00133 };
00134 
00135 
00136 #endif