From 708aeb2e72a27e1c029656fc76031cedc20120e6 Mon Sep 17 00:00:00 2001 From: Dirk Jahnke Date: Wed, 14 Nov 2018 11:12:36 +0100 Subject: [PATCH] Added clock-halt handling and improved display. --- README.md | 4 ++-- src/display.cpp | 36 ++++++++++++++++++++++-------------- src/display.h | 1 + src/fastclock.cpp | 6 ++++++ src/fastclock.h | 5 ++++- src/main.cpp | 16 ++++++++++++++++ 6 files changed, 51 insertions(+), 17 deletions(-) diff --git a/README.md b/README.md index 6155bc1..c8ab696 100644 --- a/README.md +++ b/README.md @@ -13,8 +13,8 @@ The tasks of the master clock / controller is: ## Links / References - Digitrax Loconet PE: http://www.digitrax.com/static/apps/cms/media/documents/loconet/loconetpersonaledition.pdf --- we do not support loconet yet, but it is an option to be added later --- see page 14 ff. about FAST Clock definitions + - we do not support loconet yet, but it is an option to be added later + - see page 14 ff. about FAST Clock definitions - Rocrail with a description of the loconet interface (German): https://wiki.rocrail.net/doku.php?id=loconet:ln-pe-de - List of loconet interfaces: http://loconetovertcp.sourceforge.net/Client/index.html, and here especially the fast clock: http://loconetovertcp.cvs.sourceforge.net/loconetovertcp/tcp/client/Clock/ - FREMO had some discussions about fast clocks, see the forum (available to members only) diff --git a/src/display.cpp b/src/display.cpp index dd7fa37..d6790ed 100644 --- a/src/display.cpp +++ b/src/display.cpp @@ -128,12 +128,18 @@ void Display::addLogMessage(const char *message) { void Display::showDashboard(void) { static unsigned long lastDisplayUpdate_ms = 0; + static unsigned long lastBlinkChange_ms = 0; + static bool blinkOnCycle = false; // toggles the on/off blinking phase currentScreen = DashboardScreen; // avoid flickering of the display: if (millis() - lastDisplayUpdate_ms < TIME_BETWEEN_DISPLAY_UPDATES_ms) return; lastDisplayUpdate_ms = millis(); - + if (lastDisplayUpdate_ms - lastBlinkChange_ms > BLINK_ON_OFF_TIME_ms) { + lastBlinkChange_ms = lastDisplayUpdate_ms; + blinkOnCycle = !blinkOnCycle; + } + u8g2.clearBuffer(); u8g2.setDrawColor(1); @@ -143,26 +149,28 @@ void Display::showDashboard(void) { u8g2.print(clockName); // ****** speed ***** - setNormalTextSize(); - u8g2.setCursor(55, 2*getTextHeight()-1); + setSmallTextSize(); + u8g2.setCursor(55, 2*getTextHeight()); u8g2.print(clockSpeed); // ***** time ***** - setLargeTextSize(); - u8g2.setCursor(0, u8g2.getDisplayHeight()-1); - if (clockHour < 10) u8g2.print("0"); - u8g2.print(clockHour); - u8g2.print(":"); - if (clockMinute < 10) u8g2.print("0"); - u8g2.print(clockMinute); + if (!clockHalted || blinkOnCycle) { + setLargeTextSize(); + u8g2.setCursor(0, u8g2.getDisplayHeight()-1); + if (clockHour < 10) u8g2.print("0"); + u8g2.print(clockHour); + u8g2.print(":"); + if (clockMinute < 10) u8g2.print("0"); + u8g2.print(clockMinute); + } // ***** halt/go ***** if (clockHalted) { setSmallTextSize(); - u8g2.setDrawColor(1); - u8g2.drawBox(55, u8g2.getDisplayHeight() - 2*getTextHeight()-3, 5*4+2, getTextHeight()); - u8g2.setDrawColor(0); - u8g2.setCursor(57, u8g2.getDisplayHeight() - getTextHeight()-3); + u8g2.setDrawColor(blinkOnCycle ? 1 : 0); + u8g2.drawBox(55, u8g2.getDisplayHeight() - 2*getTextHeight()-4, 4*u8g2.getMaxCharWidth()+3, getTextHeight()+1); + u8g2.setDrawColor(blinkOnCycle ? 0 : 1); + u8g2.setCursor(57, u8g2.getDisplayHeight() - getTextHeight()-4); u8g2.print("HALT"); u8g2.setDrawColor(1); } diff --git a/src/display.h b/src/display.h index 531d730..4676ad9 100644 --- a/src/display.h +++ b/src/display.h @@ -3,6 +3,7 @@ // avoid flickering of the display: #define TIME_BETWEEN_DISPLAY_UPDATES_ms 200 +#define BLINK_ON_OFF_TIME_ms 1000 #define MAX_CLOCK_NAME_LEN 8 #define MAX_CLOCK_SPEED_LEN 8 diff --git a/src/fastclock.cpp b/src/fastclock.cpp index c00104d..2bc24aa 100644 --- a/src/fastclock.cpp +++ b/src/fastclock.cpp @@ -53,6 +53,7 @@ void Fastclock::loop(void) { msPerModelSecond = 500; } if (newTimeTick - lastTimeTickUsed < MIN_TIME_ms_BETWEEN_CLOCK_UPDATES) return; + if (halted) { lastTimeTickUsed = newTimeTick; return; } int fastclockTimeAdvance = (newTimeTick - lastTimeTickUsed) * 1000 / msPerModelSecond; oldMinute = minute; @@ -72,4 +73,9 @@ void Fastclock::loop(void) { void Fastclock::begin(void) { lastTimeTickUsed = millis(); Serial.print("*** Setting up Fastclock, init lastSentTimeTick="); Serial.println(lastTimeTickUsed); + // following setters do not change anything, but they update the display as well + setClockSpeed(msPerModelSecond); + + display->setClockHalted(halted); + display->setTime(hour, minute); } diff --git a/src/fastclock.h b/src/fastclock.h index 1a0325a..e9d0778 100644 --- a/src/fastclock.h +++ b/src/fastclock.h @@ -4,10 +4,11 @@ #include "display.h" #define MIN_TIME_ms_BETWEEN_CLOCK_UPDATES 200 +#define DEFAULT_ms_PER_MODEL_SECOND 250 class Fastclock { public: - Fastclock(Display *d): display(d) { weekday=0; hour=0; minute=0; second=0; millisecond=0; msPerModelSecond=500; }; + Fastclock(Display *d): display(d) { weekday=0; hour=0; minute=0; second=0; millisecond=0; msPerModelSecond=DEFAULT_ms_PER_MODEL_SECOND; halted = true; }; void begin(void); void loop(void); void incrementClockByMilliseconds(int amount); @@ -16,6 +17,7 @@ public: void setTime(uint8_t hours, uint8_t minutes, uint8_t seconds) { hour = hours; minute = minutes; second = seconds; millisecond = 0; }; void setTime(uint8_t hours, uint8_t minutes, uint8_t seconds, uint16_t milliseconds) { hour = hours; minute = minutes; second = seconds; millisecond = milliseconds; }; void setWeekday(uint8_t _weekday) { weekday = _weekday; }; + void setClockHalted(bool setToHalt) { halted = setToHalt; display->setClockHalted(halted); }; private: Display *display; unsigned long lastTimeTickUsed; // used to calculate model time @@ -25,6 +27,7 @@ private: uint8_t minute; uint8_t second; uint16_t millisecond; + bool halted; }; diff --git a/src/main.cpp b/src/main.cpp index 594ce22..9edb7c3 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -15,6 +15,8 @@ #include "display.h" #include "fastclock.h" +#define AUTOSTART_CLOCK_AFTER_ms 10000 + //define your default values here, if there are different values in config.json, they are overwritten. //length should be max size + 1 char mqtt_server[40] = ""; @@ -229,6 +231,20 @@ void loop(void) return; } +#if defined(AUTOSTART_CLOCK_AFTER_ms) +#if AUTOSTART_CLOCK_AFTER_ms > 0 + static unsigned long autostart_ms = 0; + static bool autostartDone = false; + if (autostart_ms == 0) { + autostart_ms = millis() + AUTOSTART_CLOCK_AFTER_ms; + } else if (!autostartDone && millis() > autostart_ms) { + autostartDone = true; + fastclock.setClockHalted(false); + Serial.println("Clock started automatically"); + } +#endif +#endif + fastclock.loop(); display.showDashboard();