I'm trying to build a system to display on multiple screens, using a manager class, but I've run into trouble with a basic 16x2 Text LCD. I have the LCD attached correctly, as it displays correctly without the manager, but not with. The print command sometimes displays nonsense, but at other times, the command will seem to restart the sketch, rerunning code up until the problem command.
I realize the DisplayText class is unnecessary in this code - there are other functions I added for the LCD not relevant to the issue.
I'm running this on an a Mega 2560, with Windows 10.
#include <LiquidCrystal.h>
class DisplayText { // Object for displaying on a lcd screen
private:
public:
LiquidCrystal lcd;
DisplayText(int pinRS, int pinE, int pin0, int pin1, int pin2, int pin3)
: lcd(pinRS, pinE, pin0, pin1, pin2, pin3)
{
lcd.begin(16, 2);
lcd.clear();
lcd.setCursor(0, 0);
}
};
class DisplayManager { // manages various display types (currently only text lcds)
private:
public:
DisplayText *textLCDs;
DisplayManager() {
}
DisplayManager(byte _textLCDs[][6]) {
textLCDs = (DisplayText *)malloc(sizeof(DisplayText) * sizeof(_textLCDs)); // allocates memory for number of lcds based on array of pins
for (int i = 0; i < sizeof(textLCDs); i++) {
textLCDs[i] = DisplayText(_textLCDs[i][0], _textLCDs[i][1], _textLCDs[i][2], _textLCDs[i][3], _textLCDs[i][4], _textLCDs[i][5]); // initializes all lcds
}
}
};
byte textLCDsSetup[][6] = { { 52, 48, 36, 34, 32, 30 } };
DisplayManager display(textLCDsSetup); // initialize manager
//DisplayText displayDirect(52, 48, 36, 34, 32, 30); // initialize directly to lcd
void setup() {
//Displays correctly:
//displayDirect.lcd.print("test1");
//does not display correctly:
display.textLCDs[0].lcd.print("test2");
}
void loop() {
}
Edit:
(sizeof(_textLCDs)/sizeof(_textLCDs[0])
changed to
sizeof(_textLCDs)
as the purpose was to get number of items in _textLCDS