Error on EasyNex declaration in my own Display library

Hello,

I want to make my own Display library with the integration of the EasyNextionLibrary.
In my Diplay.h file i try to declare a public EasyNex variable with the use of Serial1.

#ifndef Display_h
#define Display_h
#include "Arduino.h" 
#include "EasyNextionLibrary.h" // Include EasyNextionLibrary

class Display {
public:
	Display();

  EasyNex NextionHMI(Serial1);  // Create an object of EasyNex class with the name < NextionHMI >
                                // Set as parameter the Hardware Serial you are going to use
                                // Serial = tx0/rx0, Serial1 = tx1,rx1, Serial2 = tx2,rx2, Serial3 = tx3,rx3

  void Setup();
  void Loop();
  void Save();
  void Restore();
  void Update();

private:
  // Declare constants                      
  const int REFRESH_TIME = 100;   // time to refresh the Nextion page every 100 ms

  // Declare variables
  unsigned long refresh_timer = millis();  // timer for refreshing Nextion's page
  int prevPageID = -1;
  bool initialized = false;
};

#endif

The compiler keeps giving me the error that Serial1 is not a type. This should be a Hardwareserial type which is included in the arduino.h library. Including the Hardwareserial.h library will not solve the problem.
Sure i'am doing something wrong, but unfortunately i can not find the problem.
Someone having any idea to solve this problem?

try
EasyNex NextionHMI(Stream * ser = &Serial1);

Instance data members whose constructors take an argument must be initialized via an initializer list. Also, it makes more sense for the user of your library to select which HardwareSerial port to use rather than hardcoding it. So:

#include "EasyNextionLibrary.h"

class Display {
  public:
    Display(HardwareSerial &str) : NextionHMI(str) {}
    EasyNex NextionHMI;
};

And in the user's code:

#include "Diplay.h"
Display myDisplay(Serial1);

Finally, in most use cases, you'd want NextionHMI to be private or restricted so the user can't access it directly.

Thanks for your reply.
EasyNex NextionHMI(Stream * ser = &Serial1);
This resolves the compiling error of the .h file but gives a bounce of reference errors on the .cpp file.

Thanks for your answer. That seems like a good solution. However, I choose to extend my class by inheriting the Easynex class.

#ifndef Display_h
#define Display_h
#include "Arduino.h" 
#include "EasyNextionLibrary.h" // Include EasyNextionLibrary
#include "tank.h"               // Include Tank library

//class Display{
class Display : public EasyNex {
public:
	Display(HardwareSerial& serial);
};

I'm not sure this is a better solution, but this way i can directly call all of the Easynex methods without the need of creating an object of the Easynex class.

That's a perfectly valid thing to do. The choice of extending a class through inheritance or encapsulating it through construction depends on the use case, and there is some overlap. However, similar to before, you must call the constructor of the base class (EasyNex) using an initializer list:

class Display : public EasyNex {
public:
  Display(HardwareSerial& serial) : EasyNex(serial) {}
};

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