[SOLVED-OOP] Class declaration and how to use it in Arduino with platformio

Greetings.

First of all, let me know if there is an appropriate forum to ask this question because I can’t find any and bit frustrated to find an answer for it :expressionless:

My Working Code:

#include <LCDWIKI_GUI.h>
#include <LCDWIKI_KBV.h>
LCDWIKI_KBV my_lcd = LCDWIKI_KBV(ILI9481, 40, 38, 39, 44, 41);

void loop() {
  my_lcd.Init_LCD();
  my_lcd.Fill_Screen(0x0);
  my_lcd.Set_Rotation(1);
  ....
  my_lcd.Print_String("Hello World", left, 25);
  my_lcd.Set_Text_Size(2);
  ....

It prints this “hello world”… I refactored it into 2 classes called TftDisplay.h and TftDisplay.cpp They are on src/tft/ directory.

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.cpp
...

#include <tft/TFTDisplay.h>

TFTDisplay my_tft = TFTDisplay();

void setup() {
    ...
}

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

main.cpp can be compiled w/o any error. But I can’t see “Hello world” printed in LCD.

I am using LCDWIKI and LCDGUI which are copied manually into .platformio/lib directory.

Someone can give me a hint what goes wrong? Or maybe a hint a forum that can help me find my answer?

Thanks in advance.

1 Like

I get the answer.

I cant put LCD initialization on TFTDisplay constructor. It seems that it must be initialized in setup() so that later I can use it on loop(). Meanwhile at the same time, cmiiw in C, I cant declare an empty reference or a reference without its object.

In order to solve it, I move out Lcd initialization from constructor into method init() which is called in setup().

and wallah, it works. it prints all text.

1 Like