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

AsciiFont

This is a library to draw the ASCII font characters.

Example

#include "mbed.h"
#include "AsciiFont.h"

#define WIDTH           (12)
#define HEIGHT          (16)
#define BYTE_PER_PIXEL  (1u)
#define STRIDE          (((WIDTH * BYTE_PER_PIXEL) + 7u) & ~7u) //multiple of 8

uint8_t text_field[STRIDE * HEIGHT];

//for debug
void print_text_field() {
    int idx = 0;

    for (int i = 0; i < HEIGHT; i++) {
        for (int j = 0; j < STRIDE; j++) {
            printf("%02x", text_field[idx++]);
        }
        printf("\r\n");
    }
    printf("\r\n");
}

int main() {
    AsciiFont ascii_font(text_field, WIDTH, HEIGHT, STRIDE, BYTE_PER_PIXEL);

    ascii_font.Erase(0xcc);
    ascii_font.DrawStr("AB", 0, 0, 0x11, 1);
    ascii_font.DrawChar('C', AsciiFont::CHAR_PIX_WIDTH,
                        AsciiFont::CHAR_PIX_HEIGHT, 0x22, 1);
    print_text_field(); //debug print

    ascii_font.Erase();
    ascii_font.DrawStr("D", 0, 0, 0xef, 2);
    print_text_field(); //debug print

    ascii_font.Erase(0x11, 6, 0, 6, 8);
    print_text_field(); //debug print
}


API

Import library

Public Member Functions

  AsciiFont (uint8_t *p_buf, int width, int height, int stride, int byte_per_pixel, uint32_t const colour=0)
  Constructor: Initializes AsciiFont .
void  Erase ()
  Erase text field.
void  Erase (uint32_t const colour)
  Erase text field.
void  Erase (uint32_t const colour, int x, int y, int width, int height)
  Erase text field.
int  DrawStr (char *str, int x, int y, uint32_t const colour, int font_size=1, uint16_t const max_char_num=0xffff)
  Draw a string.
bool  DrawChar (char c, int x, int y, uint32_t const colour, int font_size=1)
  Draw a character.

Static Public Attributes

static const int  CHAR_PIX_WIDTH = 6
  The pixel width of a character.
static const int  CHAR_PIX_HEIGHT = 8
  The pixel height of a character.
