String DOW_Str; // (actually received as string from a functon call, from another library)
DOW_Str = "Monday";
u8g2.drawStr(0, 29, DOW_Str); // obviously wrong, see the function prototype above and the error message below
Error message: no matching function for call to 'U8G2_SSD1306_128X32_UNIVISION_F_HW_I2C::drawStr(int, int, String&)'
Unfortunately, all my attempts to pass to draw.Str a variable derived from DOW_str (for example by using toCharArray or string.c_str() ) failed.
How should it be properly done? An example with eventually a variable declaration, its initialization and the call to drawStr would be very appreciated.
It is a mystery why anyone would choose to use String.
Many libraries provide two overloaded versions of a "print()" method e.g. one for String and one for char array. Edit. U8g2lib inherits Print.h methods
U8g2lib only supports print() drawStr() with char arrays in SRAM.
So you just have to convert your String into a char array with c_str()
This does not work for Strings in PROGMEM.
You have to create a String in SRAM e.g. with an assignment.
But it is just as easy to use separate setCursor() and print()
#include <Arduino.h>
#include <U8g2lib.h>
#ifdef U8X8_HAVE_HW_SPI
#include <SPI.h>
#endif
#ifdef U8X8_HAVE_HW_I2C
#include <Wire.h>
#endif
U8G2_SSD1306_128X64_NONAME_1_4W_HW_SPI u8g2(U8G2_R0, /* cs=*/ 10, /* dc=*/ 9, /* reset=*/ 8);
String name = "David Prentice"; //in SRAM
void setup(void)
{
u8g2.begin();
u8g2.setFont(u8g2_font_ncenB08_tr); // choose a suitable font
}
void loop(void)
{
String fixed = F("COPY from FLASH");
u8g2.firstPage();
do {
u8g2.drawStr(0, 10, "Hello World!"); // write something to the internal memory
u8g2.drawStr(10, 20, name.c_str()); // write something to the internal memory
u8g2.drawStr(5, 30, fixed.c_str()); // write String from SRAM
//u8g2.drawStr(0, 40, F("ORIGINAL IN FLASH").c_str()); // does not work
u8g2.setCursor(0, 40); //separate setCursor(), print() accepts all Print.h arguments
u8g2.print(F("ORIGINAL IN FLASH")); //
} while (u8g2.nextPage());
delay(1000);
}
David.
Edit. Corrected my comment about print(). Added print() example to sketch.
david_prentice:
It is a mystery why anyone would choose to use String
David.
Maybe because for guys like me who are programming occasionally (and there are many of us), the concept of String is more natural and for this reason they are easier to use than things like char[] or char *s, this despite some disadvantages.
david_prentice:
U8g2lib only supports regular char arrays in SRAM.
U8g2 also includes "print()", which is indeed the original Arduino "print()" function used with Serial.print() and others.
Indeed: U8g2 is derived from Arduino print class. This means: I can confirm that u8g2.print() also support strings encapsulated with the F() macro (which means the string is in PROGMEM area).
I also wouldn't wonder if print() (and u8g2) support the "String" class.