-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConwaysLife.ino
790 lines (585 loc) · 16.7 KB
/
ConwaysLife.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
/* ConwaysLife --- Conway's Game of Life on Nokia 1202 LCD 2012-09-22 */
/* Copyright (c) 2012 John Honniball */
/* Released under the GNU Public Licence (GPL) */
#include <SPI.h>
#include "font.h"
// Direct port I/O defines for Arduino with ATmega328
// Change these if running on Mega Arduino
#define LCDOUT PORTB
#define CS 0x02
#define SDA 0x08
#define SCLK 0x20
// Connections to Nokia 1202 LCD, via CD4050 level-shifter
#define slaveSelectPin 9
#define SDAPin 11
#define SCLKPin 13
// Size of Nokia 1202 LCD screen
#define MAXX 96
#define MAXY 68
#define MAXROWS 9 // 9 rows of bytes, last row only half used
// Co-ord of centre of screen
#define CENX (MAXX / 2)
#define CENY (MAXY / 2)
// The following two arrays fill up very nearly all of the RAM
// on the Arduino:
// The cells, as a bit-map, 864 bytes
unsigned char Cells[MAXROWS][MAXX];
// The frame buffer, 864 bytes
unsigned char Frame[MAXROWS][MAXX];
void setup (void)
{
int i;
//Serial.begin (9600);
lcd1202_begin ();
clrFrame ();
fillRoundRect (CENX - (3 * 12) - 2, CENY - 6, CENX + (3 * 12) + 2, CENY + 16, 7);
setText (CENX - (3 * 12), CENY, "Conways Game");
setText (CENX - (3 * 7), CENY + 8, "of Life");
updscreen ();
delay (2000);
drawInitialCells ();
updscreen ();
delay (2000);
}
void loop (void)
{
static long int generation = 0;
char genstr[16];
long int start, now;
int elapsed;
// Record timer in milliseconds at start of frame cycle
start = millis ();
// Let one generation pass
newGeneration ();
// That's now the old generation
oldGeneration ();
// Overlay generation counter in top left-hand corner
sprintf (genstr, "%ld", ++generation);
setText (0, 0, genstr);
// Update LCD for this frame
updscreen ();
// Work out timing for this frame
now = millis ();
elapsed = now - start;
//Serial.print (elapsed);
//Serial.println ("ms.");
// We'll never have time to actually delay, because we
// take about 100ms to compute each generation
if (elapsed < 20)
delay (20 - elapsed);
}
/* drawInitialCells --- seed the playing area with live cells */
void drawInitialCells (void)
{
int i;
memset (Cells, 0, sizeof (Cells));
//#ifdef F_PENTOMINO
Cells[CENY / 8][CENX - 1] = 0x02;
Cells[CENY / 8][CENX] = 0x07;
Cells[CENY / 8][CENX + 1] = 0x01;
//#endif
#ifdef DIEHARD
Cells[CENY / 8][CENX - 4] = 0x02;
Cells[CENY / 8][CENX - 3] = 0x06;
Cells[CENY / 8][CENX + 1] = 0x04;
Cells[CENY / 8][CENX + 2] = 0x05;
Cells[CENY / 8][CENX + 3] = 0x04;
#endif
#ifdef INFINITE3
for (i = -18; i <= -5; i++)
if (i != -10)
Cells[CENY / 8][CENX + i] = 0x01;
Cells[CENY / 8][CENX - 1] = 0x01;
Cells[CENY / 8][CENX] = 0x01;
Cells[CENY / 8][CENX + 1] = 0x01;
for (i = 8; i <= 20; i++)
if ( i != 15)
Cells[CENY / 8][CENX + i] = 0x01;
#endif
memcpy (Frame, Cells, sizeof (Frame));
}
/* newGeneration --- execute life for one generation of cells */
int newGeneration (void)
{
int x, y;
int ncells = 0;
int nn;
unsigned int xp1, xm1; // X plus/minus one
unsigned int yp1, ym1; // Y plus/minus one
unsigned int yrow, yp1row, ym1row;
unsigned char ymask, yp1mask, ym1mask;
// New generation is created in the Frame array, so clear it
clrFrame ();
for (y = 0; y < MAXY; y++) {
// Precompute array index and bit-mask for this row of cells
if (y == 0)
ym1 = MAXY - 1;
else
ym1 = y - 1;
if (y == (MAXY - 1))
yp1 = 0;
else
yp1 = y + 1;
ymask = 1 << (y & 7);
yrow = y / 8;
ym1mask = 1 << (ym1 & 7);
ym1row = ym1 / 8;
yp1mask = 1 << (yp1 & 7);
yp1row = yp1 / 8;
// Run along this row of cells
for (x = 0; x < MAXX; x++) {
// Allow for wrap-around at edges (as if on a toroid)
if (x == 0)
xm1 = MAXX - 1;
else
xm1 = x - 1;
if (x == (MAXX - 1))
xp1 = 0;
else
xp1 = x + 1;
// Count the number of neighbouring cells that are alive
nn = cellAt2 (xm1, ym1row, ym1mask);
nn += cellAt2 (xm1, yrow, ymask);
nn += cellAt2 (xm1, yp1row, yp1mask);
nn += cellAt2 (x, ym1row, ym1mask);
nn += cellAt2 (x, yp1row, yp1mask);
nn += cellAt2 (xp1, ym1row, ym1mask);
nn += cellAt2 (xp1, yrow, ymask);
nn += cellAt2 (xp1, yp1row, yp1mask);
// Now follow the rules for the current cell at (x, y)
if (cellAt2 (x, yrow, ymask)) {
if (nn == 2 || nn == 3) {
setCell (x, y);
ncells++; // This cell lives on
}
// else
// Dies by isolation or overcrowding
}
else { // Current cell is not living
if (nn == 3) {
setCell (x, y);
ncells++; // New cell is born
}
}
}
}
return (ncells);
}
/* cellAt --- return state of cell at a given location */
int cellAt (int x, int y)
{
if (x < 0)
x = MAXX - 1;
if (x >= MAXX)
x = 0;
if (y < 0)
y = MAXY - 1;
if (y >= MAXY)
y = 0;
return ((Cells[y / 8][x] & (1 << (y & 7)))? 1: 0);
}
/* cellAt2 --- return state of cell at a given location */
inline int cellAt2 (unsigned int x, unsigned int row, unsigned char mask)
{
return ((Cells[row][x] & mask)? 1: 0);
}
/* setCell --- set the state of a cell to be alive */
void setCell (unsigned int x, unsigned int y)
{
Frame[y / 8][x] |= 1 << (y & 7);
}
/* oldGeneration --- the new generation now becomes the old */
void oldGeneration (void)
{
memcpy (Cells, Frame, sizeof (Cells));
}
/* clrFrame --- clear the entire frame to white */
void clrFrame (void)
{
memset (Frame, 0, sizeof (Frame));
}
/* setText --- draw text into buffer using predefined font */
void setText (int x, int y, const char *str)
{
// This function does not, as yet, allow for pixel row positioning of text.
// The Y co-ordinate is rounded to the nearest row of display RAM bytes.
// The font (475 bytes) is held in program memory (Flash) to reduce RAM
// usage. The AVR is a Harvard architecture machine and needs a special
// instruction to read program memory, which is implemented in C as the
// 'pgm_read_byte_near' function.
int row;
int i;
int d;
row = y >> 3;
for ( ; *str; str++) {
d = (*str - ' ') * 5;
for (i = 0; i < 5; i++, d++) {
Frame[row][x++] = pgm_read_byte_near (font_data + d);
}
Frame[row][x++] = 0;
}
}
/* drawRoundRect --- draw a rounded rectangle */
void drawRoundRect (int x0, int y0, int x1, int y1, int r)
{
setHline (x0 + r, x1 - r, y0);
setHline (x0 + r, x1 - r, y1);
setVline (x0, y0 + r, y1 - r);
setVline (x1, y0 + r, y1 - r);
drawSplitCircle (x0 + r, y0 + r, x1 - r, y1 - r, r, 1, -1);
}
/* fillRoundRect --- fill a rounded rectangle */
void fillRoundRect (int x0, int y0, int x1, int y1, int r)
{
int y;
drawSplitCircle (x0 + r, y0 + r, x1 - r, y1 - r, r, 1, 0);
setHline (x0 + r, x1 - r, y0);
setHline (x0 + r, x1 - r, y1);
setVline (x0, y0 + r, y1 - r);
setVline (x1, y0 + r, y1 - r);
for (y = y0 + r; y < (y1 - r); y++)
clrHline (x0 + 1, x1 - 1, y);
}
/* drawSplitCircle --- draw a split circle with edge and fill colours */
void drawSplitCircle (int x0, int y0, int x1, int y1, int r, int ec, int fc)
{
// Michener's circle algorithm. Originally coded on the IBM PC
// with EGA card in 1986.
int x, y;
int d;
x = 0;
y = r;
d = 3 - (2 * r);
if (fc >= 0) {
while (x < y) {
splitcfill (x0, y0, x1, y1, x, y, fc);
if (d < 0) {
d += (4 * x) + 6;
}
else {
d += (4 * (x - y)) + 10;
y--;
}
x++;
}
if (x == y)
splitcfill (x0, y0, x1, y1, x, y, fc);
}
x = 0;
y = r;
d = 3 - (2 * r);
while (x < y) {
splitcpts8 (x0, y0, x1, y1, x, y, ec);
if (d < 0) {
d += (4 * x) + 6;
}
else {
d += (4 * (x - y)) + 10;
y--;
}
x++;
}
if (x == y)
splitcpts8 (x0, y0, x1, y1, x, y, ec);
}
/* cfill --- draw horizontal lines to fill a circle */
static void splitcfill (int x0, int y0, int x1, int y1, int x, int y, int fc)
{
if (fc) {
setHline (x0 - x, x1 + x, y1 + y);
setHline (x0 - x, x1 + x, y0 - y);
setHline (x0 - y, x1 + y, y1 + x);
setHline (x0 - y, x1 + y, y0 - x);
}
else {
clrHline (x0 - x, x1 + x, y1 + y);
clrHline (x0 - x, x1 + x, y0 - y);
clrHline (x0 - y, x1 + y, y1 + x);
clrHline (x0 - y, x1 + y, y0 - x);
}
}
/* splitcpts8 --- draw eight pixels to form the edge of a split circle */
static void splitcpts8 (int x0, int y0, int x1, int y1, int x, int y, int ec)
{
splitcpts4 (x0, y0, x1, y1, x, y, ec);
// if (x != y)
splitcpts4 (x0, y0, x1, y1, y, x, ec);
}
/* splitcpts4 --- draw four pixels to form the edge of a split circle */
static void splitcpts4 (int x0, int y0, int x1, int y1, int x, int y, int ec)
{
if (ec) {
setPixel (x1 + x, y1 + y);
// if (x != 0)
setPixel (x0 - x, y1 + y);
// if (y != 0)
setPixel (x1 + x, y0 - y);
// if ((x != 0) && (y != 0))
setPixel (x0 - x, y0 - y);
}
else {
clrPixel (x1 + x, y1 + y);
// if (x != 0)
clrPixel (x0 - x, y1 + y);
// if (y != 0)
clrPixel (x1 + x, y0 - y);
// if ((x != 0) && (y != 0))
clrPixel (x0 - x, y0 - y);
}
}
/* setVline --- draw vertical line */
void setVline (unsigned int x, unsigned int y1, unsigned int y2)
{
unsigned int y;
for (y = y1; y <= y2; y++)
setPixel (x, y);
}
/* clrVline --- draw vertical line */
void clrVline (unsigned int x, unsigned int y1, unsigned int y2)
{
unsigned int y;
for (y = y1; y <= y2; y++)
clrPixel (x, y);
}
/* setHline --- set pixels in a horizontal line */
void setHline (unsigned int x1, unsigned int x2, unsigned int y)
{
unsigned int x;
unsigned int row;
unsigned char b;
row = y / 8;
b = 1 << (y & 7);
for (x = x1; x <= x2; x++)
Frame[row][x] |= b;
}
/* clrHline --- clear pixels in a horizontal line */
void clrHline (unsigned int x1, unsigned int x2, unsigned int y)
{
unsigned int x;
unsigned int row;
unsigned char b;
if (y < MAXY) {
row = y / 8;
b = ~(1 << (y & 7));
for (x = x1; x <= x2; x++)
Frame[row][x] &= b;
}
}
/* setRect --- set pixels in a (non-filled) rectangle */
void setRect (int x1, int y1, int x2, int y2)
{
setHline (x1, x2, y1);
setVline (x2, y1, y2);
setHline (x1, x2, y2);
setVline (x1, y1, y2);
}
/* fillRect --- set pixels in a filled rectangle */
void fillRect (int x1, int y1, int x2, int y2, int ec, int fc)
{
int y;
for (y = y1; y <= y2; y++)
if (fc == 0)
clrHline (x1, x2, y);
else if (fc == 1)
setHline (x1, x2, y);
if (ec == 1) {
setHline (x1, x2, y1);
setVline (x2, y1, y2);
setHline (x1, x2, y2);
setVline (x1, y1, y2);
}
else if (ec == 0) {
clrHline (x1, x2, y1);
clrVline (x2, y1, y2);
clrHline (x1, x2, y2);
clrVline (x1, y1, y2);
}
}
/* setPixel --- set a single pixel */
void setPixel (unsigned int x, unsigned int y)
{
if ((x < MAXX) && (y < MAXY))
Frame[y / 8][x] |= 1 << (y & 7);
else {
// Serial.print ("setPixel(");
// Serial.print (x);
// Serial.print (",");
// Serial.print (y);
// Serial.println (")");
}
}
/* clrPixel --- clear a single pixel */
void clrPixel (unsigned int x, unsigned int y)
{
if ((x < MAXX) && (y < MAXY))
Frame[y / 8][x] &= ~(1 << (y & 7));
else {
// Serial.print ("clrPixel(");
// Serial.print (x);
// Serial.print (",");
// Serial.print (y);
// Serial.println (")");
}
}
/* updscreen --- update the physical screen from the buffer */
void updscreen (void)
{
// This function contains an eight-way unrolled loop. In the Arduino
// IDE, the default GCC optimisation switch is -Os, which optimises
// for space. No automatic loop unrolling is done by the compiler, so
// we do it explicitly here to save a few microseconds.
// long int before, after;
// unsigned char r, c;
unsigned char *p;
int i;
//lcdGotoRC (0, 0);
lcdSpi (0xB0); // Set page address to 'r'
lcdSpi (0x10); // Sets DDRAM column addr - upper 3-bit
lcdSpi (0x00); // lower 4-bit
// before = micros ();
p = &Frame[0][0];
for (i = 0; i < ((MAXROWS * MAXX) / 8); i++) {
lcdData (*p++);
lcdData (*p++);
lcdData (*p++);
lcdData (*p++);
lcdData (*p++);
lcdData (*p++);
lcdData (*p++);
lcdData (*p++);
}
/*
The slow way...
for (r = 0; r < MAXROWS; r++) {
for (c = 0; c < MAXX; c++) {
lcdData (Frame[r][c]);
}
}
*/
// after = micros ();
// Serial.print (after - before);
// Serial.println ("us updscreen");
}
/* lcd1202_begin --- initialise the Nokia 1202 LCD */
void lcd1202_begin (void)
{
// LCD initialisation code from Greeeg of the Dangerous Prototypes forum.
// The chip on the Nokia 1202 LCD is an ST Microelectronics STE2007.
// Greeeg also designed the 5x7 pixel font that's used by 'setText'.
/* Configure I/O pins on Arduino */
pinMode (slaveSelectPin, OUTPUT);
pinMode (SDAPin, OUTPUT);
pinMode (SCLKPin, OUTPUT);
digitalWrite (slaveSelectPin, HIGH);
digitalWrite (SDAPin, HIGH);
digitalWrite (SCLKPin, HIGH);
SPI.begin ();
// The following line fails on arduino-0021 due to a bug in the SPI library
// Compile with arduino-0022 or later
SPI.setClockDivider (SPI_CLOCK_DIV4);
SPI.setBitOrder (MSBFIRST);
SPI.setDataMode (SPI_MODE3);
// For the moment, disable SPI and return the pins to normal I/O use
SPCR &= ~(1<<SPE);
/* Start configuring the STE2007 LCD controller */
// LCDOUT |= RESET; // Hard reset
lcdSpi (0xE2); // SW reset.
// lcdSpi (0xA5); // Power saver ON, display all pixels ON
lcdSpi (0xA4); // Power saver OFF
lcdSpi (0x2F); // Power control set
// lcdSpi (0xAE); // Display OFF, blank
lcdSpi (0xAF); // Display ON
// These next two commands (A0/A1) don't work on my 1202 LCD
// lcdSpi (0xA0); // Display not flipped
// lcdSpi (0xA1); // Display flipped
// 'Normal' makes the bit-mapped display work top/left to
// bottom/right when the LCD is oriented with the connector
// at the top edge. I don't know which way up the LCD is
// supposed to be mounted in the phone, but this setup
// makes more sense to me on the breadboard.
lcdSpi (0xC0); // Display common driver normal
// lcdSpi (0xC8); // Display common driver flipped
lcdSpi (0x80 | 16); // Electronic volume to 16
lcdClr ();
// v--- Likely these aren't needed...And might not be working :P ---v
lcdSpi (0xef); // Set refresh rate
lcdSpi (3); // 65 Hz
lcdSpi (0x3d); // Set Charge Pump multiply factor
lcdSpi (0); // 5x
lcdSpi (0x36); // Bias ratio 1/4
lcdSpi (0xad); // set contrast
lcdSpi (0x20 | 20); // 20/32
lcdSpi (0xe1);
lcdSpi (0);
lcdSpi (0xa6); // Display normal
// lcdSpi (0xA7); // Display reversed (inverse video)
}
void lcdClr (void)
{
int i;
lcdGotoRC (0, 0);
for (i = 0; i < 16 * 6 * 9; i++) {
lcdData (0x00); // fill DDRAM with Zeros
}
}
void lcdGotoRC (int r, int c)
{
lcdSpi (0xB0 | (r & 0x0F)); // Set page address to 'r'
lcdSpi (0x10 | (c >> 4)); // Sets DDRAM column addr - upper 3-bit
lcdSpi (0x00 | (c & 0x0F)); // lower 4-bit
}
/* lcdData --- send a data byte to the LCD by fast hardware SPI */
inline void lcdData (unsigned char d)
{
// Data bytes are distinguished from command bytes by an initial
// '1' bit (followed by 8 data bits). AVR SPI hardware cannot do 9-bit
// transfers, so we do the initial bit by bit-banging, then switch on
// the SPI hardware and send a byte directly from the SPDR register.
// This method is about twice as fast as the software method and
// gives us a complete screen update in under 4 milliseconds.
char i;
LCDOUT &= ~CS;
LCDOUT |= SDA; // Leading '1' bit for LCD data
LCDOUT &= ~SCLK; // Toggle the clock
LCDOUT |= SCLK;
#ifdef SOFTWARE_SPI
for (i = 0; i < 8; i++) {
if (d & 0x80)
LCDOUT |= SDA;
else
LCDOUT &= ~SDA;
LCDOUT &= ~SCLK; // Toggle the clock
LCDOUT |= SCLK;
d <<= 1;
}
#else
SPCR |= 1 << SPE;
SPDR = d;
while (!(SPSR & (1 << SPIF)))
;
SPCR &= ~(1 << SPE);
#endif
LCDOUT |= CS;
}
/* lcdSpi --- send a command byte to the LCD by bit-banging SPI */
inline void lcdSpi (int d)
{
char i;
LCDOUT &= ~CS;
// digitalWrite (slaveSelectPin, LOW);
for (i = 0; i < 9; i++) {
if (d & 0x100)
LCDOUT |= SDA;
// digitalWrite (SDAPin, HIGH);
else
LCDOUT &= ~SDA;
// digitalWrite (SDAPin, LOW);
LCDOUT &= ~SCLK;
// digitalWrite (SCLKPin, LOW);
LCDOUT |= SCLK;
// digitalWrite (SCLKPin, HIGH);
d <<= 1;
}
LCDOUT |= CS;
// digitalWrite (slaveSelectPin, HIGH);
}