-
Notifications
You must be signed in to change notification settings - Fork 0
/
SSD1306.cpp
412 lines (309 loc) · 11.3 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
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
// Copyright (c) 2018 Jock Murphy. [email protected]
//
// 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.
#include "mbed.h"
#include <Font.h>
#include "SSD1306.h"
#define SSD1306_SETCONTRAST 0x81
#define SSD1306_DISPLAYALLON_RESUME 0xA4
#define SSD1306_DISPLAYALLON 0xA5
#define SSD1306_NORMALDISPLAY 0xA6
#define SSD1306_INVERTDISPLAY 0xA7
#define SSD1306_DISPLAYOFF 0xAE
#define SSD1306_DISPLAYON 0xAF
#define SSD1306_SETDISPLAYOFFSET 0xD3
#define SSD1306_SETCOMPINS 0xDA
#define SSD1306_SETVCOMDETECT 0xDB
#define SSD1306_SETDISPLAYCLOCKDIV 0xD5
#define SSD1306_SETPRECHARGE 0xD9
#define SSD1306_SETMULTIPLEX 0xA8
#define SSD1306_SETLOWCOLUMN 0x00
#define SSD1306_SETHIGHCOLUMN 0x10
#define SSD1306_SETSTARTLINE 0x40
#define SSD1306_MEMORYMODE 0x20
#define SSD1306_COLUMNADDR 0x21
#define SSD1306_PAGEADDR 0x22
#define SSD1306_COMSCANINC 0xC0
#define SSD1306_COMSCANDEC 0xC8
#define SSD1306_SEGREMAP 0xA0
#define SSD1306_CHARGEPUMP 0x8D
#define SSD1306_EXTERNALVCC 0x1
#define SSD1306_SWITCHCAPVCC 0x2
// Scrolling #defines
#define SSD1306_ACTIVATE_SCROLL 0x2F
#define SSD1306_DEACTIVATE_SCROLL 0x2E
#define SSD1306_SET_VERTICAL_SCROLL_AREA 0xA3
#define SSD1306_RIGHT_HORIZONTAL_SCROLL 0x26
#define SSD1306_LEFT_HORIZONTAL_SCROLL 0x27
#define SSD1306_VERTICAL_AND_RIGHT_HORIZONTAL_SCROLL 0x29
#define SSD1306_VERTICAL_AND_LEFT_HORIZONTAL_SCROLL 0x2A
void SSD1306::initDisplay()
{
uint8_t vccState = SSD1306_SWITCHCAPVCC;
this->sendCommand(SSD1306_DISPLAYOFF);
this->sendCommand(SSD1306_SETDISPLAYCLOCKDIV);
this->sendCommand(0x80); // the suggested ratio 0x80
this->sendCommand(SSD1306_SETMULTIPLEX);
this->sendCommand(this->displayHeight-1);
this->sendCommand(SSD1306_SETDISPLAYOFFSET);
this->sendCommand(0x0); // no offset
this->sendCommand(SSD1306_SETSTARTLINE | 0x0); // line #0
this->sendCommand(SSD1306_CHARGEPUMP);
this->sendCommand((vccState == SSD1306_EXTERNALVCC) ? 0x10 : 0x14);
this->sendCommand(SSD1306_MEMORYMODE);
this->sendCommand(0x00); // 0x0 act like ks0108
this->sendCommand(SSD1306_SEGREMAP | 0x1);
this->sendCommand(SSD1306_COMSCANDEC);
this->sendCommand(SSD1306_SETCOMPINS);
this->sendCommand(this->displayHeight == 32 ? 0x02 : 0x12);
this->sendCommand(SSD1306_SETCONTRAST);
this->sendCommand(this->displayHeight == 32 ? 0x8F : ((vccState == SSD1306_EXTERNALVCC) ? 0x9F : 0xCF) );
this->sendCommand(SSD1306_SETPRECHARGE);
this->sendCommand((vccState == SSD1306_EXTERNALVCC) ? 0x22 : 0xF1);
this->sendCommand(SSD1306_SETVCOMDETECT);
this->sendCommand(0x40);
this->sendCommand(SSD1306_DISPLAYALLON_RESUME);
this->sendCommand(SSD1306_NORMALDISPLAY);
this->sendCommand(SSD1306_DISPLAYON);
this->sendCommand(SSD1306_DISPLAYOFF); // 0xAE
this->sendCommand(SSD1306_SETDISPLAYCLOCKDIV); // 0xD5
this->sendCommand(0x80); // the suggested ratio 0x80
this->sendCommand(SSD1306_SETMULTIPLEX); // 0xA8
this->sendCommand(this->displayHeight - 1);
this->sendCommand(SSD1306_SETDISPLAYOFFSET); // 0xD3
this->sendCommand(0x0); // no offset
this->sendCommand(SSD1306_SETSTARTLINE | 0x0); // line #0
this->sendCommand(SSD1306_CHARGEPUMP); // 0x8D
this->sendCommand((vccState == SSD1306_EXTERNALVCC) ? 0x10 : 0x14);
this->sendCommand(SSD1306_MEMORYMODE); // 0x20
this->sendCommand(0x00); // 0x0 act like ks0108
this->sendCommand(SSD1306_SEGREMAP | 0x1);
this->sendCommand(SSD1306_COMSCANDEC);
if(this->displayHeight == 32) {
// 128x32
this->sendCommand(SSD1306_SETCOMPINS); // 0xDA
this->sendCommand(0x02);
this->sendCommand(SSD1306_SETCONTRAST); // 0x81
this->sendCommand(0x8F);
} else if(this->displayHeight == 64) {
// 128x64
this->sendCommand(SSD1306_SETCOMPINS); // 0xDA
this->sendCommand(0x12);
this->sendCommand(SSD1306_SETCONTRAST); // 0x81
this->sendCommand((vccState == SSD1306_EXTERNALVCC) ? 0x9F : 0xCF);
} else {
// 96x16
this->sendCommand(SSD1306_SETCOMPINS); // 0xDA
this->sendCommand(0x2); //ada x12
this->sendCommand(SSD1306_SETCONTRAST); // 0x81
this->sendCommand((vccState == SSD1306_EXTERNALVCC) ? 0x10 : 0xAF);
}
this->sendCommand(SSD1306_SETPRECHARGE); // 0xd9
this->sendCommand((vccState == SSD1306_EXTERNALVCC) ? 0x22 : 0xF1);
this->sendCommand(SSD1306_SETVCOMDETECT); // 0xDB
this->sendCommand(0x40);
this->sendCommand(SSD1306_DISPLAYALLON_RESUME); // 0xA4
this->sendCommand(SSD1306_NORMALDISPLAY); // 0xA6
this->sendCommand(SSD1306_DEACTIVATE_SCROLL);
this->sendCommand(SSD1306_DISPLAYON);//--turn on oled panel
}
void SSD1306::transferCallback(int foo)
{
this->transferInProgress = false;
}
void SSD1306::update()
{
this->sendCommand(SSD1306_COLUMNADDR);
this->sendCommand(0); // Column start address (0 = reset)
this->sendCommand(this->displayWidth - 1); // Column end address (127 = reset)
this->sendCommand(SSD1306_PAGEADDR);
this->sendCommand(0); // Page start address (0 = reset)
uint8_t pageAddr = 0;
if(this->displayHeight == 16) {
pageAddr = 1;
} else if(this->displayHeight == 16) {
pageAddr = 3;
} else {
pageAddr = 7;
}
this->sendCommand(pageAddr); // Page end address
transferBuffer[0] = 0x40;
memcpy(transferBuffer + 1, this->displayBuffer, sizeof(this->displayBuffer));
this->i2c->write(this->i2cAddr, (const char *) this->transferBuffer, sizeof(this->displayBuffer) + 1);
}
void SSD1306::updateAsync()
{
// Abord if a transfer is already happening
if(this->transferInProgress) {
return;
}
this->sendCommand(SSD1306_COLUMNADDR);
this->sendCommand(0); // Column start address (0 = reset)
this->sendCommand(this->displayWidth - 1); // Column end address (127 = reset)
this->sendCommand(SSD1306_PAGEADDR);
this->sendCommand(0); // Page start address (0 = reset)
uint8_t pageAddr = 0;
if(this->displayHeight == 16) {
pageAddr = 1;
} else if(this->displayHeight == 16) {
pageAddr = 3;
} else {
pageAddr = 7;
}
this->sendCommand(pageAddr); // Page end address
transferBuffer[0] = 0x40;
event_callback_t callbackEvent;
callbackEvent.attach(this, &SSD1306::transferCallback);
this->transferInProgress = true;
memcpy(transferBuffer + 1, this->displayBuffer, sizeof(this->displayBuffer));
this->i2c->transfer(this->i2cAddr, (const char *) transferBuffer, sizeof(this->displayBuffer) + 1, (char *)transferResponseBuffer, sizeof(transferResponseBuffer), callbackEvent);
}
void SSD1306::drawBitmap(uint8_t x, uint8_t y, uint8_t *bitmap, uint8_t w, uint8_t h)
{
uint16_t bytePos = 0;
int8_t bitPos = 7;
uint8_t currByte = bitmap[bytePos++];
for(int yPos = y; yPos < y + h; ++yPos) {
for(int xPos = x; xPos < x + w; ++xPos) {
uint8_t c = currByte & (1 << bitPos--);
this->drawPixel(xPos, yPos, c);
if(bitPos < 0) {
currByte = bitmap[bytePos++];
bitPos = 7;
}
}
}
}
bool SSD1306::xorBitmap(uint8_t x, uint8_t y, uint8_t *bitmap, uint8_t w, uint8_t h)
{
bool ret = false;
uint16_t bytePos = 0;
int8_t bitPos = 7;
uint8_t currByte = bitmap[bytePos++];
for(int yPos = y; yPos < y + h; ++yPos) {
for(int xPos = x; xPos < x + w; ++xPos) {
uint8_t c = currByte & (1 << bitPos--);
bool col = this->xorPixel(xPos, yPos, c);
ret |= col;
if(bitPos < 0) {
currByte = bitmap[bytePos++];
bitPos = 7;
}
}
}
return ret;
}
void SSD1306::drawChar(uint8_t x, uint8_t y, const char ch, uint8_t color)
{
const uint8_t *fontChar = font + ((uint16_t) ch * 5);
for(uint8_t xOffset = 0; xOffset < 5; ++xOffset) {
for(uint8_t yOffset = 0; yOffset < 7; ++yOffset) {
uint8_t pixel = (fontChar[xOffset] >> yOffset) & 1;
this->drawPixel(x + xOffset, y + yOffset, color ? pixel : !pixel);
}
}
}
void SSD1306::scrollLeft(uint8_t n, uint8_t fromY, uint8_t toY)
{
uint8_t targetX = 0;
uint8_t sourceX = targetX + n;
uint8_t colCount = this->displayWidth - n;
for(uint8_t i = 0; i < colCount; ++i) {
for(uint8_t y = fromY; y < toY; ++y) {
uint8_t c = this->getPixel(sourceX, y);
this->drawPixel(targetX, y, c);
}
++sourceX;
++targetX;
}
for(uint8_t x = this->displayWidth - n; x < this->displayWidth; ++x) {
for(uint8_t y = fromY; y < toY; ++y) {
this->drawPixel(x, y, 0);
}
}
}
void SSD1306::scrollRight(uint8_t n, uint8_t fromY, uint8_t toY)
{
uint8_t targetX = this->displayWidth - 1;
uint8_t sourceX = targetX - n;
uint8_t colCount = this->displayWidth - n;
for(uint8_t i = 0; i < colCount; ++i) {
for(uint8_t y = fromY; y < toY; ++y) {
uint8_t c = this->getPixel(sourceX, y);
this->drawPixel(targetX, y, c);
}
--sourceX;
--targetX;
}
for(uint8_t x = 0; x < n; ++x) {
for(uint8_t y = fromY; y < toY; ++y) {
this->drawPixel(x, y, 0);
}
}
}
void SSD1306::scrollUp(uint8_t n, uint8_t fromX, uint8_t toX)
{
uint8_t targetY = 0;
uint8_t sourceY = targetY + n;
uint8_t rowCount = this->displayHeight - n;
for(uint8_t i = 0; i < rowCount; ++i) {
for(uint8_t x = 0; x < this->displayWidth; ++x) {
uint8_t c = this->getPixel(x, sourceY);
this->drawPixel(x, targetY, c);
}
++sourceY;
++targetY;
}
for(uint8_t y = this->displayHeight - n; y < this->displayHeight; ++y) {
for(uint8_t x = fromX; x < toX; ++x) {
this->drawPixel(x, y, 0);
}
}
}
void SSD1306::scrollDown(uint8_t n, uint8_t fromX, uint8_t toX)
{
uint8_t targetY = this->displayHeight - 1;
uint8_t sourceY = targetY - n;
uint8_t rowCount = this->displayHeight - n;
for(uint8_t i = 0; i < rowCount; ++i) {
for(uint8_t x = 0; x < this->displayWidth; ++x) {
uint8_t c = this->getPixel(x, sourceY);
this->drawPixel(x, targetY, c);
}
--sourceY;
--targetY;
}
for(uint8_t y = 0; y < n; ++y) {
for(uint8_t x = fromX; x < toX; ++x) {
this->drawPixel(x, y, 0);
}
}
}
void SSD1306::sendData(uint8_t d)
{
this->transferBuffer[0] = 0x40; // Data Mode
this->transferBuffer[1] = d;
this->i2c->write(this->i2cAddr, (const char *)this->transferBuffer, 2);
}
void SSD1306::sendCommand(uint8_t c)
{
this->transferBuffer[0] = 0; // Command Mode
this->transferBuffer[1] = c;
this->i2c->write(this->i2cAddr, (const char *)this->transferBuffer, 2);
}