From a543ef470ece852ad4f482a57d40dd765ac1ef56 Mon Sep 17 00:00:00 2001 From: Chrome Legion Date: Wed, 9 May 2018 00:19:12 -0700 Subject: [PATCH] Qrome - Updated with Clock when Printer is Off --- printermonitor/Settings.h | 1 + printermonitor/printermonitor.ino | 93 ++++++++++++++++++++++++++++--- 2 files changed, 85 insertions(+), 9 deletions(-) diff --git a/printermonitor/Settings.h b/printermonitor/Settings.h index c27c60e..a5abcec 100644 --- a/printermonitor/Settings.h +++ b/printermonitor/Settings.h @@ -61,6 +61,7 @@ char* www_username = "admin"; // User account for the Web Interface char* www_password = "password"; // Password for the Web Interface float UtcOffset = -7; // Hour offset from GMT for your timezone int minutesBetweenDataRefresh = 60; +boolean DISPLAYCLOCK = true; // true = Show Clock when not printing / false = turn off display when not printing // Display Settings const int I2C_DISPLAY_ADDRESS = 0x3c; // I2C Address of your Display (usually 0x3c or 0x3d) diff --git a/printermonitor/printermonitor.ino b/printermonitor/printermonitor.ino index 87d0d31..f2cca75 100644 --- a/printermonitor/printermonitor.ino +++ b/printermonitor/printermonitor.ino @@ -27,7 +27,7 @@ SOFTWARE. #include "Settings.h" -#define VERSION "1.0" +#define VERSION "1.1" #define HOSTNAME "ESP8266-" #define CONFIG "/conf.txt" @@ -54,12 +54,17 @@ void drawScreen1(OLEDDisplay *display, OLEDDisplayUiState* state, int16_t x, int void drawScreen2(OLEDDisplay *display, OLEDDisplayUiState* state, int16_t x, int16_t y); void drawScreen3(OLEDDisplay *display, OLEDDisplayUiState* state, int16_t x, int16_t y); void drawHeaderOverlay(OLEDDisplay *display, OLEDDisplayUiState* state); +void drawClock(OLEDDisplay *display, OLEDDisplayUiState* state, int16_t x, int16_t y); +void drawClockHeaderOverlay(OLEDDisplay *display, OLEDDisplayUiState* state); // Set the number of Frames supported const int numberOfFrames = 3; FrameCallback frames[numberOfFrames]; +FrameCallback clockFrame[1]; +boolean isClockOn = false; OverlayCallback overlays[] = { drawHeaderOverlay }; +OverlayCallback clockOverlay[] = { drawClockHeaderOverlay }; int numberOfOverlays = 1; // Time @@ -92,6 +97,7 @@ const String CHANGE_FORM = "
OctoPrint API Key (get from your server)" "" "" + " Display Clock when printer is off

" "Time Refresh (minutes)

" "Theme Color

" "" @@ -182,10 +188,11 @@ void setup() { ui.setFrameAnimation(SLIDE_LEFT); ui.setTargetFPS(30); ui.disableAllIndicators(); - ui.setFrames(frames, numberOfFrames); + ui.setFrames(frames, (numberOfFrames)); frames[0] = drawScreen1; frames[1] = drawScreen2; frames[2] = drawScreen3; + clockFrame[0] = drawClock; ui.setOverlays(overlays, numberOfOverlays); // Inital UI takes care of initalising the display too. @@ -278,7 +285,7 @@ void loop() { } } - checkDisplay(); // this will see if we need to turn it on or off for night mode. + checkDisplay(); // Check to see if the printer is on or offline and change display. ui.update(); @@ -321,6 +328,7 @@ void handleUpdateConfig() { OctoPrintApiKey = server.arg("octoPrintApiKey"); OctoPrintServer = server.arg("octoPrintAddress"); OctoPrintPort = server.arg("octoPrintPort").toInt(); + DISPLAYCLOCK = server.hasArg("isClockEnabled"); minutesBetweenDataRefresh = server.arg("refresh").toInt(); themeColor = server.arg("theme"); UtcOffset = server.arg("utcoffset").toFloat(); @@ -329,6 +337,9 @@ void handleUpdateConfig() { temp = server.arg("stationpassword"); temp.toCharArray(www_password, sizeof(temp)); writeSettings(); + printerClient.updateOctoPrintClient(OctoPrintApiKey, OctoPrintServer, OctoPrintPort); + printerClient.getPrinterJobResults(); + checkDisplay(); lastEpoch = 0; redirectHome(); } @@ -366,6 +377,11 @@ void handleConfigure() { form.replace("%OCTOKEY%", OctoPrintApiKey); form.replace("%OCTOADDRESS%", OctoPrintServer); form.replace("%OCTOPORT%", String(OctoPrintPort)); + String isClockChecked = ""; + if (DISPLAYCLOCK) { + isClockChecked = "checked='checked'"; + } + form.replace("%IS_CLOCK_CHECKED%", isClockChecked); String options = ""; options.replace(">"+String(minutesBetweenDataRefresh)+"<", " selected>"+String(minutesBetweenDataRefresh)+"<"); form.replace("%OPTIONS%", options); @@ -415,11 +431,18 @@ void redirectHome() { } String getHeader() { + return getHeader(false); +} + +String getHeader(boolean refresh) { String menu = String(WEB_ACTIONS); String html = ""; html += "Printer Monitor"; html += ""; + if (refresh) { + html += ""; + } html += ""; html += ""; html += ""; @@ -467,7 +490,7 @@ void displayPrinterStatus() { server.sendHeader("Expires", "-1"); server.setContentLength(CONTENT_LENGTH_UNKNOWN); server.send(200, "text/html", ""); - server.sendContent(String(getHeader())); + server.sendContent(String(getHeader(true))); html += "

