Draw the character of the ASCII code.

Dependents:   mbed-os_Watson-IoT_ZXing_sample mbed-os_Watson-IoT_ZXing_sample MovPlayer GR-PEACH_HVC-P2_sample_client ... more

Fork of AsciiFont by Daiki Kato

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers AsciiFont.h Source File

AsciiFont.h

Go to the documentation of this file.
00001 /**************************************************************************//**
00002 * @file          AsciiFont.h
00003 * @brief         AsciiFont API
00004 ******************************************************************************/
00005 
00006 #ifndef ASCII_FONT_H
00007 #define ASCII_FONT_H
00008 
00009 #include "mbed.h"
00010 
00011 /** Draw the character of the ASCII code.
00012  *
00013  * Example
00014  * @code
00015  * #include "mbed.h"
00016  * #include "AsciiFont.h"
00017  * 
00018  * #define WIDTH           (12)
00019  * #define HEIGHT          (16)
00020  * #define BYTE_PER_PIXEL  (1u)
00021  * #define STRIDE          (((WIDTH * BYTE_PER_PIXEL) + 7u) & ~7u) //multiple of 8
00022  * 
00023  * uint8_t text_field[STRIDE * HEIGHT];
00024  * 
00025  * //for debug
00026  * void print_text_field() {
00027  *     int idx = 0;
00028  * 
00029  *     for (int i = 0; i < HEIGHT; i++) {
00030  *         for (int j = 0; j < STRIDE; j++) {
00031  *             printf("%02x", text_field[idx++]);
00032  *         }
00033  *         printf("\r\n");
00034  *     }
00035  *     printf("\r\n");
00036  * }
00037  * 
00038  * int main() {
00039  *     AsciiFont ascii_font(text_field, WIDTH, HEIGHT, STRIDE, BYTE_PER_PIXEL);
00040  * 
00041  *     ascii_font.Erase(0xcc);
00042  *     ascii_font.DrawStr("AB", 0, 0, 0x11, 1);
00043  *     ascii_font.DrawChar('C', AsciiFont::CHAR_PIX_WIDTH,
00044  *                         AsciiFont::CHAR_PIX_HEIGHT, 0x22, 1);
00045  *     print_text_field(); //debug print
00046  * 
00047  *     ascii_font.Erase();
00048  *     ascii_font.DrawStr("D", 0, 0, 0xef, 2);
00049  *     print_text_field(); //debug print
00050  * 
00051  *     ascii_font.Erase(0x11, 6, 0, 6, 8);
00052  *     print_text_field(); //debug print
00053  * }
00054  * @endcode
00055  */
00056 
00057 class AsciiFont {
00058 public:
00059 
00060     /** Constructor: Initializes AsciiFont.
00061      *
00062      * @param p_buf Text field address
00063      * @param width Text field width
00064      * @param height Text field height
00065      * @param stride Buffer stride
00066      * @param colour Background color
00067      * @param byte_per_pixel Byte per pixel
00068      */
00069     AsciiFont(uint8_t * p_buf, int width, int height, int stride, int byte_per_pixel, uint32_t const colour = 0);
00070 
00071     /** Erase text field
00072      *
00073      */
00074     void Erase();
00075 
00076     /** Erase text field
00077      *
00078      * @param colour Background color
00079      */
00080     void Erase(uint32_t const colour);
00081 
00082     /** Erase text field
00083      *
00084      * @param colour Background color
00085      * @param x Erase start position of x coordinate
00086      * @param y Erase start position of y coordinate
00087      * @param width Erase field width
00088      * @param height Erase field height
00089      */
00090     void Erase(uint32_t const colour, int x, int y, int width, int height);
00091 
00092     /** Draw a string
00093      *
00094      * @param str String
00095      * @param x Drawing start position of x coordinate
00096      * @param y Drawing start position of y coordinate
00097      * @param color Font color
00098      * @param font_size Font size (>=1)
00099      * @param max_char_num The maximum number of characters
00100      * @return The drawn number of characters
00101      */
00102     int DrawStr(const char * str, int x, int y, uint32_t const colour, int font_size = 1, uint16_t const max_char_num = 0xffff);
00103 
00104     /** Draw a character
00105      *
00106      * @param x Drawing start position of x coordinate
00107      * @param y Drawing start position of y coordinate
00108      * @param color Font color
00109      * @param font_size Font size (>=1)
00110      * @return true if successfull
00111      */
00112     bool DrawChar(char c, int x, int y, uint32_t const colour, int font_size = 1);
00113 
00114     /** The pixel width of a character. (font_size=1)
00115      *
00116      */
00117     static const int CHAR_PIX_WIDTH  = 6;
00118 
00119     /** The pixel height of a character. (font_size=1)
00120      *
00121      */
00122     static const int CHAR_PIX_HEIGHT = 8;
00123 
00124 private:
00125     uint8_t * p_text_field;
00126     int max_width;
00127     int max_height;
00128     int buf_stride;
00129     int pixel_num;
00130     uint32_t background_colour;
00131 };
00132 #endif