How to make "if branch" before making global instance

Hi, I'm using UNO and Adafruit TFT LCD library.

Basically it works, but I have a little complex issues.

  • I want to call the TFT display instance(normally it was made before setup, as global instance) from timer interrupt.
  • But I have to prepare two variation of pin map for LCD. Because I have made two different revision of hardware, and I want to make a compatible firmware on both of them.
  • I want to set "if branch" which reads the hardware revision, before making a global instance which sets the pin map.
  • But it seems that I cannot write any digital read or "if branch" before Arduino's setup(). And interrupt function cannot access to the private instance.

Would anyone tell me how to resolve this issue? Please.

I'm using this template code:

I want to change the #define pinmap values based on the judgement pin(it is set L/H on each different revision board). But it looks difficult...

casper2020:
I want to change the #define pinmap values based on the judgement pin(it is set L/H on each different revision board). But it looks difficult...

It sounds impossible to me.

The #defines are set at compile time but the pins cannot be checked until run time for the fairly obvious reason that the program is not on the Arduino at compile time.

Perhaps it would be possible to create an interface for both modules at compile time and in setup() choose which one to use.

...R

Yes, so I tried to declare the instance in the setup(), but then interrupts cannot call the LCD functions.

I wonder if possible

  • change the pin map after declaration&initialize
  • some trick to put the if branch before setup...

Adafruit_TFTLCD tft(LCD_CS, LCD_CD, LCD_WR, LCD_RD, LCD_RESET);

if only I can set freely these arguments as a global instance, it's perfect.

  • I want to set "if branch" which reads the hardware revision, before making a global instance which sets the pin map.

is the "hardware revision" in a #define ? You could use conditional compilation to generate code for the right hardware but still keep everything in one source code

Yes, so I tried to declare the instance in the setup(), but then interrupts cannot call the LCD functions.

I'm not sure I buy that... if the instance is a global variable, it will be known everywhere. Also I'm Not sure it's a good idea to talk to the TFT in an interrupt in the first place. that's probably slow.

J-M-L:
is the "hardware revision" in a #define ? You could use conditional compilation to generate code for the right hardware but still keep everything in one source code
I'm not sure I buy that... if the instance is a global variable, it will be known everywhere. Also I'm Not sure it's a good idea to talk to the TFT in an interrupt in the first place. that's probably slow.

I'd like to automatically follow the hardware, old or new. I use a blank pin to manage the revision, so if I just digitalread, I can find it's old or new.
And actually the interrupts is for the sensor capture. And if the interrupt function find the signal(it's rare), it calls the display instance. So it's not slow for me.

OK so you detect the hardware type at run time. (one pin won't give you much options for the long term - just HIGH or LOW so 2 possible hardware)

you could have a global variable that is of pointer type:

Adafruit_TFTLCD * tft;and assuming your hardware pin isconst byte hardwareTypePin = 3; // whatever it isand you have defined different sets of pins

// hardware revision #1
#define LCD_CS1 A3
#define LCD_CD1 A2 
#define LCD_WR1 A1
#define LCD_RD1 A0 
#define LCD_RESET1 A4

// hardware revision #2
#define LCD_CS2 A1
#define LCD_CD2 A3 
#define LCD_WR2 A2
#define LCD_RD2 A0 
#define LCD_RESET2 A4

then in the setup() you could do

void setup()
{
  pinMode(hardwareTypePin, INPUT);
  ...
  if (digitalRead(hardwareTypePin) == LOW) {
     tft = new Adafruit_TFTLCD(LCD_CS1, LCD_CD1, LCD_WR1, LCD_RD1, LCD_RESET1);
  } else {
     tft = new Adafruit_TFTLCD(LCD_CS2, LCD_CD2, LCD_WR2, LCD_RD2, LCD_RESET2);
 }
 ...
}

of course, since tft is a pointer now, everywhere you were using

tft.someMethod()

you need to change it to

tft->someMethod()

would that work?

Thank you!! Probably I understand your idea, I just keep the address of instance in the global area, and after that I call constructor in the setup. I'll do it right now.

I did it! Thank you very much sir!!

:slight_smile: