[LCDWIKI] Cant initializate LCD before _setup()_ .

Greetings,

TftDisplay.h

class TFTDisplay {
     public:
         TFTDisplay();
         void showTitle();
     private:
          LCDWIKI_KBV my_lcd = LCDWIKI_KBV(ILI9481, 40, 38, 39, 44, 41);
}
..
..
TftDisplay.cpp

TFTDisplay::TFTDisplay() {
    ...
    this->my_lcd.Init_LCD();
    this->my_lcd.Fill_Screen(0x0);
    this->my_lcd.Set_Rotation(1);
}

void TFTDisplay::showTitle() {
      my_lcd.Print_String("Hello World", left, 25);
      my_lcd.Set_Text_Size(2);
      ....
 }

main.ino

#include <tft/TFTDisplay.h>

TFTDisplay my_tft = TFTDisplay();

void setup() {
    ...
}

void loop() {
    my_tft.showTitle();
}

It was my old code. I built a wrapper class called TftDisplay to wrap LCDWIKI library.

Code above didn't work until I do the following changes :

  • I move LCDWIKI initialization on init() method. It leaves an empty constructor
  • I call init() on setup()
  • And later I can call showTitle() on loop()

My conclusion is I cant do LCD initialization (my_lcd.Init_LCD()) outside setup() or loop() methods. I tried it but "HelloWorld" was not printed.

The question is why? Does someone have an insight into what happens?

Because for me it is natural that I define smth that must be done directly after object creation in constructor incl initialization.

Thank you in advance for any enlightenment for this Arduino and C programmer newbie.

I can't see enough of the resources used by this code but, typically, the Arduino environment is initialised relatively late compared with the initialisation of constructors, leading to problems if the constructor relies on a part of the Arduino environment being ready. The Arduino environment is setup in function init() in wiring.h.

The usual work around is as you have, that is create a global object with a bare constructor and have a begin() method which is called in setup() to add detail. Example: Serial