That does however not work with a Heltec ESP_LoRa module that I want to use.
The display use the Heltec library #include "heltec.h"
is instantiated with
Heltec.begin(true /DisplayEnable Enable/, false /LoRa Disable/, true /Serial Enable/);
and use the functions as Heltec.display->drawString(10, 0, "| Set");
Do I have a way to use the the classical notation display.drawString(10, 0, "| Set");
with this library?
I just want to swap the libraries and eventually the instantiations with #ifdef directives.
but keep the display instructions unchanged.
Thank you! that looks very promising!
Would that mean that I just enter
auto &display = *(Heltec.display);
in the display code block and i would then be able to use the codelines as
display.drawString(10, 0, "| Set");
Does the reference then prevent the usage of display.drawString(10, 0, "| Set"); using the original library?
Trying to understand the caveat:
that means that if the library uses static variables (which is probably the case) I can't use two references in the whole program?
Normally I use display code lines only within setup() and within my function display()
So entering the reference there should be safe?
Yes, because it will shadow the definition of SSD1306Wire display. You'll need to have some way to decide which one you use at compile-time, probably using a preprocessor #if or #ifdef.
The problem is that the global variable Heltec is defined in a library file. If you define a global reference to Heltec.display, you don't know whether Heltec is going to be initialized before your reference. If Heltec is initialized first, there's no problem, but if your reference is initialized first, it'll refer to an uninitialized *Heltec.display, i.e. a null pointer in this case.
Since you cannot guarantee the initialization order of global variables across files (translation units), you either have to use a local reference:
This avoids both the initialization order problem, and avoids the need for explicitly calling a function every time you want to use the display (display().drawString(...)), but it's a bit cryptic to my taste. Abstracted away in a library, I would be fine with it, but as boilerplate at the top of a sketch, it's a bit much, and I'd probably prefer the reference-to-pointer approach.
/*
HelTec Automation(TM) ESP32 Series Dev boards OLED draw Simple Function test code
- Some OLED draw Simple Function function test;
by LXYZN from HelTec AutoMation, ChengDu, China
www.heltec.cn
this project also realess in GitHub:
https://github.com/HelTecAutomation/Heltec_ESP32
*/
// This example just provide basic function test;
// For more informations, please vist www.heltec.cn or mail to support@heltec.cn
#include "Arduino.h"
#include "heltec.h"
//#include "images.h"
#define DEMO_DURATION 3000
typedef void (*Demo)(void);
int demoMode = 0;
int counter = 1;
void setup() {
auto &display = *(Heltec.display);
Heltec.begin(true /*DisplayEnable Enable*/, false /*LoRa Disable*/, true /*Serial Enable*/);
display.flipScreenVertically();
display.setFont(ArialMT_Plain_10);
}
void loop() {
auto &display = *(Heltec.display);
// clear the display
display.clear();
display.setTextAlignment(TEXT_ALIGN_LEFT);
display.setFont(ArialMT_Plain_10);
display.drawString(0, 0, "Hello world");
display.setFont(ArialMT_Plain_16);
display.drawString(0, 10, "Hello world");
display.setFont(ArialMT_Plain_24);
display.drawString(0, 26, "Hello world");
display.setTextAlignment(TEXT_ALIGN_RIGHT);
display.drawString(10, 128, String(millis()));
// write the buffer to the display
display.display();
delay(10);
}
Thank you!
You are the best!