Arduino GFX library cannot create global instance

I am using ESP32 Devkit, Arduino IDE, Arduino GFX library 1.30 and a TFT display with ST7789 driver. I want to achieve this:

#include <Arduino_GFX_Library.h>  //this is not adafruit
#define TFT_SCK    18
#define TFT_MOSI   23
#define TFT_MISO   19
#define TFT_CS     22
#define TFT_DC     21
#define TFT_RESET  17
Arduino_ESP32SPI bus; 
Arduino_ST7789 tft;  
void draw_sth() {
  tft.drawLine(0,0,200,200,0x8000);
}
void setup(){
 bus = Arduino_ESP32SPI(TFT_DC, TFT_CS, TFT_SCK, TFT_MOSI, TFT_MISO);
 tft =  Arduino_ST7789(&bus, TFT_RESET);
 tft.begin();
 tft.fillScreen(BLACK);
 draw_sth();
}
void loop() {
  draw_sth();
 delay(5000);
}

This raises
"sketch_nov05a:4:16: error: no matching function for call to 'Arduino_ST7789::Arduino_ST7789()'
** Arduino_ST7789 tft;"**

Then I can get it to compile by doing this, which is bad, but anyway:

#include <Arduino_GFX_Library.h>
#define TFT_SCK    18
#define TFT_MOSI   23
#define TFT_MISO   19
#define TFT_CS     22
#define TFT_DC     21
#define TFT_RESET  17
Arduino_ESP32SPI bus = Arduino_ESP32SPI(TFT_DC, TFT_CS, TFT_SCK, TFT_MOSI, TFT_MISO);
Arduino_ST7789 tft = Arduino_ST7789(&bus, TFT_RESET); 
void draw_sth() {
  tft.drawLine(0,0,200,200,0x8000);
}
void setup(){
 Arduino_ESP32SPI bus = Arduino_ESP32SPI(TFT_DC, TFT_CS, TFT_SCK, TFT_MOSI, TFT_MISO);
 tft = Arduino_ST7789(&bus, TFT_RESET);
 tft.begin();
 tft.fillScreen(BLACK);
 draw_sth();
}
void loop() {
 draw_sth();
 delay(5000);
}

which then lets the display flicker when entering loop.

Any ideas what's wrong? Thank you.

Why is it bad?

What makes you believe that if you could create the object the way you want, it would not flicker?

Maybe try

#include <Arduino_GFX_Library.h>  //this is not adafruit
#define TFT_SCK    18
#define TFT_MOSI   23
#define TFT_MISO   19
#define TFT_CS     22
#define TFT_DC     21
#define TFT_RESET  17
Arduino_ESP32SPI *bus; 
Arduino_ST7789 *tft;  
void draw_sth() {
  tft->drawLine(0,0,200,200,0x8000);
}
void setup(){
 bus = &Arduino_ESP32SPI(TFT_DC, TFT_CS, TFT_SCK, TFT_MOSI, TFT_MISO);
 tft = &Arduino_ST7789(bus, TFT_RESET);
 tft->begin();
 tft->fillScreen(BLACK);
 draw_sth();
}
void loop() {
  draw_sth();
 delay(5000);
}

(Untested)

It flickers very destructive every second like on/off, and not a typical flicker from drawing every 5 sec.
Also slight correction, with this here

void setup(){
 bus = Arduino_ESP32SPI(TFT_DC, TFT_CS, TFT_SCK, TFT_MOSI, TFT_MISO);

drawing would not happen at all (screen remains blank=white), while this

void setup(){
 Arduino_ESP32SPI bus = Arduino_ESP32SPI(TFT_DC, TFT_CS, TFT_SCK, TFT_MOSI, TFT_MISO);

let's the destructive flicker happen.

Here's the result of your suggestion

Arduino: 1.8.9 (Windows 10), Board: "ESP32 Dev Module, Disabled, Default 4MB with spiffs (1.2MB APP/1.5MB SPIFFS), 240MHz (WiFi/BT), QIO, 80MHz, 4MB (32Mb), 921600, None"

WARNING: Category '' in library GFX Library for Arduino is not valid. Setting to 'Uncategorized'
WARNING: Category '' in library GFX Library for Arduino is not valid. Setting to 'Uncategorized'
C:\...\sketch_nov05a\sketch_nov05a.ino: In function 'void setup()':

sketch_nov05a:19:69: error: taking address of temporary [-fpermissive]
  bus = &Arduino_ESP32SPI(TFT_DC, TFT_CS, TFT_SCK, TFT_MOSI, TFT_MISO);
                                                                     ^
sketch_nov05a:20:39: error: no matching function for call to 'Arduino_ST7789::Arduino_ST7789(Arduino_ESP32SPI**, int)'
  tft = &Arduino_ST7789(&bus, TFT_RESET);
                                       ^
In file included from C:\arduino\libraries\arduino_61062\src/Arduino_GFX_Library.h:33:0,
                 from C:\...\sketch_nov05a\sketch_nov05a.ino:1:

and a lot of other lines

You are not using the code I suggested.

Ok, let's try

#include <Arduino_GFX_Library.h>  //this is not adafruit
#define TFT_SCK    18
#define TFT_MOSI   23
#define TFT_MISO   19
#define TFT_CS     22
#define TFT_DC     21
#define TFT_RESET  17
Arduino_ESP32SPI *bus; 
Arduino_ST7789 *tft;  
void draw_sth() {
  tft->drawLine(0,0,200,200,0x8000);
}
void setup(){
 bus = new Arduino_ESP32SPI(TFT_DC, TFT_CS, TFT_SCK, TFT_MOSI, TFT_MISO);
 tft = new Arduino_ST7789(bus, TFT_RESET);
 tft->begin();
 tft->fillScreen(BLACK);
 draw_sth();
}
void loop() {
  draw_sth();
 delay(5000);
}
You are not using the code I suggested.

Definitely, I did.

Thanks for your patience. We are stuck, though :pensive:, I guess. Screenshot for better evidence.

Arduino: 1.8.9 (Windows 10), TD: 1.52, Board: "ESP32 Dev Module, Disabled, Default 4MB with spiffs (1.2MB APP/1.5MB SPIFFS), 240MHz (WiFi/BT), QIO, 80MHz, 4MB (32Mb), 921600, None"

WARNING: Category '' in library GFX Library for Arduino is not valid. Setting to 'Uncategorized'
WARNING: Category '' in library GFX Library for Arduino is not valid. Setting to 'Uncategorized'
C:\...\sketch_nov05a\sketch_nov05a.ino: In function 'void setup()':

sketch_nov05a:15:42: error: no matching function for call to 'Arduino_ST7789::Arduino_ST7789(Arduino_ESP32SPI**, int)'

  tft = new Arduino_ST7789(&bus, TFT_RESET);

and so on

Nope.

I suggested

You tried

Spot the difference.

But even if you had used my suggestion exactly, it wasn't correct. I forgot the C++ "new" keyword.

Then you tried

but my suggestion was

Same difference.

You're spot on. Compiles and runs perfectly now.
Many thanks!!
Andy

So the flickering is gone?

I still don't know what was causing that. I expected my suggestions would show that creating these objects dynamically instead of statically would make no difference to the flickering. You believed it would make a difference, and it did. Can you explain your theory further, because it seems you were right.

Indeed, the flickering is completely gone. As you may imagine the real code is much more complex and it works totally smooth now. I am only occasionally on C any more and my live C experience is from 35 years ago :wink: Having no idea about the strange compiler errors, my guts feeling is that the SPI bus driver collapsed somehow at runtime when the tft was accessed from inside loop() and the SPI bus was reset, which reinitialized the display. This led to the impression of strong flicker.

Well, your theory doesn't make much sense to me, but I don't have a better one! I'm glad it's working for you now. :grin:

Oh, ok, I think I can explain those.

In your original code, "bus" was an object of the class Arduino_ESP32SPI.

The constructor method for the Arduino_ST7789 class was expecting a parameter which is a pointer or reference to an Arduino_ESP32SPI object. That's why the "&" was used here:

tft =  Arduino_ST7789(&bus, TFT_RESET);

The "&" turns an object into a pointer or reference to an object (in other words, the address in memory of the object).

When we changed the code to create the objects dynamically instead of statically, we changed the definition of "bus":

Arduino_ESP32SPI *bus;

Now, "bus" is no longer the object itself, it's a pointer/reference to the object.

When the code calls the method to create the TFT object, the "&" is no longer needed:

tft = new Arduino_ST7789(bus, TFT_RESET);

because "bus" is already a pointer.

If you leave the "&" in:

tft = new Arduino_ST7789(&bus, TFT_RESET);

then you are giving a pointer-to-a-pointer-to-the-object, when it is expecting a pointer-to-the-object.
Hence the error message:

The "**" indicates you gave it a pointer-to-a-pointer, which it didn't expect.

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