-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsmartframe.ino
354 lines (293 loc) · 10.1 KB
/
smartframe.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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
#define ENABLE_GxEPD2_GFX 1
//#include <GxEPD2_4G.h> // needs be first include
#include <GxEPD2_BW.h>
#include <Fonts/FreeMonoBold24pt7b.h>
#include <Fonts/FreeSans9pt7b.h>
#include "futura.h"
#include "futura8.h"
#include "futura12.h"
#include "header.h"
#include "nowifi.h"
#include <ds3231.h>
#include <Wire.h>
#include <WiFi.h>
#include <WiFiMulti.h>
#include <HTTPClient.h>
#include <ArduinoJson.h>
#include "credentials.h"
#define uS_TO_S_FACTOR 1000000 /* Conversion factor for micro seconds to seconds */
#define TIME_TO_SLEEP 60 /* Time ESP32 will go to sleep (in seconds) */
RTC_DATA_ATTR int bootCount = 0;
//4G doesn't support partial refresh on 750_T7
//GxEPD2_4G<GxEPD2_750_T7, GxEPD2_750_T7::HEIGHT/2> display(GxEPD2_750_T7(/*CS=5*/ SS, /*DC=*/ 4, /*RST=*/ 21, /*BUSY=*/ 19));
GxEPD2_BW < GxEPD2_750_T7, GxEPD2_750_T7::HEIGHT / 2 > display(GxEPD2_750_T7(/*CS=5*/ SS, /*DC=*/ 4, /*RST=*/ 21, /*BUSY=*/ 19));
//Wifi multiconnection object
WiFiMulti wifiMulti;
//Manna API for random bible verses
const char* serverName = "http://beta.ourmanna.com/api/v1/get/?format=json&order=random";
String retrievedVerse;
const int hOffset = -60;
const int tOffset = 20;
const int mOffset = 100;
const int rotation = 3;
const int lineLimit = 32; //Max characters per line for bible verse
const int characterLimit = lineLimit * 4;
String strings[100]; // Max amount of strings anticipated
String renderContainer[5];
int renderCountainerCharCount[5];
String verseFull = "Here is a trustworthy saying that deserves full acceptance: Christ Jesus came into the world to save sinners—of whom I am the worst. ";
String verseReference = "1 Timothy 1:15";
const char *hour[12] = {"TWELVE", "ONE", "TWO", "THREE", "FOUR", "FIVE", "SIX", "SEVEN", "EIGHT", "NINE", "TEN", "ELEVEN"};
const char *tenth[5] = {"O'", "TWENTY", "THIRTY", "FORTY", "FIFTY"};
const char *minute[9] = {"ONE", "TWO", "THREE", "FOUR", "FIVE", "SIX", "SEVEN", "EIGHT", "NINE"};
const char *teen[10] = {"TEN", "ELEVEN", "TWELVE", "THIRTEEN", "FOURTEEN", "FIFTEEN", "SIXTEEN", "SEVENTEEN", "EIGHTEEN", "NINETEEN"};
const char sharp[] = "SHARP"; //Special case for when minute is at 00
//Time Struct
ts t;
int lastMinute = 0;
//Split String helper function
int split(String string, char c)
{
String data = "";
int bufferIndex = 0;
for (int i = 0; i < string.length(); ++i) {
char c = string[i];
if (c != ' ')
data += c;
else {
//data += '\0';
strings[bufferIndex++] = data;
data = "";
}
}
return bufferIndex;
}
void parseVerse() {
Serial.println("Splitting String Start");
int count = split(verseFull, ' ');
Serial.println("Splitting String Complete");
int charCount = 0;
int i = 0;
for (int j = 0; j < count; ++j)
{
charCount += strings[j].length();
if (charCount >= lineLimit + i * lineLimit) {
i++;
Serial.println("Breaking String into next array");
}
renderContainer[i] = renderContainer[i] + ' ' + strings[j];
}
Serial.println("Breaking String Complete");
// for(int i = 0; i < 5; i++){
// Serial.println(renderContainer[i]);
// }
}
int xPos(const char* string) {
int16_t tbx, tby; uint16_t tbw, tbh;
display.getTextBounds(string, 0, 0, &tbx, &tby, &tbw, &tbh);
return ((display.width() - tbw) / 2) - tbx;
}
int yPos(const char* string) {
int16_t tbx, tby; uint16_t tbw, tbh;
display.getTextBounds(string, 0, 0, &tbx, &tby, &tbw, &tbh);
return ((display.height() - tbh) / 2) - tby;
}
void renderPartialTextBox(const char* string, int offset) {
//Set up Screen
display.setRotation(rotation);
display.setFont(&FuturaBookfont40pt7b);
int16_t tbx, tby; uint16_t tbw, tbh;
//Set Up Window
display.getTextBounds(string, 0, 0, &tbx, &tby, &tbw, &tbh);
display.setPartialWindow(0, ((display.height() - tbh) / 2) + offset, display.width(), tbh);
int x = xPos(string);
int y = yPos(string);
//Render box and text
display.firstPage();
do
{
display.setCursor(x, y + offset);
display.fillScreen(GxEPD_WHITE);
display.setFont(&FuturaBookfont40pt7b);
display.setTextColor(GxEPD_BLACK);
display.print(string);
} while (display.nextPage());
}
void drawTimePartial(int _hour, int _minute, bool fullUpdate = false) {
//Do a full refresh every 5 minutes
if (_minute % 10 == 0) {
getVerseFromAPI();
drawTimeFull();
return;
}
if(wifiMulti.run() != WL_CONNECTED) {
Serial.println("WiFi Disconnected");
display.setPartialWindow(420, 60, 30, 30);
display.firstPage();
do{
display.drawBitmap(420, 60, gImage_nowifi, 30, 30, GxEPD_BLACK);
}while (display.nextPage());
}else{
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
display.setPartialWindow(420, 60, 30, 30);
display.firstPage();
do{
display.drawBitmap(420, 60, gImage_nowifi, 30, 30, GxEPD_WHITE);
}while (display.nextPage());
}
//Draw Hour
const char* hString = hour[_hour % 12];
//Draw Tenth, Teen or Sharp
const char* tString;
if (_minute == 0) { //Set Sharp
tString = sharp;
} else if (_minute < 10) { //Set "O'" Special Case
tString = tenth[0];
} else if (_minute >= 10 && _minute < 20) { //Set Teen
tString = teen[_minute % 10];
} else {
tString = tenth[_minute / 10 - 1];
}
//Draw Minute
const char* mString;
if (_minute % 10 == 0 || (_minute > 10 && _minute < 20)) { //null cases
mString = NULL; //null string
} else {
mString = minute[_minute % 10 - 1];
}
//renderPartialTextBox(hString, hOffset);
renderPartialTextBox(tString, tOffset);
if (mString != NULL) {
renderPartialTextBox(mString, mOffset);
}
}
void drawTimeFull()
{
DS3231_get(&t);
int _hour = t.hour;
int _minute = t.min;
// int _hour = 9;
// int _minute = 30;
display.setRotation(rotation);
display.setFullWindow();
display.fillScreen(GxEPD_WHITE);
display.firstPage();
do
{
//Set up Screen
display.fillScreen(GxEPD_WHITE);
display.setFont(&FuturaBookfont40pt7b);
display.setTextColor(GxEPD_BLACK);
display.drawBitmap(0, 80, gImage_header, 480, 181, GxEPD_BLACK); // Print Subscribers symbol (POSITION_X, POSITION_Y, IMAGE_NAME, IMAGE_WIDTH, IMAGE_HEIGHT, COLOR);
if(wifiMulti.run() != WL_CONNECTED) {
Serial.println("WiFi not connected!");
display.drawBitmap(420, 60, gImage_nowifi, 30, 30, GxEPD_BLACK); // Print Subscribers symbol (POSITION_X, POSITION_Y, IMAGE_NAME, IMAGE_WIDTH, IMAGE_HEIGHT, COLOR);
}
display.drawLine(120, 280, 360, 280, GxEPD_BLACK); // Draw line (x0,y0,x1,y1,color)
display.drawLine(120, 560, 360, 560, GxEPD_BLACK); // Draw line (x0,y0,x1,y1,color)
//Draw Hour
display.setCursor(xPos(hour[_hour % 12]), yPos(hour[_hour % 12]) + hOffset);
display.print(hour[_hour % 12]);
if (_minute == 0) {
display.setCursor(xPos("SHARP"), yPos("SHARP") + tOffset);
display.print("SHARP");
}
else if (_minute >= 10 && _minute < 20) {
display.setCursor(xPos(teen[_minute % 10]), yPos(teen[_minute % 10]) + tOffset);
display.print(teen[_minute % 10]);
} else {
if (_minute < 10 ) {
display.setCursor(xPos(tenth[0]), yPos(tenth[0]) + tOffset);
display.print(tenth[0]);
} else {
display.setCursor(xPos(tenth[_minute / 10 - 1]), yPos(tenth[_minute / 10 - 1]) + tOffset);
display.print(tenth[_minute / 10 - 1]);
}
if (_minute % 10 != 0) {
display.setCursor(xPos(minute[_minute % 10 - 1]), yPos(minute[_minute % 10 - 1]) + mOffset);
display.print(minute[_minute % 10 - 1]);
}
}
display.setFont(&futuralight12pt7b);
int i = 0;
while(renderContainer[i] != ""){
display.setFont(&futuralight12pt7b);
display.setCursor(xPos(renderContainer[i].c_str()), yPos(renderContainer[i].c_str()) + 200 + i*30);
display.print(renderContainer[i].c_str());
i++;
}
display.setCursor(xPos(verseReference.c_str()), yPos(verseReference.c_str()) + 200 + i*30);
display.print(verseReference);
}
while (display.nextPage());
}
void getVerseFromAPI(){
bool withinCharLimit = true;
do{
if (WiFi.status() == WL_CONNECTED) {
//Clear container
for(int i = 0; i < 5; i++){
renderContainer[i] = "";
}
HTTPClient http; //Object of class HTTPClient
http.begin(serverName);
int httpCode = http.GET();
//Check the returning code
if (httpCode > 0) {
// Parsing
const size_t bufferSize = JSON_OBJECT_SIZE(2) + JSON_OBJECT_SIZE(3) + JSON_OBJECT_SIZE(5) + JSON_OBJECT_SIZE(8) + 370;
DynamicJsonBuffer jsonBuffer(bufferSize);
JsonObject& root = jsonBuffer.parseObject(http.getString());
const char* verse = root["verse"]["details"]["text"];
verseFull = String(verse);
verseFull = verseFull + ' '; //pad an extra space
const char* reference = root["verse"]["details"]["reference"];
verseReference = reference;
if(strlen(verse) > characterLimit)
withinCharLimit = false;
else
withinCharLimit = true;
}
http.end(); //Close connection
}
}while(!withinCharLimit);
parseVerse();
}
void setup() {
delay(1000);
display.init(115200);
//Draw frame header
display.setRotation(rotation);
Wire.begin(27, 26);
DS3231_init(DS3231_INTCN);
DS3231_get(&t);
//Draw a full frame on startup
lastMinute = t.min;
// //Connect to list of wifi networks
// for(int i = 0; i < numberOfNetworks; i++){
// wifiMulti.addAP(ssid[i], password[i]);
// Serial.println(ssid[i]);
// Serial.println(password[i]);
// }
wifiMulti.addAP("Yongs", "eb33aef3f6");
Serial.println("Connecting Wifi...");
if(wifiMulti.run() == WL_CONNECTED) {
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
}
getVerseFromAPI();
drawTimeFull();
}
void loop() {
// put your main code here, to run repeatedly:
DS3231_get(&t);
if (t.min != lastMinute) {
drawTimePartial(t.hour, t.min);
lastMinute = t.min;
}
delay(1000);
}