I want to be able to change parameters in my library without using a constructor, so I am trying to use #define in my .ino to control the .h. Here is my code :
#if defined(SSD1306)
#include <Adafruit_SSD1306.h>
typedef Adafruit_SSD1306 DisplayDriver; // Alias for uniform handling
#define DEFAULT_OLED_ADDRESS 0x3D
#elif defined(SH1106)
#include <Adafruit_SH110X.h>
typedef Adafruit_SH1106G DisplayDriver; // Alias for uniform handling
#define DEFAULT_OLED_ADDRESS 0x3C
#else
#error "Le type d'écran utiliser doit être défini."
#endif
and the code in my .ino
#define SH1106 // Define the macro only if it isn't already defined
#include "Technoshield_ui.cpp"
Technoshield_ui ui;
void setup() {
ui.begin();
}
void loop() {
// Main code here
}
I always get the error, so my #define isnt reaching the header file at all. Is my code wrong, or is it just not possible to do it like that. Thanks for your help!
Why? It looks like both of those classes inherit from Adafruit_GFX. So, define the one you want in the .ino and pass it to your library's constructor as a Reference to a Adafruit_GFX object.
The logic behind what it is you are trying to do is unclear to me.
If you have a separate Arduino library with .cpp files, they're compiled separetely from your .ino file. Nothing you define in the .ino file will ever be seen by the library's .cpp files.
I see no .h file anywhere. Is that your unlabeled first bit of code? If so, you're not including it in your .ino file. I see your code try to include a .cpp file that is local to your sketch . That will not likely work.
The .cpp is supposed to be a .h, but it still doesnt work with a .h . What other method could I use to achieve this? I could use a constructor but it's less pratctical, since I have to include both libraries.
Hard to tell what you mean by "it doesn't work" because you have not provided a complete example.
When I try
#define SH1106 // Define the macro only if it isn't already defined
//#define SSD1306
#include "Technoshield_ui.h"
Technoshield_ui ui;
void setup()
{
Serial.begin (115200);
ui.begin();
}
void loop()
{
// Main code here
}
with Technoshield_ui.h as
#if defined(SSD1306)
#include <Adafruit_SSD1306.h>
typedef Adafruit_SSD1306 DisplayDriver; // Alias for uniform handling
#define DEFAULT_OLED_ADDRESS 0x3D
#define NAME "SSD1306"
#elif defined(SH1106)
#include <Adafruit_SH110X.h>
typedef Adafruit_SH1106G DisplayDriver; // Alias for uniform handling
#define DEFAULT_OLED_ADDRESS 0x3C
#define NAME "SH1106"
#else
#error "Le type d'écran utiliser doit être défini."
#endif
class Technoshield_ui
{
public:
void begin() {
Serial.print ("Display is ");
Serial.println (NAME);
}
};
It works as I expect (which may be different to what you expect).
I don't think you really understand how #include works.
Including both .h files does not have a down side in this case. Only the functions actually used will be included in the output object code.
You really need to provide move information about what you're trying to do. Meaning .... post a complete code!!! What is your Technoshield_ui class supposed to do?
Based on your current, inadequate description, the proper way to do it may be:
You would probably also want a Technoshield_ui.cpp file for longer method implementations. But, again, your intentions are too unclear to give better advise.