Trying to create a font for the arduino project. And the display library contains code that I understand what I am doing, but I don’t understand how. Because of this section, it is impossible to add Russian font.

void Paint::DrawCharAt(int x, int y, char ascii_char, sFONT* font, int colored) { int i, j; //Height=16, Width=11 unsigned int char_offset = (ascii_char - ' ') * font->Height * (font->Width / 8 + (font->Width % 8 ? 1 : 0)); const unsigned char* ptr = &font->table[char_offset]; for (j = 0; j < font->Height; j++) { for (i = 0; i < font->Width; i++) { if (pgm_read_byte(ptr) & (0x80 >> (i % 8))) { DrawPixel(x + i, y + j, colored);//рисуем пиксель } if (i % 8 == 7) { ptr++; } } if (font->Width % 8 != 0) { ptr++; } } } const uint8_t Font16_Table[] PROGMEM = { // @0 ' ' (11 pixels wide) 0x00, 0x00, // 0x00, 0x00, // 0x00, 0x00, // xxx }; sFONT Font16 = { Font16_Table, 11, /* Width */ 16, /* Height */ }; 

The code above takes and goes over the table Font16_Table , where the comma-separated HEX codes are indicated. This seems to be understandable, but adding your own font to this table is not displayed. In this case, the 32-HEX codes form 1 letter. Those. The procedure receives the character code of the letter and somehow understands the position of its HEX code.

Explain how to pass the character code if you set 0, 0, etc. Does not help? And in general, what does that procedure do?

Here is the procedure that calls it:

 void Paint::DrawStringAt(int x, int y, const char* text, sFONT* font, int colored) { const char* p_text = text; unsigned int counter = 0; int refcolumn = x; /* Send the string character by character on EPD */ while (*p_text != 0) { /* Display one character on EPD */ DrawCharAt(refcolumn, y, *p_text, font, colored); /* Decrement the column position by 16 */ refcolumn += font->Width; /* Point on the next character */ p_text++; counter++; } } 

Do I understand correctly that she takes the text, receives the character char code of the letter, then this code is a number, say the Russian letter A will have the code 192. I still can’t understand how the code points to the HEX table where to find the beginning of the Russian letter A. If you just add the letters A to the end of the HEX - I get hieroglyphs or something else but not my painted letter. With the correct formation of the letter, I figured out - 100% sure that I draw the letter correctly. I tried replacing the English letter, for example H and drew our Russian U instead. When I type the text, H displays Y - everything is ok. But the Cyrillic alphabet is not seen as a bud. Help to understand, from the answer it became partially clear, but not quite :-(

    1 answer 1

    unsigned int char_offset = (ascii_char - ' ') * font->Height * (font->Width / 8 + (font->Width % 8 ? 1 : 0));

    Offset in the table: (ascii_char - ' ') - the number of the symbol relative to the space (0x20)

    (font->Width / 8 + (font->Width % 8 ? 1 : 0)) in this case (width 11) 2 bytes are allocated to the horizontal scan of the character

    char* ptr = &font->table[char_offset]; points to the beginning of the character line

    (pgm_read_byte(ptr) & (0x80 >> (i % 8))) the i-th bit is allocated, counting from the most significant (MSB), and if it is single, it is drawn on the screen

    if (i % 8 == 7) { ptr++; jump to the second byte of the scan-line symbol

    thus, for example, the sequence 0x80,0x20 , repeated 16 times, should output two vertical lines along the edges of the character

    Edit:

    Russian letter A will have code 192

    If this is not confused with encodings , then the description of the character with the number 192 should be in the table with the index 192-32 = 160 (or the offset in bytes from the beginning of 320)

    • Thank you for the answer. This code above is called by the following code (it probably made sense to provide it immediately, I did not think): - Localix dots
    • The code in the comments is not readable, I threw in the question, but on the application, then everything should be clear - MBo
    • those. so that he sees the Russian letter A, it is necessary to add to the table to the current number of indices so many indices so that my beginning of the Cyrillic alphabet on the 160 index is correct? - Localix dots
    • It takes char_offset to become 160 (and make sure that A really is 192) - MBo
    • Is it possible to fill in empty non-significant indices with zeros? 0, 0, 0, 0, etc.? - Localix dots