forked from adafruit/Adafruit-SSD1351-library
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAdafruit_SSD1351.cpp
525 lines (397 loc) · 11.7 KB
/
Adafruit_SSD1351.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
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
/***************************************************
This is a library for the 1.5" & 1.27" 16-bit Color OLEDs
with SSD1331 driver chip
Pick one up today in the adafruit shop!
------> http://www.adafruit.com/products/1431
------> http://www.adafruit.com/products/1673
These displays use SPI to communicate, 4 or 5 pins are required to
interface
Adafruit invests time and resources providing this open source code,
please support Adafruit and open-source hardware by purchasing
products from Adafruit!
Written by Limor Fried/Ladyada for Adafruit Industries.
BSD license, all text above must be included in any redistribution
****************************************************/
#include "Adafruit_GFX.h"
#include "Adafruit_SSD1351.h"
#include "glcdfont.c"
#ifdef __AVR
#include <avr/pgmspace.h>
#elif defined(ESP8266)
#include <pgmspace.h>
#endif
#include "pins_arduino.h"
#include "wiring_private.h"
#include <SPI.h>
#ifndef _BV
#define _BV(bit) (1<<(bit))
#endif
/********************************** low level pin interface */
inline void Adafruit_SSD1351::spiwrite(uint8_t c) {
//Serial.println(c, HEX);
if (!_sid) {
SPI.transfer(c);
// might be able to make this even faster but
// a delay -is- required
delayMicroseconds(1);
return;
}
int8_t i;
*sclkport |= sclkpinmask;
for (i=7; i>=0; i--) {
*sclkport &= ~sclkpinmask;
//SCLK_PORT &= ~_BV(SCLK);
if (c & _BV(i)) {
*sidport |= sidpinmask;
//digitalWrite(_sid, HIGH);
//SID_PORT |= _BV(SID);
} else {
*sidport &= ~sidpinmask;
//digitalWrite(_sid, LOW);
//SID_PORT &= ~_BV(SID);
}
*sclkport |= sclkpinmask;
//SCLK_PORT |= _BV(SCLK);
}
}
void Adafruit_SSD1351::writeCommand(uint8_t c) {
*rsport &= ~ rspinmask;
//digitalWrite(_rs, LOW);
*csport &= ~ cspinmask;
//digitalWrite(_cs, LOW);
//Serial.print("C ");
spiwrite(c);
*csport |= cspinmask;
//digitalWrite(_cs, HIGH);
}
void Adafruit_SSD1351::writeData(uint8_t c) {
*rsport |= rspinmask;
//digitalWrite(_rs, HIGH);
*csport &= ~ cspinmask;
//digitalWrite(_cs, LOW);
// Serial.print("D ");
spiwrite(c);
*csport |= cspinmask;
//digitalWrite(_cs, HIGH);
}
/***********************************/
void Adafruit_SSD1351::goTo(int x, int y) {
if ((x >= SSD1351WIDTH) || (y >= SSD1351HEIGHT)) return;
// set x and y coordinate
writeCommand(SSD1351_CMD_SETCOLUMN);
writeData(x);
writeData(SSD1351WIDTH-1);
writeCommand(SSD1351_CMD_SETROW);
writeData(y);
writeData(SSD1351HEIGHT-1);
writeCommand(SSD1351_CMD_WRITERAM);
}
uint16_t Adafruit_SSD1351::Color565(uint8_t r, uint8_t g, uint8_t b) {
uint16_t c;
c = r >> 3;
c <<= 6;
c |= g >> 2;
c <<= 5;
c |= b >> 3;
return c;
}
void Adafruit_SSD1351::fillScreen(uint16_t fillcolor) {
fillRect(0, 0, SSD1351WIDTH, SSD1351HEIGHT, fillcolor);
}
// Draw a filled rectangle with no rotation.
void Adafruit_SSD1351::rawFillRect(uint16_t x, uint16_t y, uint16_t w, uint16_t h, uint16_t fillcolor) {
// Bounds check
if ((x >= SSD1351WIDTH) || (y >= SSD1351HEIGHT))
return;
// Y bounds check
if (y+h > SSD1351HEIGHT)
{
h = SSD1351HEIGHT - y - 1;
}
// X bounds check
if (x+w > SSD1351WIDTH)
{
w = SSD1351WIDTH - x - 1;
}
/*
Serial.print(x); Serial.print(", ");
Serial.print(y); Serial.print(", ");
Serial.print(w); Serial.print(", ");
Serial.print(h); Serial.println(", ");
*/
// set location
writeCommand(SSD1351_CMD_SETCOLUMN);
writeData(x);
writeData(x+w-1);
writeCommand(SSD1351_CMD_SETROW);
writeData(y);
writeData(y+h-1);
// fill!
writeCommand(SSD1351_CMD_WRITERAM);
for (uint16_t i=0; i < w*h; i++) {
writeData(fillcolor >> 8);
writeData(fillcolor);
}
}
/**************************************************************************/
/*!
@brief Draws a filled rectangle using HW acceleration
*/
/**************************************************************************/
void Adafruit_SSD1351::fillRect(uint16_t x, uint16_t y, uint16_t w, uint16_t h, uint16_t fillcolor) {
// Transform x and y based on current rotation.
switch (getRotation()) {
case 0: // No rotation
rawFillRect(x, y, w, h, fillcolor);
break;
case 1: // Rotated 90 degrees clockwise.
swap(x, y);
x = WIDTH - x - h;
rawFillRect(x, y, h, w, fillcolor);
break;
case 2: // Rotated 180 degrees clockwise.
x = WIDTH - x - w;
y = HEIGHT - y - h;
rawFillRect(x, y, w, h, fillcolor);
break;
case 3: // Rotated 270 degrees clockwise.
swap(x, y);
y = HEIGHT - y - w;
rawFillRect(x, y, h, w, fillcolor);
break;
}
}
// Draw a horizontal line ignoring any screen rotation.
void Adafruit_SSD1351::rawFastHLine(int16_t x, int16_t y, int16_t w, uint16_t color) {
// Bounds check
if ((x >= SSD1351WIDTH) || (y >= SSD1351HEIGHT))
return;
// X bounds check
if (x+w > SSD1351WIDTH)
{
w = SSD1351WIDTH - x - 1;
}
if (w < 0) return;
// set location
writeCommand(SSD1351_CMD_SETCOLUMN);
writeData(x);
writeData(x+w-1);
writeCommand(SSD1351_CMD_SETROW);
writeData(y);
writeData(y);
// fill!
writeCommand(SSD1351_CMD_WRITERAM);
for (uint16_t i=0; i < w; i++) {
writeData(color >> 8);
writeData(color);
}
}
// Draw a vertical line ignoring any screen rotation.
void Adafruit_SSD1351::rawFastVLine(int16_t x, int16_t y, int16_t h, uint16_t color) {
// Bounds check
if ((x >= SSD1351WIDTH) || (y >= SSD1351HEIGHT))
return;
// X bounds check
if (y+h > SSD1351HEIGHT)
{
h = SSD1351HEIGHT - y - 1;
}
if (h < 0) return;
// set location
writeCommand(SSD1351_CMD_SETCOLUMN);
writeData(x);
writeData(x);
writeCommand(SSD1351_CMD_SETROW);
writeData(y);
writeData(y+h-1);
// fill!
writeCommand(SSD1351_CMD_WRITERAM);
for (uint16_t i=0; i < h; i++) {
writeData(color >> 8);
writeData(color);
}
}
void Adafruit_SSD1351::drawFastVLine(int16_t x, int16_t y, int16_t h, uint16_t color) {
// Transform x and y based on current rotation.
switch (getRotation()) {
case 0: // No rotation
rawFastVLine(x, y, h, color);
break;
case 1: // Rotated 90 degrees clockwise.
swap(x, y);
x = WIDTH - x - h;
rawFastHLine(x, y, h, color);
break;
case 2: // Rotated 180 degrees clockwise.
x = WIDTH - x - 1;
y = HEIGHT - y - h;
rawFastVLine(x, y, h, color);
break;
case 3: // Rotated 270 degrees clockwise.
swap(x, y);
y = HEIGHT - y - 1;
rawFastHLine(x, y, h, color);
break;
}
}
void Adafruit_SSD1351::drawFastHLine(int16_t x, int16_t y, int16_t w, uint16_t color) {
// Transform x and y based on current rotation.
switch (getRotation()) {
case 0: // No rotation.
rawFastHLine(x, y, w, color);
break;
case 1: // Rotated 90 degrees clockwise.
swap(x, y);
x = WIDTH - x - 1;
rawFastVLine(x, y, w, color);
break;
case 2: // Rotated 180 degrees clockwise.
x = WIDTH - x - w;
y = HEIGHT - y - 1;
rawFastHLine(x, y, w, color);
break;
case 3: // Rotated 270 degrees clockwise.
swap(x, y);
y = HEIGHT - y - w;
rawFastVLine(x, y, w, color);
break;
}
}
void Adafruit_SSD1351::drawPixel(int16_t x, int16_t y, uint16_t color)
{
// Transform x and y based on current rotation.
switch (getRotation()) {
// Case 0: No rotation
case 1: // Rotated 90 degrees clockwise.
swap(x, y);
x = WIDTH - x - 1;
break;
case 2: // Rotated 180 degrees clockwise.
x = WIDTH - x - 1;
y = HEIGHT - y - 1;
break;
case 3: // Rotated 270 degrees clockwise.
swap(x, y);
y = HEIGHT - y - 1;
break;
}
// Bounds check.
if ((x >= SSD1351WIDTH) || (y >= SSD1351HEIGHT)) return;
if ((x < 0) || (y < 0)) return;
goTo(x, y);
// setup for data
*rsport |= rspinmask;
*csport &= ~ cspinmask;
spiwrite(color >> 8);
spiwrite(color);
*csport |= cspinmask;
}
void Adafruit_SSD1351::begin(void) {
// set pin directions
pinMode(_rs, OUTPUT);
if (_sclk) {
pinMode(_sclk, OUTPUT);
pinMode(_sid, OUTPUT);
} else {
// using the hardware SPI
SPI.begin();
SPI.setDataMode(SPI_MODE3);
}
// Toggle RST low to reset; CS low so it'll listen to us
pinMode(_cs, OUTPUT);
digitalWrite(_cs, LOW);
if (_rst) {
pinMode(_rst, OUTPUT);
digitalWrite(_rst, HIGH);
delay(500);
digitalWrite(_rst, LOW);
delay(500);
digitalWrite(_rst, HIGH);
delay(500);
}
// Initialization Sequence
writeCommand(SSD1351_CMD_COMMANDLOCK); // set command lock
writeData(0x12);
writeCommand(SSD1351_CMD_COMMANDLOCK); // set command lock
writeData(0xB1);
writeCommand(SSD1351_CMD_DISPLAYOFF); // 0xAE
writeCommand(SSD1351_CMD_CLOCKDIV); // 0xB3
writeCommand(0xF1); // 7:4 = Oscillator Frequency, 3:0 = CLK Div Ratio (A[3:0]+1 = 1..16)
writeCommand(SSD1351_CMD_MUXRATIO);
writeData(127);
writeCommand(SSD1351_CMD_SETREMAP);
writeData(0x74);
writeCommand(SSD1351_CMD_SETCOLUMN);
writeData(0x00);
writeData(0x7F);
writeCommand(SSD1351_CMD_SETROW);
writeData(0x00);
writeData(0x7F);
writeCommand(SSD1351_CMD_STARTLINE); // 0xA1
if (SSD1351HEIGHT == 96) {
writeData(96);
} else {
writeData(0);
}
writeCommand(SSD1351_CMD_DISPLAYOFFSET); // 0xA2
writeData(0x0);
writeCommand(SSD1351_CMD_SETGPIO);
writeData(0x00);
writeCommand(SSD1351_CMD_FUNCTIONSELECT);
writeData(0x01); // internal (diode drop)
//writeData(0x01); // external bias
// writeCommand(SSSD1351_CMD_SETPHASELENGTH);
// writeData(0x32);
writeCommand(SSD1351_CMD_PRECHARGE); // 0xB1
writeCommand(0x32);
writeCommand(SSD1351_CMD_VCOMH); // 0xBE
writeCommand(0x05);
writeCommand(SSD1351_CMD_NORMALDISPLAY); // 0xA6
writeCommand(SSD1351_CMD_CONTRASTABC);
writeData(0xC8);
writeData(0x80);
writeData(0xC8);
writeCommand(SSD1351_CMD_CONTRASTMASTER);
writeData(0x0F);
writeCommand(SSD1351_CMD_SETVSL );
writeData(0xA0);
writeData(0xB5);
writeData(0x55);
writeCommand(SSD1351_CMD_PRECHARGE2);
writeData(0x01);
writeCommand(SSD1351_CMD_DISPLAYON); //--turn on oled panel
}
void Adafruit_SSD1351::invert(boolean v) {
if (v) {
writeCommand(SSD1351_CMD_INVERTDISPLAY);
} else {
writeCommand(SSD1351_CMD_NORMALDISPLAY);
}
}
/********************************* low level pin initialization */
Adafruit_SSD1351::Adafruit_SSD1351(uint8_t cs, uint8_t rs, uint8_t sid, uint8_t sclk, uint8_t rst) : Adafruit_GFX(SSD1351WIDTH, SSD1351HEIGHT) {
_cs = cs;
_rs = rs;
_sid = sid;
_sclk = sclk;
_rst = rst;
csport = portOutputRegister(digitalPinToPort(cs));
cspinmask = digitalPinToBitMask(cs);
rsport = portOutputRegister(digitalPinToPort(rs));
rspinmask = digitalPinToBitMask(rs);
sidport = portOutputRegister(digitalPinToPort(sid));
sidpinmask = digitalPinToBitMask(sid);
sclkport = portOutputRegister(digitalPinToPort(sclk));
sclkpinmask = digitalPinToBitMask(sclk);
}
Adafruit_SSD1351::Adafruit_SSD1351(uint8_t cs, uint8_t rs, uint8_t rst) : Adafruit_GFX(SSD1351WIDTH, SSD1351HEIGHT) {
_cs = cs;
_rs = rs;
_sid = 0;
_sclk = 0;
_rst = rst;
csport = portOutputRegister(digitalPinToPort(cs));
cspinmask = digitalPinToBitMask(cs);
rsport = portOutputRegister(digitalPinToPort(rs));
rspinmask = digitalPinToBitMask(rs);
}