Skip to content

Commit 00286e5

Browse files
committed
Change API for random quotes and implement TextBox function
1 parent f4c45ca commit 00286e5

File tree

37 files changed

+6636
-1094
lines changed

37 files changed

+6636
-1094
lines changed

examples/Inkplate10/Projects/Inkplate10_Quotables/Fonts/FreeMonoBold24pt7b.h

Lines changed: 672 additions & 0 deletions
Large diffs are not rendered by default.

examples/Inkplate10/Projects/Inkplate10_Quotables/Inkplate10_Quotables.ino

Lines changed: 24 additions & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -30,19 +30,21 @@
3030
//---------- CHANGE HERE -------------:
3131

3232
// Put in your ssid and password
33-
char ssid[] = "Soldered";
34-
char pass[] = "dasduino";
33+
char ssid[] = "";
34+
char pass[] = "";
3535

3636
//----------------------------------
3737

3838
// Include Inkplate library to the sketch
3939
#include "Inkplate.h"
4040

4141
// Include fonts used
42-
#include "Fonts/exmouth_40pt7b.h"
42+
#include "Fonts/FreeMonoBold24pt7b.h"
4343

4444
// Our networking functions, declared in Network.cpp
4545
#include "Network.h"
46+
#include "driver/rtc_io.h" // Include ESP32 library for RTC pin I/O (needed for rtc_gpio_isolate() function)
47+
#include <rom/rtc.h> // Include ESP32 library for RTC (needed for rtc_get_reset_reason() function)
4648

4749
// create object with all networking functions
4850
Network network;
@@ -51,8 +53,10 @@ Network network;
5153
Inkplate display(INKPLATE_1BIT);
5254

5355
// Delay between API calls in seconds, 300 seconds is 5 minutes
54-
#define DELAY_S 300
55-
#define DELAY_WIFI_RETRY_SECONDS 10
56+
// Since the function this is used in expects time in microseconds,
57+
// we have to multiply with 1000000
58+
#define DELAY_S 300 * 1000000
59+
#define DELAY_WIFI_RETRY_SECONDS 5
5660
// Our functions declared below setup and loop
5761
void drawAll();
5862

@@ -66,9 +70,10 @@ void setup()
6670

6771
// Initial display settings
6872
display.begin();
69-
display.setTextWrap(false); // Set text wrapping to true
7073
display.setTextColor(BLACK);
71-
display.setTextSize(3);
74+
display.setTextWrap(false);
75+
display.clearDisplay();
76+
display.display();
7277

7378
// Try connecting to a WiFi network.
7479
// Parameters are network SSID, password, timeout in seconds and whether to print to serial.
@@ -92,113 +97,32 @@ void setup()
9297
(void)esp_deep_sleep_start();
9398
}
9499

100+
Serial.print("Retrying retriving data");
95101
while (!network.getData(quote, author))
96102
{
97103
Serial.print('.');
98104
delay(1000);
99105
}
100106

101107
display.clearDisplay();
102-
drawAll(); //Call funtion to draw screen
108+
//Draw the quote inside a textbox element
109+
display.drawTextBox(48, display.height() / 2 - 36, display.width()-48,display.height()/2+200,quote,1,&FreeMonoBold24pt7b,36,false,38);
110+
111+
//Print the author in the bottom right corner
112+
uint16_t w, h;
113+
int16_t x, y;
114+
display.getTextBounds(author, 0, 0, &x, &y, &w, &h);
115+
display.setCursor(display.width() - w - 50, display.height() - 30); // Set cursor to fit author name in lower right corner
116+
display.print("-");
117+
display.println(author); // Print author
103118
display.display();
104119

105120
// Go to sleep before checking again
106-
// This is set in microseconds, so it needs to be
107-
// multiplied by million to get seconds
108-
esp_sleep_enable_timer_wakeup(1000000 * DELAY_S);
121+
esp_sleep_enable_timer_wakeup(DELAY_S);
109122
(void)esp_deep_sleep_start();
110123
}
111124

