@@ -16,18 +16,29 @@ BSD license, check license.txt for more information
1616All text above, and the splash screen below must be included in any redistribution
1717*********************************************************************/
1818
19- // #include <Wire.h>
20- #include < avr/pgmspace.h>
21- #include < util/delay.h>
22- #include < stdlib.h>
19+ #ifdef __AVR__
20+ #include < avr/pgmspace.h>
21+ #include < util/delay.h>
22+ #elif defined(ESP8266)
23+ #include < pgmspace.h>
24+ #else
25+ #define pgm_read_byte (addr ) (*(const unsigned char *)(addr))
26+ #endif
2327
28+ #include < stdlib.h>
29+ #include < SPI.h>
2430#include " Adafruit_GFX.h"
2531#include " Adafruit_SSD1325.h"
26-
2732#include " glcdfont.c"
2833
34+ #ifdef SPI_HAS_TRANSACTION
35+ SPISettings oledspi = SPISettings(4000000 , MSBFIRST, SPI_MODE0);
36+ #else
37+ #define ADAFRUIT_SSD1325_SPI SPI_CLOCK_DIV2
38+ #endif
39+
2940// a 5x7 font table
30- extern uint8_t PROGMEM font[];
41+ extern const uint8_t PROGMEM font[];
3142
3243// the memory buffer for the LCD
3344
@@ -99,25 +110,27 @@ static uint8_t buffer[SSD1325_LCDHEIGHT * SSD1325_LCDWIDTH / 8] = {
99110};
100111
101112// the most basic function, set a single pixel
102- void Adafruit_SSD1325::drawPixel (uint16_t x, uint16_t y, uint16_t color) {
103- if ((x >= width ()) || (y >= height ()))
113+ void Adafruit_SSD1325::drawPixel (int16_t x, int16_t y, uint16_t color) {
114+ if ((x >= width ()) || (y >= height ()) || (x < 0 ) || (y < 0 ) )
104115 return ;
105116
106117 // check rotation, move pixel around if necessary
107118 switch (getRotation ()) {
108119 case 1 :
109- swap (x, y);
120+ AdaGFX_swap (x, y);
110121 x = WIDTH - x - 1 ;
111122 break ;
112123 case 2 :
113124 x = WIDTH - x - 1 ;
114125 y = HEIGHT - y - 1 ;
115126 break ;
116127 case 3 :
117- swap (x, y);
128+ AdaGFX_swap (x, y);
118129 y = HEIGHT - y - 1 ;
119130 break ;
120131 }
132+
133+ // Serial.print("("); Serial.print(x); Serial.print(","); Serial.print(y); Serial.println(")");
121134
122135 // x is which column
123136 if (color == WHITE)
@@ -127,23 +140,25 @@ void Adafruit_SSD1325::drawPixel(uint16_t x, uint16_t y, uint16_t color) {
127140}
128141
129142void Adafruit_SSD1325::begin (void ) {
130- constructor (128 , 64 );
131-
132143 // set pin directions
133- pinMode (sid, OUTPUT);
134- pinMode (sclk, OUTPUT);
144+ if (sclk != -1 ) {
145+ pinMode (sid, OUTPUT);
146+ pinMode (sclk, OUTPUT);
147+ #ifdef __AVR__
148+ clkport = portOutputRegister (digitalPinToPort (sclk));
149+ clkpinmask = digitalPinToBitMask (sclk);
150+ mosiport = portOutputRegister (digitalPinToPort (sid));
151+ mosipinmask = digitalPinToBitMask (sid);
152+ #endif
153+ } else {
154+ // hardware SPI
155+ SPI.begin ();
156+ }
157+
135158 pinMode (dc, OUTPUT);
136159 pinMode (rst, OUTPUT);
137160 pinMode (cs, OUTPUT);
138161
139- clkport = portOutputRegister (digitalPinToPort (sclk));
140- clkpinmask = digitalPinToBitMask (sclk);
141- mosiport = portOutputRegister (digitalPinToPort (sid));
142- mosipinmask = digitalPinToBitMask (sid);
143- csport = portOutputRegister (digitalPinToPort (cs));
144- cspinmask = digitalPinToBitMask (cs);
145- dcport = portOutputRegister (digitalPinToPort (dc));
146- dcpinmask = digitalPinToBitMask (dc);
147162
148163 digitalWrite (rst, HIGH);
149164 // VDD (3.3V) goes high at start, lets just chill for a ms
@@ -155,6 +170,8 @@ void Adafruit_SSD1325::begin(void) {
155170 // bring out of reset
156171 digitalWrite (rst, HIGH);
157172
173+ Serial.println (" reset" );
174+ delay (500 );
158175 command (SSD1325_DISPLAYOFF); /* display off */
159176 command (SSD1325_SETCLOCK); /* set osc division */
160177 command (0xF1 ); /* 145 */
@@ -219,27 +236,49 @@ void Adafruit_SSD1325::invertDisplay(uint8_t i) {
219236}
220237
221238void Adafruit_SSD1325::command (uint8_t c) {
222- // digitalWrite(cs, HIGH);
223- *csport |= cspinmask;
224- // digitalWrite(dc, LOW);
225- *dcport &= ~dcpinmask;
226- // digitalWrite(cs, LOW);
227- *csport &= ~cspinmask;
228- fastSPIwrite (c);
229- // digitalWrite(cs, HIGH);
230- *csport |= cspinmask;
239+ digitalWrite (cs, HIGH);
240+ digitalWrite (dc, LOW);
241+ delay (1 );
242+ if (sclk == -1 ) {
243+ #ifdef SPI_HAS_TRANSACTION
244+ SPI.beginTransaction (oledspi);
245+ #else
246+ SPI.setDataMode (SPI_MODE0);
247+ SPI.setClockDivider (ADAFRUIT_SSD1325_SPI);
248+ #endif
249+ }
250+
251+ digitalWrite (cs, LOW);
252+ spixfer (c);
253+ digitalWrite (cs, HIGH);
254+
255+ #ifdef SPI_HAS_TRANSACTION
256+ if (sclk == -1 )
257+ SPI.endTransaction (); // release the SPI bus
258+ #endif
231259}
232260
233261void Adafruit_SSD1325::data (uint8_t c) {
234- // digitalWrite(cs, HIGH);
235- *csport |= cspinmask;
236- // digitalWrite(dc, HIGH);
237- *dcport |= dcpinmask;
238- // digitalWrite(cs, LOW);
239- *csport &= ~cspinmask;
240- fastSPIwrite (c);
241- // digitalWrite(cs, HIGH);
242- *csport |= cspinmask;
262+ digitalWrite (cs, HIGH);
263+ digitalWrite (dc, HIGH);
264+
265+ if (sclk == -1 ) {
266+ #ifdef SPI_HAS_TRANSACTION
267+ SPI.beginTransaction (oledspi);
268+ #else
269+ SPI.setDataMode (SPI_MODE0);
270+ SPI.setClockDivider (ADAFRUIT_SSD1325_SPI);
271+ #endif
272+ }
273+
274+ digitalWrite (cs, LOW);
275+ spixfer (c);
276+ digitalWrite (cs, HIGH);
277+
278+ #ifdef SPI_HAS_TRANSACTION
279+ if (sclk == -1 )
280+ SPI.endTransaction (); // release the SPI bus
281+ #endif
243282}
244283
245284void Adafruit_SSD1325::display (void ) {
@@ -251,9 +290,19 @@ void Adafruit_SSD1325::display(void) {
251290 command (0x00 ); /* set row start address */
252291 command (0x3f ); /* set row end address */
253292
254- *csport |= cspinmask;
255- *dcport |= dcpinmask;
256- *csport &= ~cspinmask;
293+ if (sclk == -1 ) {
294+ #ifdef SPI_HAS_TRANSACTION
295+ SPI.beginTransaction (oledspi);
296+ #else
297+ SPI.setDataMode (SPI_MODE0);
298+ SPI.setClockDivider (ADAFRUIT_SSD1325_SPI);
299+ #endif
300+ }
301+
302+ digitalWrite (cs, HIGH);
303+ digitalWrite (dc, HIGH);
304+ digitalWrite (cs, LOW);
305+ delay (1 );
257306
258307 for (uint16_t x=0 ; x<128 ; x+=2 ) {
259308 for (uint16_t y=0 ; y<64 ; y+=8 ) { // we write 8 pixels at once
@@ -263,11 +312,17 @@ void Adafruit_SSD1325::display(void) {
263312 uint8_t d = 0 ;
264313 if (left8 & (1 << p)) d |= 0xF0 ;
265314 if (right8 & (1 << p)) d |= 0x0F ;
266- fastSPIwrite (d);
315+ spixfer (d);
267316 }
268317 }
269318 }
270- *csport |= cspinmask;
319+
320+ digitalWrite (cs, HIGH);
321+
322+ #ifdef SPI_HAS_TRANSACTION
323+ if (sclk == -1 )
324+ SPI.endTransaction (); // release the SPI bus
325+ #endif
271326}
272327
273328// clear everything
@@ -276,26 +331,25 @@ void Adafruit_SSD1325::clearDisplay(void) {
276331}
277332
278333
279- inline void Adafruit_SSD1325::fastSPIwrite (uint8_t d) {
280-
334+ void Adafruit_SSD1325::spixfer (uint8_t x) {
335+ if (sclk == -1 ) {
336+ SPI.transfer (x);
337+ return ;
338+ }
339+ // software spi
340+ // Serial.println("Software SPI");
341+
281342 for (uint8_t bit = 0x80 ; bit; bit >>= 1 ) {
343+ #if defined(AVR)
282344 *clkport &= ~clkpinmask;
283- if (d & bit) *mosiport |= mosipinmask;
345+ if (x & bit) *mosiport |= mosipinmask;
284346 else *mosiport &= ~mosipinmask;
285347 *clkport |= clkpinmask;
348+ #else
349+ digitalWrite (sclk, LOW);
350+ if (x & bit) digitalWrite (sid, HIGH);
351+ else digitalWrite (sid, LOW);
352+ digitalWrite (sclk, HIGH);
353+ #endif
286354 }
287- // *csport |= cspinmask;
288355}
289-
290- inline void Adafruit_SSD1325::slowSPIwrite (uint8_t d) {
291- for (int8_t i=7 ; i>=0 ; i--) {
292- digitalWrite (sclk, LOW);
293- if (d & _BV (i)) {
294- digitalWrite (sid, HIGH);
295- } else {
296- digitalWrite (sid, LOW);
297- }
298- digitalWrite (sclk, HIGH);
299- }
300- }
301-
0 commit comments