problem with tft.width() in class

Hello,
Sorry for my poor engish.

I have a function with many argument or parameter. Inside I calculate many value depending of tft.width().

I want create a class to replace it and learn about OOP.

I have tested this to understand

// define here your screen
# define  <class_of_my_screen.h>
class_of_my_screen tft;


class class_mywidth{
  int var_mywidth;
  int var_arwidth;
  int var_lowidth;
  public : class_mywidth(int var_clwidth,int var_puwidth = tft.width()){
    var_mywidth = tft.width();
    var_arwidth = var_clwidth;
    var_lowidth = var_puwidth;
  }
  void debug_mywidth(int var_dewidth,int var_buwidth = tft.width() ){
    Serial.print("tft.width() : ");        Serial.println( tft.width());
    Serial.print(" var_mywidth() : ");     Serial.println( var_mywidth);
    Serial.print(" var_arwidth() : ");     Serial.println( var_arwidth);
    Serial.print(" var_lowidth() : ");     Serial.println( var_lowidth);
//    Serial.print(" var_clwidth() : ");   Serial.println( var_clwidth);  // error
//    Serial.print(" var_puwidth() : ");   Serial.println( var_puwidth);  // error
    Serial.print(" var_dewidth() : ");     Serial.println( var_dewidth);
    Serial.print(" var_buwidth() : ");     Serial.println( var_buwidth);
  }
};

class_mywidth show_mywidth(tft.width());

void setup(void) {
  Serial.begin(9600);
  tft.begin();
  for(int i = 0; i<2;i++){
    tft.setRotation(i);
    Serial.print("||||\ttft.setRotation(");Serial.print(i);Serial.println(")");
    Serial.print("||||\t");
    Serial.print("tft.height(");
    Serial.print(tft.height());
    Serial.print(")\ttft.width(");
    Serial.print(tft.width());
    Serial.println(")");
    show_mywidth.debug_mywidth(tft.width());
  }
}

void loop(void){}

I need to calculate many value from tft.width(), how can I do it in class ?

up !

It isn't entirely clear from your post exactly what your question or problem is.

What happens when you run your code? What did you expect to happen? How do those two things differ?

tft is a class. You can call another class from your class except you are doing it in the constructor. The other class may not be constructed yet

You need to add a begin() method and only call that after the tft.begin().

Delta_G:
It isn’t entirely clear from your post exactly what your question or problem is.

What happens when you run your code? What did you expect to happen? How do those two things differ?

I want have in this variable var_mywidth of my class, the right value of tft.width().
In sketch I wrote, I test all possible to declare variable and initialze with tft.width();
Now only var_dewidth() and var_buwidth() have good value, but it is too late in my structure of class.

When I run my sketch I have this in serial monitor, my tft screen is 240 x 320
Value with |||| come from setup and the other from class.

|||| tft.setRotation(1)
|||| tft.height(240) tft.width(320)
tft.width() : 320
 var_mywidth() : 240
 var_arwidth() : 240
 var_lowidth() : 240
 var_dewidth() : 320
 var_buwidth() : 320
|||| tft.setRotation(2)
|||| tft.height(320) tft.width(240)
tft.width() : 240
 var_mywidth() : 240
 var_arwidth() : 240
 var_lowidth() : 240
 var_dewidth() : 240
 var_buwidth() : 240
|||| tft.setRotation(3)
|||| tft.height(240) tft.width(320)
tft.width() : 320
 var_mywidth() : 240
 var_arwidth() : 240
 var_lowidth() : 240
 var_dewidth() : 320
 var_buwidth() : 320
|||| tft.setRotation(4)
|||| tft.height(320) tft.width(240)
tft.width() : 240
 var_mywidth() : 240
 var_arwidth() : 240
 var_lowidth() : 240
 var_dewidth() : 240
 var_buwidth() : 240

MorganS:
tft is a class. You can call another class from your class except you are doing it in the constructor. The other class may not be constructed yet

You need to add a begin() method and only call that after the tft.begin().

Sorry, I do not undestand.

When I call show_mywidth.debug_mywidth(tft.width()); it is not same as begin() ?

Can you modify my sketch to have good value in var_mywidth ?
I understand better with example.

Because you do this class_mywidth show_mywidth(tft.width()); outside of any function. The order of operations is not guaranteed. tft may not exist at that time.

Additionally, the tft class may exist but it may not have a width set until you call begin.

Many of the Adafruit TFT libraries use the begin() to tell the library which screen size you have connected.

Width can even change during the program, if you rotate the screen.

I do not speak english and I use google to translate .
It is not easy to understand (for me) every thing I read.

By browsing the internet, I think I understood that I should use & * :: -> , but I can not put it into practice ...
Can you help me? thank you

padouet:
I do not speak english and I use google to translate .
It is not easy to understand (for me) every thing I read.

By browsing the internet, I think I understood that I should use & * :: -> , but I can not put it into practice ...
Can you help me? thank you

It looks like your problem can be solved with simple functions rather than a new class.

If you are trying to add functionality to an existing class, look at inheritance as a very easy solution. Just make a new class that inherits from TFT class.

BulldogLowell:
It looks like your problem can be solved with simple functions rather than a new class.

If you are trying to add functionality to an existing class, look at inheritance as a very easy solution. Just make a new class that inherits from TFT class.

