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();
2 Answers 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());
-
even with a u8g2 lib, I'm still getting the same compile errorMartin Velinský– Martin Velinský2017年09月24日 12:38:51 +00:00Commented Sep 24, 2017 at 12:38
-
2Post a complete updated sketch with u8g2lib to better resolve.user31481– user314812017年09月24日 12:47:42 +00:00Commented Sep 24, 2017 at 12:47
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());
drawStr
function in the current U8glib library that takes only one parameter. What version are you using and where did you get it?