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
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.
|
|
AsciiFont.h@0:99222c303e8f, 2016-09-27 (annotated)
- Committer:
- dkato
- Date:
- Tue Sep 27 07:03:20 2016 +0000
- Revision:
- 0:99222c303e8f
- Child:
- 2:d0bc8c2974e0
first commit
Who changed what in which revision?
| User | Revision | Line number | New 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 | 0:99222c303e8f | 11 | /** A class to communicate a ascii_font |
| dkato | 0:99222c303e8f | 12 | * |
| dkato | 0:99222c303e8f | 13 | */ |
| dkato | 0:99222c303e8f | 14 | class AsciiFont { |
| dkato | 0:99222c303e8f | 15 | public: |
| dkato | 0:99222c303e8f | 16 | |
| dkato | 0:99222c303e8f | 17 | /** Constructor: Initializes AsciiFont. |
| dkato | 0:99222c303e8f | 18 | * |
| dkato | 0:99222c303e8f | 19 | * @param p_buf Text field address |
| dkato | 0:99222c303e8f | 20 | * @param width Text field width |
| dkato | 0:99222c303e8f | 21 | * @param height Text field height |
| dkato | 0:99222c303e8f | 22 | * @param stride Buffer stride |
| dkato | 0:99222c303e8f | 23 | * @param colour Background color |
| dkato | 0:99222c303e8f | 24 | * @param byte_per_pixel Byte per pixel |
| dkato | 0:99222c303e8f | 25 | */ |
| dkato | 0:99222c303e8f | 26 | AsciiFont(uint8_t * p_buf, int width, int height, int stride, int byte_per_pixel, uint32_t const colour = 0); |
| dkato | 0:99222c303e8f | 27 | |
| dkato | 0:99222c303e8f | 28 | /** Erase text field |
| dkato | 0:99222c303e8f | 29 | * |
| dkato | 0:99222c303e8f | 30 | */ |
| dkato | 0:99222c303e8f | 31 | void erase(); |
| dkato | 0:99222c303e8f | 32 | |
| dkato | 0:99222c303e8f | 33 | /** Erase text field |
| dkato | 0:99222c303e8f | 34 | * |
| dkato | 0:99222c303e8f | 35 | * @param colour Background color |
| dkato | 0:99222c303e8f | 36 | */ |
| dkato | 0:99222c303e8f | 37 | void erase(uint32_t const colour); |
| dkato | 0:99222c303e8f | 38 | |
| dkato | 0:99222c303e8f | 39 | /** Erase text field |
| dkato | 0:99222c303e8f | 40 | * |
| dkato | 0:99222c303e8f | 41 | * @param colour Background color |
| dkato | 0:99222c303e8f | 42 | * @param x Erase start position of x coordinate |
| dkato | 0:99222c303e8f | 43 | * @param y Erase start position of y coordinate |
| dkato | 0:99222c303e8f | 44 | * @param width Erase field width |
| dkato | 0:99222c303e8f | 45 | * @param height Erase field height |
| dkato | 0:99222c303e8f | 46 | */ |
| dkato | 0:99222c303e8f | 47 | void erase(uint32_t const colour, int x, int y, int width, int height); |
| dkato | 0:99222c303e8f | 48 | |
| dkato | 0:99222c303e8f | 49 | /** Draw a string |
| dkato | 0:99222c303e8f | 50 | * |
| dkato | 0:99222c303e8f | 51 | * @param str String |
| dkato | 0:99222c303e8f | 52 | * @param x Drawing start position of x coordinate |
| dkato | 0:99222c303e8f | 53 | * @param y Drawing start position of y coordinate |
| dkato | 0:99222c303e8f | 54 | * @param color Font color |
| dkato | 0:99222c303e8f | 55 | * @param font_size Font size (>=1) |
| dkato | 0:99222c303e8f | 56 | * @param max_char_num The maximum number of characters |
| dkato | 0:99222c303e8f | 57 | * @return The drawn number of characters |
| dkato | 0:99222c303e8f | 58 | */ |
| dkato | 0:99222c303e8f | 59 | int draw_string(char * str, int x, int y, uint32_t const colour, int font_size = 1, uint16_t const max_char_num = 0xffff); |
| dkato | 0:99222c303e8f | 60 | |
| dkato | 0:99222c303e8f | 61 | /** Draw a character |
| dkato | 0:99222c303e8f | 62 | * |
| dkato | 0:99222c303e8f | 63 | * @param x Drawing start position of x coordinate |
| dkato | 0:99222c303e8f | 64 | * @param y Drawing start position of y coordinate |
| dkato | 0:99222c303e8f | 65 | * @param color Font color |
| dkato | 0:99222c303e8f | 66 | * @param font_size Font size (>=1) |
| dkato | 0:99222c303e8f | 67 | * @return true if successfull |
| dkato | 0:99222c303e8f | 68 | */ |
| dkato | 0:99222c303e8f | 69 | bool draw_char(char c, int x, int y, uint32_t const colour, int font_size = 1); |
| dkato | 0:99222c303e8f | 70 | |
| dkato | 0:99222c303e8f | 71 | private: |
| dkato | 0:99222c303e8f | 72 | uint8_t * p_text_field; |
| dkato | 0:99222c303e8f | 73 | int max_width; |
| dkato | 0:99222c303e8f | 74 | int max_height; |
| dkato | 0:99222c303e8f | 75 | int buf_stride; |
| dkato | 0:99222c303e8f | 76 | int pixel_num; |
| dkato | 0:99222c303e8f | 77 | uint32_t background_colour; |
| dkato | 0:99222c303e8f | 78 | }; |
| dkato | 0:99222c303e8f | 79 | #endif |
