From 196d504fa781401eb577849c8d7be9fc1913499b Mon Sep 17 00:00:00 2001 From: Dirk Jahnke Date: Thu, 30 May 2019 20:41:01 +0200 Subject: [PATCH 1/2] Fixed led flickering on update -- configurable time between updates --- src/SevenSegmentClock.cpp | 35 +++++++++++++++++++++-------------- 1 file changed, 21 insertions(+), 14 deletions(-) diff --git a/src/SevenSegmentClock.cpp b/src/SevenSegmentClock.cpp index 17b0682..6e80a95 100644 --- a/src/SevenSegmentClock.cpp +++ b/src/SevenSegmentClock.cpp @@ -181,22 +181,29 @@ void SevenSegmentClock::displaySeperator(char seperatorCharacter) { } } +static uint32_t lastUpdate_ms = 0; + void SevenSegmentClock::displayTime(int hour, int minute) { char displayText[4]; - clockHour = hour; - clockMinute = minute; - Serial.print("SevenSegmentClock: new time "); Serial.print(clockHour); Serial.print(":"); Serial.println(clockMinute); - displayText[0] = (hour > 9) ? '0' + (hour/10) : ' '; - displayText[1] = '0' + hour % 10; - displayText[2] = '0' + minute / 10; - displayText[3] = '0' + minute % 10; - displayDigit(0, displayText[0]); - displayDigit(1, displayText[1]); - displayDigit(2, displayText[2]); - displayDigit(3, displayText[3]); - displaySeperator(':'); - strip->show(); - Serial.print("Shown: "); Serial.print(displayText[0]); Serial.print(displayText[1]); Serial.print(':'); Serial.print(displayText[2]); Serial.println(displayText[3]); + + if (clockHour != hour || clockMinute != minute || millis()-lastUpdate_ms > TIME_BETWEEN_DISPLAY_UPDATES_ms) { + clockHour = hour; + clockMinute = minute; + Serial.print("SevenSegmentClock: new time "); Serial.print(clockHour); Serial.print(":"); Serial.println(clockMinute); + displayText[0] = (hour > 9) ? '0' + (hour/10) : ' '; + displayText[1] = '0' + hour % 10; + displayText[2] = '0' + minute / 10; + displayText[3] = '0' + minute % 10; + displayDigit(0, displayText[0]); + displayDigit(1, displayText[1]); + displayDigit(2, displayText[2]); + displayDigit(3, displayText[3]); + displaySeperator(':'); + strip->show(); + Serial.print("Shown: "); Serial.print(displayText[0]); Serial.print(displayText[1]); + Serial.print(':'); Serial.print(displayText[2]); Serial.println(displayText[3]); + lastUpdate_ms = millis(); + } }; uint32_t SevenSegmentClock::red, SevenSegmentClock::green, SevenSegmentClock::blue, SevenSegmentClock::white, SevenSegmentClock::black; From c9e366b54259e34f33afe25b1b77a69c09a68933 Mon Sep 17 00:00:00 2001 From: Dirk Jahnke Date: Thu, 30 May 2019 21:30:49 +0200 Subject: [PATCH 2/2] Added blinking support. --- src/SevenSegmentClock.cpp | 93 +++++++++++++++++++++++++++++++++------ src/SevenSegmentClock.h | 7 ++- src/main.cpp | 16 ++++--- 3 files changed, 96 insertions(+), 20 deletions(-) diff --git a/src/SevenSegmentClock.cpp b/src/SevenSegmentClock.cpp index 6e80a95..055529d 100644 --- a/src/SevenSegmentClock.cpp +++ b/src/SevenSegmentClock.cpp @@ -172,39 +172,104 @@ void SevenSegmentClock::displaySeperator(char seperatorCharacter) { strip->setPixelColor(clockSeperatorLed2, currentColor); break; default: + Serial.print("SevenSegmentClock::displaySeperator: Unknown character to be displayed: "); + Serial.println(seperatorCharacter); + case ' ': + case 0: strip->setPixelColor(decimalPointLed, black); strip->setPixelColor(clockSeperatorLed1, black); strip->setPixelColor(clockSeperatorLed2, black); - Serial.print("SevenSegmentClock::displaySeperator: Unknown character to be displayed: "); - Serial.println(seperatorCharacter); break; } } -static uint32_t lastUpdate_ms = 0; void SevenSegmentClock::displayTime(int hour, int minute) { + clockHour = hour; + clockMinute = minute; + Serial.print("SevenSegmentClock: new time "); + Serial.print(clockHour); Serial.print(":"); Serial.println(clockMinute); + displayUpdate(); +}; + +void SevenSegmentClock::displayUpdate(void) { char displayText[4]; - - if (clockHour != hour || clockMinute != minute || millis()-lastUpdate_ms > TIME_BETWEEN_DISPLAY_UPDATES_ms) { - clockHour = hour; - clockMinute = minute; - Serial.print("SevenSegmentClock: new time "); Serial.print(clockHour); Serial.print(":"); Serial.println(clockMinute); + static int lastHour=0, lastMinute=0; + static uint32_t lastUpdate_ms = 0; + static uint32_t nextBlinkSwitch_ms = 0; + static boolean currentlyBlinkOn = false; + + if (clockHour != lastHour || clockMinute != lastMinute || millis()-lastUpdate_ms > TIME_BETWEEN_DISPLAY_UPDATES_ms) { + lastHour = clockHour; + lastMinute = clockMinute; displayText[0] = (hour > 9) ? '0' + (hour/10) : ' '; displayText[1] = '0' + hour % 10; displayText[2] = '0' + minute / 10; displayText[3] = '0' + minute % 10; - displayDigit(0, displayText[0]); - displayDigit(1, displayText[1]); - displayDigit(2, displayText[2]); - displayDigit(3, displayText[3]); - displaySeperator(':'); + switch (blinkMode) { + case NoBlinking: + displayDigit(0, displayText[0]); + displayDigit(1, displayText[1]); + displayDigit(2, displayText[2]); + displayDigit(3, displayText[3]); + displaySeperator(':'); + break; + case ClockBlinking: + if (currentlyBlinkOn) { + displayDigit(0, displayText[0]); + displayDigit(1, displayText[1]); + displayDigit(2, displayText[2]); + displayDigit(3, displayText[3]); + displaySeperator(':'); + } else { + displayDigit(0, ' '); + displayDigit(1, ' '); + displayDigit(2, ' '); + displayDigit(3, ' '); + displaySeperator(' '); + } + if (millis() > nextBlinkSwitch_ms) { + currentlyBlinkOn = !currentlyBlinkOn; + nextBlinkSwitch_ms = millis() + (currentlyBlinkOn ? BLINK_ON_TIME_ms : BLINK_OFF_TIME_ms); + } + break; + case SeperatorBlinking: + displayDigit(0, displayText[0]); + displayDigit(1, displayText[1]); + displayDigit(2, displayText[2]); + displayDigit(3, displayText[3]); + if (currentlyBlinkOn) { + displaySeperator(':'); + } else { + displaySeperator(' '); + } + if (millis() > nextBlinkSwitch_ms) { + currentlyBlinkOn = !currentlyBlinkOn; + nextBlinkSwitch_ms = millis() + (currentlyBlinkOn ? BLINK_ON_TIME_ms : BLINK_OFF_TIME_ms); + } + break; + case DecimalPointBlinking: + displayDigit(0, displayText[0]); + displayDigit(1, displayText[1]); + displayDigit(2, displayText[2]); + displayDigit(3, displayText[3]); + if (currentlyBlinkOn) { + displaySeperator('.'); + } else { + displaySeperator(' '); + } + if (millis() > nextBlinkSwitch_ms) { + currentlyBlinkOn = !currentlyBlinkOn; + nextBlinkSwitch_ms = millis() + (currentlyBlinkOn ? BLINK_ON_TIME_ms : BLINK_OFF_TIME_ms); + } + break; + } strip->show(); Serial.print("Shown: "); Serial.print(displayText[0]); Serial.print(displayText[1]); Serial.print(':'); Serial.print(displayText[2]); Serial.println(displayText[3]); lastUpdate_ms = millis(); } -}; +} uint32_t SevenSegmentClock::red, SevenSegmentClock::green, SevenSegmentClock::blue, SevenSegmentClock::white, SevenSegmentClock::black; uint8_t SevenSegmentClock::LedDataPin; diff --git a/src/SevenSegmentClock.h b/src/SevenSegmentClock.h index 500c08d..9d7b8dd 100644 --- a/src/SevenSegmentClock.h +++ b/src/SevenSegmentClock.h @@ -5,7 +5,8 @@ // avoid flickering of the display: #define TIME_BETWEEN_DISPLAY_UPDATES_ms 300 -#define BLINK_ON_OFF_TIME_ms 1000 +#define BLINK_OFF_TIME_ms 600 +#define BLINK_ON_TIME_ms 400 #define defaultLedDataPin 2 class SevenSegmentClock { public: @@ -13,7 +14,10 @@ public: SevenSegmentClock(uint8_t dataPin) { LedDataPin=dataPin; init(); }; void begin(void); void displayTime(int hour, int minute); + void displayUpdate(void); //void setClockSpeed(int _msPerModelSecond) { msPerModelSecond = _msPerModelSecond; setClockSpeed("x"); }; + enum BlinkMode { NoBlinking, ClockBlinking, SeperatorBlinking, DecimalPointBlinking }; + void setBlinkMode(BlinkMode _blinkMode) { blinkMode = _blinkMode; }; void setClockHalted(bool halted) { clockHalted = halted; }; static uint32_t red, green, blue, white, black; enum ClockDisplayStatus { Off, Booting, Halted, StandardClock, FastClock }; @@ -23,6 +27,7 @@ private: void init(void) { displayStatus = Off; clockHour=12; clockMinute=34; setClockHalted(true); }; static uint8_t LedDataPin; static Adafruit_NeoPixel *strip; + static BlinkMode blinkMode; ClockDisplayStatus displayStatus; int clockHour; int clockMinute; diff --git a/src/main.cpp b/src/main.cpp index c48a855..a380b4a 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -181,11 +181,17 @@ void setup() { } int hours = 0, minutes = 0; +uint32_t nextUpdate_ms = 0; void loop() { - sevenSegmentClock.displayTime(hours, minutes++); - if (minutes > 99) { minutes = 0; } - if (minutes % 5 == 0) hours++; - if (hours > 99) hours = 0; - delay(1000); + if (millis() > nextUpdate_ms) { + nextUpdate_ms = millis() + 1000; + minutes++; + if (minutes > 99) { minutes = 0; } + if (minutes % 5 == 0) hours++; + if (hours > 99) hours = 0; + sevenSegmentClock.displayTime(hours, minutes); + if (hours % 4 == 0) sevenSegmentClock.setBlinkMode(SeperatorBlinking); else sevenSegmentClock.setBlinkMode(NoBlinking); + } + sevenSegmentClock.displayUpdate(); }