Skip to content

Commit 00af4d5

Browse files
authored
Merge pull request #77 from squix78/add-sh1106-spi
Add SH1106Spi support
2 parents 103f756 + 9bc04a5 commit 00af4d5

File tree

6 files changed

+210
-49
lines changed

6 files changed

+210
-49
lines changed

SH1106Spi.h

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
/**
2+
* The MIT License (MIT)
3+
*
4+
* Copyright (c) 2016 by Daniel Eichhorn
5+
* Copyright (c) 2016 by Fabrice Weinberg
6+
*
7+
* Permission is hereby granted, free of charge, to any person obtaining a copy
8+
* of this software and associated documentation files (the "Software"), to deal
9+
* in the Software without restriction, including without limitation the rights
10+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11+
* copies of the Software, and to permit persons to whom the Software is
12+
* furnished to do so, subject to the following conditions:
13+
*
14+
* The above copyright notice and this permission notice shall be included in all
15+
* copies or substantial portions of the Software.
16+
*
17+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23+
* SOFTWARE.
24+
*
25+
* Credits for parts of this code go to Mike Rankin. Thank you so much for sharing!
26+
*/
27+
28+
#ifndef SH1106Spi_h
29+
#define SH1106Spi_h
30+
31+
#include "OLEDDisplay.h"
32+
#include <SPI.h>
33+
34+
class SH1106Spi : public OLEDDisplay {
35+
private:
36+
uint8_t _rst;
37+
uint8_t _dc;
38+
39+
public:
40+
41+
SH1106Spi(uint8_t _rst, uint8_t _dc) {
42+
this->_rst = _rst;
43+
this->_dc = _dc;
44+
}
45+
46+
bool connect(){
47+
pinMode(_dc, OUTPUT);
48+
pinMode(_rst, OUTPUT);
49+
50+
SPI.begin ();
51+
SPI.setClockDivider (SPI_CLOCK_DIV2);
52+
53+
// Pulse Reset low for 10ms
54+
digitalWrite(_rst, HIGH);
55+
delay(1);
56+
digitalWrite(_rst, LOW);
57+
delay(10);
58+
digitalWrite(_rst, HIGH);
59+
return true;
60+
}
61+
62+
void display(void) {
63+
#ifdef OLEDDISPLAY_DOUBLE_BUFFER
64+
uint8_t minBoundY = ~0;
65+
uint8_t maxBoundY = 0;
66+
67+
uint8_t minBoundX = ~0;
68+
uint8_t maxBoundX = 0;
69+
70+
uint8_t x, y;
71+
72+
// Calculate the Y bounding box of changes
73+
// and copy buffer[pos] to buffer_back[pos];
74+
for (y = 0; y < (DISPLAY_HEIGHT / 8); y++) {
75+
for (x = 0; x < DISPLAY_WIDTH; x++) {
76+
uint16_t pos = x + y * DISPLAY_WIDTH;
77+
if (buffer[pos] != buffer_back[pos]) {
78+
minBoundY = _min(minBoundY, y);
79+
maxBoundY = _max(maxBoundY, y);
80+
minBoundX = _min(minBoundX, x);
81+
maxBoundX = _max(maxBoundX, x);
82+
}
83+
buffer_back[pos] = buffer[pos];
84+
}
85+
yield();
86+
}
87+
88+
// If the minBoundY wasn't updated
89+
// we can savely assume that buffer_back[pos] == buffer[pos]
90+
// holdes true for all values of pos
91+
if (minBoundY == ~0) return;
92+
93+
// Calculate the colum offset
94+
uint8_t minBoundXp2H = (minBoundX + 2) & 0x0F;
95+
uint8_t minBoundXp2L = 0x10 | ((minBoundX + 2) >> 4 );
96+
97+
for (y = minBoundY; y <= maxBoundY; y++) {
98+
sendCommand(0xB0 + y);
99+
sendCommand(minBoundXp2H);
100+
sendCommand(minBoundXp2L);
101+
digitalWrite(_dc, HIGH); // data mode
102+
for (x = minBoundX; x <= maxBoundX; x++) {
103+
SPI.transfer(buffer[x + y * DISPLAY_WIDTH]);
104+
}
105+
yield();
106+
}
107+
#else
108+
for (uint8_t y=0; y<DISPLAY_HEIGHT/8; y++) {
109+
sendCommand(0xB0 + y);
110+
sendCommand(0x02);
111+
sendCommand(0x10);
112+
digitalWrite(_dc, HIGH); // data mode
113+
for( uint8_t x=0; x < DISPLAY_WIDTH; x++) {
114+
SPI.transfer(buffer[x + y * DISPLAY_WIDTH]);
115+
}
116+
yield();
117+
}
118+
#endif
119+
}
120+
121+
private:
122+
inline void sendCommand(uint8_t com) __attribute__((always_inline)){
123+
digitalWrite(_dc, LOW);
124+
SPI.transfer(com);
125+
}
126+
};
127+
128+
#endif

