-
Notifications
You must be signed in to change notification settings - Fork 9
/
WeatherDisplay.ino
188 lines (142 loc) · 3.54 KB
/
WeatherDisplay.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
/*
Fetch data from weather service and render on screen
ESP8266 Hardware SPI pins
GPIO12 MISO
GPIO14 CLOCK - (D0 or CLK)
GPIO13 MOSI - (D1 or DIN)
GPIO15 CS
GPIO2 DC
GPIO16 RESET
*/
#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <ESP_SSD1306.h>
#include <EEPROM.h>
#include <Ticker.h>
extern "C" {
#include "user_interface.h"
}
// local includes
#include "global.h"
#include "config.h"
#include "webPage.h"
// custom GLCD fonts
#include "Helv32.h"
#include "TPSS8.h"
#include "Unifont16.h"
#include "Weather32.h"
#include "Splash.h"
// app variables
// OLED display (128x64) used in SPI mode
ESP_SSD1306 display(ESP_OLED_DC, ESP_OLED_RESET, ESP_OLED_CS);
// logging variables
String logLines[MAX_LOG_LINES];
int currentLogLine = 0;
// program variables
byte displayFormat = DISPLAY_CONSOLE;
boolean screen1 = false;
static unsigned int lastDisplayMode = displayFormat;
// wifi operation model - AP+STA or just AP
boolean stayAPMode = false;
boolean staModeOperation = true;
boolean firstTime = true;
// ------------------------
void setup() {
// ------------------------
Serial.begin(115200);
// start display device
initDisplay();
delay(2000);
// setup console show switch - pullup
pinMode(PIN_LANDSCAPE, INPUT_PULLUP);
pinMode(PIN_MISC, INPUT_PULLUP);
initStructure();
// load the configuration from flash memory
if( !readConfig() || !digitalRead(PIN_MISC) )
staModeOperation = false;
if(staModeOperation)
setupSTA();
else
// setup AP server
setupAP();
// http server for configuring this device
startConfigServer();
log("Free Heap: " + String(ESP.getFreeHeap()));
}
// ------------------------
void loop() {
// ------------------------
scanServeConfigClient();
if(staModeOperation) {
// scheduled tasks - flip screen, refresh weather, misc
handleTasks();
// read and process status of switches
handleSwitches();
}
// reevaluate after a while
delay(200);
}
// ------------------------
void handleTasks() {
// ------------------------
static unsigned long last1 = 0;
static unsigned long last2 = 0;
static unsigned long last3 = 0;
if ( last1 < millis() ) {
last1 = millis() + SCREEN_FLIP_INTERVAL;
// toggle screen 2 flag
screen1 = !screen1;
// invoke draw to update display
draw();
}
if ( last2 < millis() ) {
last2 = millis() + config.interval;
// update weather information
boolean gotWeather = getWeatherData();
if(firstTime && gotWeather) {
firstTime = false;
// switch the display from console to default
displayFormat = DISPLAY_PORTRAIT;
}
// invoke draw to update display
draw();
}
if ( last3 < millis() ) {
last3 = millis() + MISC_REFRESH_INTERVAL;
// debugging info, print heap memory stat
Serial.print("Heap: ");Serial.println(ESP.getFreeHeap());
}
}
// ------------------------
void handleSwitches() {
// ------------------------
// read and act on tilt switch
if( digitalRead(PIN_LANDSCAPE) ) {
if(displayFormat == DISPLAY_PORTRAIT) {
displayFormat = DISPLAY_LANDSCAPE;
draw();
}
}
else {
if(displayFormat == DISPLAY_LANDSCAPE) {
displayFormat = DISPLAY_PORTRAIT;
draw();
}
}
}
// ------------------------
void log(String msg) {
// ------------------------
// log onto serial port
Serial.println(msg);
// append log message to end of buffer
logLines[currentLogLine] = msg;
if(++currentLogLine == MAX_LOG_LINES)
currentLogLine = 0;
// invoke draw to update display if console visible
if(displayFormat == DISPLAY_CONSOLE)
draw();
}