diff --git a/ssd1306_i2c.cpp b/ssd1306_i2c.cpp index 0aaee7567ca11ccb77db5fced60d5bc912d9ffe1..2bd40d445c661bff2ca0685d2956c84983449272 100644 --- a/ssd1306_i2c.cpp +++ b/ssd1306_i2c.cpp @@ -119,7 +119,40 @@ void SSD1306::setChar(int x, int y, unsigned char data) { } } +// Code form http://playground.arduino.cc/Main/Utf8ascii +byte SSD1306::utf8ascii(byte ascii) { + if ( ascii<128 ) { // Standard ASCII-set 0..0x7F handling + lastChar=0; + return( ascii ); + } + + // get previous input + byte last = lastChar; // get last char + lastChar=ascii; // remember actual character + + switch (last) // conversion depnding on first UTF8-character + { case 0xC2: return (ascii); break; + case 0xC3: return (ascii | 0xC0); break; + case 0x82: if(ascii==0xAC) return(0x80); // special case Euro-symbol + } + + return (0); // otherwise: return zero, if character has to be ignored +} + +// Code form http://playground.arduino.cc/Main/Utf8ascii +String SSD1306::utf8ascii(String s) { + String r= ""; + char c; + for (int i=0; i<s.length(); i++) + { + c = utf8ascii(s.charAt(i)); + if (c!=0) r+=c; + } + return r; +} + void SSD1306::drawString(int x, int y, String text) { + text = utf8ascii(text); unsigned char currentByte; int charX, charY; int currentBitCount; @@ -212,6 +245,7 @@ void SSD1306::drawStringMaxWidth(int x, int y, int maxLineWidth, String text) { } int SSD1306::getStringWidth(String text) { + text = utf8ascii(text); int stringWidth = 0; char charCode; for (int j=0; j < text.length(); j++) { @@ -259,7 +293,7 @@ void SSD1306::drawRect(int x, int y, int width, int height) { void SSD1306::fillRect(int x, int y, int width, int height) { for (int i = x; i < x + width; i++) { - for (int j = 0; j < y + height; j++) { + for (int j = y; j < y + height; j++) { setPixel(i, j); } } @@ -316,4 +350,4 @@ void SSD1306::sendInitCommands(void) { sendCommand(NORMALDISPLAY); sendCommand(0x2e); // stop scroll sendCommand(DISPLAYON); -} +} \ No newline at end of file diff --git a/ssd1306_i2c.h b/ssd1306_i2c.h index 52bb8c44c4cd2de47692286bcd76f2274711aef2..a3cb628d797cf7779dd23d7285a430315d6edfb9 100644 --- a/ssd1306_i2c.h +++ b/ssd1306_i2c.h @@ -79,6 +79,7 @@ private: uint8_t buffer[128 * 64 / 8]; int myTextAlignment = TEXT_ALIGN_LEFT; int myColor = WHITE; + byte lastChar; const char *myFontData = ArialMT_Plain_10; public: @@ -139,6 +140,14 @@ public: // Sets the color of all pixel operations void setColor(int color); + // converts utf8 characters to extended ascii + // taken from http://playground.arduino.cc/Main/Utf8ascii + byte utf8ascii(byte ascii); + + // converts utf8 string to extended ascii + // taken from http://playground.arduino.cc/Main/Utf8ascii + String utf8ascii(String s); + // Draws a string at the given location void drawString(int x, int y, String text); @@ -161,4 +170,4 @@ public: // ArialMT_Plain_10, ArialMT_Plain_16, ArialMT_Plain_24 void setFont(const char *fontData); -}; +}; \ No newline at end of file