112125
void loop()
113126
{
114127
// Never here
115128
}
116-
117-
// Our main drawing function
118-
// Our main drawing function
119-
void drawAll()
120-
{
121-
display.setFont(&exmouth_40pt7b); // Set custom font
122-
display.setTextSize(1);
123-
display.setTextColor(BLACK); //Set text color to random color
124-
printInBoundaries(quote,50,247,1100,330,65);
125-
uint16_t w,h;
126-
int16_t x,y;
127-
display.getTextBounds(author, 0, 0, &x, &y, &w, &h);
128-
display.setCursor(display.width() - w - 50, display.height() - 30); // Set cursor to fit author name in lower right corner
129-
display.print("-");
130-
display.println(author); // Print author
131-
}
132-
133-
// Function to print text within a text box
134-
void printInBoundaries(char *text, int x0, int y0, int print_width, int print_height, int rowHeight)
135-
{
136-
int currentCharIndex = 0;
137-
char currentWordBuf[128] = {0};
138-
char currentWordBufWithThreeDots[128] = {0};
139-
bool lastWord = false;
140-
bool lastRow = false;
141-
int currentRow = 0;
142-
int textLen = strlen(text);
143-
144-
display.setCursor(x0, y0);
145-
146-
while (true)
147-
{
148-
int i = currentCharIndex;
149-
while (text[i] != ' ')
150-
{
151-
i++;
152-
if (i > textLen)
153-
{
154-
lastWord = true;
155-
break;
156-
}
157-
}
158-
159-
memset(currentWordBuf, 0, 128);
160-
memcpy(currentWordBuf, text + currentCharIndex, i - currentCharIndex);
161-
162-
int16_t printing_x0, printing_y0, printing_x1, printing_y1;
163-
uint16_t printing_w, printing_h;
164-
165-
printing_x0 = display.getCursorX();
166-
printing_y0 = display.getCursorY();
167-
168-
lastRow = (printing_y0 + printing_h + rowHeight > y0 + print_height);
169-
if (!lastRow)
170-
{
171-
display.getTextBounds(currentWordBuf, printing_x0, printing_y0, &printing_x1, &printing_y1, &printing_w,
172-
&printing_h);
173-
}
174-
else
175-
{
176-
memset(currentWordBufWithThreeDots, 0, 128);
177-
memcpy(currentWordBufWithThreeDots, currentWordBuf, i - currentCharIndex);
178-
strcat(currentWordBufWithThreeDots, " ...");
179-
display.getTextBounds(currentWordBufWithThreeDots, printing_x0, printing_y0, &printing_x1, &printing_y1,
180-
&printing_w, &printing_h);
181-
}
182-
bool wordCrossesWidthBoundary = printing_x1 + printing_w > x0 + print_width;
183-
184-
if (wordCrossesWidthBoundary)
185-
{
186-
currentRow++;
187-
display.setCursor(x0, y0 + rowHeight * currentRow);
188-
if (lastRow)
189-
{
190-
display.setCursor(printing_x0, printing_y0);
191-
display.print(" ...");
192-
return;
193-
}
194-
}
195-
display.print(currentWordBuf);
196-
display.print(" ");
197-
198-
if (lastWord)
199-
{
200-
return;
201-
}
202-
currentCharIndex = i + 1;
203-
}
204-
}

examples/Inkplate10/Projects/Inkplate10_Quotables/Network.cpp

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424

2525
// Must be installed for this example to work
2626
#include <ArduinoJson.h>
27-
2827
// external parameters from our main file
2928
extern char ssid[];
3029
extern char pass[];
@@ -35,28 +34,33 @@ extern Inkplate display;
3534
// Static Json from ArduinoJson library
3635
StaticJsonDocument<30000> doc;
3736

