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.
Revision:
2:d0bc8c2974e0
Parent:
0:99222c303e8f
Child:
3:0d8bc371d109
--- a/AsciiFont.h	Mon Oct 03 02:20:49 2016 +0000
+++ b/AsciiFont.h	Mon Oct 03 06:33:40 2016 +0000
@@ -8,9 +8,52 @@
 
 #include "mbed.h"
 
-/** A class to communicate a ascii_font
+/** Draw the character of the ASCII code.
  *
+ * Example
+ * @code
+ * #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.DrawString("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.DrawString("D", 0, 0, 0xef, 2);
+ *     print_text_field(); //debug print
+ * 
+ *     ascii_font.Erase(0x11, 6, 0, 6, 8);
+ *     print_text_field(); //debug print
+ * }
+ * @endcode
  */
+
 class AsciiFont {
 public:
 
@@ -28,13 +71,13 @@
     /** Erase text field
      *
      */
-    void erase();
+    void Erase();
 
     /** Erase text field
      *
      * @param colour Background color
      */
-    void erase(uint32_t const colour);
+    void Erase(uint32_t const colour);
 
     /** Erase text field
      *
@@ -44,7 +87,7 @@
      * @param width Erase field width
      * @param height Erase field height
      */
-    void erase(uint32_t const colour, int x, int y, int width, int height);
+    void Erase(uint32_t const colour, int x, int y, int width, int height);
 
     /** Draw a string
      *
@@ -56,7 +99,7 @@
      * @param max_char_num The maximum number of characters
      * @return The drawn number of characters
      */
-    int draw_string(char * str, int x, int y, uint32_t const colour, int font_size = 1, uint16_t const max_char_num = 0xffff);
+    int DrawString(char * str, int x, int y, uint32_t const colour, int font_size = 1, uint16_t const max_char_num = 0xffff);
 
     /** Draw a character
      *
@@ -66,7 +109,17 @@
      * @param font_size Font size (>=1)
      * @return true if successfull
      */
-    bool draw_char(char c, int x, int y, uint32_t const colour, int font_size = 1);
+    bool DrawChar(char c, int x, int y, uint32_t const colour, int font_size = 1);
+
+    /** The pixel width of a character. (font_size=1)
+     *
+     */
+    static const int CHAR_PIX_WIDTH  = 6;
+
+    /** The pixel height of a character. (font_size=1)
+     *
+     */
+    static const int CHAR_PIX_HEIGHT = 8;
 
 private:
     uint8_t * p_text_field;