examples/SSD1306ClockDemo/SSD1306ClockDemo.ino

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,19 +24,21 @@
2424
*
2525
*/
2626

27-
#include <Wire.h>
2827
#include <TimeLib.h>
2928

3029
// Include the correct display library
3130
// For a connection via I2C using Wire include
3231
#include <Wire.h> // Only needed for Arduino 1.6.5 and earlier
3332
#include "SSD1306.h" // alias for `#include "SSD1306Wire.h"`
33+
// or #include "SH1106.h" alis for `#include "SH1106Wire.h"`
3434
// For a connection via I2C using brzo_i2c (must be installed) include
3535
// #include <brzo_i2c.h> // Only needed for Arduino 1.6.5 and earlier
3636
// #include "SSD1306Brzo.h"
37+
// #include "SH1106Brzo.h"
3738
// For a connection via SPI include
3839
// #include <SPI.h> // Only needed for Arduino 1.6.5 and earlier
3940
// #include "SSD1306Spi.h"
41+
// #include "SH1106SPi.h"
4042

4143
// Include the UI lib
4244
#include "OLEDDisplayUi.h"
@@ -53,14 +55,19 @@
5355
// D2 -> DC
5456
// D8 -> CS
5557
// SSD1306Spi display(D0, D2, D8);
58+
// or
59+
// SH1106Spi display(D0, D2);
5660

5761
// Initialize the OLED display using brzo_i2c
5862
// D3 -> SDA
59-
// D4 -> SCL
63+
// D5 -> SCL
6064
// SSD1306Brzo display(0x3c, D3, D5);
65+
// or
66+
// SH1106Brzo display(0x3c, D3, D5);
6167

6268
// Initialize the OLED display using Wire library
6369
SSD1306 display(0x3c, D3, D5);
70+
// SH1106 display(0x3c, D3, D5);
6471

6572
OLEDDisplayUi ui ( &display );
6673

examples/SSD1306DrawingDemo/SSD1306DrawingDemo.ino

Lines changed: 36 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -24,34 +24,42 @@
2424
*
2525
*/
2626

