-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOneTenClock.ino
1077 lines (915 loc) · 30.9 KB
/
OneTenClock.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
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING
This code requires significant fiddling inside of the Arduino distribution to
free up enough RAM to run without crashing.
1) Shrink the I2C twi BUFFER_SIZE from 32 to 8 in the following file:
[Arduino Path]/Resources/Java/libraries/Wire/utility/twi.h
2) Shrink the I2C Wire BUFFER_LENGTH from 32 to 8 in the following file:
[Arduino Path]/Resources/Java/libraries/Wire/Wire.h
3) Shrink the Hardware Serial buffers from 16 to 4 in the following file:
[Arduino Path]/Resources/Java/hardware/arduino/cores/arduino/HardwareSerial.cpp
41c41
< #define SERIAL_BUFFER_SIZE 4
---
> #define SERIAL_BUFFER_SIZE 16
43c43
< #define SERIAL_BUFFER_SIZE 4
---
> #define SERIAL_BUFFER_SIZE 64
*/
#include <EEPROM.h> // stock header
#include <avr/pgmspace.h> //AVR library for writing to ROM
#include "Charliplexing.h"
#include <Wire.h> // stock header
#include "RealTimeClockDS1307.h"
#include "tinyfont.h"
#include <stdio.h>
#include <dht11.h> // I'll copy this over eventually
dht11 DHT11;
#define DHT11PIN 17
#include <OneWire.h> // from the Arduino Playground
#define ONE_WIRE_PIN 16
OneWire ds(ONE_WIRE_PIN); // on pin 10
byte ds_addr[8];
#define ONEWIRE_SEARCH 1
#define SET_BUTTON_PIN 14
#define INC_BUTTON_PIN 15
#define COLS 10 // usually x
#define ROWS 11 // usually y
// set to true to enable serial monitor - warning: uses ~2k of progmem and just
// shy of 256 bytes of ram.
#define _DEBUG_ true
// Do you want the cells to dim as they age or stay the same brightness?
#define AGING true
// Do you want the world to be a toroid?
#define TOROID 1
// How often should it display a clock?
#define CLOCK_EVERY 5000
uint8_t max_brightness=31;
byte world[COLS][ROWS][2]; // Create a double buffered world.
byte frame_log[COLS][ROWS];
uint8_t hours;
uint8_t minutes;
boolean isSettingHours = true;
boolean isSettingMinutes = false;
boolean isSettingBrightness = false;
#define BOUNCE_TIME_BUTTON 300 // bounce time in ms for the menu button - too short and every click is an adventure, too long and it's ponderous.
// last time a button was pushed; used for debouncing;
volatile unsigned long lastButtonTime = 0;
// indicates that one of the menu options (0..MAX_OPTION) is currently displayed;
boolean isSettingTime = false;
// when alternating between displaying quotes and time, start with the time;
boolean isDisplayingTime = true;
boolean isDisplayingQuote = false;
// buffer for time display;
char timeBuffer[] = "12:34";
char tempnhum[] = "tttt< hhh;";
//--------------------------------------------------------------------------------
void setup() {
pinMode(SET_BUTTON_PIN, INPUT_PULLUP); // A0
pinMode(INC_BUTTON_PIN, INPUT_PULLUP); // A1
pinMode(DHT11PIN, INPUT);
if(_DEBUG_) { Serial.begin(115200); }
// Reset the OneWire bus before LedSign::Init monkeys with ports. (possibly before SetBrightness?)
ds.reset();
if ( !ds.search(ds_addr)) {
ds.reset_search();
}
// Read the saved defaults
EEReadSettings();
// Initialize the screen
LedSign::Init(GRAYSCALE);
LedSign::SetBrightness(max_brightness);
// Initialize the I2C bus
Wire.begin();
// let the clock seed our randomizer - seems to work
RTC.readClock();
randomSeed(RTC.getSeconds()); // is actually less random than it should be. hrm.
updateTimeBuffer();
// start the oscillator if it isn't already started.
RTC.start();
}
void loop() {
// check SET button;
if (digitalRead(SET_BUTTON_PIN) == 0) {
// "Set time" button was pressed;
processSetButton();
}
// check INC button;
if (isSettingTime) {
if (digitalRead(INC_BUTTON_PIN) == 0) {
// "Inc time" button was pressed;
processIncButton();
// save the new time;
setTime();
}
}
// display the menu option for 5 seconds after menu button was pressed;
if ((lastButtonTime > 0) && (millis() - lastButtonTime < 5000)) {
return; // avoids hitting the next conditional.
}
// return the main mode if no button was pressed for 5 seconds;
if (isSettingTime) {
// just finished setting up the time;
isSettingTime = false;
updateTimeBuffer();
resetDisplay();
// This is smart enough to not keep writing the same value to the eeprom
EESaveSettings();
}
else {
//clear the current world of whatever had been in it.
for(uint8_t y = 0; y < ROWS; y++) { for(uint8_t x = 0; x < COLS; x++) { world[x][y][0] = 0; world[x][y][1] = 0; } }
unsigned long now=millis();
switch(random(4)) {
case 0:
Rain(now,5000);
break;
case 1 ... 3:
Life();
break;
}
if(_DEBUG_) { Serial.println("-----"); Serial.println("Back in loop"); }
// this takes a while, may as well ask for it before a guaranteed second display.
if(_DEBUG_) { Serial.println("Requesting temp."); }
RequestDS18B20Temp();
// Display the time for 3000ms
if(_DEBUG_) { Serial.println("Updating time"); }
updateTimeBuffer();
DisplayTime(3000);
// The brightness routine does strange things to the DHT reading code causing it to timeout more often than I'd like. So I pin it to the maximum and the reads tend to work out nicer.
LedSign::Clear();
LedSign::SetBrightness(127);
// And the additional delay seems to knock out the last one or two errored readings.
delay(50);
float ftemp = GetDS18B20Temp();
char temperature[5];
int chk = DHT11.read(DHT11PIN);
LedSign::SetBrightness(max_brightness);
// if the temperature is within a human range, go ahead
if((ftemp < 50.00) && (ftemp > 2)) {
ftemp = (ftemp*1.8) + 32;
dtostrf(ftemp, -4, 1, temperature);
if(_DEBUG_) {
Serial.print(hours, DEC); Serial.print(":"); Serial.println(minutes, DEC);
Serial.print("Temp: "); Serial.println(temperature);
}
if(chk == 0) {
int humidity = DHT11.humidity;
int dhttemp = DHT11.temperature;
if(_DEBUG_) {
Serial.print("RH: "); Serial.println(humidity, DEC);
Serial.print("DHT Temp: "); Serial.println(dhttemp, DEC);
}
sprintf(tempnhum, "%s< %d;", temperature, humidity);
Banner(tempnhum, 100, random(6));
}
else {
if(_DEBUG_) {
Serial.print("RH: ");
Serial.println("ERR");
Serial.print("Chksum: "); Serial.println(chk);
}
sprintf(tempnhum, "%s<", temperature);
Banner(tempnhum, 100, random(6));
}
}
else {
if(_DEBUG_) {
Serial.print("Temp Error: ");
Serial.println(ftemp);
}
}
}
}
//--------------------------------------------------------------------------------
// functions
void EEReadSettings (void) { // TODO: Detect ANY bad values, not just 255.
byte detectBad = 0;
byte value = 255;
value = EEPROM.read(0);
if ((value > 49) || (value < 31))
detectBad = 1;
else
max_brightness = value; // MainBright has maximum possible value of 8.
if (detectBad) {
max_brightness = 49;
}
}
void EESaveSettings (void){
//EEPROM.write(Addr, Value);
// Careful if you use this function: EEPROM has a limited number of write
// cycles in its life. Good for human-operated buttons, bad for automation.
byte value = EEPROM.read(0);
if(value != max_brightness) {
EEPROM.write(0, max_brightness);
if(_DEBUG_) { Serial.println("eesave"); }
}
else {
if(_DEBUG_) { Serial.println("eenosave"); }
}
}
void Logo(unsigned long runtime) {
if(_DEBUG_) { Serial.println("-----"); Serial.println("Logo starting"); }
LedSign::Clear();
switch(random(2)) {
case 0: // USAF logo. Sort of.
for(byte g=0; g<=7; g++) {
LedSign::Set(0,1,g); LedSign::Set(0,2,g); LedSign::Set(0,3,g); LedSign::Set(0,4,g); LedSign::Set(0,5,g); LedSign::Set(1,0,g); LedSign::Set(1,3,g); LedSign::Set(1,4,g); LedSign::Set(1,5,g); LedSign::Set(1,6,g); LedSign::Set(2,4,g); LedSign::Set(2,5,g); LedSign::Set(2,6,g); LedSign::Set(3,5,g); LedSign::Set(3,6,g); LedSign::Set(3,9,g); LedSign::Set(4,7,g); LedSign::Set(4,8,g); LedSign::Set(4,9,g); LedSign::Set(4,10,g); LedSign::Set(5,7,g); LedSign::Set(5,8,g); LedSign::Set(5,9,g); LedSign::Set(5,10,g); LedSign::Set(6,5,g); LedSign::Set(6,6,g); LedSign::Set(6,9,g); LedSign::Set(7,4,g); LedSign::Set(7,5,g); LedSign::Set(7,6,g); LedSign::Set(8,0,g); LedSign::Set(8,3,g); LedSign::Set(8,4,g); LedSign::Set(8,5,g); LedSign::Set(8,6,g); LedSign::Set(9,1,g); LedSign::Set(9,2,g); LedSign::Set(9,3,g); LedSign::Set(9,4,g); LedSign::Set(9,5,g);
delay(50);
}
delay(runtime);
for(byte g=7; g>=1; g--) {
LedSign::Set(0,1,g); LedSign::Set(0,2,g); LedSign::Set(0,3,g); LedSign::Set(0,4,g); LedSign::Set(0,5,g); LedSign::Set(1,0,g); LedSign::Set(1,3,g); LedSign::Set(1,4,g); LedSign::Set(1,5,g); LedSign::Set(1,6,g); LedSign::Set(2,4,g); LedSign::Set(2,5,g); LedSign::Set(2,6,g); LedSign::Set(3,5,g); LedSign::Set(3,6,g); LedSign::Set(3,9,g); LedSign::Set(4,7,g); LedSign::Set(4,8,g); LedSign::Set(4,9,g); LedSign::Set(4,10,g); LedSign::Set(5,7,g); LedSign::Set(5,8,g); LedSign::Set(5,9,g); LedSign::Set(5,10,g); LedSign::Set(6,5,g); LedSign::Set(6,6,g); LedSign::Set(6,9,g); LedSign::Set(7,4,g); LedSign::Set(7,5,g); LedSign::Set(7,6,g); LedSign::Set(8,0,g); LedSign::Set(8,3,g); LedSign::Set(8,4,g); LedSign::Set(8,5,g); LedSign::Set(8,6,g); LedSign::Set(9,1,g); LedSign::Set(9,2,g); LedSign::Set(9,3,g); LedSign::Set(9,4,g); LedSign::Set(9,5,g);
delay(50);
}
LedSign::Clear();
delay(200);
break;
case 1: // USAF
for(byte g=0; g<=7; g++) {
Font_Draw('U', 0,0,g);
Font_Draw('S', 5,0,g);
Font_Draw('A', 0,6,g);
Font_Draw('F', 5,6,g);
delay(50);
}
delay(runtime);
for(byte g=7; g>=1; g--) {
Font_Draw('U', 0,0,g);
Font_Draw('S', 5,0,g);
Font_Draw('A', 0,6,g);
Font_Draw('F', 5,6,g);
delay(50);
}
LedSign::Clear();
delay(200);
break;
}
}
void initialize_frame_log() {
for(uint8_t y=0; y < ROWS; y++) {
for(uint8_t x=0; x < COLS; x++) {
frame_log[x][y] = -1;
}
}
}
void log_current_frame() {
for(uint8_t y=0; y < ROWS; y++) {
for(uint8_t x=0; x < COLS; x++) {
frame_log[x][y] = world[x][y][0];
}
}
}
void set_random_next_frame(void) {
// blank out the world
resetDisplay();
uint8_t density = random(40,80);
for(uint8_t y=0; y<ROWS; y++) {
for(uint8_t x=0; x<COLS; x++) {
if(random(100) > density) {
world[x][y][1] = 7;
if(_DEBUG_) { Serial.print("X"); }
}
else {
if(_DEBUG_) { Serial.print("."); }
}
}
if(_DEBUG_) { Serial.println(""); }
}
// A blinker for testing toroidicity
// world[0][0][1] = 7;
// world[0][1][1] = 7;
// world[0][2][1] = 7;
// A glider for testing toroidicity
// world[1][0][1] = 7;
// world[2][1][1] = 7;
// world[0][2][1] = 7;
// world[1][2][1] = 7;
// world[2][2][1] = 7;
}
char current_equals_next() {
uint8_t x, y;
for(y=0; y<ROWS; y++) {
for(x=0; x<COLS; x++) {
if( world[x][y][0] != world[x][y][1] ) {
return 0;
}
}
}
return 1;
}
uint8_t next_equals_glider() {
uint8_t comp1, comp2, comp3, cellcount, glidermatch;
comp1 = 0;
comp2 = 0;
comp3 = 0;
cellcount = 0;
glidermatch = 0;
for(uint8_t y=0; y<ROWS; y++) {
comp1 = 0;
for(uint8_t x=0; x<COLS; x++) {
if(world[x][y][1] >= 1) {
// VERY IMPORTANT LINE.
comp1 = comp1 + (1 << x);
cellcount++;
}
}
// Shift everything over to the "left" (right shifting, I know. Shut up.) - don't go all the way down to 1 otherwise you get false positives.
while((comp3 > 2) && (comp2 > 2) && (comp1 > 2)) { comp3 >>= 1; comp2 >>=1; comp1 >>=1; }
// match for glider fingerprints -- all vertical gliders transform into
// horizontal gliders during their life, so I only need to match 4 possible
// orientations
if((comp1 == 7) && (comp2 == 4) && (comp3 == 2) || // DR glider
(comp1 == 14) && (comp2 == 2) && (comp3 == 4) || // DL glider
(comp1 == 7) && (comp2 == 1) && (comp3 == 2) || // DL glider
(comp1 == 4) && (comp2 == 2) && (comp3 == 14) || // UL Glider
(comp1 == 2) && (comp2 == 1) && (comp3 == 7) || // UL Glider
(comp1 == 2) && (comp2 == 4) && (comp3 == 7)) // UR Glider
{ glidermatch++; }
// rotate everything down the line.
comp3 = comp2; comp2 = comp1; comp1 = 0;
}
// If there's a glider-like shape with more than 5 living cells, it's a false
// positive. If it is a glider and a seperate colony, the glider will collide
// with the colony eventually.
if((cellcount == 5) and (glidermatch > 0)) { return 1; }
else { return 0; }
}
uint8_t next_equals_logged_frame(){
for(uint8_t y = 0; y<ROWS; y++) {
for(uint8_t x = 0; x<COLS; x++) {
if(world[x][y][1] != frame_log[x][y] ) {
return 0;
}
}
}
return 1;
}
void Life() {
if(_DEBUG_) { Serial.println("-----"); Serial.println("Life starting"); }
int frame_number, generation, temp_generation;
unsigned int curtime, lasttime;
curtime = 0;
lasttime = 0;
frame_number = 0;
generation = 0;
temp_generation = 0;
initialize_frame_log(); // blank out the frame_log world
// flash the screen - ~1000msec
for(uint8_t y=0; y < ROWS; y++) { for(uint8_t x=0; x < COLS; x++) { world[x][y][1] = 7; } }
fade_to_next_frame(30);
delay(300);
for(uint8_t y=0; y < ROWS; y++) { for(uint8_t x=0; x < COLS; x++) { world[x][y][1] = 0; } }
fade_to_next_frame(30);
delay(300);
// draw the initial generation
set_random_next_frame();
fade_to_next_frame(30);
delay(150);
unsigned long starttime = millis();
boolean end_before_next_time=false;
while(1) {
// this should probably get simplified.
lasttime = curtime;
curtime = abs(millis());
int difftime = curtime - lasttime;
// show the clock every CLOCK_EVERY seconds
if(abs(millis()) >= starttime + CLOCK_EVERY) {
if(end_before_next_time == true) {
for(int f=0; f<500; f++) {
draw_frame();
}
break;
}
delay(150);
LedSign::Clear();
noInterrupts();
RequestDS18B20Temp();
interrupts();
updateTimeBuffer();
if(_DEBUG_) {
Serial.print(hours, DEC); Serial.print(":"); Serial.print(minutes, DEC);
Serial.print(" @ gen "); Serial.println(generation);
}
DisplayTime(1000);
// If we're deep in generations (2 generations/second, roughly), start showing the time again.
if(temp_generation >= 60) {
temp_generation = 0;
LedSign::Clear();
noInterrupts();
float ftemp = GetDS18B20Temp();
interrupts();
if((ftemp < 50.00) && (ftemp > 2)) { // sanity check the resulting data.
ftemp=(ftemp*1.8)+32;
char temperature[5];
dtostrf(ftemp, -4, 1, temperature);
if(_DEBUG_) {
Serial.print("Temp: "); Serial.println(temperature);
}
sprintf(tempnhum, "%s<", temperature);
Banner(tempnhum, 100, random(6));
}
else {
Serial.println("Temp Error: ");
Serial.println(ftemp);
}
}
starttime = millis();
if (digitalRead(SET_BUTTON_PIN) == 0) {
// "Set time" button was pressed
break;
}
continue;
}
// Log every 20th frame to monitor for repeats
if( frame_number == 0 ) {
log_current_frame();
}
// generate the next generation
generate_next_generation();
// Death due to still life
// if there are no changes between the current generation and the next generation (still life), break out of the loop.
// this might not work so well with the end_before_next_time flag, since it leads to blank displays.
if(( current_equals_next() == 1 ) and ( end_before_next_time == false)) {
if(_DEBUG_) { Serial.print("Died due to still life at generation "); Serial.println(generation); }
end_before_next_time = true;
// check to see if it died with an empty world or one with a static object.
byte living_cells=0;
for(uint8_t y=0; y<ROWS; y++) {
for(uint8_t x=0; x<COLS; x++) {
if(world[x][y][1] > 0) { living_cells++; }
}
}
if(living_cells == 0) {
delay(500);
break;
}
}
// Death due to oscillator
// If the next frame is the same as a frame from 20 generations ago, we're in a loop.
if(( next_equals_logged_frame() == 1 ) and (end_before_next_time == false)) {
if(_DEBUG_) { Serial.print("Died due to oscillation at generation "); Serial.println(generation); }
end_before_next_time = true;
}
// Death due to solo glider
// Gliders are relatively boring
if(( next_equals_glider() == 1 ) and (end_before_next_time == false)) {
if(_DEBUG_) { Serial.print("Died due to lonely glider at generation "); Serial.println(generation); }
end_before_next_time = true;
}
// Death due to running too long - 1800 frames is about 15 minutes. (Probably still too long, I suspect the average methuselah is a glider.)
// Congratulations, multi-element loop! Time to die!
if((generation >= 1800) and (end_before_next_time == false)) {
if(_DEBUG_) { Serial.print("Died due to methuselah's syndrome at generation "); Serial.println(generation); }
end_before_next_time = true;
}
// ~ 500msec per generation.
// Otherwise, fade to the next generation
fade_to_next_frame(50);
// this is serial-sensitive - more time spent on the serial line increases the delay.
unsigned int delaytime = abs(505 - (abs(millis()) - curtime));
if(delaytime >= 500) { delaytime = 90; }
delay(delaytime);
frame_number++;
generation++;
temp_generation++;
if(frame_number >= 20 ) {
frame_number = 0;
}
}
}
void generate_next_generation(void){ //looks at current generation, writes to next generation array
char x,y, neighbors;
for ( y=0; y<ROWS; y++ ) {
for ( x=0; x<COLS; x++ ) {
//count the number of current neighbors - currently planar.
neighbors = 0;
if( get_led_xy((x-1),(y-1)) > 0 ) { neighbors++; } //NW
if( get_led_xy(( x ),(y-1)) > 0 ) { neighbors++; } //N
if( get_led_xy((x+1),(y-1)) > 0 ) { neighbors++; } //NE
if( get_led_xy((x-1),( y )) > 0 ) { neighbors++; } //W
if( get_led_xy((x+1),( y )) > 0 ) { neighbors++; } //E
if( get_led_xy((x-1),(y+1)) > 0 ) { neighbors++; } //SW
if( get_led_xy(( x ),(y+1)) > 0 ) { neighbors++; } //S
if( get_led_xy((x+1),(y+1)) > 0 ) { neighbors++; } //SE
if( world[x][y][0] > 0 ){
//current cell is alive
if( neighbors < 2 ){
//Any live cell with fewer than two live neighbours dies, as if caused by under-population
world[x][y][1] = 0;
}
if( (neighbors == 2) || (neighbors == 3) ){
//Any live cell with two or three live neighbours lives on to the next generation
if(AGING) {
if(get_led_xy(x,y) > 3) { world[x][y][1] = (get_led_xy(x,y)-1); }
else { world[x][y][1] = get_led_xy(x,y); }
}
else {
world[x][y][1] = 7;
}
}
if( neighbors > 3 ){
//Any live cell with more than three live neighbours dies, as if by overcyding
world[x][y][1] = 0;
}
}
else {
//current cell is dead
if( neighbors == 3 ){
// Any dead cell with exactly three live neighbours becomes a live cell, as if by reproduction
world[x][y][1] = 7;
}
else {
//stay dead for next generation
world[x][y][1] = 0;
}
}
}
}
}
char get_led_xy (char col, char row) {
if(TOROID == 1) {
if(col < 0) { col = COLS-1; }
if(col > COLS-1) { col = 0; }
if(row < 0) { row = ROWS-1; }
if(row > ROWS-1) { row = 0; }
}
else {
if(col < 0 | col > COLS-1) { return 0; }
if(row < 0 | row > ROWS-1) { return 0; }
}
return world[col][row][0];
}
void DisplayTime(unsigned long int runtime) {
LedSign::Clear();
int x;
char text[2];
// hours on the top
itoa(hours,text,10);
if(hours < 10) { // right justify 1-9
x = Font_Draw(text[0],5,0,7);
}
else if(hours >= 10 && hours < 20) {
x = Font_Draw(text[0],1,0,7);
Font_Draw(text[1],x+1,0,7);
}
else {
x = Font_Draw(text[0],0,0,7);
Font_Draw(text[1],x,0,7);
}
// minutes on the bottom
itoa(minutes,text,10);
if(minutes < 10) {
x = Font_Draw(48, 0, 6, 7);
Font_Draw(text[0], x, 6, 7);
}
else if (minutes >=10 && minutes <=19) {
x = Font_Draw(text[0], 1, 6, 7);
Font_Draw(text[1], x+1, 6, 7);
}
else {
x = Font_Draw(text[0],0,6,7);
Font_Draw(text[1],x,6,7);
}
delay(runtime);
}
void setTime() {
RTC.switchTo24h();
RTC.stop();
// attempt to not cause the clock to lose as much time in the setting mode
// RTC.setSeconds(0);
RTC.setMinutes(minutes);
RTC.setHours(hours);
// dummy values, since they are not displayed anyway;
RTC.setDayOfWeek(1);
RTC.setDate(1);
RTC.setMonth(1);
RTC.setYear(9);
RTC.setClock(); // important! Without this, all of the prior settings are lost by the next getter
RTC.start();
}
void updateTimeBuffer() {
RTC.readClock();
minutes = RTC.getMinutes();
hours = RTC.getHours();
// On the one hand, I want to put something here to deal with garbage times.
// On the other hand, I want the device to be functional even if the battery
// is dead.
// build the string containing formatted time;
sprintf(timeBuffer, "%2d:%02d", hours, minutes);
}
void resetDisplay() {
for(uint8_t y = 0; y <=ROWS; y++) {
for(uint8_t x = 0; x <=COLS; x++) {
world[x][y][0] = 0;
world[x][y][1] = 0;
}
}
}
void processSetButton() {
if((hours > 23) or (minutes > 59)) {
updateTimeBuffer();
}
// debouncing;
if (abs(millis() - lastButtonTime) < BOUNCE_TIME_BUTTON)
return;
lastButtonTime = millis();
isSettingTime = true;
if(isSettingHours == true) {
isSettingHours = false;
isSettingMinutes = true;
isSettingBrightness = false;
}
else if(isSettingMinutes == true) {
isSettingHours = false;
isSettingMinutes = false;
isSettingBrightness = true;
}
else if(isSettingBrightness == true) {
isSettingHours = true;
isSettingMinutes = false;
isSettingBrightness = false;
}
if(_DEBUG_) {
Serial.print("h: "); Serial.println(isSettingHours, DEC);
Serial.print("m: "); Serial.println(isSettingMinutes, DEC);
Serial.print("b: "); Serial.println(isSettingBrightness, DEC);
}
// this was breaking it for reasons I can't quite pin down.
// resetDisplay();
int x;
char text[2];
if(isSettingHours) {
LedSign::Clear();
itoa(hours,text,10);
x = Font_Draw(text[0],0,0,7);
Font_Draw(text[1],x,0,7);
delay(10);
}
else if(isSettingMinutes) {
LedSign::Clear();
itoa(minutes,text,10);
x = Font_Draw(text[0],0,6,7);
Font_Draw(text[1],x,6,7);
delay(10);
}
else if(isSettingBrightness) {
LedSign::Clear(7);
uint8_t disp_brightness = (max_brightness-30);
itoa(disp_brightness,text,10);
x = Font_Draw(text[0],0,3,0);
Font_Draw(text[1],x,3,0);
delay(10);
}
}
//-----------------------------------------------------------
void processIncButton() {
if((hours > 23) or (minutes > 59)) {
updateTimeBuffer();
}
// debouncing;
if (abs(millis() - lastButtonTime) < BOUNCE_TIME_BUTTON)
return;
lastButtonTime = millis();
if (isSettingHours) {
hours++;
if (hours > 23) hours = 0;
}
else if(isSettingMinutes) {
minutes++;
if (minutes > 59) minutes = 0;
}
else if(isSettingBrightness) {
max_brightness++;
if(max_brightness > 49) max_brightness = 31;
}
int x;
char text[2];
if(isSettingHours) {
LedSign::Clear();
itoa(hours,text,10);
x = Font_Draw(text[0],0,0,7);
Font_Draw(text[1],x,0,7);
delay(10);
}
else if(isSettingMinutes) {
LedSign::Clear();
itoa(minutes,text,10);
x = Font_Draw(text[0],0,6,7);
Font_Draw(text[1],x,6,7);
delay(10);
}
else if(isSettingBrightness) {
LedSign::Clear(7);
uint8_t disp_brightness = (max_brightness-30);
itoa(disp_brightness,text,10);
x = Font_Draw(text[0],0,3,0);
Font_Draw(text[1],x,3,0);
LedSign::SetBrightness(max_brightness);
delay(10);
}
}
void Rain(unsigned long now, unsigned long runtime) {
// Higher numbers make for heavier rain.
uint8_t density = random(20,80);
// Higher numbers make for slower rain
uint8_t rainspeed = random(2,20);
if(_DEBUG_) { Serial.println("-----"); Serial.println("Rain starting: "); Serial.print(density, DEC); Serial.print(" "); Serial.println(rainspeed, DEC); }
uint8_t stopraining = 0;
while(1) {
if(millis() > (now+runtime)) { // get out of rain eventually.
stopraining++;
}
// move everything down one row
for(uint8_t y = 0; y < ROWS; y++) {
for(uint8_t x = 0; x < COLS; x++) {
if(world[x][y][0] > 0) { // if there's a value in the current frame, copy it to the next frame, 1 row down
world[x][y+1][1] = world[x][y][0];
}
else { // otherwise blank the LED in the next frame.
world[x][y+1][1] = 0;
}
}
}
// fill in the now vacant top row with random lights
for(uint8_t x = 0; x < COLS; x++) {
if(stopraining < ROWS+1) {
if(random(100) > density) {
world[x][0][1] = random(1,7);
}
else {
world[x][0][1] = 0;
}
}
else if(stopraining >= ROWS*COLS) {
return;
}
else {
world[x][0][1] = 0;
}
}
// draw the changes - after this world[0] will be identical to world[1], so keep that in mind.
fade_to_next_frame(rainspeed);
}
}
void Breathe(unsigned long now, unsigned long runtime) {
while(1) {
if(millis() > (now+runtime)) {
return;
}
for(uint8_t y=0; y < ROWS; y++) { for(uint8_t x=0; x < COLS; x++) { world[x][y][1] = 7; } }
if(millis() > (now+runtime)) { return; }
fade_to_next_frame(30);
delay(300);
if(millis() > (now+runtime)) { return; }
for(uint8_t y=0; y < ROWS; y++) { for(uint8_t x=0; x < COLS; x++) { world[x][y][1] = 0; } }
fade_to_next_frame(30);
if(millis() > (now+runtime)) { return; }
delay(300);
}
}
// Theory of operation: Bring world[x][y][0] towards world[x][y][1] and draw it. When nothing changes, break out of the loop.
void fade_to_next_frame(uint8_t speed) {
char x,y, changes;
while(1) {
changes = 0;
for(y = 0; y < ROWS; y++) {
for(x = 0; x < COLS; x++) {
if( world[x][y][0] < world[x][y][1] ) {
world[x][y][0]++;
changes++;
}
if( world[x][y][0] > world[x][y][1] ) {
world[x][y][0]--;
changes++;
}
}
}
draw_frame();
// give those changes a second to apply - LedSign's interrupt driven nature
// means otherwise the whole update could happen between polling intervals
// and it would just jump from frame to the next frame.
delay(speed);
if( changes == 0 ) {
break;
}
}
}
// Draws the current data in world[0].
void draw_frame (void) {
char x, y;
for(y=0; y < ROWS; y++) {
for(x=0; x < COLS; x++) {
LedSign::Set(x,y,world[x][y][0]);
}
}
}
/** Writes an array to the display
* @param str is the array to display
* @param speed is the inter-frame speed
*/
//void Banner ( String str, int speed ) { // this works, hogs memory.
void Banner ( char* str, int speed, int y) {
// length is length(array)
// width is the width of the array in pixels.
// these can be unsigned - int8 might be too small
uint8_t width=0, length=0;
if(_DEBUG_) { Serial.print("Banner text: "); Serial.println(str); }
// figure out pixel width of str
while(str[length]!='\0') { //read to the end of the string
int charwidth=0;
length++;
// get the character's width by calling Font_Draw in the "Don't draw this, just tell me how wide it is" mode.
// It would probably be faster to have a lookup table.
charwidth = Font_Draw(str[length],0,0,0);
// adds the width of ths current character to the pixel width.
width=width+charwidth;
}
// these need to be signed, otherwise I can't draw off the left side of the screen.
int8_t x=0, x2=0; // x position buckets
// j is the virtual display width from the actual rightmost column to a
// virtual column off the left hand side. Decrements, so the scroll goes
// to the left.
for(int8_t j=5; j>=-(width+5); j--) { // FIXME: this might need fixing for wider arrays
// x starts out at j (which is always moving to the left, remember)
x=j;
// clear the sign
LedSign::Clear();
// walk through the array, drawing letters where they belong.
for(int i=0; i<length; i++) {
x2 = Font_Draw(str[i],x,y,7);
// sets the new xpos based off of the old xpos + the width of the
// current character.
x+=x2;
}
delay(speed);
}
}
/** Draws a figure (0-Z). You should call it with set=1,
* wait a little them call it again with set=0
* @param figure is the figure [0-9]
* @param x,y coordinates,
* @param set is 1 or 0 to draw or clear it
*/
uint8_t Font_Draw(unsigned char letter,int x,int y,int set) {
uint16_t maxx=0;