ThingPulse / esp8266-oled-ssd1306

Driver for the SSD1306 and SH1106 based 128x64, 128x32, 64x48 pixel OLED display running on ESP8266/ESP32
https://thingpulse.com
Other
1.98k stars 636 forks source link

Unable to drawString characters as (char) above 127 #344

Closed Shdwdrgn closed 2 years ago

Shdwdrgn commented 3 years ago

While attempting to display a sample of a font on the screen I discovered you cannot directly show characters above 127 with the drawString command. For example:

display.setTextAlignment(TEXT_ALIGN_CENTER);
uint8_t c;
for(int i=0; i<6; i++) {
  for(int j=0; j<16; j++) {
    c = 128 + (16*i) + j;
    display.drawString(j*8+3, i*10, String((char)c));
  }
}

A couple of characters are shown here (I believe these are 0xC2 and 0xC3 which are specifically managed in DefaultFontTableLookup), but the rest are blank. However if I copy/paste text into my code they seem to show up correctly. This is likely due to differences between ASCII and UTF-8 management, but I don't know enough about how UTF-8 characters work to reliably determine the difference in what the user is requesting.

My solution to this problem was to add a new function to OLEDDisplay.cpp to display specific chars.

void OLEDDisplay::drawChar(int16_t xMove, int16_t yMove, char ch) {
  uint16_t lineHeight = pgm_read_byte(fontData + HEIGHT_POS);

  char* text = &ch;
  drawStringInternal(xMove, yMove, text, 1, getStringWidth(text, 1));
}

To OLEDDisplay.h I added this line:

void drawChar(int16_t x, int16_t y, char ch);

Now in the example above you can replace the drawString line with: display.drawChar(j*8+3, i*10, (char)c); and all of the upper characters from 128-255 can be displayed individually on the screen.

Maybe someone with more knowledge of the subject will be able to find a better solution, but this should work without breaking any existing functionality.

stale[bot] commented 2 years ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.