-
Notifications
You must be signed in to change notification settings - Fork 0
/
Adafruit_SSD1331.cpp
340 lines (289 loc) · 8.54 KB
/
Adafruit_SSD1331.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
/***************************************************
This is a library for the 0.96" 16-bit Color OLED with SSD1331 driver chip
Pick one up today in the adafruit shop!
------> http://www.adafruit.com/products/684
These displays use hardware SPI to communicate
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
Feb 2017 - code modified by Jochen Peters (no-go, Deadlockz)
to run on a feather M0 / Arduino Zero (?) with hardware SPI
****************************************************/
#include "Adafruit_GFX.h"
#include "Adafruit_SSD1331.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>
/********************************** low level pin interface */
inline void Adafruit_SSD1331::spiwrite(uint8_t c) {
SPI.transfer(c);
return;
}
void Adafruit_SSD1331::writeCommand(uint8_t c) {
digitalWrite(_cs, HIGH);
digitalWrite(_rs, LOW);
digitalWrite(_cs, LOW);
spiwrite(c);
digitalWrite(_cs, HIGH);
}
void Adafruit_SSD1331::writeData(uint8_t c) {
digitalWrite(_rs, HIGH);
digitalWrite(_cs, LOW);
spiwrite(c);
digitalWrite(_cs, HIGH);
}
/***********************************/
void Adafruit_SSD1331::goHome(void) {
goTo(0,0);
}
void Adafruit_SSD1331::goTo(int x, int y) {
if ((x >= WIDTH) || (y >= HEIGHT)) return;
// set x and y coordinate
writeCommand(SSD1331_CMD_SETCOLUMN);
writeCommand(x);
writeCommand(WIDTH-1);
writeCommand(SSD1331_CMD_SETROW);
writeCommand(y);
writeCommand(HEIGHT-1);
}
uint16_t Adafruit_SSD1331::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;
}
/**************************************************************************/
/*!
@brief Draws a filled rectangle using HW acceleration
*/
/**************************************************************************/
/*
void Adafruit_SSD1331::fillRect(uint16_t x, uint16_t y, uint16_t w, uint16_t h, uint16_t fillcolor)
{
//Serial.println("fillRect");
// check rotation, move rect around if necessary
switch (getRotation()) {
case 1:
swap(x, y);
swap(w, h);
x = WIDTH - x - 1;
break;
case 2:
x = WIDTH - x - 1;
y = HEIGHT - y - 1;
break;
case 3:
swap(x, y);
swap(w, h);
y = HEIGHT - y - 1;
break;
}
// Bounds check
if ((x >= TFTWIDTH) || (y >= TFTHEIGHT))
return;
// Y bounds check
if (y+h > TFTHEIGHT)
{
h = TFTHEIGHT - y;
}
// X bounds check
if (x+w > TFTWIDTH)
{
w = TFTWIDTH - x;
}
// fill!
writeCommand(SSD1331_CMD_FILL);
writeCommand(0x01);
writeCommand(SSD1331_CMD_DRAWRECT);
writeCommand(x & 0xFF); // Starting column
writeCommand(y & 0xFF); // Starting row
writeCommand((x+w-1) & 0xFF); // End column
writeCommand((y+h-1) & 0xFF); // End row
// Outline color
writeCommand((uint8_t)((fillcolor >> 11) << 1));
writeCommand((uint8_t)((fillcolor >> 5) & 0x3F));
writeCommand((uint8_t)((fillcolor << 1) & 0x3F));
// Fill color
writeCommand((uint8_t)((fillcolor >> 11) << 1));
writeCommand((uint8_t)((fillcolor >> 5) & 0x3F));
writeCommand((uint8_t)((fillcolor << 1) & 0x3F));
// Delay while the fill completes
delay(SSD1331_DELAYS_HWFILL);
}
*/
void Adafruit_SSD1331::drawLine(int16_t x0, int16_t y0, int16_t x1, int16_t y1, uint16_t color) {
// check rotation, move pixel around if necessary
switch (getRotation()) {
case 1:
gfx_swap(x0, y0);
gfx_swap(x1, y1);
x0 = WIDTH - x0 - 1;
x1 = WIDTH - x1 - 1;
break;
case 2:
x0 = WIDTH - x0 - 1;
y0 = HEIGHT - y0 - 1;
x1 = WIDTH - x1 - 1;
y1 = HEIGHT - y1 - 1;
break;
case 3:
gfx_swap(x0, y0);
gfx_swap(x1, y1);
y0 = HEIGHT - y0 - 1;
y1 = HEIGHT - y1 - 1;
break;
}
// Boundary check
if ((y0 >= TFTHEIGHT) && (y1 >= TFTHEIGHT))
return;
if ((x0 >= TFTWIDTH) && (x1 >= TFTWIDTH))
return;
if (x0 >= TFTWIDTH)
x0 = TFTWIDTH - 1;
if (y0 >= TFTHEIGHT)
y0 = TFTHEIGHT - 1;
if (x1 >= TFTWIDTH)
x1 = TFTWIDTH - 1;
if (y1 >= TFTHEIGHT)
y1 = TFTHEIGHT - 1;
writeCommand(SSD1331_CMD_DRAWLINE);
writeCommand(x0);
writeCommand(y0);
writeCommand(x1);
writeCommand(y1);
delay(SSD1331_DELAYS_HWLINE);
writeCommand((uint8_t)((color >> 11) << 1));
writeCommand((uint8_t)((color >> 5) & 0x3F));
writeCommand((uint8_t)((color << 1) & 0x3F));
delay(SSD1331_DELAYS_HWLINE);
}
void Adafruit_SSD1331::drawPixel(int16_t x, int16_t y, uint16_t color)
{
if ((x < 0) || (x >= width()) || (y < 0) || (y >= height())) return;
// check rotation, move pixel around if necessary
switch (getRotation()) {
case 1:
gfx_swap(x, y);
x = WIDTH - x - 1;
break;
case 2:
x = WIDTH - x - 1;
y = HEIGHT - y - 1;
break;
case 3:
gfx_swap(x, y);
y = HEIGHT - y - 1;
break;
}
goTo(x, y);
digitalWrite(_rs, HIGH);
digitalWrite(_cs, LOW);
spiwrite(color >> 8);
spiwrite(color);
digitalWrite(_cs, HIGH);
}
void Adafruit_SSD1331::pushColor(uint16_t color) {
// setup for data
digitalWrite(_rs, HIGH);
digitalWrite(_cs, LOW);
spiwrite(color >> 8);
spiwrite(color);
digitalWrite(_cs, HIGH);
}
void Adafruit_SSD1331::begin(void) {
// set pin directions
pinMode(_rs, OUTPUT);
// using the hardware SPI
SPI.begin();
//SPI.setDataMode(SPI_MODE3);
#ifdef SPI_HAS_TRANSACTION
SPI.beginTransaction(SPISettings(8000000, MSBFIRST, SPI_MODE0));
#else
SPI.setClockDivider (4);
#endif
// Toggle RST low to reset; CS low so it'll listen to us
pinMode(_cs, OUTPUT);
digitalWrite(_cs, LOW);
digitalWrite(_rs, LOW);
cspin = digitalPinToBitMask(_cs);
//csportreg = portOutputRegister(digitalPinToPort(_cs));
rspin = digitalPinToBitMask(_rs);
//rsportreg = portOutputRegister(digitalPinToPort(_rs));
if (_rst) {
pinMode(_rst, OUTPUT);
digitalWrite(_rst, HIGH);
delay(500);
digitalWrite(_rst, LOW);
delay(500);
digitalWrite(_rst, HIGH);
delay(500);
}
// Initialization Sequence
writeCommand(SSD1331_CMD_DISPLAYOFF); // 0xAE
writeCommand(SSD1331_CMD_SETREMAP); // 0xA0
#if defined SSD1331_COLORORDER_RGB
writeCommand(0x72); // RGB Color
#else
writeCommand(0x76); // BGR Color
#endif
writeCommand(SSD1331_CMD_STARTLINE); // 0xA1
writeCommand(0x0);
writeCommand(SSD1331_CMD_DISPLAYOFFSET); // 0xA2
writeCommand(0x0);
writeCommand(SSD1331_CMD_NORMALDISPLAY); // 0xA4
writeCommand(SSD1331_CMD_SETMULTIPLEX); // 0xA8
writeCommand(0x3F); // 0x3F 1/64 duty
writeCommand(SSD1331_CMD_SETMASTER); // 0xAD
writeCommand(0x8E);
writeCommand(SSD1331_CMD_POWERMODE); // 0xB0
writeCommand(0x0B);
writeCommand(SSD1331_CMD_PRECHARGE); // 0xB1
writeCommand(0x31);
writeCommand(SSD1331_CMD_CLOCKDIV); // 0xB3
writeCommand(0xF0); // 7:4 = Oscillator Frequency, 3:0 = CLK Div Ratio (A[3:0]+1 = 1..16)
writeCommand(SSD1331_CMD_PRECHARGEA); // 0x8A
writeCommand(0x64);
writeCommand(SSD1331_CMD_PRECHARGEB); // 0x8B
writeCommand(0x78);
writeCommand(SSD1331_CMD_PRECHARGEA); // 0x8C
writeCommand(0x64);
writeCommand(SSD1331_CMD_PRECHARGELEVEL); // 0xBB
writeCommand(0x3A);
writeCommand(SSD1331_CMD_VCOMH); // 0xBE
writeCommand(0x3E);
writeCommand(SSD1331_CMD_MASTERCURRENT); // 0x87
writeCommand(0x06);
writeCommand(SSD1331_CMD_CONTRASTA); // 0x81
writeCommand(0x91);
writeCommand(SSD1331_CMD_CONTRASTB); // 0x82
writeCommand(0x50);
writeCommand(SSD1331_CMD_CONTRASTC); // 0x83
writeCommand(0x7D);
writeCommand(SSD1331_CMD_DISPLAYON); //--turn on oled panel
}
/********************************* low level pin initialization */
Adafruit_SSD1331::Adafruit_SSD1331(uint8_t cs, uint8_t rs, uint8_t sid, uint8_t sclk, uint8_t rst) : Adafruit_GFX(TFTWIDTH, TFTHEIGHT) {
_cs = cs;
_rs = rs;
_sid = sid;
_sclk = sclk;
_rst = rst;
}
Adafruit_SSD1331::Adafruit_SSD1331(uint8_t cs, uint8_t rs, uint8_t rst) : Adafruit_GFX(TFTWIDTH, TFTHEIGHT) {
_cs = cs;
_rs = rs;
_sid = 0;
_sclk = 0;
_rst = rst;
}