It is a good idea but this is not exactly the answer I needed …

If I change tft screen, maybe I need to change the class name inheritance in my class…
It is exactly that I do no want to do.
I just want use tft.width() from class_of_my_screen.

I have modified my skecth above…
I added

define <class_of_my_screen.h>

class_of_my_screen tft;

Out of curiosity I tried
class class_mywidth: public class_of_my_screen{
in place of
class class_mywidth{
and value of tft.width() are wrong…

padouet:
I just want use tft.width() from class_of_my_screen.

Not knowing what you are trying to do makes it difficult to help.

I think you want to pass TFT objects to your class. I took a minute to write an example of an approach that may work for you. Try not to focus on the "simulation" TFT class I made. I did that so that each TFT object would merely return a unique value, just as if you has several different TFT's in your program and used your TFT library. Of course your class constructor looks more comprehensive for actual TFTs.

// all I am doing here is generating a unique number for each TFT instance that is created
// so that we see a different result for each TFT object we create.  Just as each of your TFT
// objects return different sizes if they are unique.

class TFT{
  public:
    TFT(){thisInsance = ++instance;};
    int getWidth(){return thisInsance * 80;};
    static int instance;
  private:
    int thisInsance;
};

int TFT::instance = 0;

// ********** end of TFT "simulation" ********** //

class MyClass{   // this definition must be done after TFT is defined
  public:
    MyClass(){};
    int myMethod(TFT screen){return screen.getWidth();};  // because it uses TFT objects
};

TFT tftA;
TFT tftB;
MyClass myClass;

void setup() 
{
  Serial.begin(9600);
  Serial.print(F("Width of Screen A:\t"));
  Serial.println(myClass.myMethod(tftA));  // passing TFT object to the myMethod function in MyClass object
  Serial.print(F("Width of Screen B:\t"));
  Serial.println(myClass.myMethod(tftB));  
}

void loop() {}

Is it for your class h_scroller you want help ?

padouet:
what do you think about this sketch I wrote ?

// define here your display

unsigned long currentMillis;
class h_scroller
{
  unsigned long c_previousMillis;
  unsigned long c_interval;
  int c_av;
  int c_po;
  int c_ts; 
  int c_dw;
  int c_lc;
  int c_ca;
  int c_tl;
  int c_ed;
  int c_re;
  int c_su;
  int c_px;
  int c_py;
  String c_tx;
 
  public:
  h_scroller(int p_ts, int p_dw, int p_py, String p_tx, unsigned long p_interval)
  {    c_interval = p_interval;  // frequency to scroll
    c_previousMillis = c_interval;
    c_av = 0;  // advance in texte
    c_po = 0;  // position on screen
    c_su = 0;  // nb char beginning to display at the end
    c_px = 0;  // x position text
    c_py = p_py;  // y position text
    c_ts = p_ts; // text size on screen
    c_dw = p_dw; // speed to scroll  // 1 is slow and fluid
    c_lc = c_ts * 6;  // large pixel char
    c_tl = c_tx.length();  // text length
    c_ca = (tft.width() / c_lc) + 3;  // number char in line
    c_re = c_ca; // nb char remaining to display at the beginning
    c_tx = p_tx;  // text to scroll

}
  void doit()
  {
    currentMillis = millis();
    if (currentMillis - c_previousMillis >= c_interval)
    {  c_previousMillis = currentMillis;
      tft.setTextSize(c_ts);
      tft.setTextColor(0xFFFF,0x0000);
      if(c_tl < c_ca)
      {
        for(int i = c_ca/c_tl ; i>0 ;i–)
        {
          c_tx = c_tx + c_tx;
        }
        c_tl = c_tx.length();
      }
      if( c_po >= c_lc)  //  next char to begin
      {
        c_po =  0;
        c_av = c_av < c_tl - 1 ?  c_av + 1 : 0;
      }
      c_re = c_tl - c_av >= c_ca ? c_av + c_ca + 1 : c_tl;
      c_su = c_tl - c_av >= c_ca ? 0 : 1 + c_ca - c_tl + c_av;
      tft.setTextWrap(false);
      tft.setCursor(-c_po,c_py);
      tft.print(c_tx.substring(c_av,c_re) + c_tx.substring(0,c_su));
      c_po += c_dw;
    }
  }
};

h_scroller text1(4, 1,180,"_abcdefghijklmnopqrstuvwxyz",100);
h_scroller text2(4, 1,80,"_12345",500);
h_scroller text3(4, 1,130,"+12345",5);

void setup(void)
{
// init here your display for 320x240 screen landscape
    uint16_t ID = tft.readID();
    tft.begin(ID);
  tft.setRotation(1);
    tft.fillScreen(0xFFE0);
    tft.fillScreen(0x0000);
}

void loop(void)
{
  text1.doit();
  text2.doit(); 
  text3.doit(); 
}




I know it is not perfect...
On right border it is not very nice, I will work about.
I have a problem in class h_scroller with 
c_ca = (tft.width() / c_lc) + 3; // number char in line
tft.width() is not the right value after tft.setRotation(1); in setup.
I do not understand why. 
Is the right way to deplace "c_ca = (tft.width() / c_lc) + 3; // number char in line" from public to doit ?