Created
February 16, 2021 00:19
-
-
Save fuzziebrain/1fe0a548caa0e1a61d4cf7283c746400 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <M5EPD.h> | |
#include <WiFi.h> | |
#include <HTTPClient.h> | |
#include <ArduinoJson.h> | |
M5EPD_Canvas canvas(&M5.EPD); // create the canvas | |
rtc_time_t RTCtime; | |
rtc_date_t RTCDate; | |
const char SSID[] = "mywifissid"; | |
const char WLAN_PASSWORD[] = "supersecret"; | |
const uint16_t SCREEN_WIDTH = 960; | |
const uint16_t SCREEN_HEIGHT = 540; | |
const uint8_t JPN_FONT_SIZE = 96; | |
const uint8_t ROMAJI_FONT_SIZE = 48; | |
const uint8_t ENG_FONT_SIZE = 72; | |
const uint16_t WAKE_INTERVAL_SECONDS = 900; // 15 minutes | |
const uint16_t SHUTDOWN_DELAY_SECONDS = 1; | |
const uint8_t FOOTER_BAR_HEIGHT = 50; | |
void setup() { | |
bool payloadAvailable = false; | |
M5.begin(); // Initialize the device | |
M5.RTC.begin(); // Initialize the RTC | |
M5.TP.SetRotation(0); // Set the orientation of the touch panel | |
M5.EPD.SetRotation(0); // Set the orientation of the display | |
M5.EPD.Clear(true); // Clears the screen content | |
canvas.loadFont("/GenSenRounded-R.ttf", SD); // Load the font from the MicroSD card. | |
canvas.createCanvas(960, 540); // Create the canvas with the maximum dimension of the e-paper in landscape orientation. | |
displayFooter("Starting..."); | |
WiFi.begin(SSID, WLAN_PASSWORD); | |
while (WiFi.status() != WL_CONNECTED) { | |
delay(500); | |
Serial.print("."); // print dots to the serial monitor to know that we are still waiting for a connection. | |
} | |
displayFooter("WiFi connected."); | |
HTTPClient http; | |
String payload; | |
http.begin("https://apeks.app/ords/lab/words/random"); | |
int httpCode = http.GET(); | |
// httpCode will be negative on error | |
if(httpCode > 0) { | |
if(httpCode == HTTP_CODE_OK) { | |
payload = http.getString(); // get the HTTP response | |
payloadAvailable = true; | |
Serial.println(payload); // for debugging | |
displayFooter("Data received via Oracle REST Data Services."); | |
} | |
} else { | |
displayFooter("Failed to load data."); | |
Serial.printf("[HTTP] GET... failed, error: %s\n", http.errorToString(httpCode).c_str()); | |
} | |
http.end(); | |
// If the REST call was successful, parse the JSON content | |
if(payloadAvailable) { | |
DynamicJsonDocument doc(2048); | |
deserializeJson(doc, payload); | |
// Only one row should have been returned by the REST call. | |
String jpnText = doc["items"][0]["jpn_text"]; | |
String romaji = doc["items"][0]["romaji"]; | |
String engText = doc["items"][0]["eng_text"]; | |
displayFooter("Data entered via Oracle Application Express parsed and loaded."); | |
displayContent(jpnText, romaji, engText); | |
} | |
displayFooter("Powered by the Oracle Cloud"); | |
} | |
void loop() { | |
delay(SHUTDOWN_DELAY_SECONDS * 1000); // wait to allow refresh to complete before shutdown | |
M5.shutdown(WAKE_INTERVAL_SECONDS - SHUTDOWN_DELAY_SECONDS); | |
} | |
void displayContent(String jpnText, String romaji, String engText) { | |
canvas.fillCanvas(0); | |
uint16_t x = (960 - canvas.textWidth(jpnText)) / 2; | |
uint16_t y = 100; | |
canvas.createRender(JPN_FONT_SIZE, 256); | |
canvas.setTextColor(15); | |
canvas.setTextSize(JPN_FONT_SIZE); | |
canvas.setTextDatum(TC_DATUM); | |
canvas.drawString(jpnText, x, y); | |
canvas.destoryRender(0); | |
x = (960 - canvas.textWidth(romaji)) / 2; | |
y = 200; | |
canvas.createRender(ROMAJI_FONT_SIZE, 256); | |
canvas.setTextColor(15); | |
canvas.setTextSize(ROMAJI_FONT_SIZE); | |
canvas.setTextDatum(TC_DATUM); | |
canvas.drawString(romaji, x, y); | |
canvas.destoryRender(0); | |
x = (960 - canvas.textWidth(engText)) / 2; | |
y = 280; | |
canvas.createRender(ENG_FONT_SIZE, 256); | |
canvas.setTextColor(15); | |
canvas.setTextSize(ENG_FONT_SIZE); | |
canvas.setTextDatum(TC_DATUM); | |
canvas.drawString(engText, x, y); | |
canvas.destoryRender(0); | |
char lastUpdatedText[48]; | |
M5.RTC.getTime(&RTCtime); | |
M5.RTC.getDate(&RTCDate); | |
sprintf( | |
lastUpdatedText | |
, "Last updated on %02d/%02d/%04d %02d:%02d:%02d" | |
, RTCDate.day, RTCDate.mon, RTCDate.year | |
, RTCtime.hour, RTCtime.min, RTCtime.sec | |
); | |
x = (960 - canvas.textWidth(lastUpdatedText)) / 2; | |
y = SCREEN_HEIGHT - FOOTER_BAR_HEIGHT - 20; | |
canvas.createRender(24, 64); | |
canvas.setTextColor(15); | |
canvas.setTextSize(24); | |
canvas.setTextDatum(BC_DATUM); | |
canvas.drawString(lastUpdatedText, x, y); | |
canvas.destoryRender(0); | |
canvas.pushCanvas(UPDATE_MODE_DU); | |
} | |
void displayFooter(String text) { | |
uint16_t x = 20; | |
uint16_t y = SCREEN_HEIGHT - FOOTER_BAR_HEIGHT / 2; | |
canvas.fillRect(0, SCREEN_HEIGHT - FOOTER_BAR_HEIGHT, SCREEN_WIDTH, FOOTER_BAR_HEIGHT, 15); | |
canvas.createRender(24, 256); | |
canvas.setTextSize(24); | |
canvas.setTextColor(0); | |
canvas.setTextDatum(CL_DATUM); | |
canvas.drawString(text, x, y); | |
canvas.destoryRender(0); | |
canvas.pushCanvas(0, 0, UPDATE_MODE_DU); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment