Skip to content

Commit d28e5d0

Browse files
committed
Merge pull request #26 from neptune2/master
Added support for SPI version of OLED display
2 parents a538e99 + bf182d4 commit d28e5d0

File tree

7 files changed

+104
-17
lines changed

7 files changed

+104
-17
lines changed

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
# esp8266-oled-ssd1306
22

33
This is a driver for the SSD1306 based 128x64 pixel OLED display running on the Arduino/ESP8266 platform.
4+
Can be used with either the I2C or SPI version of the display
45

56
You can either download this library as a zip file and unpack it to your Arduino/libraries folder or (once it has been added) choose it from the Arduino library manager.
67

78
## Credits
89
Many thanks go to Fabrice Weinberg (@FWeinb) for optimizing and refactoring the UI library.
910
The init sequence for the SSD1306 was inspired by Adafruits library for the same display.
11+
The SPI code was inspired by somhi/ESP_SSD1306 and the Adafruit library
1012

1113
## Usage
1214

@@ -187,3 +189,9 @@ This frame demonstrates the text alignment. The coordinates in the frame show re
187189
![DemoFrame4](https://github.com/squix78/esp8266-oled-ssd1306/raw/master/resources/DemoFrame4.jpg)
188190

189191
This shows how to use define a maximum width after which the driver automatically wraps a word to the next line. This comes in very handy if you have longer texts to display.
192+
193+
### SPI version
194+
195+
![SPIVersion](https://github.com/neptune2/esp8266-oled-ssd1306/raw/master/resources/SPI_version.jpg)
196+
197+
This shows the code working on the SPI version of the display. See demo code for ESP8266 pins used.

SSD1306.cpp

Lines changed: 59 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -23,21 +23,48 @@ SOFTWARE.
2323
See more at http://blog.squix.ch
2424
2525
Credits for parts of this code go to Mike Rankin. Thank you so much for sharing!
26+
27+
SPI additions by Neptune2
2628
*/
2729

2830
#include "SSD1306.h"
29-
#include <Wire.h>
30-
3131

32+
// constructor for I2C - we indicate i2cAddress, sda and sdc
3233
SSD1306::SSD1306(int i2cAddress, int sda, int sdc) {
3334
myI2cAddress = i2cAddress;
3435
mySda = sda;
3536
mySdc = sdc;
37+
I2C_io = true;
38+
}
39+
40+
// constructor for hardware SPI - we indicate Reset, DataCommand and ChipSelect
41+
// (HW_SPI used to differentiate constructor - reserved for future use)
42+
SSD1306::SSD1306(bool HW_SPI, int rst, int dc, int cs ) {
43+
myRST = rst;
44+
myDC = dc;
45+
myCS = cs;
46+
I2C_io = false;
3647
}
3748

3849
void SSD1306::init() {
39-
Wire.begin(mySda, mySdc);
40-
Wire.setClock(400000);
50+
if (I2C_io){
51+
Wire.begin(mySda, mySdc);
52+
Wire.setClock(400000);
53+
} else {
54+
pinMode(myDC, OUTPUT);
55+
pinMode(myCS, OUTPUT);
56+
57+
SPI.begin ();
58+
SPI.setClockDivider (SPI_CLOCK_DIV2);
59+
60+
pinMode(myRST, OUTPUT);
61+
// Pulse Reset low for 10ms
62+
digitalWrite(myRST, HIGH);
63+
delay(1);
64+
digitalWrite(myRST, LOW);
65+
delay(10);
66+
digitalWrite(myRST, HIGH);
67+
}
4168
sendInitCommands();
4269
resetDisplay();
4370
}
@@ -50,7 +77,11 @@ void SSD1306::resetDisplay(void) {
5077
}
5178

5279
void SSD1306::reconnect() {
53-
Wire.begin(mySda, mySdc);
80+
if (I2C_io){
81+
Wire.begin(mySda, mySdc);
82+
} else {
83+
SPI.begin ();
84+
}
5485
}
5586

5687
void SSD1306::displayOn(void) {
@@ -84,6 +115,8 @@ void SSD1306::display(void) {
84115
sendCommand(0x0);
85116
sendCommand(0x7);
86117

118+
119+
if (I2C_io) {
87120
for (uint16_t i=0; i<(128*64/8); i++) {
88121
// send a bunch of data in one xmission
89122
Wire.beginTransmission(myI2cAddress);
@@ -96,8 +129,15 @@ void SSD1306::display(void) {
96129
yield();
97130
Wire.endTransmission();
98131
}
99-
100-
132+
} else {
133+
digitalWrite(myCS, HIGH);
134+
digitalWrite(myDC, HIGH); // data mode
135+
digitalWrite(myCS, LOW);
136+
for (uint16_t i=0; i<(128*64/8); i++) {
137+
SPI.transfer(buffer[i]);
138+
}
139+
digitalWrite(myCS, HIGH);
140+
}
101141
}
102142

103143
void SSD1306::setPixel(int x, int y) {
@@ -316,10 +356,18 @@ void SSD1306::drawXbm(int x, int y, int width, int height, const char *xbm) {
316356
}
317357

318358
void SSD1306::sendCommand(unsigned char com) {
319-
Wire.beginTransmission(myI2cAddress); //begin transmitting
320-
Wire.write(0x80); //command mode
321-
Wire.write(com);
322-
Wire.endTransmission(); // stop transmitting
359+
if (I2C_io) {
360+
Wire.beginTransmission(myI2cAddress); //begin transmitting
361+
Wire.write(0x80); //command mode
362+
Wire.write(com);
363+
Wire.endTransmission(); // stop transmitting
364+
} else {
365+
digitalWrite(myCS, HIGH);
366+
digitalWrite(myDC, LOW); //command mode
367+
digitalWrite(myCS, LOW);
368+
SPI.transfer(com);
369+
digitalWrite(myCS, HIGH);
370+
}
323371
}
324372

325373
void SSD1306::sendInitCommands(void) {

SSD1306.h

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ Credits for parts of this code go to Mike Rankin. Thank you so much for sharing!
2727
#pragma once
2828

2929
#include <Arduino.h>
30+
#include <Wire.h>
31+
#include <SPI.h>
3032
#include "SSD1306Fonts.h"
3133

3234
#define BLACK 0
@@ -73,19 +75,28 @@ Credits for parts of this code go to Mike Rankin. Thank you so much for sharing!
7375
class SSD1306 {
7476

7577
private:
78+
// I2C
7679
int myI2cAddress;
7780
int mySda;
7881
int mySdc;
82+
bool I2C_io;
83+
84+
// SPI
85+
int myDC, myRST, myCS;
86+
7987
uint8_t buffer[128 * 64 / 8];
8088
int myTextAlignment = TEXT_ALIGN_LEFT;
8189
int myColor = WHITE;
8290
byte lastChar;
8391
const char *myFontData = ArialMT_Plain_10;
8492

8593
public:
86-
// Create the display object connected to pin sda and sdc
94+
// Create the display object connected to I2C pins pin sda and sdc
8795
SSD1306(int i2cAddress, int sda, int sdc);
8896

97+
// Create the display object connected to SPI pins and rst, dc and cs (HW_SPI reserved for future use)
98+
SSD1306(bool HW_SPI, int rst, int dc, int cs );
99+
89100
// Initialize the display
90101
void init();
91102

examples/SSD1306Demo/SSD1306Demo.ino

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,33 @@ SOFTWARE.
2323
See more at http://blog.squix.ch
2424
*/
2525
#include <Wire.h>
26+
#include <SPI.h>
2627
#include "SSD1306.h"
2728
#include "SSD1306Ui.h"
2829
#include "images.h"
2930

30-
// Initialize the oled display for address 0x3c
31-
// sda-pin=14 and sdc-pin=12
32-
SSD1306 display(0x3c, D3, D4);
31+
// Pin definitions for I2C
32+
#define OLED_SDA D2 // pin 14
33+
#define OLED_SDC D4 // pin 12
34+
#define OLED_ADDR 0x3C
35+
36+
/* Hardware Wemos D1 mini SPI pins
37+
D5 GPIO14 CLK - D0 pin OLED display
38+
D6 GPIO12 MISO (DIN) - not connected
39+
D7 GPIO13 MOSI (DOUT) - D1 pin OLED display
40+
D8 GPIO15 CS / SS - CS pin OLED display
41+
D0 GPIO16 RST - RST pin OLED display
42+
D2 GPIO4 DC - DC pin OLED
43+
*/
44+
45+
// Pin definitions for SPI
46+
#define OLED_RESET D0 // RESET
47+
#define OLED_DC D2 // Data/Command
48+
#define OLED_CS D8 // Chip select
49+
50+
// Uncomment one of the following based on OLED type
51+
// SSD1306 display(true, OLED_RESET, OLED_DC, OLED_CS); // FOR SPI
52+
SSD1306 display(OLED_ADDR, OLED_SDA, OLED_SDC); // For I2C
3353
SSD1306Ui ui ( &display );
3454

3555
// this array keeps function pointers to all frames

library.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,5 @@
1818
],
1919
"frameworks": "arduino",
2020
"platforms": "espressif",
21-
"version": "2.0.1"
21+
"version": "2.0.2"
2222
}

library.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name=ESP8266 Oled Driver for SSD1306 display
2-
version=2.0.0
2+
version=2.0.2
33
author=Daniel Eichhorn, Fabrice Weinberg
44
maintainer=Daniel Eichhorn <[email protected]>
55
sentence=A display driver for SSD1306 oled displays connected to an ESP8266

resources/SPI_version.jpg

26.3 KB
Loading

0 commit comments

Comments
 (0)