forked from ThingPulse/esp8266-oled-ssd1306
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SSD1306.cpp
353 lines (296 loc) · 9.57 KB
/
SSD1306.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
/**The MIT License (MIT)
Copyright (c) 2015 by Daniel Eichhorn
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
See more at http://blog.squix.ch
Credits for parts of this code go to Mike Rankin. Thank you so much for sharing!
*/
#include "SSD1306.h"
#include <Wire.h>
SSD1306::SSD1306(int i2cAddress, int sda, int sdc) {
myI2cAddress = i2cAddress;
mySda = sda;
mySdc = sdc;
}
void SSD1306::init() {
Wire.begin(mySda, mySdc);
Wire.setClock(400000);
sendInitCommands();
resetDisplay();
}
void SSD1306::resetDisplay(void) {
displayOff();
clear();
display();
displayOn();
}
void SSD1306::reconnect() {
Wire.begin(mySda, mySdc);
}
void SSD1306::displayOn(void) {
sendCommand(0xaf); //display on
}
void SSD1306::displayOff(void) {
sendCommand(0xae); //display off
}
void SSD1306::setContrast(char contrast) {
sendCommand(0x81);
sendCommand(contrast);
}
void SSD1306::flipScreenVertically() {
sendCommand(0xA0 | 0x1); //SEGREMAP //Rotate screen 180 deg
sendCommand(0xC8); //COMSCANDEC Rotate screen 180 Deg
}
void SSD1306::clear(void) {
memset(buffer, 0, (128 * 64 / 8));
}
void SSD1306::display(void) {
sendCommand(COLUMNADDR);
sendCommand(0x0);
sendCommand(0x7F);
sendCommand(PAGEADDR);
sendCommand(0x0);
sendCommand(0x7);
for (uint16_t i=0; i<(128*64/8); i++) {
// send a bunch of data in one xmission
Wire.beginTransmission(myI2cAddress);
Wire.write(0x40);
for (uint8_t x=0; x<16; x++) {
Wire.write(buffer[i]);
i++;
}
i--;
yield();
Wire.endTransmission();
}
}
void SSD1306::setPixel(int x, int y) {
if (x >= 0 && x < 128 && y >= 0 && y < 64) {
switch (myColor) {
case WHITE: buffer[x + (y/8)*128] |= (1 << (y&7)); break;
case BLACK: buffer[x + (y/8)*128] &= ~(1 << (y&7)); break;
case INVERSE: buffer[x + (y/8)*128] ^= (1 << (y&7)); break;
}
}
}
void SSD1306::setChar(int x, int y, unsigned char data) {
for (int i = 0; i < 8; i++) {
if (bitRead(data, i)) {
setPixel(x,y + i);
}
}
}
// 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;
int charCode;
int currentCharWidth;
int currentCharStartPos;
int cursorX = 0;
int numberOfChars = pgm_read_byte(myFontData + CHAR_NUM_POS);
// iterate over string
int firstChar = pgm_read_byte(myFontData + FIRST_CHAR_POS);
int charHeight = pgm_read_byte(myFontData + HEIGHT_POS);
int currentCharByteNum = 0;
int startX = 0;
int startY = y;
if (myTextAlignment == TEXT_ALIGN_LEFT) {
startX = x;
} else if (myTextAlignment == TEXT_ALIGN_CENTER) {
int width = getStringWidth(text);
startX = x - width / 2;
} else if (myTextAlignment == TEXT_ALIGN_RIGHT) {
int width = getStringWidth(text);
startX = x - width;
}
for (int j=0; j < text.length(); j++) {
charCode = text.charAt(j)-0x20;
currentCharWidth = pgm_read_byte(myFontData + CHAR_WIDTH_START_POS + charCode);
// Jump to font data beginning
currentCharStartPos = CHAR_WIDTH_START_POS + numberOfChars;
for (int m = 0; m < charCode; m++) {
currentCharStartPos += pgm_read_byte(myFontData + CHAR_WIDTH_START_POS + m) * charHeight / 8 + 1;
}
currentCharByteNum = ((charHeight * currentCharWidth) / 8) + 1;
// iterate over all bytes of character
for (int i = 0; i < currentCharByteNum; i++) {
currentByte = pgm_read_byte(myFontData + currentCharStartPos + i);
//Serial.println(String(charCode) + ", " + String(currentCharWidth) + ", " + String(currentByte));
// iterate over all bytes of character
for(int bit = 0; bit < 8; bit++) {
//int currentBit = bitRead(currentByte, bit);
currentBitCount = i * 8 + bit;
charX = currentBitCount % currentCharWidth;
charY = currentBitCount / currentCharWidth;
if (bitRead(currentByte, bit)) {
setPixel(startX + cursorX + charX, startY + charY);
}
}
yield();
}
cursorX += currentCharWidth;
}
}
void SSD1306::drawStringMaxWidth(int x, int y, int maxLineWidth, String text) {
int currentLineWidth = 0;
int startsAt = 0;
int endsAt = 0;
int lineNumber = 0;
char currentChar = ' ';
int lineHeight = pgm_read_byte(myFontData + HEIGHT_POS);
String currentLine = "";
for (int i = 0; i < text.length(); i++) {
currentChar = text.charAt(i);
if (currentChar == ' ' || currentChar == '-') {
String lineCandidate = text.substring(startsAt, i);
if (getStringWidth(lineCandidate) <= maxLineWidth) {
endsAt = i;
} else {
drawString(x, y + lineNumber * lineHeight, text.substring(startsAt, endsAt));
lineNumber++;
startsAt = endsAt + 1;
}
}
}
drawString(x, y + lineNumber * lineHeight, text.substring(startsAt));
}
int SSD1306::getStringWidth(String text) {
text = utf8ascii(text);
int stringWidth = 0;
char charCode;
for (int j=0; j < text.length(); j++) {
charCode = text.charAt(j)-0x20;
stringWidth += pgm_read_byte(myFontData + CHAR_WIDTH_START_POS + charCode);
}
return stringWidth;
}
void SSD1306::setTextAlignment(int textAlignment) {
myTextAlignment = textAlignment;
}
void SSD1306::setFont(const char *fontData) {
myFontData = fontData;
}
void SSD1306::drawBitmap(int x, int y, int width, int height, const char *bitmap) {
for (int i = 0; i < width * height / 8; i++ ){
unsigned char charColumn = 255 - pgm_read_byte(bitmap + i);
for (int j = 0; j < 8; j++) {
int targetX = i % width + x;
int targetY = (i / (width)) * 8 + j + y;
if (bitRead(charColumn, j)) {
setPixel(targetX, targetY);
}
}
}
}
void SSD1306::setColor(int color) {
myColor = color;
}
void SSD1306::drawRect(int x, int y, int width, int height) {
for (int i = x; i < x + width; i++) {
setPixel(i, y);
setPixel(i, y + height);
}
for (int i = y; i < y + height; i++) {
setPixel(x, i);
setPixel(x + width, i);
}
}
void SSD1306::fillRect(int x, int y, int width, int height) {
for (int i = x; i < x + width; i++) {
for (int j = y; j < y + height; j++) {
setPixel(i, j);
}
}
}
void SSD1306::drawXbm(int x, int y, int width, int height, const char *xbm) {
if (width % 8 != 0) {
width = ((width / 8) + 1) * 8;
}
for (int i = 0; i < width * height / 8; i++ ){
unsigned char charColumn = pgm_read_byte(xbm + i);
for (int j = 0; j < 8; j++) {
int targetX = (i * 8 + j) % width + x;
int targetY = (8 * i / (width)) + y;
if (bitRead(charColumn, j)) {
setPixel(targetX, targetY);
}
}
}
}
void SSD1306::sendCommand(unsigned char com) {
Wire.beginTransmission(myI2cAddress); //begin transmitting
Wire.write(0x80); //command mode
Wire.write(com);
Wire.endTransmission(); // stop transmitting
}
void SSD1306::sendInitCommands(void) {
sendCommand(DISPLAYOFF);
sendCommand(NORMALDISPLAY);
sendCommand(SETDISPLAYCLOCKDIV);
sendCommand(0x80);
sendCommand(SETMULTIPLEX);
sendCommand(0x3F);
sendCommand(SETDISPLAYOFFSET);
sendCommand(0x00);
sendCommand(SETSTARTLINE | 0x00);
sendCommand(CHARGEPUMP);
sendCommand(0x14);
sendCommand(MEMORYMODE);
sendCommand(0x00);
sendCommand(SEGREMAP);
sendCommand(COMSCANINC);
sendCommand(SETCOMPINS);
sendCommand(0x12);
sendCommand(SETCONTRAST);
sendCommand(0xCF);
sendCommand(SETPRECHARGE);
sendCommand(0xF1);
sendCommand(SETVCOMDETECT);
sendCommand(0x40);
sendCommand(DISPLAYALLON_RESUME);
sendCommand(NORMALDISPLAY);
sendCommand(0x2e); // stop scroll
sendCommand(DISPLAYON);
}