Added blinking support.
This commit is contained in:
parent
196d504fa7
commit
c9e366b542
|
@ -172,39 +172,104 @@ void SevenSegmentClock::displaySeperator(char seperatorCharacter) {
|
||||||
strip->setPixelColor(clockSeperatorLed2, currentColor);
|
strip->setPixelColor(clockSeperatorLed2, currentColor);
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
|
Serial.print("SevenSegmentClock::displaySeperator: Unknown character to be displayed: ");
|
||||||
|
Serial.println(seperatorCharacter);
|
||||||
|
case ' ':
|
||||||
|
case 0:
|
||||||
strip->setPixelColor(decimalPointLed, black);
|
strip->setPixelColor(decimalPointLed, black);
|
||||||
strip->setPixelColor(clockSeperatorLed1, black);
|
strip->setPixelColor(clockSeperatorLed1, black);
|
||||||
strip->setPixelColor(clockSeperatorLed2, black);
|
strip->setPixelColor(clockSeperatorLed2, black);
|
||||||
Serial.print("SevenSegmentClock::displaySeperator: Unknown character to be displayed: ");
|
|
||||||
Serial.println(seperatorCharacter);
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static uint32_t lastUpdate_ms = 0;
|
|
||||||
|
|
||||||
void SevenSegmentClock::displayTime(int hour, int minute) {
|
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];
|
char displayText[4];
|
||||||
|
static int lastHour=0, lastMinute=0;
|
||||||
if (clockHour != hour || clockMinute != minute || millis()-lastUpdate_ms > TIME_BETWEEN_DISPLAY_UPDATES_ms) {
|
static uint32_t lastUpdate_ms = 0;
|
||||||
clockHour = hour;
|
static uint32_t nextBlinkSwitch_ms = 0;
|
||||||
clockMinute = minute;
|
static boolean currentlyBlinkOn = false;
|
||||||
Serial.print("SevenSegmentClock: new time "); Serial.print(clockHour); Serial.print(":"); Serial.println(clockMinute);
|
|
||||||
|
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[0] = (hour > 9) ? '0' + (hour/10) : ' ';
|
||||||
displayText[1] = '0' + hour % 10;
|
displayText[1] = '0' + hour % 10;
|
||||||
displayText[2] = '0' + minute / 10;
|
displayText[2] = '0' + minute / 10;
|
||||||
displayText[3] = '0' + minute % 10;
|
displayText[3] = '0' + minute % 10;
|
||||||
displayDigit(0, displayText[0]);
|
switch (blinkMode) {
|
||||||
displayDigit(1, displayText[1]);
|
case NoBlinking:
|
||||||
displayDigit(2, displayText[2]);
|
displayDigit(0, displayText[0]);
|
||||||
displayDigit(3, displayText[3]);
|
displayDigit(1, displayText[1]);
|
||||||
displaySeperator(':');
|
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();
|
strip->show();
|
||||||
Serial.print("Shown: "); Serial.print(displayText[0]); Serial.print(displayText[1]);
|
Serial.print("Shown: "); Serial.print(displayText[0]); Serial.print(displayText[1]);
|
||||||
Serial.print(':'); Serial.print(displayText[2]); Serial.println(displayText[3]);
|
Serial.print(':'); Serial.print(displayText[2]); Serial.println(displayText[3]);
|
||||||
lastUpdate_ms = millis();
|
lastUpdate_ms = millis();
|
||||||
}
|
}
|
||||||
};
|
}
|
||||||
|
|
||||||
uint32_t SevenSegmentClock::red, SevenSegmentClock::green, SevenSegmentClock::blue, SevenSegmentClock::white, SevenSegmentClock::black;
|
uint32_t SevenSegmentClock::red, SevenSegmentClock::green, SevenSegmentClock::blue, SevenSegmentClock::white, SevenSegmentClock::black;
|
||||||
uint8_t SevenSegmentClock::LedDataPin;
|
uint8_t SevenSegmentClock::LedDataPin;
|
||||||
|
|
|
@ -5,7 +5,8 @@
|
||||||
|
|
||||||
// avoid flickering of the display:
|
// avoid flickering of the display:
|
||||||
#define TIME_BETWEEN_DISPLAY_UPDATES_ms 300
|
#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
|
#define defaultLedDataPin 2
|
||||||
class SevenSegmentClock {
|
class SevenSegmentClock {
|
||||||
public:
|
public:
|
||||||
|
@ -13,7 +14,10 @@ public:
|
||||||
SevenSegmentClock(uint8_t dataPin) { LedDataPin=dataPin; init(); };
|
SevenSegmentClock(uint8_t dataPin) { LedDataPin=dataPin; init(); };
|
||||||
void begin(void);
|
void begin(void);
|
||||||
void displayTime(int hour, int minute);
|
void displayTime(int hour, int minute);
|
||||||
|
void displayUpdate(void);
|
||||||
//void setClockSpeed(int _msPerModelSecond) { msPerModelSecond = _msPerModelSecond; setClockSpeed("x"); };
|
//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; };
|
void setClockHalted(bool halted) { clockHalted = halted; };
|
||||||
static uint32_t red, green, blue, white, black;
|
static uint32_t red, green, blue, white, black;
|
||||||
enum ClockDisplayStatus { Off, Booting, Halted, StandardClock, FastClock };
|
enum ClockDisplayStatus { Off, Booting, Halted, StandardClock, FastClock };
|
||||||
|
@ -23,6 +27,7 @@ private:
|
||||||
void init(void) { displayStatus = Off; clockHour=12; clockMinute=34; setClockHalted(true); };
|
void init(void) { displayStatus = Off; clockHour=12; clockMinute=34; setClockHalted(true); };
|
||||||
static uint8_t LedDataPin;
|
static uint8_t LedDataPin;
|
||||||
static Adafruit_NeoPixel *strip;
|
static Adafruit_NeoPixel *strip;
|
||||||
|
static BlinkMode blinkMode;
|
||||||
ClockDisplayStatus displayStatus;
|
ClockDisplayStatus displayStatus;
|
||||||
int clockHour;
|
int clockHour;
|
||||||
int clockMinute;
|
int clockMinute;
|
||||||
|
|
16
src/main.cpp
16
src/main.cpp
|
@ -181,11 +181,17 @@ void setup() {
|
||||||
}
|
}
|
||||||
|
|
||||||
int hours = 0, minutes = 0;
|
int hours = 0, minutes = 0;
|
||||||
|
uint32_t nextUpdate_ms = 0;
|
||||||
|
|
||||||
void loop() {
|
void loop() {
|
||||||
sevenSegmentClock.displayTime(hours, minutes++);
|
if (millis() > nextUpdate_ms) {
|
||||||
if (minutes > 99) { minutes = 0; }
|
nextUpdate_ms = millis() + 1000;
|
||||||
if (minutes % 5 == 0) hours++;
|
minutes++;
|
||||||
if (hours > 99) hours = 0;
|
if (minutes > 99) { minutes = 0; }
|
||||||
delay(1000);
|
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();
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue