How to display a String variable with the u8g2 library?

Display used: 0.91 Inch 128x32 IIC I2C Blue, OLED LCD Display DIY Oled Module, SSD1306 Driver IC

From: https://github.com/olikraus/u8g2/wiki/u8g2reference#drawstr

I use the call to: drawStr

C++/Arduino Prototype: u8g2_uint_t U8g2::drawStr(u8g2_uint_t x, u8g2_uint_t y, const char *s)

Example that works:

    u8g2.setFont(u8g2_font_logisoso20_tr);
    u8g2.drawStr(0, 29, "Monday");

Example that doesn't work:

 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.

Because - it's there!

It's tempting to those who do not know the problem. :roll_eyes:

It offers to do something.

It's in the "manual". No warnings given. :astonished:

Thanks a lot, David, your code was very useful!

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.

victor-ch: this despite some disadvantages.

Such as random program crashes. :roll_eyes:

Paul__B:
Such as random program crashes. :roll_eyes:

An example of a proper usage of strings that nevertheless leads to crashes would be enlightening :slight_smile:

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.

Oliver

My apologies. I have corrected my comment in #1 and added a print() example to my pasted sketch.

U8g2lib has the convenience of single line drawStr() And flexibility of separate setCursor(), print() ...

This seems the best way to handle F("message in Flash")

David.

david_prentice: My apologies. I have corrected my comment in #1 and added a print() example to my pasted sketch.

No problem at all. In fact I am more than happy about your answers on u8g2 topics during my absence here.

Oliver