Skip to content

Commit 5fe08d8

Browse files
committed
Add TextDisplay example
1 parent e7379ab commit 5fe08d8

File tree

1 file changed

+121
-0
lines changed

1 file changed

+121
-0
lines changed

examples/TextDisplay/TextDisplay.ino

+121
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
/*********************************************************************
2+
Web Thing which draws text provided as a property.
3+
Adapted from the Adafruit SSD1306 example:
4+
5+
This is an example for our Monochrome OLEDs based on SSD1306 drivers
6+
7+
Pick one up today in the adafruit shop!
8+
------> http://www.adafruit.com/category/63_98
9+
10+
This example is for a 128x64 size display using SPI to communicate
11+
4 or 5 pins are required to inteface
12+
13+
Adafruit invests time and resources providing this open source code,
14+
please support Adafruit and open-source hardware by purchasing
15+
products from Adafruit!
16+
17+
Written by Limor Fried/Ladyada for Adafruit Industries.
18+
BSD license, check license.txt for more information
19+
All text above, and the splash screen must be included in any redistribution
20+
*********************************************************************/
21+
22+
#include <ESP8266WiFi.h>
23+
#include <WiFiClient.h>
24+
#include <Thing.h>
25+
#include <WebThingAdapter.h>
26+
27+
const char* ssid = "...";
28+
const char* password = "...";
29+
30+
#include <SPI.h>
31+
#include <Wire.h>
32+
#include <Adafruit_GFX.h>
33+
#include <Adafruit_SSD1306.h>
34+
35+
// If using software SPI (the default case):
36+
#define OLED_MOSI 2
37+
#define OLED_CLK 16
38+
#define OLED_DC 0
39+
#define OLED_CS 13
40+
#define OLED_RESET 15
41+
Adafruit_SSD1306 display(OLED_MOSI, OLED_CLK, OLED_DC, OLED_RESET, OLED_CS);
42+
43+
/* Uncomment this block to use hardware SPI
44+
#define OLED_DC 6
45+
#define OLED_CS 7
46+
#define OLED_RESET 8
47+
Adafruit_SSD1306 display(OLED_DC, OLED_RESET, OLED_CS);
48+
*/
49+
50+
const int textHeight = 8;
51+
const int textWidth = 6;
52+
const int width = 128;
53+
const int height = 64;
54+
55+
WebThingAdapter adapter("esp8266");
56+
ThingDevice textDisplay("textDisplay", "Text display", "textDisplay");
57+
ThingProperty text("text", "", STRING);
58+
59+
String lastText = "moz://a iot";
60+
61+
void displayString(const String& str) {
62+
int len = str.length();
63+
int strWidth = len * textWidth;
64+
int strHeight = textHeight;
65+
int scale = width / strWidth;
66+
if (strHeight > strWidth / 2) {
67+
scale = height / strHeight;
68+
}
69+
int x = width / 2 - strWidth * scale / 2;
70+
int y = height / 2 - strHeight * scale / 2;
71+
72+
display.clearDisplay();
73+
display.setTextColor(WHITE);
74+
display.setTextSize(scale);
75+
display.setCursor(x, y);
76+
display.println(str);
77+
display.display();
78+
}
79+
80+
void setup() {
81+
Serial.begin(115200);
82+
83+
// by default, we'll generate the high voltage from the 3.3v line internally! (neat!)
84+
display.begin(SSD1306_SWITCHCAPVCC);
85+
86+
// display the splashscreen as requested :)
87+
display.display();
88+
delay(2000);
89+
90+
WiFi.mode(WIFI_STA);
91+
WiFi.begin(ssid, password);
92+
Serial.println("");
93+
94+
// Wait for connection
95+
while (WiFi.status() != WL_CONNECTED) {
96+
delay(500);
97+
Serial.print(".");
98+
}
99+
100+
Serial.println("");
101+
Serial.print("Connected to ");
102+
Serial.println(ssid);
103+
Serial.print("IP address: ");
104+
Serial.println(WiFi.localIP());
105+
106+
displayString(lastText);
107+
108+
ThingPropertyValue value;
109+
value.string = &lastText;
110+
text.setValue(value);
111+
112+
textDisplay.addProperty(&text);
113+
adapter.addDevice(&textDisplay);
114+
adapter.begin();
115+
}
116+
117+
void loop() {
118+
adapter.update();
119+
displayString(lastText);
120+
}
121+

0 commit comments

Comments
 (0)