afiskon / stm32-st7735

STM32 HAL-based library for ST7735 displays
https://eax.me/stm32-st7735/
MIT License
235 stars 65 forks source link

Proposal of new WriteChar function with a bugfix #14

Open cosmolabs-ru opened 9 months ago

cosmolabs-ru commented 9 months ago

Hello! I used MatrixFont to make Cyrillic fonts for use with your library. Somehow, I encountered a bug: with some fonts, WriteChar() adds a big margin (up to 30% width) on the left of a glyph, and crops off as much on the right. I checked MatrixFont's output, and found it suitable for use with this lib. But somehow, built in fonts work, third-party-generated ones don't.

So i rewrote WriteChar() to display third-party fonts without aberrations. Check it out.

void ST7735_WriteChar(uint16_t x, uint16_t y, char ch, FontDef font, uint16_t color, uint16_t bgcolor) {
    ST7735_SetAddressWindow(x, y, x+font.width-1, y+font.height-1);
    uint16_t glyph_start = (ch-32)*font.height;
    for(uint16_t row = 0; row < font.height; row++) {
        for(uint16_t col = 0; col < font.width; col++) {
            if( font.data[glyph_start+row] & (1<<col) )  {/*pixel is ON*/
                uint8_t data[] = { color >> 8, color & 0xFF };
                ST7735_WriteData(data, sizeof(data));
            } else {
                uint8_t data[] = { bgcolor >> 8, bgcolor & 0xFF };
                ST7735_WriteData(data, sizeof(data));
            }
        }
    }
}
afiskon commented 9 months ago

Looks reasonable. To clarify: does it work with the fonts built-in into the library? Did you try to replace WriteChar with your implementation and execute the example code: https://github.com/afiskon/stm32-st7735/blob/master/Src/main.c ?