Skip to content

Commit 8fc51db

Browse files
committed
Add support for seamless multi display support
1 parent 1254ef9 commit 8fc51db

File tree

3 files changed

+209
-0
lines changed

3 files changed

+209
-0
lines changed

SSD1306Wire.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ class SSD1306Wire : public OLEDDisplay {
3636
uint8_t _address;
3737
uint8_t _sda;
3838
uint8_t _scl;
39+
bool _doI2cAutoInit = false;
3940

4041
public:
4142
SSD1306Wire(uint8_t _address, uint8_t _sda, uint8_t _scl) : SSD1306Wire(GEOMETRY_128_64, _address, _sda, _scl) {
@@ -68,6 +69,7 @@ class SSD1306Wire : public OLEDDisplay {
6869
}
6970

7071
void display(void) {
72+
initI2cIfNeccesary();
7173
const int x_offset = (128 - this->width()) / 2;
7274
#ifdef OLEDDISPLAY_DOUBLE_BUFFER
7375
uint8_t minBoundY = ~0;
@@ -157,14 +159,24 @@ class SSD1306Wire : public OLEDDisplay {
157159
#endif
158160
}
159161

162+
void setI2cAutoInit(bool doI2cAutoInit) {
163+
_doI2cAutoInit = doI2cAutoInit;
164+
}
165+
160166
private:
161167
inline void sendCommand(uint8_t command) __attribute__((always_inline)){
168+
initI2cIfNeccesary();
162169
Wire.beginTransmission(_address);
163170
Wire.write(0x80);
164171
Wire.write(command);
165172
Wire.endTransmission();
166173
}
167174

175+
void initI2cIfNeccesary() {
176+
if (_doI2cAutoInit) {
177+
Wire.begin(this->_sda, this->_scl);
178+
}
179+
}
168180

169181
};
170182

Lines changed: 169 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,169 @@
1+
/**
2+
* The MIT License (MIT)
3+
*
4+
* Copyright (c) 2018 by ThingPulse, Daniel Eichhorn
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in all
14+
* copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
* SOFTWARE.
23+
*
24+
* ThingPulse invests considerable time and money to develop these open source libraries.
25+
* Please support us by buying our products (and not the clones) from
26+
* https://thingpulse.com
27+
*
28+
*/
29+
30+
// Include the correct display library
31+
// For a connection via I2C using Wire include
32+
#include <Wire.h> // Only needed for Arduino 1.6.5 and earlier
33+
#include "SSD1306.h" // alias for `#include "SSD1306Wire.h"`
34+
#include "images.h"
35+
36+
// Initialize the OLED display using Wire library
37+
SSD1306 display(GEOMETRY_128_64, 0x3c, D3, D5);
38+
SSD1306 display2(GEOMETRY_128_64, 0x3c, D1, D2);
39+
40+
41+
#define DEMO_DURATION 3000
42+
typedef void (*Demo)(SSD1306 *display);
43+
44+
int demoMode = 0;
45+
int counter = 1;
46+
47+
void setup() {
48+
Serial.begin(115200);
49+
Serial.println();
50+
Serial.println();
51+
52+
53+
// Initialising the UI will init the display too.
54+
display.init();
55+
display.setI2cAutoInit(true);
56+
display2.setI2cAutoInit(true);
57+
58+
59+
display.flipScreenVertically();
60+
display.setFont(ArialMT_Plain_10);
61+
62+
display2.init();
63+
//display2.setFont(ArialMT_Plain_10);
64+
65+
}
66+
67+
void drawFontFaceDemo(SSD1306 *d) {
68+
// Font Demo1
69+
// create more fonts at http://oleddisplay.squix.ch/
70+
d->setTextAlignment(TEXT_ALIGN_LEFT);
71+
d->setFont(ArialMT_Plain_10);
72+
d->drawString(0, 0, "Hello world");
73+
d->setFont(ArialMT_Plain_16);
74+
d->drawString(0, 10, "Hello world");
75+
d->setFont(ArialMT_Plain_24);
76+
d->drawString(0, 26, "Hello world");
77+
}
78+
79+
void drawTextFlowDemo(SSD1306 *d) {
80+
d->setFont(ArialMT_Plain_10);
81+
d->setTextAlignment(TEXT_ALIGN_LEFT);
82+
d->drawStringMaxWidth(0, 0, 128,
83+
"Lorem ipsum\n dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore." );
84+
}
85+
86+
void drawTextAlignmentDemo(SSD1306 *d) {
87+
// Text alignment demo
88+
d->setFont(ArialMT_Plain_10);
89+
90+
// The coordinates define the left starting point of the text
91+
d->setTextAlignment(TEXT_ALIGN_LEFT);
92+
d->drawString(0, 10, "Left aligned (0,10)");
93+
94+
// The coordinates define the center of the text
95+
d->setTextAlignment(TEXT_ALIGN_CENTER);
96+
d->drawString(64, 22, "Center aligned (64,22)");
97+
98+
// The coordinates define the right end of the text
99+
d->setTextAlignment(TEXT_ALIGN_RIGHT);
100+
d->drawString(128, 33, "Right aligned (128,33)");
101+
}
102+
103+
void drawRectDemo(SSD1306 *d) {
104+
// Draw a pixel at given position
105+
for (int i = 0; i < 10; i++) {
106+
d->setPixel(i, i);
107+
d->setPixel(10 - i, i);
108+
}
109+
d->drawRect(12, 12, 20, 20);
110+
111+
// Fill the rectangle
112+
d->fillRect(14, 14, 17, 17);
113+
114+
// Draw a line horizontally
115+
d->drawHorizontalLine(0, 40, 20);
116+
117+
// Draw a line horizontally
118+
d->drawVerticalLine(40, 0, 20);
119+
}
120+
121+
void drawCircleDemo(SSD1306 *d) {
122+
for (int i=1; i < 8; i++) {
123+
d->setColor(WHITE);
124+
d->drawCircle(32, 32, i*3);
125+
if (i % 2 == 0) {
126+
d->setColor(BLACK);
127+
}
128+
d->fillCircle(96, 32, 32 - i* 3);
129+
}
130+
}
131+
132+
void drawProgressBarDemo(SSD1306 *d) {
133+
int progress = (counter / 5) % 100;
134+
// draw the progress bar
135+
d->drawProgressBar(0, 32, 120, 10, progress);
136+
137+
// draw the percentage as String
138+
d->setTextAlignment(TEXT_ALIGN_CENTER);
139+
d->drawString(64, 15, String(progress) + "%");
140+
}
141+
142+
void drawImageDemo(SSD1306 *d) {
143+
// see http://blog.squix.org/2015/05/esp8266-nodemcu-how-to-create-xbm.html
144+
// on how to create xbm files
145+
d->drawXbm(34, 14, WiFi_Logo_width, WiFi_Logo_height, WiFi_Logo_bits);
146+
}
147+
148+
149+
Demo demos[] = {drawFontFaceDemo, drawTextFlowDemo, drawTextAlignmentDemo, drawRectDemo, drawCircleDemo, drawProgressBarDemo, drawImageDemo};
150+
int demoLength = (sizeof(demos) / sizeof(Demo));
151+
long timeSinceLastModeSwitch = 0;
152+
153+
void loop() {
154+
display.clear();
155+
demos[demoMode](&display);
156+
display.display();
157+
158+
display2.clear();
159+
demos[(demoMode + 1) % demoLength](&display2);
160+
display2.display();
161+
162+
163+
if (millis() - timeSinceLastModeSwitch > DEMO_DURATION) {
164+
demoMode = (demoMode + 1) % demoLength;
165+
timeSinceLastModeSwitch = millis();
166+
}
167+
counter++;
168+
delay(10);
169+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#define WiFi_Logo_width 60
2+
#define WiFi_Logo_height 36
3+
const uint8_t WiFi_Logo_bits[] PROGMEM = {
4+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xF8,
5+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0xFF, 0x07, 0x00, 0x00, 0x00,
6+
0x00, 0x00, 0xE0, 0xFF, 0x1F, 0x00, 0x00, 0x00, 0x00, 0x00, 0xF8, 0xFF,
7+
0x7F, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFC, 0xFF, 0xFF, 0x00, 0x00, 0x00,
8+
0x00, 0x00, 0xFE, 0xFF, 0xFF, 0x01, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF,
9+
0xFF, 0x03, 0x00, 0x00, 0x00, 0xFC, 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00,
10+
0x00, 0xFF, 0xFF, 0xFF, 0x07, 0xC0, 0x83, 0x01, 0x80, 0xFF, 0xFF, 0xFF,
11+
0x01, 0x00, 0x07, 0x00, 0xC0, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x0C, 0x00,
12+
0xC0, 0xFF, 0xFF, 0x7C, 0x00, 0x60, 0x0C, 0x00, 0xC0, 0x31, 0x46, 0x7C,
13+
0xFC, 0x77, 0x08, 0x00, 0xE0, 0x23, 0xC6, 0x3C, 0xFC, 0x67, 0x18, 0x00,
14+
0xE0, 0x23, 0xE4, 0x3F, 0x1C, 0x00, 0x18, 0x00, 0xE0, 0x23, 0x60, 0x3C,
15+
0x1C, 0x70, 0x18, 0x00, 0xE0, 0x03, 0x60, 0x3C, 0x1C, 0x70, 0x18, 0x00,
16+
0xE0, 0x07, 0x60, 0x3C, 0xFC, 0x73, 0x18, 0x00, 0xE0, 0x87, 0x70, 0x3C,
17+
0xFC, 0x73, 0x18, 0x00, 0xE0, 0x87, 0x70, 0x3C, 0x1C, 0x70, 0x18, 0x00,
18+
0xE0, 0x87, 0x70, 0x3C, 0x1C, 0x70, 0x18, 0x00, 0xE0, 0x8F, 0x71, 0x3C,
19+
0x1C, 0x70, 0x18, 0x00, 0xC0, 0xFF, 0xFF, 0x3F, 0x00, 0x00, 0x08, 0x00,
20+
0xC0, 0xFF, 0xFF, 0x1F, 0x00, 0x00, 0x0C, 0x00, 0x80, 0xFF, 0xFF, 0x1F,
21+
0x00, 0x00, 0x06, 0x00, 0x80, 0xFF, 0xFF, 0x0F, 0x00, 0x00, 0x07, 0x00,
22+
0x00, 0xFE, 0xFF, 0xFF, 0xFF, 0xFF, 0x01, 0x00, 0x00, 0xF8, 0xFF, 0xFF,
23+
0xFF, 0x7F, 0x00, 0x00, 0x00, 0x00, 0xFE, 0xFF, 0xFF, 0x01, 0x00, 0x00,
24+
0x00, 0x00, 0xFC, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00, 0x00, 0xF8, 0xFF,
25+
0x7F, 0x00, 0x00, 0x00, 0x00, 0x00, 0xE0, 0xFF, 0x1F, 0x00, 0x00, 0x00,
26+
0x00, 0x00, 0x80, 0xFF, 0x07, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFC,
27+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
28+
};

0 commit comments

Comments
 (0)