arduinoi2carduino-esp32adafruit

SSD1306 Display not displaying Umlaute


I am trying to get the German Umlaute ÄÖÜäöü and ß to be displayed on a standard SSD1309 OLED Display. I used the Adafruit GFX Font customizer to get the byte code for the extra letters. After trying to display the characters, they are still not shown. I tried writing a function that replaces the characters with their Unicode String.

This is part of my main.cpp:

  display.clearDisplay();
  display.setTextColor(SSD1306_WHITE);
  display.setCursor(0, 0);
  display.setFont(&FreeSans9pt7b_Umlaute);
  display.println(F("ÖÄÜöäüß"));
  display.display();

And this my replace function:

String utf8ToDisplay(const char* str) {
  String result = "";
  while (*str) {
    if ((*str & 0x80) == 0) {
      // ASCII character
      result += *str;
    } else if ((*str & 0xE0) == 0xC0) {
      // 2-byte UTF-8 character
      result += *str;
      result += *(str + 1);
      str++;
    } else if ((*str & 0xF0) == 0xE0) {
      // 3-byte UTF-8 character
      result += *str;
      result += *(str + 1);
      result += *(str + 2);
      str += 2;
    }
    str++;
  }
  return result;
}

What do i have to change so that my Code uses the new chars?

My libraries for the display are Adafruit_GFX Adafruit_SSD1306

If you want to check my Font u can get it here and uplaod it to the Adafruit GFX Converter.


Solution

  • So after some tinkering with my utf8ToDisplay function i figure out that i handled thee conversion to 2-byte and 3-byte UTF-8 chars incorrectly. It might not be the cleanest solution, but the function is now checking for the individual Characters and appending them to the result.

    String utf8ToDisplay(const char* str) {
      String result = "";
      while (*str) {
        if ((*str & 0x80) == 0) {
          // ASCII character
          result += *str;
        } else if ((*str & 0xE0) == 0xC0) {
          // 2-byte UTF-8 character
          uint16_t unicode = ((*str & 0x1F) << 6) | (*(str + 1) & 0x3F);
          if (unicode == 0xC4 || unicode == 0xD6 || unicode == 0xDC || unicode == 0xE4 || unicode == 0xF6 || unicode == 0xFC || unicode == 0xDF) {
            result += (char)unicode;
          }
          str++;
        } else if ((*str & 0xF0) == 0xE0) {
          // 3-byte UTF-8 character
          result += *str;
          result += *(str + 1);
          result += *(str + 2);
          str += 2;
        }
        str++;
      }
      return result;
    }