Can one make a condition on what library is included? [SOLVED]

I don't want to have a library dynamically loaded, but I want to make it easy for the end user to have a choice on which library is loaded, depending on what he is going to use as an output screen.

Something like this:

// set output to 1 if you want to use LCD
// set output to 2 if you want to use LED
// set output to 3 if you want to use VGA
// set output to 4 if you want to use TV-OUT

byte select = 1;

  switch (select) {
    case 1:
      #include  <LiquidCrystal.h>
      break;
    case 2:
      #include "font_7X5.h"
      #include <HT1632.h>
      break;
    case 3:
      #include <arduino_uvga.h>
      #include <conio.h>
      break;
    case 4:
      #include <TVout.h>
      #include <font8x8ext.h>
      break;
  }

What you want is conditional compilation in the first place. Then you want a #define line that the user can change to affetct what gets compiled in.
Like this:

// select the screen to use
// set OUTPUT_TYPE to 1 if you want to use LCD
// set OUTPUT_TYPE to 2 if you want to use LED
// set OUTPUT_TYPE to 3 if you want to use VGA
// set OUTPUT_TYPE to 4 if you want to use TV-OUT
#define OUTPUT_TYPE 1               // use 1 2 3 or 4

// user shouldn't change anything below this line

#if OUTPUT_TYPE == 1
    #include  <LiquidCrystal.h>
#elif OUTPUT_TYPE == 2
    #include "font_7X5.h"
    #include <HT1632.h>
#elif OUTPUT_TYPE == 3
    #include <arduino_uvga.h>
    #include <conio.h>
#elif OUTPUT_TYPE == 4
    #include <TVout.h>
    #include <font8x8ext.h>
#endif

HTH

JO3RI:
...

  switch (select) {

case 1:
      #include  <LiquidCrystal.h>
      break;

Absolutely not. You are trying to turn a run-time condition into a compile-time condition. This isn't Windows. You don't have run-time inclusion of DLLs.

@mromani: thanks for your help, I knew I would need conditional compilation, but I didn't know how to do that.

@Nick Gammon: I know, but I had to write something to make my question clear, and it helped, because mromani gave me a solution :wink:

BUT, it doesn't work. There seems to be a bug in the Arduino IDE: http://arduino.cc/forum/index.php/topic,60081.0.html

This makes I can't use #if

Uh, interesting! Well, that post contains also the workaround...

Well it seems that I made some other mistake, so actually your code does work. :blush:

It tried it this way too:

#define VID    // choose LED, LCD, VGA or VID

#ifdef LCD
    #include  <LiquidCrystal.h>
#endif

#ifdef LED
    #include <HT1632.h>
    #include "font_7X5.h"
#endif

#ifdef VGA
    //#include <arduino_uvga.h>
    //#include <conio.h>
#endif

#ifdef VID
    #include <TVout.h>
    #include <font8x8ext.h>
#endif

Thanks again

Not bad! :slight_smile:

One could also add a sorto of spell check like this:

#ifdef A
do something
#elif defined B
do something else
#else
#error "Please define A or B"
#endif

Ok, you corrected me again XD

it should have put:

#define VID    // choose LED, LCD, VGA or VID

#ifdef LCD
    #include  <LiquidCrystal.h>
#elif defined LED
    #include <HT1632.h>
    #include "font_7X5.h"
#elif defined VGA
    #include <arduino_uvga.h>
    #include <conio.h>
#elif defined VID
    #include <TVout.h>
    #include <font8x8ext.h>
#else
    #error "Please define LED, LCD, VGA or VID"
#endif

Oh and nice thing you suggest with the error check.

I decided to use your example, because people only need to fill out 1, 2, 3 or 4. But indeed also in your example you can use that error check, and I will use it :wink:

Thanks again

glad to help, when I can :slight_smile: