-
Notifications
You must be signed in to change notification settings - Fork 4
/
Wordclock.ino
627 lines (546 loc) · 16.9 KB
/
Wordclock.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
//#include <Adafruit_GFX.h>
#include <EEPROM.h>
#include <SoftwareSerial.h>
#include <FastLED.h>
#include <DS3232RTC.h> //http://github.com/JChristensen/DS3232RTC
#include <Time.h> //https://github.com/PaulStoffregen/Time
#include <Timezone.h> //https://github.com/JChristensen/Timezone
#include <Wire.h> //http://arduino.cc/en/Reference/Wire (included with Arduino IDE)
//#include <glcdfont.c>
#define EEPROM_VERSION 3
SoftwareSerial btSerial(8, 9);
CRGB leds[10 * 11 + 4];
const byte ES[] = {0, 0, 2};
const byte IST[] = {0, 3, 3};
const byte FUENF[] = {0, 7, 4};
const byte ZEHN[] = {1, 0, 4};
const byte ZWANZIG[] = {1, 4, 7};
const byte VIERTEL[] = {2, 4, 7};
const byte NACH[] = {3, 2, 4};
const byte VOR[] = {3, 6, 3};
const byte HALB[] = {4, 0, 4};
const byte UHR[] = {9, 8, 3};
const byte HOUR[][3] = {
//EIN EINS ZWEI DREI VIER FUENF
{5, 2, 3}, {5, 2, 4}, {5, 0, 4}, {6, 1, 4}, {7, 7, 4}, {6, 7, 4},
//SECHS SIEBEN ACHT NEUN ZEHN ELF ZWÖLF
{9, 1, 5}, {5, 5, 6}, {8, 1, 4}, {7, 3, 4}, {8, 5, 4}, {7, 0, 3}, {4, 5, 5}
};
/**
* Contains all added words since the last call of generateWords().
*/
const byte *new_words[6];
byte new_words_length = 0;
/**
* Contains all removed words since the last call of generateWords().
*/
const byte *old_words[6];
byte old_words_length = 0;
/*
* Contains all unchanged words since the last call of generateWords()
*/
const byte *const_words[6];
byte const_words_length = 0;
/**
* Fore- and background color for the letters.
*/
CRGB foreground = CRGB(0, 0, 255);
CRGB background = CRGB(0, 0, 0); //CRGB(0, 255, 0) CRGB(10, 50, 5)
/**
* Stores the effect number.
*/
byte effect = 2;
/**
* Hardware version.
*/
byte hwVersion = 0;
/**
* Threshold for dark mode (light sensor)
*/
byte lightThreshold = 15;
/**
* Brightness in dark mode
*/
byte darkBrightness = 40;
/**
* Should "Es ist" be showed?
*/
bool showEsIst = true;
//TimeChangeRule aEDT = {"AEDT", First, Sun, Oct, 2, 660}; //UTC + 11 hours
//TimeChangeRule aEST = {"AEST", First, Sun, Apr, 3, 600}; //UTC + 10 hours
TimeChangeRule CEST = {"CEST", Last, Sun, Mar, 2, 120}; //Central European Summer Time
TimeChangeRule CET = {"CET ", Last, Sun, Oct, 3, 60}; //Central European Standard Time
//TimeChangeRule BST = {"BST", Last, Sun, Mar, 1, 60}; //British Summer Time
//TimeChangeRule GMT = {"GMT", Last, Sun, Oct, 2, 0}; //Standard Time
//TimeChangeRule usEDT = {"EDT", Second, Sun, Mar, 2, -240}; //Eastern Daylight Time = UTC - 4 hours
//TimeChangeRule usEST = {"EST", First, Sun, Nov, 2, -300}; //Eastern Standard Time = UTC - 5 hours
//TimeChangeRule usCDT = {"CDT", Second, dowSunday, Mar, 2, -300};
//TimeChangeRule usCST = {"CST", First, dowSunday, Nov, 2, -360};
//TimeChangeRule usMDT = {"MDT", Second, dowSunday, Mar, 2, -360};
//TimeChangeRule usMST = {"MST", First, dowSunday, Nov, 2, -420};
//TimeChangeRule usPDT = {"PDT", Second, dowSunday, Mar, 2, -420};
//TimeChangeRule usPST = {"PST", First, dowSunday, Nov, 2, -480};
//Timezone ausET(aEDT, aEST); //Australia Eastern Time Zone (Sydney, Melbourne)
Timezone CE(CEST, CET); //Central European Time (Frankfurt, Paris)
//Timezone UK(BST, GMT); //United Kingdom (London, Belfast)
//Timezone usET(usEDT, usEST); //US Eastern Time Zone (New York, Detroit)
//Timezone usCT(usCDT, usCST); //US Central Time Zone (Chicago, Houston)
//Timezone usMT(usMDT, usMST); //US Mountain Time Zone (Denver, Salt Lake City)
//Timezone usAZ(usMST, usMST); //Arizona is US Mountain Time Zone but does not use DST
//Timezone usPT(usPDT, usPST); //US Pacific Time Zone (Las Vegas, Los Angeles)
Timezone timezones[] = {CE};
byte timezone = 0;
void setup() {
Serial.begin(9600);
//setup real time clock
setSyncProvider(RTC.get);
if(timeStatus() != timeSet)
Serial.println("Unable to sync with the RTC");
else
Serial.println("RTC has set the system time");
//setup FastLED
delay( 1000 ); // power-up safety delay
FastLED.addLeds<NEOPIXEL, 7>(leds, 10 * 11 + 4);
FastLED.setBrightness(255);
for (int i = 0; i < 10 * 11 + 4; i++) {
leds[i] = CRGB::Black;
}
FastLED.show();
delay(100);
btSerial.begin(9600);
/*showChar(0, 2, 'A', CRGB(255, 255, 255));
showChar(6, 2, 'O', CRGB(255, 255, 255));
FastLED.show();
delay(1500); */
// update EEPROM if other EEPROM_VERSION is used in code
if (EEPROM.read(0) != EEPROM_VERSION || EEPROM.read(1) != EEPROM_VERSION) {
storeSettings();
}
loadSettings();
Serial.println("initialized");
}
void loop()
{
checkLightSensor();
generateWords();
handleBluetooth();
showCorners(foreground, background);
switch (effect) {
case 1: showFade(foreground, background); break;
case 2: showTypewriter(foreground, background); break;
case 3: showMatrix(foreground, background); break;
case 4: showRollDown(foreground, background); break;
case 5: showParty(foreground, background); break;
case 0:
default:
showSimple(foreground, background);
break;
}
FastLED.show();
delay(100);
}
void checkLightSensor() {
byte lightLevel = map(analogRead(A6), 0, 1023, 0, 255);
if (lightLevel < lightThreshold) {
FastLED.setBrightness(darkBrightness);
}
else {
FastLED.setBrightness(255);
}
}
/**
* Generates the words to show using the actual time.
*/
void generateWords() {
time_t local = timezones[timezone].toLocal(now());
clearWords();
if (showEsIst) {
addWord(ES);
addWord(IST);
}
byte shour = hourFormat12(local);
switch(minute(local) / 5) {
case 0: if (shour == 1) shour = 0; addWord(UHR); break;
case 1: addWord(FUENF); addWord(NACH); break;
case 2: addWord(ZEHN); addWord(NACH); break;
case 3: addWord(VIERTEL); addWord(NACH); break;
case 4: addWord(ZWANZIG); addWord(NACH); break;
case 5: addWord(FUENF); addWord(VOR); addWord(HALB); shour++; break;
case 6: addWord(HALB); shour++; break;
case 7: addWord(FUENF); addWord(NACH); addWord(HALB); shour++; break;
case 8: addWord(ZWANZIG); addWord(VOR); shour++; break;
case 9: addWord(VIERTEL); addWord(VOR); shour++; break;
case 10: addWord(ZEHN); addWord(VOR); shour++; break;
case 11: addWord(FUENF); addWord(VOR); shour++; break;
}
if (shour == 13) shour = 1;
addWord(HOUR[shour]);
}
void showCorners(CRGB on, CRGB off) {
byte num = minute() % 5;
for (byte i = 0; i < 4; i++) {
leds[10 * 11 + i] = (i < num) ? on : off;
}
}
/**
* Shows all the new and constant words.
*/
void showSimple(CRGB on, CRGB off) {
fillLeds(off);
showAllWords(on, new_words, new_words_length);
showAllWords(on, const_words, const_words_length);
}
/**
* Fades new words in and old words out.
*/
void showFade(CRGB on, CRGB off) {
if (new_words_length > 0 || old_words_length > 0) {
for (int e = 1; e <= 256/8; e++) {
int i = e * 8 - 1;
fillLeds(off);
showAllWords(on, const_words, const_words_length);
showAllWords(off.lerp8(on, i), new_words, new_words_length);
showAllWords(on.lerp8(off, i), old_words, old_words_length);
FastLED.show();
delay(50);
}
}
showSimple(on, off);
}
/**
* Rolls characters out and in to change display..
*/
void showRollDown(CRGB on, CRGB off) {
if (new_words_length > 0 || old_words_length > 0) {
for (int e = 1; e <= 10; e++) {
fillLeds(off);
showAllWords(on, const_words, const_words_length, 0, e);
showAllWords(on, old_words, old_words_length, 0, e);
FastLED.show();
delay(80 + (10 - e) * 5);
}
for (int e = 10; e >= 0; e--) {
fillLeds(off);
showAllWords(on, const_words, const_words_length, 0, -e);
showAllWords(on, new_words, new_words_length, 0, -e);
FastLED.show();
delay(80 + (10 - e) * 8);
}
}
showSimple(on, off);
}
/**
* Shows the "matrix effect" in background color and the time in foreground color.
*/
byte matrix_worms[11] = {-5, -10, -3, -13, -1, 0, -1, -5, -6, -11, -4};
void showMatrix(CRGB on, CRGB off) {
_fadeall();
for (byte i = 0; i < 11; i++) {
if (matrix_worms[i] < 10) {
setLeds(matrix_worms[i], i, off, 1, false);
}
else if (matrix_worms[i] == 10) {
matrix_worms[i] = -random8(14);
}
matrix_worms[i]++;
}
showAllWords(on, new_words, new_words_length);
showAllWords(on, const_words, const_words_length);
}
void _fadeall() { for(int i = 0; i < 10 * 11; i++) { leds[i].nscale8(180); } } //180
/**
* Removes old words and inserts new words in a typewriter style.
*/
void showTypewriter(CRGB on, CRGB off) {
if (new_words_length > 0 || old_words_length > 0) {
byte max_old_word = 0;
byte max_new_word = 0;
for (byte i = 0; i < old_words_length; i++)
max_old_word = max(max_old_word, old_words[i][2]);
for (byte i = 0; i < new_words_length; i++)
max_new_word = max(max_new_word, new_words[i][2]);
for (byte i = 0; i <= max_old_word; i++) {
fillLeds(off);
showAllWords(on, old_words, old_words_length, 0, 0, 200, i);
showAllWords(on, const_words, const_words_length);
FastLED.show();
delay(180);
}
fillLeds(off);
showAllWords(on, const_words, const_words_length);
for (byte i = 0; i <= max_new_word; i++) {
showAllWords(on, new_words, new_words_length, 0, 0, i, 0);
FastLED.show();
delay(180);
}
}
showSimple(on, off);
}
/**
* Rolls characters out and in to change display..
*/
void showParty(CRGB on, CRGB off) {
static byte e = 0;
e++;
if (e < 3) return;
e = 0;
for(int i = 0; i < 110; i++) {
leds[i] = CHSV(random8(), 255, 180);
}
showAllWords(on, new_words, new_words_length);
showAllWords(on, const_words, const_words_length);
}
/**
* Parses and executes the bluetooh commands.
*/
void handleBluetooth() {
while (btSerial.available() >= 4) {
byte type = btSerial.read();
switch (type) {
case 'F': { //foreground color
byte red = btSerial.read();
byte green = btSerial.read();
byte blue = btSerial.read();
foreground = CRGB(red, green, blue);
break;
}
case 'B': { //background color
byte red = btSerial.read();
byte green = btSerial.read();
byte blue = btSerial.read();
background = CRGB(red, green, blue);
break;
}
case 'E':
effect = btSerial.read();
showEsIst = (btSerial.read() == 1);
btSerial.read();
break;
case 'T': { //time
byte h = btSerial.read();
byte m = btSerial.read();
byte s = btSerial.read();
setTime(h, m, s, day(), month(), year());
RTC.set(now());
break;
}
case 'D': { //date
byte d = btSerial.read();
byte m = btSerial.read();
byte y = btSerial.read();
setTime(hour(), minute(), second(), d, m, y);
RTC.set(now());
break;
}
case 'Z': { //timeZone
timezone = btSerial.read();
btSerial.read(); btSerial.read();
break;
}
case 'S': {
btSerial.read(); btSerial.read(); btSerial.read(); storeSettings();
break;
}
case 'L': {
lightThreshold = btSerial.read();
darkBrightness = btSerial.read();
btSerial.read();
break;
}
case 'G': { //get
switch (btSerial.read()) {
case 'F':
btSerial.write('F');
btSerial.write(foreground.r);
btSerial.write(foreground.g);
btSerial.write(foreground.b);
break;
case 'B':
btSerial.write('B');
btSerial.write(background.r);
btSerial.write(background.g);
btSerial.write(background.b);
break;
case 'E':
btSerial.write('E');
btSerial.write(effect);
btSerial.write(showEsIst);
btSerial.write('E');
break;
case 'T':
btSerial.write('T');
btSerial.write(hour());
btSerial.write(minute());
btSerial.write(second());
break;
case 'D':
btSerial.write('D');
btSerial.write(day());
btSerial.write(month());
btSerial.write(year());
break;
case 'Z':
btSerial.write('Z');
btSerial.write(timezone);
btSerial.write('Z'); btSerial.write('Z');
break;
case 'L':
btSerial.write('L');
btSerial.write(lightThreshold);
btSerial.write(darkBrightness);
btSerial.write('L');
break;
}
btSerial.read(); btSerial.read();
break;
}
default: {
Serial.print("Unknown command ");
Serial.println(type);
break;
}
}
}
}
/**
* Adds a word to the new words array. When it is was previously in the new words it is added to the constant words.
*/
void addWord(const byte part[]) {
bool in_old_words = 0;
for (byte i = 0; i < old_words_length; i++) {
if (part == old_words[i]) {
in_old_words = 1;
const_words[const_words_length] = part;
const_words_length++;
//remove in old_words
old_words_length--;
for (byte e = i; e < old_words_length; e++) {
old_words[e] = old_words[e + 1];
}
}
}
if (!in_old_words) {
new_words[new_words_length] = part;
new_words_length++;
}
}
/**
* Put all new and constant words to the old words.
*/
void clearWords() {
for (byte i = 0; i < new_words_length; i++) {
old_words[i] = new_words[i];
}
old_words_length = new_words_length;
for (byte i = 0; i < const_words_length; i++) {
old_words[old_words_length] = const_words[i];
old_words_length++;
}
new_words_length = 0;
const_words_length = 0;
}
/**
* Set all Leds to a single color.
*/
void fillLeds(CRGB off) {
setLeds(0, 0, off, 11 * 10, false);
}
/**
* Shows a array of words in a specific color.
*/
void showAllWords(CRGB color, const byte *wds[], byte wds_length) {
showAllWords(color, wds, wds_length, 0, 0, 200, 0);
}
/**
* Shows a array of words in a specific color.
*/
void showAllWords(CRGB color, const byte *wds[], byte wds_length, char xadd, char yadd) {
showAllWords(color, wds, wds_length, xadd, yadd, 200, 0);
}
/**
* Shows a array of words in a specific color.
* @param maxlen determines the max length of every word.
* @param cut cuts the x last characters of every word
*/
void showAllWords(CRGB color, const byte *wds[], byte wds_length, char xadd, char yadd, byte maxlen, byte cut) {
for (byte i = 0; i < wds_length; i++) {
setLeds(wds[i][0] + yadd, wds[i][1] + xadd, color, min(max(wds[i][2] - cut, 0), maxlen), false);
}
}
/**
* Sets a number of leds starting at x, y to the a specific color.
* When add is true the color is added to the existing color at the positions.
*/
void setLeds(int y, int x, CRGB color, int len, bool add) {
if (hwVersion == 2) y = 9 - y;
if (y < 0 || y > 9) return;
int start_led = !(y % 2) ? y * 11 + x : y * 11 + (11 - x) - 1;
int dir = !(y % 2) ? 1 : -1;
if (hwVersion == 1) dir = !dir;
if (add) {
for (int i = 0; i < len; i++) {
leds[start_led + i * dir] += color;
}
}
else {
for (int i = 0; i < len; i++) {
leds[start_led + i * dir] = color;
}
}
}
/**
* Stores settings to EEPROM.
* Increase EEPROM_VERSION if you change the schema.
*/
void storeSettings() {
EEPROM.write(0, EEPROM_VERSION);
EEPROM.write(1, EEPROM_VERSION);
EEPROM.write(2, foreground.r);
EEPROM.write(3, foreground.g);
EEPROM.write(4, foreground.b);
EEPROM.write(5, background.r);
EEPROM.write(6, background.g);
EEPROM.write(7, background.b);
EEPROM.write(8, effect);
EEPROM.write(9, showEsIst);
EEPROM.write(10, hwVersion);
EEPROM.write(11, timezone);
EEPROM.write(12, lightThreshold);
EEPROM.write(13, darkBrightness);
}
/**
* Loads settings from EEPROM.
* Increase EEPROM_VERSION if you change the schema.
*/
void loadSettings() {
foreground.r = EEPROM.read(2);
foreground.g = EEPROM.read(3);
foreground.b = EEPROM.read(4);
background.r = EEPROM.read(5);
background.g = EEPROM.read(6);
background.b = EEPROM.read(7);
effect = EEPROM.read(8);
showEsIst = EEPROM.read(9);
hwVersion = EEPROM.read(10);
timezone = EEPROM.read(11);
lightThreshold = EEPROM.read(12);
darkBrightness = EEPROM.read(13);
}
/**
* Shows a 5x7 char at a specific position.
void showChar(byte x, byte y, unsigned char c, CRGB color) {
for (int8_t i=0; i<6; i++ ) {
uint8_t line;
if (i == 5)
line = 0x0;
else
line = pgm_read_byte(font+(c*5)+i);
for (int8_t j = 0; j<8; j++) {
if (line & 0x1) {
setLeds(y+j, x+i, color, 1, false);
}
line >>= 1;
}
}
}
*/