Skip to content

Commit 63835de

Browse files
committed
add SSD1306ScrollVerticalDemo example
this shows how the return value of drawStringMaxWidth() can be used
1 parent 196faae commit 63835de

File tree

1 file changed

+91
-0
lines changed

1 file changed

+91
-0
lines changed
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
/**
2+
The MIT License (MIT)
3+
4+
Copyright (c) 2022 by Stefan Seyfried
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+
25+
// Include the correct display library
26+
// For a connection via I2C using Wire include
27+
#include <Wire.h> // Only needed for Arduino 1.6.5 and earlier
28+
#include "SSD1306Wire.h" // legacy include: `#include "SSD1306.h"`
29+
// or #include "SH1106Wire.h", legacy include: `#include "SH1106.h"`
30+
// For a connection via I2C using brzo_i2c (must be installed) include
31+
// #include <brzo_i2c.h> // Only needed for Arduino 1.6.5 and earlier
32+
// #include "SSD1306Brzo.h"
33+
// #include "SH1106Brzo.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+
// #include "SH1106Spi.h"
38+
39+
// Use the corresponding display class:
40+
41+
// Initialize the OLED display using SPI
42+
// D5 -> CLK
43+
// D7 -> MOSI (DOUT)
44+
// D0 -> RES
45+
// D2 -> DC
46+
// D8 -> CS
47+
// SSD1306Spi display(D0, D2, D8);
48+
// or
49+
// SH1106Spi display(D0, D2);
50+
51+
// Initialize the OLED display using brzo_i2c
52+
// D3 -> SDA
53+
// D5 -> SCL
54+
// SSD1306Brzo display(0x3c, D3, D5);
55+
// or
56+
// SH1106Brzo display(0x3c, D3, D5);
57+
58+
// Initialize the OLED display using Wire library
59+
SSD1306Wire display(0x3c, SDA, SCL); // ADDRESS, SDA, SCL - SDA and SCL usually populate automatically based on your board's pins_arduino.h e.g. https://github.com/esp8266/Arduino/blob/master/variants/nodemcu/pins_arduino.h
60+
// SH1106Wire display(0x3c, SDA, SCL);
61+
62+
// UTF-8 sprinkled within, because it tests special conditions in the char-counting code
63+
const String loremipsum = "Lorem ipsum dolor sit ämet, "
64+
"consetetur sadipscing elitr, sed diam nonümy eirmöd "
65+
"tempor invidunt ut labore et dolore mägnä aliquyam erat, "
66+
"sed diam voluptua. At vero eos et accusam et justo duo "
67+
"dolores et ea rebum. Stet clita kasd gubergren, no sea "
68+
"takimata sanctus est Lorem ipsum dolor sit amet. "
69+
"äöü-ÄÖÜ/߀é/çØ.";
70+
71+
void setup() {
72+
display.init();
73+
display.setContrast(255);
74+
display.setTextAlignment(TEXT_ALIGN_LEFT);
75+
display.setFont(ArialMT_Plain_16);
76+
display.display();
77+
}
78+
79+
void loop() {
80+
static uint16_t start_at = 0;
81+
display.clear();
82+
uint16_t firstline = display.drawStringMaxWidth(0, 0, 128, loremipsum.substring(start_at));
83+
display.display();
84+
if (firstline != 0) {
85+
start_at += firstline;
86+
} else {
87+
start_at = 0;
88+
delay(1000); // additional pause before going back to start
89+
}
90+
delay(1000);
91+
}

0 commit comments

Comments
 (0)