Pages: [1]   Go Down
Author Topic: SOLVED: Problems using UTFT in own class  (Read 663 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 17
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
#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
« Last Edit: April 21, 2013, 12:09:32 pm by dodgerts » Logged

0
Offline Offline
Shannon Member
****
Karma: 215
Posts: 12463
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

[ I won't respond to messages, use the forum please ]

Offline Offline
Newbie
*
Karma: 0
Posts: 17
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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!
Logged

0
Offline Offline
Shannon Member
****
Karma: 215
Posts: 12463
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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).
Logged

[ I won't respond to messages, use the forum please ]

Pages: [1]   Go Up
Jump to: