User menu: How do I define the liquidCrystal in my custom library? (1602)

I tried making a custom menu library. The purpose would be that I could say 'menu1.all(string 1, string 2, string3, string4) and that the class would take care of the position of these 4 strings on the lcd. 1 on the top left, 2 on the top right etc.
When I define LiquidCrystal lcd(pinconnections) then I can't define it with the same pinconnections in my library.
I'd like to be able to use lcd.print("Hello"); in the main, but also the same lcd.print(..) in my own library.
I'm guessing I'll have to make the library know that the main program is already using an lcd, so that the library can use the same?
The default lcd program, editted to use my "menu" library:

#include <Wire.h>

// include the library code:

#include <LiquidCrystal.h>

#include <Menu.h>

Menu menu1;

// initialize the library with the numbers of the interface pins
LiquidCrystal lcd1(12, 11, 4, 5, 6, 7);

void setup() {
  // set up the LCD's number of columns and rows: 
  lcd1.begin(16, 2);
  // Print a message to the LCD.
   menu1.all("a","b","c","d");
}

void loop() {}

The part of the library that should communicate with the lcd:

#include "Menu.h"
#include <LiquidCrystal.h>

LiquidCrystal lcd1(22, 23, 24, 25, 26, 27);


void Menu::printMenu(){
	lcd1.clear();
	lcd1.setCursor(0, 0);
	Serial.println(_lB.length());
	String s;
	if (_lB.length()>0)	lcd1.print(_lB);
	if (_rB.length() > 0)	{
		int pos = 16 - _rB.length() - 1;
		lcd1.setCursor(pos, 0);
		lcd1.print(" " + _rB);
	}
	if (_lO.length()>0)	lcd1.print(_lO);
	if (_rO.length() > 0)	{
		int pos = 16 - _rO.length()-1;
		lcd1.setCursor(pos, 1);
		lcd1.print(" " + _rO);
	}
}

How can I pass-trough the 6 integers that define the lcd to my own class?
for instance in the main:
LiquidCrystal lcd1(pin1, pin2, pin4, pin5, pin6, pin7);
And in my library: lcd1(&pin1, &pin2,...)?

Your question is: How can I use class/object A in my own new class/object B.
Unfortunately, the answer is: There are two options. You have to decide which one to use:
Composition or Inheritance

Both methods have thire pros and cons. It is difficult to explain without going much deeper in the technical implementation. So, let me just answer your question like this (now knowing that the actual answer is much more complicated): Use composition, which means, that you define a member variable, which is a pointer to the LiquidCrystal object:

class myclass
{
 LiquidCrystal *lcd;
}

You can define a member function, which must be called by the user to set the LiquidCrystal object:

myclass:setLC(LiquidCrystal *_lcd) { lcd = _lcd; }

Of course you can use the existing member function of LiquidCrystal in your member functions:

void Menu::printMenu(){
	lcd->clear();
	lcd->setCursor(0, 0);
	Serial.println(_lB.length());

Better answers might be in the programming section of this forum.

Oliver

I have to admit, my skill to pose a question is worthless... :blush:
This code:

class myclass
{
 LiquidCrystal *lcd;
}

Is it supposed to be in the header of my Menu library, as a second class?

myclass:setLC(LiquidCrystal *_lcd) { lcd = _lcd; }

Should belong in the Menu.cpp file as a new constructor?

My current header:

#ifndef _Menu_h
#define _Menu_h

#include "Arduino.h"

class Menu
{
public:
	Menu();
	void all(String lB, String rB, String lO, String rO);
	void linksBoven(String s); //leftAbove on lcd
	void linksOnder(String s); //leftUnder on lcd
	void rechtsBoven(String s); //rightAbove on lcd
	void rechtsOnder(String s); //richtUnder on lcd
	void printMenu();
private:
	String _lO; //string	links onder/ 'right under'
	String _lB; //			links boven  'right above'
	String _rO; //			rechts onder	'right under'
	String _rB;//			rechts boven	'right above'
};
#endif

And current .cpp:

#include "Menu.h"
#include <LiquidCrystal.h>

LiquidCrystal lcd1(22, 23, 24, 25, 26, 27);

Menu::Menu(){
	Serial.println("Menu");

	lcd1.begin(16, 2);
}


#pragma region update text
void Menu::all(String lB, String rB, String lO, String rO){
	linksBoven(lB);
	rechtsBoven(rB);
	linksOnder(lO);
	rechtsOnder(rO);
	printMenu();
}

void Menu::linksBoven(String s){
	_lB = s;
}

void Menu::linksOnder(String s){
	_lO = s;
}

void Menu::rechtsBoven(String s){
	_rB = s;
}

void Menu::rechtsOnder(String s){
	_rO = s;
}

#pragma endregion

void Menu::printMenu(){
	lcd1.clear();
	lcd1.setCursor(0, 0);
	Serial.println(_lB.length());
	String s;
	if (_lB.length()>0)	lcd1.print(_lB);
	if (_rB.length() > 0)	{
		int pos = 16 - _rB.length() - 1;
		lcd1.setCursor(pos, 0);
		lcd1.print(" " + _rB);
	}
	if (_lO.length()>0)	lcd1.print(_lO);
	if (_rO.length() > 0)	{
		int pos = 16 - _rO.length()-1;
		lcd1.setCursor(pos, 1);
		lcd1.print(" " + _rO);
	}
}

Ward123:
This code:

class myclass

{
LiquidCrystal *lcd;
}



Is it supposed to be in the header of my Menu library, as a second class?

Add " LiquidCrystal *lcd" as a new member variable to your menu class in your header.

Ward123:

myclass:setLC(LiquidCrystal *_lcd) { lcd = _lcd; }

Should belong in the Menu.cpp file as a new constructor?

Yes, this would also be usefull for the user.

class Menu
{
public:
	Menu();
       ...

will become

class Menu
{
private:
        LiquidCrystal *lcd;
public:
	Menu(LiquidCrystal *_lcd);
       ...

Oliver

LiquidCrystal *lcd;
Menu menu1(lcd);

Having it like this in my sketch, I'm able to upload it. But obviously the arduino doesn't know the pin connections. Where should I define these?
I've tried things like Menu menu1(lcd(4, 5, 6, 7, 8, 12)); but with no luck...
testbestand_leds11.ino:17: error: 'lcd' cannot be used as a function Is the error I get.

Using the "Glyphduino" library; I managed to find my mistakes.
It should be as following:
Constructor:

Menu::Menu(LiquidCrystal *lcd){
this->lcd = lcd;
	this->lcd->begin(16, 2);
	this->lcd->clear();
}

Header:

class Menu
{
private:
	LiquidCrystal *lcd;
public:
	Menu(LiquidCrystal *lcd);
};

Sketch:

LiquidCrystal lcd(4, 5, 6, 7, 8, 12);
Menu* menu1 = new Menu(&lcd);

void setup() {
	menu1->printMenu();
}

Thank you Oliver for helping me out!

LiquidCrystal lcd(pin1,pin2, ..., pinn);
Menu menu1(&lcd);

Oliver