From 49dcafda2d2cd05ce96e094fa864d86b52cc6cf3 Mon Sep 17 00:00:00 2001
From: Daniel Eichhorn <dani.eichhorn@squix.ch>
Date: Sat, 12 Dec 2015 08:36:02 +0100
Subject: [PATCH] Added conversion from utf8 string to extended ascii to fix
 issue #5

---
 library.properties |  2 +-
 ssd1306_i2c.cpp    | 38 +++++++++++++++++++++++++++++++++++++-
 ssd1306_i2c.h      |  9 +++++++++
 3 files changed, 47 insertions(+), 2 deletions(-)

diff --git a/library.properties b/library.properties
index defc41e..6ffce01 100644
--- a/library.properties
+++ b/library.properties
@@ -1,5 +1,5 @@
 name=ESP8266 Oled Driver for SSD1306 display
-version=1.0.1
+version=1.0.2
 author=Daniel Eichhorn
 maintainer=Daniel Eichhorn <squix78@gmail.com> 
 sentence=A display driver for SSD1306 oled displays connected to an ESP8266
diff --git a/ssd1306_i2c.cpp b/ssd1306_i2c.cpp
index 77f4866..37c628c 100644
--- a/ssd1306_i2c.cpp
+++ b/ssd1306_i2c.cpp
@@ -88,6 +88,7 @@ void SSD1306::display(void) {
     sendCommand(0x0);
     sendCommand(0x7);
 
+    
     for (uint16_t i=0; i<(128*64/8); i++) {
       // send a bunch of data in one xmission
       //Wire.begin(mySda, mySdc);
@@ -101,6 +102,7 @@ void SSD1306::display(void) {
       yield();
       Wire.endTransmission();
     }
+    
 
 
 }
@@ -124,7 +126,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;
@@ -219,6 +254,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++) {
@@ -266,7 +302,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);
     }
   }
diff --git a/ssd1306_i2c.h b/ssd1306_i2c.h
index 59cbb74..078868c 100644
--- a/ssd1306_i2c.h
+++ b/ssd1306_i2c.h
@@ -96,6 +96,7 @@ private:
    int myFrameTransitionTicks = 25;
    int myTextAlignment = TEXT_ALIGN_LEFT;
    int myColor = WHITE;
+   byte lastChar;
    const char *myFontData = ArialMT_Plain_10;
    void (**myFrameCallbacks)(int x, int y);
 
@@ -159,6 +160,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);
 
-- 
GitLab