Committer:
dkato
Date:
Tue Mar 07 05:04:28 2017 +0000
Revision:
5:1eaa4942db53
Parent:
4:8fb5219bbc09
Added range check to Erase function.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
dkato 0:99222c303e8f 1 /**************************************************************************//**
dkato 0:99222c303e8f 2 * @file AsciiFont.h
dkato 0:99222c303e8f 3 * @brief AsciiFont API
dkato 0:99222c303e8f 4 ******************************************************************************/
dkato 0:99222c303e8f 5
dkato 0:99222c303e8f 6 #ifndef ASCII_FONT_H
dkato 0:99222c303e8f 7 #define ASCII_FONT_H
dkato 0:99222c303e8f 8
dkato 0:99222c303e8f 9 #include "mbed.h"
dkato 0:99222c303e8f 10
dkato 2:d0bc8c2974e0 11 /** Draw the character of the ASCII code.
dkato 0:99222c303e8f 12 *
dkato 2:d0bc8c2974e0 13 * Example
dkato 2:d0bc8c2974e0 14 * @code
dkato 2:d0bc8c2974e0 15 * #include "mbed.h"
dkato 2:d0bc8c2974e0 16 * #include "AsciiFont.h"
dkato 2:d0bc8c2974e0 17 *
dkato 2:d0bc8c2974e0 18 * #define WIDTH (12)
dkato 2:d0bc8c2974e0 19 * #define HEIGHT (16)
dkato 2:d0bc8c2974e0 20 * #define BYTE_PER_PIXEL (1u)
dkato 2:d0bc8c2974e0 21 * #define STRIDE (((WIDTH * BYTE_PER_PIXEL) + 7u) & ~7u) //multiple of 8
dkato 2:d0bc8c2974e0 22 *
dkato 2:d0bc8c2974e0 23 * uint8_t text_field[STRIDE * HEIGHT];
dkato 2:d0bc8c2974e0 24 *
dkato 2:d0bc8c2974e0 25 * //for debug
dkato 2:d0bc8c2974e0 26 * void print_text_field() {
dkato 2:d0bc8c2974e0 27 * int idx = 0;
dkato 2:d0bc8c2974e0 28 *
dkato 2:d0bc8c2974e0 29 * for (int i = 0; i < HEIGHT; i++) {
dkato 2:d0bc8c2974e0 30 * for (int j = 0; j < STRIDE; j++) {
dkato 2:d0bc8c2974e0 31 * printf("%02x", text_field[idx++]);
dkato 2:d0bc8c2974e0 32 * }
dkato 2:d0bc8c2974e0 33 * printf("\r\n");
dkato 2:d0bc8c2974e0 34 * }
dkato 2:d0bc8c2974e0 35 * printf("\r\n");
dkato 2:d0bc8c2974e0 36 * }
dkato 2:d0bc8c2974e0 37 *
dkato 2:d0bc8c2974e0 38 * int main() {
dkato 2:d0bc8c2974e0 39 * AsciiFont ascii_font(text_field, WIDTH, HEIGHT, STRIDE, BYTE_PER_PIXEL);
dkato 2:d0bc8c2974e0 40 *
dkato 2:d0bc8c2974e0 41 * ascii_font.Erase(0xcc);
dkato 3:0d8bc371d109 42 * ascii_font.DrawStr("AB", 0, 0, 0x11, 1);
dkato 2:d0bc8c2974e0 43 * ascii_font.DrawChar('C', AsciiFont::CHAR_PIX_WIDTH,
dkato 2:d0bc8c2974e0 44 * AsciiFont::CHAR_PIX_HEIGHT, 0x22, 1);
dkato 2:d0bc8c2974e0 45 * print_text_field(); //debug print
dkato 2:d0bc8c2974e0 46 *
dkato 2:d0bc8c2974e0 47 * ascii_font.Erase();
dkato 3:0d8bc371d109 48 * ascii_font.DrawStr("D", 0, 0, 0xef, 2);
dkato 2:d0bc8c2974e0 49 * print_text_field(); //debug print
dkato 2:d0bc8c2974e0 50 *
dkato 2:d0bc8c2974e0 51 * ascii_font.Erase(0x11, 6, 0, 6, 8);
dkato 2:d0bc8c2974e0 52 * print_text_field(); //debug print
dkato 2:d0bc8c2974e0 53 * }
dkato 2:d0bc8c2974e0 54 * @endcode
dkato 0:99222c303e8f 55 */
dkato 2:d0bc8c2974e0 56
dkato 0:99222c303e8f 57 class AsciiFont {
dkato 0:99222c303e8f 58 public:
dkato 0:99222c303e8f 59
dkato 0:99222c303e8f 60 /** Constructor: Initializes AsciiFont.
dkato 0:99222c303e8f 61 *
dkato 0:99222c303e8f 62 * @param p_buf Text field address
dkato 0:99222c303e8f 63 * @param width Text field width
dkato 0:99222c303e8f 64 * @param height Text field height
dkato 0:99222c303e8f 65 * @param stride Buffer stride
dkato 0:99222c303e8f 66 * @param colour Background color
dkato 0:99222c303e8f 67 * @param byte_per_pixel Byte per pixel
dkato 0:99222c303e8f 68 */
dkato 0:99222c303e8f 69 AsciiFont(uint8_t * p_buf, int width, int height, int stride, int byte_per_pixel, uint32_t const colour = 0);
dkato 0:99222c303e8f 70
dkato 0:99222c303e8f 71 /** Erase text field
dkato 0:99222c303e8f 72 *
dkato 0:99222c303e8f 73 */
dkato 2:d0bc8c2974e0 74 void Erase();
dkato 0:99222c303e8f 75
dkato 0:99222c303e8f 76 /** Erase text field
dkato 0:99222c303e8f 77 *
dkato 0:99222c303e8f 78 * @param colour Background color
dkato 0:99222c303e8f 79 */
dkato 2:d0bc8c2974e0 80 void Erase(uint32_t const colour);
dkato 0:99222c303e8f 81
dkato 0:99222c303e8f 82 /** Erase text field
dkato 0:99222c303e8f 83 *
dkato 0:99222c303e8f 84 * @param colour Background color
dkato 0:99222c303e8f 85 * @param x Erase start position of x coordinate
dkato 0:99222c303e8f 86 * @param y Erase start position of y coordinate
dkato 0:99222c303e8f 87 * @param width Erase field width
dkato 0:99222c303e8f 88 * @param height Erase field height
dkato 0:99222c303e8f 89 */
dkato 2:d0bc8c2974e0 90 void Erase(uint32_t const colour, int x, int y, int width, int height);
dkato 0:99222c303e8f 91
dkato 0:99222c303e8f 92 /** Draw a string
dkato 0:99222c303e8f 93 *
dkato 0:99222c303e8f 94 * @param str String
dkato 0:99222c303e8f 95 * @param x Drawing start position of x coordinate
dkato 0:99222c303e8f 96 * @param y Drawing start position of y coordinate
dkato 0:99222c303e8f 97 * @param color Font color
dkato 0:99222c303e8f 98 * @param font_size Font size (>=1)
dkato 0:99222c303e8f 99 * @param max_char_num The maximum number of characters
dkato 0:99222c303e8f 100 * @return The drawn number of characters
dkato 0:99222c303e8f 101 */
dkato 4:8fb5219bbc09 102 int DrawStr(const char * str, int x, int y, uint32_t const colour, int font_size = 1, uint16_t const max_char_num = 0xffff);
dkato 0:99222c303e8f 103
dkato 0:99222c303e8f 104 /** Draw a character
dkato 0:99222c303e8f 105 *
dkato 0:99222c303e8f 106 * @param x Drawing start position of x coordinate
dkato 0:99222c303e8f 107 * @param y Drawing start position of y coordinate
dkato 0:99222c303e8f 108 * @param color Font color
dkato 0:99222c303e8f 109 * @param font_size Font size (>=1)
dkato 0:99222c303e8f 110 * @return true if successfull
dkato 0:99222c303e8f 111 */
dkato 2:d0bc8c2974e0 112 bool DrawChar(char c, int x, int y, uint32_t const colour, int font_size = 1);
dkato 2:d0bc8c2974e0 113
dkato 2:d0bc8c2974e0 114 /** The pixel width of a character. (font_size=1)
dkato 2:d0bc8c2974e0 115 *
dkato 2:d0bc8c2974e0 116 */
dkato 2:d0bc8c2974e0 117 static const int CHAR_PIX_WIDTH = 6;
dkato 2:d0bc8c2974e0 118
dkato 2:d0bc8c2974e0 119 /** The pixel height of a character. (font_size=1)
dkato 2:d0bc8c2974e0 120 *
dkato 2:d0bc8c2974e0 121 */
dkato 2:d0bc8c2974e0 122 static const int CHAR_PIX_HEIGHT = 8;
dkato 0:99222c303e8f 123
dkato 0:99222c303e8f 124 private:
dkato 0:99222c303e8f 125 uint8_t * p_text_field;
dkato 0:99222c303e8f 126 int max_width;
dkato 0:99222c303e8f 127 int max_height;
dkato 0:99222c303e8f 128 int buf_stride;
dkato 0:99222c303e8f 129 int pixel_num;
dkato 0:99222c303e8f 130 uint32_t background_colour;
dkato 0:99222c303e8f 131 };
dkato 0:99222c303e8f 132 #endif