2

I made a little class for handling menus. When creating a new instance, I pass a reference to the oled object made in main.cpp. Yet when I want to render a string using a variable, it throws an error. But I can pass a string directly to the render function with no problems. See comments in the code for what is working and what is not.

#include "Arduino.h"
#include "U8glib.h"
#pragma once
class Menu {
 U8GLIB_SSD1306_128X64& oled;
 public:
 Menu(U8GLIB_SSD1306_128X64& oled): oled(oled) {
 this->oled = oled;
 };
 void render() {
 this->oled.firstPage();
 do {
 String text = "test2";
 this->oled.setFont(u8g_font_unifont);
 this->oled.setPrintPos(0, 20);
 this->oled.drawStr(text); // not working
 this->oled.drawStr(text.c_str()); // not working
 this->oled.drawStr("works"); // works
 } while (this->oled.nextPage());
 }
 };

In the case of this->oled.drawStr(text); the error is:

error: no matching function for call to 'U8GLIB_SSD1306_128X64::drawStr(String&)

In case of this->oled.drawStr(text.c_str()) the error is:

error: no matching function for call to 'U8GLIB_SSD1306_128X64::drawStr(const char*)

How I create the instance in main.cpp:

U8GLIB_SSD1306_128X64 oled(U8G_I2C_OPT_NONE);
Menu menu(oled);
menu.render();
VE7JRO
2,51519 gold badges27 silver badges29 bronze badges
asked Sep 24, 2017 at 10:31
3
  • I can find no mention of a drawStr function in the current U8glib library that takes only one parameter. What version are you using and where did you get it? Commented Sep 24, 2017 at 10:44
  • I udated to u8g2 lib, passed 3 parameters (int int c_str) and I'm still getting the same error Commented Sep 24, 2017 at 12:39
  • @MartinVelinský if you updated it, then edit the question and ADD the new code, otherwise it is impossible to properly answer Commented Sep 25, 2017 at 15:59

2 Answers 2

2

Try with the new version, U8g2lib.

With that version, you can write code like this:

String t1 = "Grow";
String t2 = "Room";
u8g2.firstPage();
do {
 u8g2.setFont(u8g2_font_logisoso24_tf);
 u8g2.drawStr(32, 24, t1.c_str());
 u8g2.drawStr(32, 60, t2.c_str());
} while(u8g2.nextPage());
dda
1,5951 gold badge12 silver badges17 bronze badges
answered Sep 24, 2017 at 10:42
2
  • even with a u8g2 lib, I'm still getting the same compile error Commented Sep 24, 2017 at 12:38
  • 2
    Post a complete updated sketch with u8g2lib to better resolve. Commented Sep 24, 2017 at 12:47
0

Forget the String class. It wastes memory on a system that is already constrained by memory. Define your strings as const char * and you won't have any problems.

const char* t1 = "Grow";
const char* t2 = "Room";
u8g2.firstPage();
do {
 u8g2.setFont(u8g2_font_logisoso24_tf);
 u8g2.drawStr(32, 24, t1);
 u8g2.drawStr(32, 60, t2);
} while(u8g2.nextPage());
answered Sep 24, 2017 at 23:02

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.