mithat
March 10, 2018, 8:32pm
1
What's the best way to convert strings in PROGMEM to something U8g2 's drawStr() can use?
If you try to display a string stored in PROGMEM with the F() macro:
disp.drawStr(32, 32, F("Hello."));
you'll get a compile time error:
U8g2lib.h:238:17: note: no known conversion for argument 3 from 'const __FlashStringHelper*' to 'const char*'
Doing a reinterpret_cast<const char*> or using PSTR() instead of F() will let the code compile, but neither produces the desired output.
Can anyone suggest a solution that doesn't use more memory than the memory you're trying save by using F() in the first place?
Koepel
March 10, 2018, 9:08pm
2
The drawStr() does not support a string from flash as far as I know.
You can use strcpy_P() with a buffer on the stack or a common global buffer.
char buffer[40];
const char text1[] PROGMEM = "Hello.";
strcpy_P( buffer, text1);
disp.drawStr(32, 32, buffer);
mithat
March 11, 2018, 6:45pm
3
Thanks, Koepel. I was hoping for/wondering if there was a solution that didn't involve an intermediary buffer. I suppose the answer probably is if you can't spare 20 or 40 bytes you might have bigger problems.
Koepel
March 11, 2018, 7:41pm
4
The PSTR() should work as well, as a parameter for the strcpy_P() function. I use the sprintf_P() a lot for these situations.
char buffer[40];
strcpy_P( buffer, PSTR( "Hello"));
disp.drawStr(32, 32, buffer);
u8g2 also includes the Arduino print function:
disp.setCursor(32, 32);
disp.print(F("Hello."));
This is also available as an example here:
/*
PrintHelloWorld.ino
Use the (Arduino compatible) u8g2 function "print" to draw a text.
Also use the Arduino F() macro to place the text into PROGMEM area.
Universal 8bit Graphics Library (https://github.com/olikraus/u8g2/)
Copyright (c) 2016, olikraus@gmail.com
All rights reserved.
Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice, this list
of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice, this
list of conditions and the following disclaimer in the documentation and/or other
This file has been truncated. show original
A more complex example is here:
/*
PrintProgmem.ino
AVR Architecture:
Use the (Arduino compatible) u8g2 function "print" to draw a string,
which is located in PROGMEM.
For string constants use the F() macro, but this example use global
strings instead.
Universal 8bit Graphics Library (https://github.com/olikraus/u8g2/)
Copyright (c) 2016, olikraus@gmail.com
All rights reserved.
Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice, this list
of conditions and the following disclaimer.
This file has been truncated. show original
Oliver
mithat
March 16, 2018, 2:38am
6
olikraus:
u8g2 also includes the Arduino print function:
Totally missed that. Thanks!