U8g2: drawstring a variable u8x8

Created on 23 Feb 2018  路  4Comments  路  Source: olikraus/u8g2

Oli, all,

I'm hitting my head against a wall. I'm not the worlds best programmer, so if I'm asking something that's more "this is how you program" rather than "this is something wrong with the library", please point me in the right direction and I'll stay out of this thread!

I can print fixed strings just fine. Code like the following shows great
"u8x8.drawString(0,0," Text here ");"

But as soon as I try to use
u8x8.drawString(0,0, VariableInUse);

It falls apart.

VariableInUse is an INT, so I tried converting it in the line beforehand
String viuString= String(VariableInUse);

But then the arduino (Leonardo) compiler complains at me.

Any advice? Directions?

Many thanks

Most helpful comment

For anyone else who ended up here and forgot how the degenerate string clusterf*ck in C works.

This worked for me:

int counter = 10;
u8x8.drawString(0, 0, String(counter).c_str());

You're welcome.

All 4 comments

Well, drawString does not automatically convert a variable into a string, Maybe you could use print, which u8x8 took over from serial.print(). This is also why the documentation for u8x8.print() is here: https://www.arduino.cc/en/Serial/Print

So the full example is:

u8x8.setFont(u8x8_font_amstrad_cpc_extended_r);
u8x8.setCursor(0, 0);
u8x8.print(VariableInUse);  

Of course there are other ways for converting a variable to a string, but print() is common in Arduino world.

Maybe I should document this. The only reason for the existence of u8x8.print() is the fact that it prints variables.

Thanks Oli, fixed my issue too.

:)

For anyone else who ended up here and forgot how the degenerate string clusterf*ck in C works.

This worked for me:

int counter = 10;
u8x8.drawString(0, 0, String(counter).c_str());

You're welcome.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Devilscave picture Devilscave  路  3Comments

hauwarea picture hauwarea  路  5Comments

mkovero picture mkovero  路  5Comments

walenw picture walenw  路  8Comments

eimogandolf picture eimogandolf  路  6Comments