Skip to content

Commit 23baef2

Browse files
committed
Added code for SPI version of the display
1 parent 20af1ca commit 23baef2

File tree

2 files changed

+71
-12
lines changed

2 files changed

+71
-12
lines changed

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

0 commit comments

Comments
 (0)