Go Down

Topic: SOLVED: Problems using UTFT in own class (Read 707 times) previous topic - next topic

dodgerts

Apr 20, 2013, 11:41 pm Last Edit: Apr 21, 2013, 07:09 pm by dodgerts Reason: 1
Hello,

I have a 5" TFT SSD1963 display and I am trying to implement a user interface with the UTFT library for one of my sketched. However, I have run into problems when using a UTFT object nested within other classes. By now, I have narrowed the problem to the following proof of principle:

Code: [Select]
#include <UTFT.h>

class UserInterface {
 public:
   UserInterface(void);
   void Display(void);
 private:  
   UTFT _display;
};

UserInterface::UserInterface(void) {
 UTFT _tmpDisplay(18);
 _display = _tmpDisplay;
 _display.InitLCD();
 _display.fillScr(255,127,0);
}

void UserInterface::Display(void) {
 _display.setColor(255, 255, 255);
 for (int i = 0; i <200; i++) {
   _display.drawPixel(i, i);
 }
}

void setup() {
 UserInterface ui;
 ui.Display();
}

void loop() {}


This works absolutely fine. However, whenever I remove "UserInterface ui;" from Setup() and put it outside any function to have it available globally, I only get weird things on my TFT and I have no clue why that is the case.

Any help would be appreciated...

Thanks a lot in advance!
dodgerts

MarkT

Dont do your initialization in the constructor - there is nothing setup at constructor time for global objects,
use an explicit initialize method.
[ I won't respond to messages, use the forum please ]

dodgerts


Dont do your initialization in the constructor - there is nothing setup at constructor time for global objects,
use an explicit initialize method.


I did not know that for global variables and assumed it is the job of the constructor to initialize the object.
Thanks a lot!

MarkT

The problem is that global objects are constructed before any other code has run, so after the
constructor runs the Arduino initialises itself and clobbers all the pin settings and malloced storage
and whatever - solution is either don't use global objects, or don't do anything in constructors that
affects the state of anything but the object's own internal member variables (ie don't call pinMode() etc).
[ I won't respond to messages, use the forum please ]

Go Up