38-
void Network::begin()
37+
void Network::begin(char *ssid, char *pass)
3938
{
4039
// Initiating wifi, like in BasicHttpClient example
4140
WiFi.mode(WIFI_STA);
4241
WiFi.begin(ssid, pass);
4342

4443
int cnt = 0;
45-
Serial.print(F("Waiting for WiFi to connect..."));
44+
display.print(F("Waiting for WiFi to connect..."));
45+
display.partialUpdate(true);
4646
while ((WiFi.status() != WL_CONNECTED))
4747
{
48-
Serial.print(F("."));
48+
display.print(F("."));
49+
display.partialUpdate(true);
4950
delay(1000);
5051
++cnt;
5152

5253
if (cnt == 20)
5354
{
54-
Serial.println("Can't connect to WIFI, restarting");
55+
display.println("Can't connect to WIFI, restarting");
56+
display.partialUpdate(true);
5557
delay(100);
5658
ESP.restart();
5759
}
5860
}
59-
Serial.println(F(" connected"));
61+
display.println(F(" connected"));
62+
display.partialUpdate(true);
63+
6064
}
6165

6266
bool Network::getData(char* text, char* auth)
@@ -71,7 +75,7 @@ bool Network::getData(char* text, char* auth)
7175
delay(5000);
7276

7377
int cnt = 0;
74-
Serial.println(F("Waiting for WiFi to reconnect..."));
78+
display.println(F("Waiting for WiFi to reconnect..."));
7579
while ((WiFi.status() != WL_CONNECTED))
7680
{
7781
// Prints a dot every second that wifi isn't connected
@@ -99,7 +103,7 @@ bool Network::getData(char* text, char* auth)
99103
http.getStream().flush();
100104

101105
// Initiate http
102-
char link[] = "https://favqs.com/api/qotd";
106+
char link[] = "https://api.quotable.kurokeita.dev/api/quotes/random";
103107
http.begin(link);
104108

105109
// Actually do request
@@ -122,16 +126,20 @@ bool Network::getData(char* text, char* auth)
122126
{
123127
// Set all data got from internet using formatTemp and formatWind defined above
124128
// This part relies heavily on ArduinoJson library
129+
if(strlen(doc["quote"]["content"])>128)
130+
{
131+
return false;
132+
}
133+
const char *buff2 = doc["quote"]["author"]["name"];
134+
strncpy(auth,buff2,35);
125135

126136
Serial.println("Success");
127137

128-
const char *buff1 = doc["body"];
129-
130-
strcpy(text, buff1);
138+
const char *buff1 = doc["quote"]["content"];
139+
strncpy(text,buff1,128);
131140

132-
const char *buff2 = doc["author"];
141+
133142

134-
strcpy(auth, buff2);
135143

136144
// Save our data to data pointer from main file
137145
f = 0;
@@ -143,7 +151,7 @@ bool Network::getData(char* text, char* auth)
143151
display.clearDisplay();
144152
display.setCursor(50, 230);
145153
display.setTextSize(2);
146-
display.println(F("Quotes have not been found!"));
154+
display.println(F("Info has not been found!"));
147155
display.display();
148156
while (1)
149157
;

examples/Inkplate10/Projects/Inkplate10_Quotables/Network.h

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,11 @@
1313
If you have any questions about licensing, please contact [email protected]
1414
Distributed as-is; no warranty is given.
1515
*/
16-
1716
#include "Arduino.h"
18-
19-
#include <HTTPClient.h>
20-
#include <WiFi.h>
21-
#include <WiFiClientSecure.h>
22-
23-
// Wifi ssid and password
24-
extern char ssid[];
25-
extern char pass[];
17+
#include "ArduinoJson.h"
18+
#include "HTTPClient.h"
19+
#include "WiFi.h"
20+
#include "WiFiClientSecure.h"
2621

2722
#ifndef NETWORK_H
2823
#define NETWORK_H
@@ -33,11 +28,12 @@ class Network
3328
{
3429
public:
3530
// Functions we can access in main file
36-
void begin();
31+
void begin(char *ssid, char *pass);
3732
bool getData(char* text, char* auth);
3833

3934
private:
4035
// Functions called from within our class
4136
};
4237

4338
#endif
39+

0 commit comments

Comments
 (0)