27-
// Include the correct display library
28-
// For a connection via I2C using Wire include
29-
#include <Wire.h> // Only needed for Arduino 1.6.5 and earlier
30-
#include "SSD1306.h" // alias for `#include "SSD1306Wire.h"`
31-
// For a connection via I2C using brzo_i2c (must be installed) include
32-
// #include <brzo_i2c.h> // Only needed for Arduino 1.6.5 and earlier
33-
// #include "SSD1306Brzo.h"
34-
// For a connection via SPI include
35-
// #include <SPI.h> // Only needed for Arduino 1.6.5 and earlier
36-
// #include "SSD1306Spi.h"
37-
38-
// Use the corresponding display class:
39-
40-
// Initialize the OLED display using SPI
41-
// D5 -> CLK
42-
// D7 -> MOSI (DOUT)
43-
// D0 -> RES
44-
// D2 -> DC
45-
// D8 -> CS
46-
// SSD1306Spi display(D0, D2, D8);
47-
48-
// Initialize the OLED display using brzo_i2c
49-
// D3 -> SDA
50-
// D4 -> SCL
51-
// SSD1306Brzo display(0x3c, D3, D5);
52-
53-
// Initialize the OLED display using Wire library
54-
SSD1306 display(0x3c, D3, D5);
27+
// Include the correct display library
28+
// For a connection via I2C using Wire include
29+
#include <Wire.h> // Only needed for Arduino 1.6.5 and earlier
30+
#include "SSD1306.h" // alias for `#include "SSD1306Wire.h"`
31+
// or #include "SH1106.h" alis for `#include "SH1106Wire.h"`
32+
// For a connection via I2C using brzo_i2c (must be installed) include
33+
// #include <brzo_i2c.h> // Only needed for Arduino 1.6.5 and earlier
34+
// #include "SSD1306Brzo.h"
35+
// #include "SH1106Brzo.h"
36+
// For a connection via SPI include
37+
// #include <SPI.h> // Only needed for Arduino 1.6.5 and earlier
38+
// #include "SSD1306Spi.h"
39+
// #include "SH1106SPi.h"
40+
41+
// Use the corresponding display class:
42+
43+
// Initialize the OLED display using SPI
44+
// D5 -> CLK
45+
// D7 -> MOSI (DOUT)
46+
// D0 -> RES
47+
// D2 -> DC
48+
// D8 -> CS
49+
// SSD1306Spi display(D0, D2, D8);
50+
// or
51+
// SH1106Spi display(D0, D2);
52+
53+
// Initialize the OLED display using brzo_i2c
54+
// D3 -> SDA
55+
// D5 -> SCL
56+
// SSD1306Brzo display(0x3c, D3, D5);
57+
// or
58+
// SH1106Brzo display(0x3c, D3, D5);
59+
60+
// Initialize the OLED display using Wire library
61+
SSD1306 display(0x3c, D3, D5);
62+
// SH1106 display(0x3c, D3, D5);
5563

