vagulus
February 15, 2018, 6:31am
1
I am displaying text on an OLED
This code
#include <Arduino.h>
#include <SPI.h>
#include <U8g2lib.h>
U8G2_SSD1325_NHD_128X64_1_4W_SW_SPI u8g2(
/* rotation zero */ U8G2_R0, /* clock=*/ 13, /* data=*/ 11,
/* cs=*/ 10, /* dc=*/ 9, /* reset=*/ 8);
void setup(void)
{
u8g2.begin();
u8g2.enableUTF8Print();
}
void loop(void)
{
u8g2.firstPage();
do {
u8g2.setFont(u8g2_font_ncenB14_tr);
u8g2.drawStr(5, 15, "Hello World!");
u8g2.drawStr(40, 35, "37");
u8g2.drawStr(60, 35, "\u0080"); // degree symbol
u8g2.drawStr(70, 35, "C");
u8g2.setFont(u8g2_font_unifont_t_symbols);
u8g2.drawStr(0, 64, "No snow in Perth");
} while (u8g2.nextPage());
delay(1000);
}
does not display the degree symbol as I expected.
What is wrong?
Thanks
MorganS
February 15, 2018, 6:51am
2
The OLED doesn't have Unicode. It probably does have a complete ASCII character set.
One problem you will hit is that the Arduino IDE does have unicode and when you type the degree symbol directly as º (ALT-167) then it will convert it into a two-byte character which then doesn't display on ASCII after you save the Arduino sketch.
One solution is to create the character as a constant.
const char DegreeSymbol = 167;
...
u8g2.drawStr(60, 35, DegreeSymbol);
I'm not sure if drawStr() will take a single character. Maybe there's a drawChar() function instead.
vagulus
February 15, 2018, 7:17am
4
MorganS:
I'm not sure if drawStr() will take a single character. Maybe there's a drawChar() function instead.
I tried a lot of variations on that theme.
This one
#include <Arduino.h>
#include <SPI.h>
#include <U8g2lib.h>
U8G2_SSD1325_NHD_128X64_1_4W_SW_SPI u8g2(
/* rotation zero */ U8G2_R0, /* clock=*/ 13, /* data=*/ 11,
/* cs=*/ 10, /* dc=*/ 9, /* reset=*/ 8);
const char* DEGREE_SYMBOL = { 167 };
void setup(void)
{
u8g2.begin();
u8g2.enableUTF8Print();
}
void loop(void)
{
u8g2.firstPage();
do {
u8g2.setFont(u8g2_font_ncenB14_tr);
u8g2.drawStr(5, 15, "Hello World!");
u8g2.drawStr(40, 35, "37");
u8g2.drawUTF8(60, 35, *DEGREE_SYMBOL); // degree symbol
u8g2.drawStr(80, 35, "C");
u8g2.setFont(u8g2_font_unifont_t_symbols);
u8g2.drawStr(0, 64, "No snow in Perth");
} while (u8g2.nextPage());
delay(1000);
}
got me THIS .
Some change - not much improvement.
I have a mixture of ideas in play here. I'll try to unravel it.
const char* DEGREE_SYMBOL = { 167 };
//[...]
u8g2.drawUTF8(60, 35, *DEGREE_SYMBOL); // degree symbol
Why on earth all the fuzz with an array?
And I think
u8g2.print("37" 0xA7);
should do the job. Or print everything in UTF8
vagulus
February 15, 2018, 8:02am
6
Thanks for that
septillion:
Why on earth all the fuzz with an array?
u8g2 defines: u8g2_uint_t U8g2::drawUTF8(u8g2_uint_t x, u8g2_uint_t y, const char *s)
septillion:
And I think
u8g2.print("37" 0xA7);
should do the job.
It didn't. Same muck in the middle but not animated.
septillion:
Or print everything in UTF8
How would I do that?
vagulus:
u8g2 defines: u8g2_uint_t U8g2::drawUTF8(u8g2_uint_t x, u8g2_uint_t y, const char *s)
You're right, but you don't give it a pointer
vagulus:
It didn't. Same muck in the middle but not animated.
Might need to be
u8g2.print("37" 0xA7 "");
vagulus:
How would I do that?
Instead of .drawStr() use .drawUTF8() And not sure but I think .print() will do UTF as well.
Advantage of .print() vs drawStr() (.drawUTF8()) is it remembers the cursor position so no need to specify the location of each print
vagulus
February 15, 2018, 9:51am
8
septillion:
You're right, but you don't give it a pointer
I guess my c++ is not up to it. Like many I have always struggled with the 'C' pointer notation. Would you please show me the code which would create const char *s where 's' has the value of a degree pointer?
Blessing and peace be upon you
septillion:
Might need to be
u8g2.print("37" 0xA7 "");
Without checking I think I can state with some confidence that the compiler will spit that line of code right out!
septillion:
Instead of .drawStr() use .drawUTF8() And not sure but I think .print() will do UTF as well.
Advantage of .print() vs drawStr() (.drawUTF8()) is it remembers the cursor position so no need to specify the location of each print
I've tried all sorts of permutations and combinations
//char DEGREE_SYMBOL = char("\u0080");
//const char* mySymbol = { DEGREE_SYMBOL };
//DEGREE_SYMBOL* = char("\u00B0");
void setup(void)
{
u8g2.begin();
u8g2.enableUTF8Print();
}
void loop(void)
{
u8g2.firstPage();
do {
u8g2.setFont(u8g2_font_ncenB14_tr);
u8g2.drawStr(5, 15, "Hello World!");
u8g2.drawStr(40, 35, "37");
//u8g2.drawStr(60, 35, *mySymbol); // degree symbol
//u8g2.drawUTF8(60, 35, 0xA7);
//u8g2.print(char("\u0080"));
u8g2.print("37" 0xA7 "");
u8g2.drawStr(80, 35, "C");
u8g2.setFont(u8g2_font_unifont_t_symbols);
u8g2.drawStr(0, 64, "No snow in Perth");
} while (u8g2.nextPage());
delay(1000);
and still getting nowhere.
It doesn't only expect a pointer to a char but a pointer to a NULL terminated char array aka a string.
So:
const char DegreeSymbol[] = {0xA7, '\0'};
u8g2.drawUTF8(60, 35, DegreeSymbol);
That's why you get artifacts, it just prints on until it finds a NULL by accident.
But I see the problem, you use "u8g2_font_ncenB14_tr" as font. If you look at the reference , you can see the t means "Transparent font, Do not use a background color" and the r means "Only glyphs on the range of the ASCII codes 32 to 127 are included in the font". Aka, it only includes normal character + some standard characters. But it does not include the degree symbol. Use "u8g2_font_ncenB14_tf" instead and it's fixed
vagulus
February 15, 2018, 11:08am
10
Well done septillion . I had to change the character code but the rest was spot on.
#include <Arduino.h>
#include <SPI.h>
#include <U8g2lib.h>
U8G2_SSD1325_NHD_128X64_1_4W_SW_SPI u8g2(
/* rotation zero */ U8G2_R0, /* clock=*/ 13, /* data=*/ 11,
/* cs=*/ 10, /* dc=*/ 9, /* reset=*/ 8);
const char DEGREE_SYMBOL[] = { 0xB0, '\0' };
void setup(void)
{
u8g2.begin();
u8g2.enableUTF8Print();
}
void loop(void)
{
u8g2.firstPage();
do {
u8g2.setFont(u8g2_font_ncenB14_tf);
u8g2.drawStr(5, 15, "Hello World!");
u8g2.drawStr(40, 35, "37");
u8g2.drawUTF8(63, 35, DEGREE_SYMBOL);
u8g2.drawStr(73, 35, "C");
u8g2.setFont(u8g2_font_unifont_t_symbols);
u8g2.drawStr(0, 64, "No snow in Perth");
} while (u8g2.nextPage());
delay(1000);
}
gives
We finally got there.
Thanks a lot to all who helped.
And the best part is the title to your thread. Now those who are stuck with the same problem have a thread to search for.
Paul
vagulus:
enableUTF8Print
Do you know how to get it working for
#include <Arduino_MKRIoTCarrier.h>
when I implemented this solution I got error...
system
Closed
August 30, 2021, 5:12pm
13
This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.