4
4
Pick one up today in the adafruit shop!
5
5
------> http://www.adafruit.com/products/684
6
6
7
- These displays use SPI to communicate, 4 or 5 pins are required to
8
- interface
7
+ These displays use hardware SPI to communicate
8
+
9
9
Adafruit invests time and resources providing this open source code,
10
10
please support Adafruit and open-source hardware by purchasing
11
11
products from Adafruit!
12
12
13
13
Written by Limor Fried/Ladyada for Adafruit Industries.
14
14
BSD license, all text above must be included in any redistribution
15
+
16
+ Feb 2017 - code modified by Jochen Peters (no-go, Deadlockz)
17
+ to run on a feather M0 / Arduino Zero (?) with hardware SPI
15
18
****************************************************/
16
19
17
20
#include " Adafruit_GFX.h"
31
34
/* ********************************* low level pin interface */
32
35
33
36
inline void Adafruit_SSD1331::spiwrite (uint8_t c) {
34
-
35
- if (!_sid) {
36
- SPI.transfer (c);
37
- return ;
38
- }
39
-
40
- int8_t i;
41
-
42
- *sclkportreg |= sclkpin;
43
-
44
- for (i=7 ; i>=0 ; i--) {
45
- *sclkportreg &= ~sclkpin;
46
- // SCLK_PORT &= ~_BV(SCLK);
47
-
48
- if (c & _BV (i)) {
49
- *sidportreg |= sidpin;
50
- // digitalWrite(_sid, HIGH);
51
- // SID_PORT |= _BV(SID);
52
- } else {
53
- *sidportreg &= ~sidpin;
54
- // digitalWrite(_sid, LOW);
55
- // SID_PORT &= ~_BV(SID);
56
- }
57
-
58
- *sclkportreg |= sclkpin;
59
- // SCLK_PORT |= _BV(SCLK);
60
- }
37
+ SPI.transfer (c);
38
+ return ;
61
39
}
62
40
63
41
64
42
void Adafruit_SSD1331::writeCommand (uint8_t c) {
65
- *rsportreg &= ~ rspin;
66
- // digitalWrite(_rs, LOW);
67
-
68
- *csportreg &= ~ cspin;
69
- // digitalWrite(_cs, LOW);
70
-
71
- // Serial.print("C ");
43
+ digitalWrite (_cs, HIGH);
44
+ digitalWrite (_rs, LOW);
45
+ digitalWrite (_cs, LOW);
72
46
spiwrite (c);
73
-
74
- *csportreg |= cspin;
75
- // digitalWrite(_cs, HIGH);
47
+ digitalWrite (_cs, HIGH);
76
48
}
77
49
78
50
79
51
void Adafruit_SSD1331::writeData (uint8_t c) {
80
- *rsportreg |= rspin;
81
- // digitalWrite(_rs, HIGH);
82
-
83
- *csportreg &= ~ cspin;
84
- // digitalWrite(_cs, LOW);
85
-
86
- // Serial.print("D ");
87
- spiwrite (c);
88
-
89
- *csportreg |= cspin;
90
- // digitalWrite(_cs, HIGH);
52
+ digitalWrite (_rs, HIGH);
53
+ digitalWrite (_cs, LOW);
54
+ spiwrite (c);
55
+ digitalWrite (_cs, HIGH);
91
56
}
92
57
93
58
/* **********************************/
@@ -149,7 +114,7 @@ void Adafruit_SSD1331::fillRect(uint16_t x, uint16_t y, uint16_t w, uint16_t h,
149
114
150
115
// Bounds check
151
116
if ((x >= TFTWIDTH) || (y >= TFTHEIGHT))
152
- return;
117
+ return;
153
118
154
119
// Y bounds check
155
120
if (y+h > TFTHEIGHT)
@@ -168,10 +133,10 @@ void Adafruit_SSD1331::fillRect(uint16_t x, uint16_t y, uint16_t w, uint16_t h,
168
133
writeCommand(0x01);
169
134
170
135
writeCommand(SSD1331_CMD_DRAWRECT);
171
- writeCommand(x & 0xFF); // Starting column
172
- writeCommand(y & 0xFF); // Starting row
173
- writeCommand((x+w-1) & 0xFF); // End column
174
- writeCommand((y+h-1) & 0xFF); // End row
136
+ writeCommand(x & 0xFF); // Starting column
137
+ writeCommand(y & 0xFF); // Starting row
138
+ writeCommand((x+w-1) & 0xFF); // End column
139
+ writeCommand((y+h-1) & 0xFF); // End row
175
140
176
141
// Outline color
177
142
writeCommand((uint8_t)((fillcolor >> 11) << 1));
@@ -187,7 +152,7 @@ void Adafruit_SSD1331::fillRect(uint16_t x, uint16_t y, uint16_t w, uint16_t h,
187
152
}
188
153
*/
189
154
190
- void Adafruit_SSD1331::drawLine (int16_t x0, int16_t y0, int16_t x1, int16_t y1, uint16_t color) {
155
+ void Adafruit_SSD1331::drawLine (int16_t x0, int16_t y0, int16_t x1, int16_t y1, uint16_t color) {
191
156
// check rotation, move pixel around if necessary
192
157
switch (getRotation ()) {
193
158
case 1 :
@@ -212,9 +177,9 @@ void Adafruit_SSD1331::drawLine(int16_t x0, int16_t y0, int16_t x1, int16_t y1,
212
177
213
178
// Boundary check
214
179
if ((y0 >= TFTHEIGHT) && (y1 >= TFTHEIGHT))
215
- return ;
180
+ return ;
216
181
if ((x0 >= TFTWIDTH) && (x1 >= TFTWIDTH))
217
- return ;
182
+ return ;
218
183
if (x0 >= TFTWIDTH)
219
184
x0 = TFTWIDTH - 1 ;
220
185
if (y0 >= TFTHEIGHT)
@@ -258,54 +223,49 @@ void Adafruit_SSD1331::drawPixel(int16_t x, int16_t y, uint16_t color)
258
223
259
224
goTo (x, y);
260
225
261
- // setup for data
262
- *rsportreg |= rspin;
263
- *csportreg &= ~ cspin;
264
-
226
+ digitalWrite (_rs, HIGH);
227
+ digitalWrite (_cs, LOW);
228
+
265
229
spiwrite (color >> 8 );
266
230
spiwrite (color);
267
231
268
- *csportreg |= cspin ;
232
+ digitalWrite (_cs, HIGH) ;
269
233
}
270
234
271
235
void Adafruit_SSD1331::pushColor (uint16_t color) {
272
236
// setup for data
273
- *rsportreg |= rspin ;
274
- *csportreg &= ~ cspin;
237
+ digitalWrite (_rs, HIGH) ;
238
+ digitalWrite (_cs, LOW);
275
239
276
240
spiwrite (color >> 8 );
277
241
spiwrite (color);
278
242
279
- *csportreg |= cspin;
243
+ digitalWrite (_cs, HIGH);
280
244
}
281
245
282
246
283
247
void Adafruit_SSD1331::begin (void ) {
284
248
// set pin directions
285
249
pinMode (_rs, OUTPUT);
286
250
287
- if (_sclk) {
288
- pinMode (_sclk, OUTPUT);
289
- sclkportreg = portOutputRegister (digitalPinToPort (_sclk));
290
- sclkpin = digitalPinToBitMask (_sclk);
291
-
292
- pinMode (_sid, OUTPUT);
293
- sidportreg = portOutputRegister (digitalPinToPort (_sid));
294
- sidpin = digitalPinToBitMask (_sid);
295
- } else {
296
251
// using the hardware SPI
297
252
SPI.begin ();
298
- SPI.setDataMode (SPI_MODE3);
299
- }
300
-
253
+ // SPI.setDataMode(SPI_MODE3);
254
+ #ifdef SPI_HAS_TRANSACTION
255
+ SPI.beginTransaction (SPISettings (8000000 , MSBFIRST, SPI_MODE0));
256
+ #else
257
+ SPI.setClockDivider (4 );
258
+ #endif
259
+
301
260
// Toggle RST low to reset; CS low so it'll listen to us
302
261
pinMode (_cs, OUTPUT);
303
262
digitalWrite (_cs, LOW);
263
+ digitalWrite (_rs, LOW);
304
264
cspin = digitalPinToBitMask (_cs);
305
- csportreg = portOutputRegister (digitalPinToPort (_cs));
265
+ // csportreg = portOutputRegister(digitalPinToPort(_cs));
306
266
307
267
rspin = digitalPinToBitMask (_rs);
308
- rsportreg = portOutputRegister (digitalPinToPort (_rs));
268
+ // rsportreg = portOutputRegister(digitalPinToPort(_rs));
309
269
310
270
if (_rst) {
311
271
pinMode (_rst, OUTPUT);
@@ -317,47 +277,47 @@ void Adafruit_SSD1331::begin(void) {
317
277
delay (500 );
318
278
}
319
279
// Initialization Sequence
320
- writeCommand (SSD1331_CMD_DISPLAYOFF); // 0xAE
321
- writeCommand (SSD1331_CMD_SETREMAP); // 0xA0
280
+ writeCommand (SSD1331_CMD_DISPLAYOFF); // 0xAE
281
+ writeCommand (SSD1331_CMD_SETREMAP); // 0xA0
322
282
#if defined SSD1331_COLORORDER_RGB
323
- writeCommand (0x72 ); // RGB Color
283
+ writeCommand (0x72 ); // RGB Color
324
284
#else
325
- writeCommand (0x76 ); // BGR Color
285
+ writeCommand (0x76 ); // BGR Color
326
286
#endif
327
- writeCommand (SSD1331_CMD_STARTLINE); // 0xA1
287
+ writeCommand (SSD1331_CMD_STARTLINE); // 0xA1
328
288
writeCommand (0x0 );
329
- writeCommand (SSD1331_CMD_DISPLAYOFFSET); // 0xA2
289
+ writeCommand (SSD1331_CMD_DISPLAYOFFSET); // 0xA2
330
290
writeCommand (0x0 );
331
- writeCommand (SSD1331_CMD_NORMALDISPLAY); // 0xA4
332
- writeCommand (SSD1331_CMD_SETMULTIPLEX); // 0xA8
333
- writeCommand (0x3F ); // 0x3F 1/64 duty
334
- writeCommand (SSD1331_CMD_SETMASTER); // 0xAD
291
+ writeCommand (SSD1331_CMD_NORMALDISPLAY); // 0xA4
292
+ writeCommand (SSD1331_CMD_SETMULTIPLEX); // 0xA8
293
+ writeCommand (0x3F ); // 0x3F 1/64 duty
294
+ writeCommand (SSD1331_CMD_SETMASTER); // 0xAD
335
295
writeCommand (0x8E );
336
- writeCommand (SSD1331_CMD_POWERMODE); // 0xB0
296
+ writeCommand (SSD1331_CMD_POWERMODE); // 0xB0
337
297
writeCommand (0x0B );
338
- writeCommand (SSD1331_CMD_PRECHARGE); // 0xB1
298
+ writeCommand (SSD1331_CMD_PRECHARGE); // 0xB1
339
299
writeCommand (0x31 );
340
- writeCommand (SSD1331_CMD_CLOCKDIV); // 0xB3
300
+ writeCommand (SSD1331_CMD_CLOCKDIV); // 0xB3
341
301
writeCommand (0xF0 ); // 7:4 = Oscillator Frequency, 3:0 = CLK Div Ratio (A[3:0]+1 = 1..16)
342
- writeCommand (SSD1331_CMD_PRECHARGEA); // 0x8A
302
+ writeCommand (SSD1331_CMD_PRECHARGEA); // 0x8A
343
303
writeCommand (0x64 );
344
- writeCommand (SSD1331_CMD_PRECHARGEB); // 0x8B
304
+ writeCommand (SSD1331_CMD_PRECHARGEB); // 0x8B
345
305
writeCommand (0x78 );
346
- writeCommand (SSD1331_CMD_PRECHARGEA); // 0x8C
306
+ writeCommand (SSD1331_CMD_PRECHARGEA); // 0x8C
347
307
writeCommand (0x64 );
348
- writeCommand (SSD1331_CMD_PRECHARGELEVEL); // 0xBB
308
+ writeCommand (SSD1331_CMD_PRECHARGELEVEL); // 0xBB
349
309
writeCommand (0x3A );
350
- writeCommand (SSD1331_CMD_VCOMH); // 0xBE
310
+ writeCommand (SSD1331_CMD_VCOMH); // 0xBE
351
311
writeCommand (0x3E );
352
- writeCommand (SSD1331_CMD_MASTERCURRENT); // 0x87
312
+ writeCommand (SSD1331_CMD_MASTERCURRENT); // 0x87
353
313
writeCommand (0x06 );
354
- writeCommand (SSD1331_CMD_CONTRASTA); // 0x81
314
+ writeCommand (SSD1331_CMD_CONTRASTA); // 0x81
355
315
writeCommand (0x91 );
356
- writeCommand (SSD1331_CMD_CONTRASTB); // 0x82
316
+ writeCommand (SSD1331_CMD_CONTRASTB); // 0x82
357
317
writeCommand (0x50 );
358
- writeCommand (SSD1331_CMD_CONTRASTC); // 0x83
318
+ writeCommand (SSD1331_CMD_CONTRASTC); // 0x83
359
319
writeCommand (0x7D );
360
- writeCommand (SSD1331_CMD_DISPLAYON); // --turn on oled panel
320
+ writeCommand (SSD1331_CMD_DISPLAYON); // --turn on oled panel
361
321
}
362
322
363
323
/* ******************************** low level pin initialization */
@@ -377,3 +337,4 @@ Adafruit_SSD1331::Adafruit_SSD1331(uint8_t cs, uint8_t rs, uint8_t rst) : Adafru
377
337
_sclk = 0 ;
378
338
_rst = rst;
379
339
}
340
+
0 commit comments