Skip to content

Commit 1da8c43

Browse files
committed
Better scrolling-in and scrolling-out
1 parent a5b8674 commit 1da8c43

File tree

1 file changed

+45
-18
lines changed

1 file changed

+45
-18
lines changed

wordclock/wordclock.ino

+45-18
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,16 @@
55

66
// Manual for library: http://lygte-info.dk/project/DisplayDriver%20UK.html
77

8-
#define numberOfDigits 16
8+
#define NUM_DIGITS 16
99
const byte dataPin = A4;
1010
const byte clockPin = A5;
1111

12-
LEDDisplayDriver display(dataPin, clockPin, true, numberOfDigits);
12+
LEDDisplayDriver display(dataPin, clockPin, true, NUM_DIGITS);
13+
14+
#define BUFFER_SIZE 100
15+
char dateTimeString[BUFFER_SIZE];
16+
char temp[BUFFER_SIZE];
1317

14-
char dateTimeString[32];
1518

1619
ThreeWire myWire(9, 8, 10); // IO, SCLK, CE
1720
RtcDS1302<ThreeWire> Rtc(myWire);
@@ -27,7 +30,7 @@ void setup() {
2730
display.showTextScroll("Hello Wordclock");
2831
delay(2000);
2932

30-
Serial.println("hello serial Wordclock");
33+
Serial.println("Hello serial Wordclock");
3134
Rtc.Begin();
3235
if (!Rtc.IsDateTimeValid()) {
3336
// Common Causes:
@@ -53,9 +56,9 @@ void setup() {
5356
populateTimeString(now);
5457
Serial.println(dateTimeString);
5558
if (now < compiled) {
56-
Serial.println("RTC is older than compile time! (Updating DateTime)");
57-
display.showTextScroll("RTC is older than compile time! (Updating DateTime)");
59+
Serial.println("RTC is older than compile time! (Updating DateTime)");
5860
Rtc.SetDateTime(compiled);
61+
display.showTextScroll("RTC is older than compile time! (Updating DateTime)");
5962
} else if (now > compiled) {
6063
Serial.println("RTC is newer than compile time. (this is expected)");
6164
} else if (now == compiled) {
@@ -76,9 +79,10 @@ void loop() {
7679
display.showTextScroll(dateTimeString);
7780
delay(2000);
7881
populateTimeString2(now);
79-
display.showTextScroll(dateTimeString);
82+
for (int i = 0; i < 5; ++i) {
83+
display.showTextScroll(dateTimeString);
84+
}
8085
}
81-
delay(10000);
8286
}
8387

8488

@@ -114,21 +118,44 @@ void populateTimeString2(const RtcDateTime& dt) {
114118

115119
if (minute >= 58 || minute <= 3) {
116120
if (hour == 0 || hour == 12 || hour == 24) {
117-
// don't show "noon o'clock"
121+
// don't show "noon o'clock" or "midnight o'clock"
118122
minuteStr = "";
119123
separator = "";
120124
} else {
121-
// swap; instead of "O'clock Ten" it's "Ten O'clock".
125+
// swap; instead of "O'clock Ten" show "Ten O'clock".
122126
const char *temp; temp = hourStr; hourStr = minuteStr; minuteStr = temp;
123127
}
124128
}
125-
snprintf_P(dateTimeString,
126-
countof(dateTimeString),
129+
snprintf_P(temp,
130+
countof(temp),
127131
PSTR("%s%s%s"),
128132
minuteStr,
129133
separator,
130134
hourStr
131135
);
136+
// Figure out how many spaces to add to the beginning and end of the string;
137+
// the string can be up to BUFFER_SIZE long, and have at most NUM_DIGITS spaces
138+
// at the beginning and end.
139+
int numSpace = 0;
140+
int len = strlen(temp);
141+
// I should math the shit out of this
142+
while (len < BUFFER_SIZE && numSpace < NUM_DIGITS) {
143+
numSpace++;
144+
len+=2;
145+
}
146+
// Serial.print("oldlen: "); Serial.println(strlen(temp));
147+
// Serial.print("newlen: "); Serial.println(len);
148+
// Serial.print("numspace: "); Serial.println(numSpace);
149+
// Clear the final destination; copy the temporary string into the right
150+
// spot, then fix the end-of-string marker.
151+
memset(dateTimeString, ' ', BUFFER_SIZE);
152+
strcpy(&dateTimeString[numSpace], temp);
153+
dateTimeString[numSpace + strlen(temp)] = ' ';
154+
dateTimeString[len] = 0;
155+
Serial.print("old string: "); Serial.println(temp);
156+
Serial.print("new string: ."); Serial.print(dateTimeString); Serial.println(".");
157+
// . Twenty to Four .
158+
// . Quarter to Four .
132159
}
133160

134161
const char *getHour(int hour) {
@@ -152,16 +179,16 @@ const char *getHour(int hour) {
152179
const char *getMinute(int minute) {
153180
switch (minute) {
154181
case 58: case 59: case 0: case 1: case 2: case 3: return "O'clock";
155-
case 4: case 5: case 6: case 7: return "Five Past";
182+
case 4: case 5: case 6: case 7: return "Five minutes Past";
156183
case 8: case 9: case 10: case 11: case 12: case 13: return "Ten Past";
157-
case 14: case 15: case 16: return "Quarter Past";
158-
case 17: case 18: case 19: case 20: case 21: case 22: case 23: return "Twenty Past";
184+
case 14: case 15: case 16: return "A Quarter Past";
185+
case 17: case 18: case 19: case 20: case 21: case 22: case 23: return "Twenty minutes Past";
159186
case 24: case 25: case 26: case 27: return "Twenty five Past";
160187
case 28: case 29: case 30: case 31: case 32: return "Half Past";
161188
case 33: case 34: case 35: case 36: return "Twenty five to";
162-
case 37: case 38: case 39: case 40: case 41: case 42: case 43: return "Twenty to";
163-
case 44: case 45: case 46: return "Quarter to";
189+
case 37: case 38: case 39: case 40: case 41: case 42: case 43: return "Twenty minutes to";
190+
case 44: case 45: case 46: return "A Quarter to";
164191
case 47: case 48: case 49: case 50: case 51: case 52: return "Ten to";
165-
case 53: case 54: case 55: case 56: case 57: return "Five to";
192+
case 53: case 54: case 55: case 56: case 57: return "Five minutes to";
166193
}
167194
}

0 commit comments

Comments
 (0)