5664
// Adapted from Adafruit_SSD1306
5765
void drawLines() {

examples/SSD1306OTADemo/SSD1306OTADemo.ino

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,15 @@
3939
// For a connection via I2C using Wire include
4040
#include <Wire.h> // Only needed for Arduino 1.6.5 and earlier
4141
#include "SSD1306.h" // alias for `#include "SSD1306Wire.h"`
42+
// or #include "SH1106.h" alis for `#include "SH1106Wire.h"`
4243
// For a connection via I2C using brzo_i2c (must be installed) include
4344
// #include <brzo_i2c.h> // Only needed for Arduino 1.6.5 and earlier
4445
// #include "SSD1306Brzo.h"
46+
// #include "SH1106Brzo.h"
4547
// For a connection via SPI include
4648
// #include <SPI.h> // Only needed for Arduino 1.6.5 and earlier
4749
// #include "SSD1306Spi.h"
50+
// #include "SH1106SPi.h"
4851

4952
// Use the corresponding display class:
5053

@@ -55,14 +58,19 @@
5558
// D2 -> DC
5659
// D8 -> CS
5760
// SSD1306Spi display(D0, D2, D8);
61+
// or
62+
// SH1106Spi display(D0, D2);
5863

5964
// Initialize the OLED display using brzo_i2c
6065
// D3 -> SDA
61-
// D4 -> SCL
66+
// D5 -> SCL
6267
// SSD1306Brzo display(0x3c, D3, D5);
68+
// or
69+
// SH1106Brzo display(0x3c, D3, D5);
6370

6471
// Initialize the OLED display using Wire library
6572
SSD1306 display(0x3c, D3, D5);
73+
// SH1106 display(0x3c, D3, D5);
6674

6775

6876
void setup() {

examples/SSD1306SimpleDemo/SSD1306SimpleDemo.ino

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,39 +23,43 @@
2323
*
2424
*/
2525

26-
#include <Wire.h>
27-
2826
// Include the correct display library
2927
// For a connection via I2C using Wire include
3028
#include <Wire.h> // Only needed for Arduino 1.6.5 and earlier
3129
#include "SSD1306.h" // alias for `#include "SSD1306Wire.h"`
30+
// or #include "SH1106.h" alis for `#include "SH1106Wire.h"`
3231
// For a connection via I2C using brzo_i2c (must be installed) include
3332
// #include <brzo_i2c.h> // Only needed for Arduino 1.6.5 and earlier
3433
// #include "SSD1306Brzo.h"
34+
// #include "SH1106Brzo.h"
3535
// For a connection via SPI include
3636
// #include <SPI.h> // Only needed for Arduino 1.6.5 and earlier
3737
// #include "SSD1306Spi.h"
38+
// #include "SH1106SPi.h"
3839

3940
// Include custom images
4041
#include "images.h"
4142

42-
// Use the corresponding display class:
43-
4443
// Initialize the OLED display using SPI
4544
// D5 -> CLK
4645
// D7 -> MOSI (DOUT)
4746
// D0 -> RES
4847
// D2 -> DC
4948
// D8 -> CS
5049
// SSD1306Spi display(D0, D2, D8);
50+
// or
51+
// SH1106Spi display(D0, D2);
5152

5253
// Initialize the OLED display using brzo_i2c
5354
// D3 -> SDA
54-
// D4 -> SCL
55+
// D5 -> SCL
5556
// SSD1306Brzo display(0x3c, D3, D5);
57+
// or
58+
// SH1106Brzo display(0x3c, D3, D5);
5659

5760
// Initialize the OLED display using Wire library
5861
SSD1306 display(0x3c, D3, D5);
62+
// SH1106 display(0x3c, D3, D5);
5963

6064

6165
#define DEMO_DURATION 3000

examples/SSD1306UiDemo/SSD1306UiDemo.ino

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -24,18 +24,19 @@
2424
*
2525
*/
2626

27-
#include <Wire.h>
28-
29-
// Include the correct display library
30-
// For a connection via I2C using Wire include
31-
#include <Wire.h> // Only needed for Arduino 1.6.5 and earlier
32-
#include "SSD1306.h" // alias for `#include "SSD1306Wire.h"`
33-
// For a connection via I2C using brzo_i2c (must be installed) include
34-
// #include <brzo_i2c.h> // Only needed for Arduino 1.6.5 and earlier
35-
// #include "SSD1306Brzo.h"
36-
// For a connection via SPI include
37-
// #include <SPI.h> // Only needed for Arduino 1.6.5 and earlier
38-
// #include "SSD1306Spi.h"
27+
// Include the correct display library
28+
// For a connection via I2C using Wire include
29+
#include <Wire.h> // Only needed for Arduino 1.6.5 and earlier
30+
#include "SSD1306.h" // alias for `#include "SSD1306Wire.h"`
31+
// or #include "SH1106.h" alis for `#include "SH1106Wire.h"`
32+
// For a connection via I2C using brzo_i2c (must be installed) include
33+
// #include <brzo_i2c.h> // Only needed for Arduino 1.6.5 and earlier
34+
// #include "SSD1306Brzo.h"
35+
// #include "SH1106Brzo.h"
36+
// For a connection via SPI include
37+
// #include <SPI.h> // Only needed for Arduino 1.6.5 and earlier
38+
// #include "SSD1306Spi.h"
39+
// #include "SH1106SPi.h"
3940

4041
// Include the UI lib
4142
#include "OLEDDisplayUi.h"
@@ -52,14 +53,19 @@
5253
// D2 -> DC
5354
// D8 -> CS
5455
// SSD1306Spi display(D0, D2, D8);
56+
// or
57+
// SH1106Spi display(D0, D2);
5558

5659
// Initialize the OLED display using brzo_i2c
5760
// D3 -> SDA
58-
// D4 -> SCL
61+
// D5 -> SCL
5962
// SSD1306Brzo display(0x3c, D3, D5);
63+
// or
64+
// SH1106Brzo display(0x3c, D3, D5);
6065

6166
// Initialize the OLED display using Wire library
6267
SSD1306 display(0x3c, D3, D5);
68+
// SH1106 display(0x3c, D3, D5);
6369

6470
OLEDDisplayUi ui ( &display );
6571

0 commit comments

Comments
 (0)