Problem with tft drawbitmap on ST7789

Hello

I have a problem trying to draw an image on my ST7789 2inch TFT. A square image is draw but it is just filled with random dots.

The bit map was created using Image2cpp and is
const unsigned char lamp [] PROGMEM = {
// 'lamp, 41x52px
0xff, 0xff, 0xff, 0xff, 0xff, 0x80, 0xff, 0xff, 0xff, 0xff, 0xff, 0x80, 0xff, 0xff, 0xff, 0xff,
0xff, 0x80, 0xff, 0xff, 0xff, 0xff, 0xff, 0x80, 0xff, 0xff, 0xff, 0xff, 0xff, 0x80, 0xff, 0xff,
0xff, 0xff, 0xff, 0x80, 0xff, 0xff, 0xff, 0xff, 0xff, 0x80, 0xff, 0xf0, 0xff, 0xff, 0xff, 0x80,
0xff, 0xc0, 0x0f, 0xff, 0xff, 0x80, 0xff, 0x80, 0x03, 0xff, 0xff, 0x80, 0xff, 0x0f, 0x00, 0x7f,
0xff, 0x80, 0xfe, 0x1f, 0xe0, 0x1f, 0xff, 0x80, 0xfe, 0x3f, 0xfc, 0x0f, 0xff, 0x80, 0xfe, 0x3f,
0xff, 0x03, 0xff, 0x80, 0xfc, 0x3f, 0xff, 0xc1, 0xff, 0x80, 0xfc, 0x7f, 0xff, 0xf0, 0xff, 0x80,
0xfc, 0x7f, 0xff, 0xf8, 0x7f, 0x80, 0xfc, 0x7f, 0xff, 0xfc, 0x3f, 0x80, 0xf8, 0x7f, 0xff, 0xfe,
0x1f, 0x80, 0xf8, 0x7f, 0xff, 0xff, 0x1f, 0x80, 0xf8, 0x7f, 0xff, 0xff, 0x8f, 0x80, 0xf8, 0xff,
0xff, 0xff, 0x8f, 0x80, 0xf8, 0xff, 0xff, 0xff, 0x8f, 0x80, 0xf8, 0xff, 0xff, 0xff, 0x87, 0x80,
0xf8, 0xff, 0xff, 0xff, 0x87, 0x80, 0xf8, 0xff, 0xff, 0xff, 0x87, 0x80, 0xf8, 0xff, 0xff, 0xff,
0x87, 0x80, 0xf8, 0xff, 0xff, 0xff, 0x8f, 0x80, 0xf8, 0xff, 0xff, 0xff, 0x8f, 0x80, 0xf8, 0x7f,
0xff, 0xff, 0x8f, 0x80, 0xf8, 0x7f, 0xff, 0xff, 0x1f, 0x80, 0xf8, 0x7f, 0xff, 0xfe, 0x1f, 0x80,
0xfc, 0x7f, 0xff, 0xfc, 0x3f, 0x80, 0xfc, 0x7f, 0xff, 0xf8, 0x7f, 0x80, 0xfc, 0x7f, 0xff, 0xf0,
0xff, 0x80, 0xfc, 0x3f, 0xff, 0xc1, 0xff, 0x80, 0xfe, 0x3f, 0xff, 0x03, 0xff, 0x80, 0xfe, 0x3f,
0xfc, 0x0f, 0xff, 0x80, 0xfe, 0x1f, 0xe0, 0x1f, 0xff, 0x80, 0xff, 0x0f, 0x00, 0x7f, 0xff, 0x80,
0xff, 0x80, 0x03, 0xff, 0xff, 0x80, 0xff, 0xc0, 0x0f, 0xff, 0xff, 0x80, 0xff, 0xf0, 0xff, 0xff,
0xff, 0x80, 0xff, 0xff, 0xff, 0xff, 0xff, 0x80, 0xff, 0xff, 0xff, 0xff, 0xff, 0x80, 0xff, 0xff,
0xff, 0xff, 0xff, 0x80, 0xff, 0xff, 0xff, 0xff, 0xff, 0x80, 0xff, 0xff, 0xff, 0xff, 0xff, 0x80,
0xff, 0xff, 0xff, 0xff, 0xff, 0x80, 0xff, 0xff, 0xff, 0xff, 0xff, 0x80, 0xff, 0xff, 0xff, 0xff,
0xff, 0x80, 0xff, 0xff, 0xff, 0xff, 0xff, 0x80
};

These are my includes

#include <Adafruit_GFX.h> // Core graphics library
#include <Adafruit_ST7735.h> // Hardware-specific library for ST7735
#include <Adafruit_ST7789.h> // Hardware-specific library for ST7789
#include <SPI.h>
#include <Automaton.h>
#include <Wire.h>

I have set up the display with
#if defined(ARDUINO_FEATHER_ESP32) // Feather Huzzah32
#define TFT_CS 14
#define TFT_RST 15
#define TFT_DC 32

#elif defined(ESP8266)
#define TFT_CS 4
#define TFT_RST 16
#define TFT_DC 5

#else
// For the breakout board, you can use any 2 or 3 pins.
// These pins will also work for the 1.8" TFT shield.
#define TFT_CS 10
#define TFT_RST 9 // Or set to -1 and connect to Arduino RESET pin
#define TFT_DC 8
#endif

The code is

tft.drawBitmap(100, 200, lamp, 50, 50, ST77XX_BLACK);

What am I doing wrong?

// 'lamp, 41x52px

tft.drawBitmap(100, 200, lamp, 50, 50, ST77XX_BLACK);

drawBitmap() is intended for monochrome images. 41x52px requires 6x52 bytes i.e. 312.
And the drawBitmap(x, y, bitmap, w, h, color); requires you to match the 41x52 in the w, h arguments.

Your bitmap looks like 19.5 rows of 16 i.e. 312 bytes.

So use
tft.drawBitmap(100, 200, lamp, 41, 52, ST77XX_BLACK);

Untested.

David.

p.s. Welcome to the Forum. Please use Code icon when posting code. (6th from left < / > )

Thanks David, I will give that a try.
Cheers
Mike

In case you wondered about this, think about a 48x52px bitmap.
48 horizontal pixels need 6 bytes i.e. 6x8 per row
41 horizontal pixels need 5.125 bytes i.e. 5.125 x 8

Since the drawBitmap() format does not like "fractional bytes" it allocates 6.0 instead of 5.125
Then it discards the 7 "unused" pixels.

Note that some formats use a packed bitmap. i.e. it starts the next row where the previous one finished. The 7 bits go to the next row

Most image generating programs will report the bitmap geometry e.g. 41x52
You don't worry about the low-level byte storage. Just use the correct w, h arguments.

David.

Thanks again David, I got part of my image displayed, now I understand the principle, the rest should follow.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.