Custom characters¶
Custom characters let you draw 5×8 pixel glyphs and use them as ordinary LCD characters.
Introduction¶
The LCD controller provides 8 custom character slots. Each slot is selected by a character code from 0 to 7.
Lcd2004 provides saveCustomChar() for creating these characters:
The first argument selects the custom character slot. The second argument is an array of exactly 5 bytes.
Tip
Prefer slots 1 through 7 for custom characters. Slot 0 works too, but using 1..7 makes strings containing custom
characters easier to read, for example:
Here \1 represents character code 1.
Character format¶
saveCustomChar() expects 5 columns, not 8 rows.
Each byte in the array describes one column, from left to right. The first byte (symbol[0]) is the leftmost column,
and the fifth byte (symbol[4]) is the rightmost column.
The 8 bits of each byte describe the pixels from top to bottom. Bit 0 (LSB) is the top pixel, bit 7 (MSB) is the bottom pixel.
For example, to draw a diamond:
const uint8_t diamond[5] = {
0b00000100, // column 0 (left)
0b00001110, // column 1
0b00011111, // column 2 (center)
0b00001110, // column 3
0b00000100 // column 4 (right)
};
This produces the following 8×5 pixel pattern (rows top to bottom):
The exact bit-to-pixel conversion is handled by saveCustomChar(). The method reads the five input bytes column by
column and sends eight pixel rows to the LCD controller.
After saving the character, its slot can be written using character code 1:
It can also be embedded into a larger string:
Defining a character¶
A complete example for a right-pointing arrow:
const uint8_t arrow[5] = {
0b00001000, // column 0
0b00001000, // column 1
0b00101010, // column 2
0b00011100, // column 3
0b00001000 // column 4
};
lcd.saveCustomChar(1, arrow);
lcd.setPosition(0, 0);
lcd.print("Next \1");
This arrow looks like:
Use a separate 5-byte array for each character you want to store.
const uint8_t smile[5] = {
0b00001000, // column 0
0b00010010, // column 1
0b00010000, // column 2
0b00010010, // column 3
0b00001000 // column 4
};
lcd.saveCustomChar(2, smile);
The slot number determines which character code is produced:
Slot limits¶
There are 8 custom character slots, numbered 0 through 7.
The library uses code % 8 when selecting the slot, so values outside this range wrap around:
uses the same slot as code 0.
For predictable code, use values from 0 to 7.