Skip to content

Commit cc47fdc

Browse files
committed
Merge with update stream
1 parent 5554319 commit cc47fdc

File tree

2 files changed

+46
-3
lines changed

2 files changed

+46
-3
lines changed

ssd1306_i2c.cpp

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,40 @@ void SSD1306::setChar(int x, int y, unsigned char data) {
119119
}
120120
}
121121

122+
// Code form http://playground.arduino.cc/Main/Utf8ascii
123+
byte SSD1306::utf8ascii(byte ascii) {
124+
if ( ascii<128 ) { // Standard ASCII-set 0..0x7F handling
125+
lastChar=0;
126+
return( ascii );
127+
}
128+
129+
// get previous input
130+
byte last = lastChar; // get last char
131+
lastChar=ascii; // remember actual character
132+
133+
switch (last) // conversion depnding on first UTF8-character
134+
{ case 0xC2: return (ascii); break;
135+
case 0xC3: return (ascii | 0xC0); break;
136+
case 0x82: if(ascii==0xAC) return(0x80); // special case Euro-symbol
137+
}
138+
139+
return (0); // otherwise: return zero, if character has to be ignored
140+
}
141+
142+
// Code form http://playground.arduino.cc/Main/Utf8ascii
143+
String SSD1306::utf8ascii(String s) {
144+
String r= "";
145+
char c;
146+
for (int i=0; i<s.length(); i++)
147+
{
148+
c = utf8ascii(s.charAt(i));
149+
if (c!=0) r+=c;
150+
}
151+
return r;
152+
}
153+
122154
void SSD1306::drawString(int x, int y, String text) {
155+
text = utf8ascii(text);
123156
unsigned char currentByte;
124157
int charX, charY;
125158
int currentBitCount;
@@ -212,6 +245,7 @@ void SSD1306::drawStringMaxWidth(int x, int y, int maxLineWidth, String text) {
212245
}
213246

214247
int SSD1306::getStringWidth(String text) {
248+
text = utf8ascii(text);
215249
int stringWidth = 0;
216250
char charCode;
217251
for (int j=0; j < text.length(); j++) {
@@ -259,7 +293,7 @@ void SSD1306::drawRect(int x, int y, int width, int height) {
259293

260294
void SSD1306::fillRect(int x, int y, int width, int height) {
261295
for (int i = x; i < x + width; i++) {
262-
for (int j = 0; j < y + height; j++) {
296+
for (int j = y; j < y + height; j++) {
263297
setPixel(i, j);
264298
}
265299
}
@@ -316,4 +350,4 @@ void SSD1306::sendInitCommands(void) {
316350
sendCommand(NORMALDISPLAY);
317351
sendCommand(0x2e); // stop scroll
318352
sendCommand(DISPLAYON);
319-
}
353+
}

ssd1306_i2c.h

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ class SSD1306 {
7979
uint8_t buffer[128 * 64 / 8];
8080
int myTextAlignment = TEXT_ALIGN_LEFT;
8181
int myColor = WHITE;
82+
byte lastChar;
8283
const char *myFontData = ArialMT_Plain_10;
8384

8485
public:
@@ -139,6 +140,14 @@ class SSD1306 {
139140
// Sets the color of all pixel operations
140141
void setColor(int color);
141142

143+
// converts utf8 characters to extended ascii
144+
// taken from http://playground.arduino.cc/Main/Utf8ascii
145+
byte utf8ascii(byte ascii);
146+
147+
// converts utf8 string to extended ascii
148+
// taken from http://playground.arduino.cc/Main/Utf8ascii
149+
String utf8ascii(String s);
150+
142151
// Draws a string at the given location
143152
void drawString(int x, int y, String text);
144153

@@ -161,4 +170,4 @@ class SSD1306 {
161170
// ArialMT_Plain_10, ArialMT_Plain_16, ArialMT_Plain_24
162171
void setFont(const char *fontData);
163172

164-
};
173+
};

0 commit comments

Comments
 (0)