Time: " + timeClient.getAmPmHours() + ":" + timeClient.getMinutes() + ":" + timeClient.getSeconds() + " " + timeClient.getAmPm() + "

"; html += "

"; @@ -585,6 +608,13 @@ void drawScreen3(OLEDDisplay *display, OLEDDisplayUiState* state, int16_t x, int display->drawString(64 + x, 14 + y, time); } +void drawClock(OLEDDisplay *display, OLEDDisplayUiState* state, int16_t x, int16_t y) { + display->setTextAlignment(TEXT_ALIGN_CENTER); + display->setFont(ArialMT_Plain_24); + String time = timeClient.getAmPmHours() + ":" + timeClient.getMinutes() + ":" + timeClient.getSeconds(); + display->drawString(64 + x, 10 + y, time); +} + String zeroPad(int value) { String rtnValue = String(value); if (value < 10) { @@ -614,6 +644,28 @@ void drawHeaderOverlay(OLEDDisplay *display, OLEDDisplayUiState* state) { display->drawHorizontalLine(0, 43, updatePos); display->drawHorizontalLine(0, 44, updatePos); display->drawHorizontalLine(0, 45, updatePos); + + drawRssi(display); +} + +void drawClockHeaderOverlay(OLEDDisplay *display, OLEDDisplayUiState* state) { + display->setColor(WHITE); + display->setFont(ArialMT_Plain_16); + String time = timeClient.getAmPm(); + display->setTextAlignment(TEXT_ALIGN_LEFT); + display->drawString(0, 48, time); + display->setFont(ArialMT_Plain_16); + display->setTextAlignment(TEXT_ALIGN_CENTER); + display->drawString(64, 48, "offline"); + display->setTextAlignment(TEXT_ALIGN_LEFT); + + display->drawRect(0, 41, 128, 2); + + drawRssi(display); +} + +void drawRssi(OLEDDisplay *display) { + int8_t quality = getWifiQuality(); for (int8_t i = 0; i < 4; i++) { @@ -653,6 +705,7 @@ void writeSettings() { f.println("themeColor=" + themeColor); f.println("www_username=" + String(www_username)); f.println("www_password=" + String(www_password)); + f.println("DISPLAYCLOCK=" + String(DISPLAYCLOCK)); } f.close(); readSettings(); @@ -709,11 +762,13 @@ void readSettings() { temp.toCharArray(www_password, sizeof(temp)); Serial.println("www_password=" + String(www_password)); } + if (line.indexOf("DISPLAYCLOCK=") >= 0) { + DISPLAYCLOCK = line.substring(line.lastIndexOf("DISPLAYCLOCK=") + 13).toInt(); + Serial.println("DISPLAYCLOCK=" + String(DISPLAYCLOCK)); + } } fr.close(); timeClient.setUtcOffset(UtcOffset); - printerClient.updateOctoPrintClient(OctoPrintApiKey, OctoPrintServer, OctoPrintPort); - printerClient.getPrinterJobResults(); } int getMinutesFromLastRefresh() { @@ -728,7 +783,10 @@ int getMinutesFromLastDisplay() { // Toggle on and off the display if user defined times void checkDisplay() { - if (displayOn && !(printerClient.isOperational() || printerClient.isPrinting())) { + if (!displayOn && DISPLAYCLOCK) { + enableDisplay(true); + } + if (displayOn && !(printerClient.isOperational() || printerClient.isPrinting()) && !DISPLAYCLOCK) { // Put Display to sleep display.clear(); display.display(); @@ -739,8 +797,9 @@ void checkDisplay() { display.display(); delay(5000); enableDisplay(false); - return; - } else if (!displayOn) { + Serial.println("Printer is offline going down to sleep..."); + return; + } else if (!displayOn && !DISPLAYCLOCK) { if (printerClient.isOperational()) { // Wake the Screen up enableDisplay(true); @@ -751,9 +810,25 @@ void checkDisplay() { display.setContrast(255); // default is 255 display.drawString(64, 5, "Printer Online\nWake up..."); display.display(); + Serial.println("Printer is online waking up..."); delay(5000); return; } + } else if (DISPLAYCLOCK) { + if (!printerClient.isOperational() && !isClockOn) { + Serial.println("Clock Mode is turned on."); + ui.disableAutoTransition(); + ui.setFrames(clockFrame, 1); + clockFrame[0] = drawClock; + ui.setOverlays(clockOverlay, numberOfOverlays); + isClockOn = true; + } else if (printerClient.isOperational() && isClockOn) { + Serial.println("Printer Monitor is active."); + ui.setFrames(frames, numberOfFrames); + ui.setOverlays(overlays, numberOfOverlays); + ui.enableAutoTransition(); + isClockOn = false; + } } }