/* Filename: 8x8life.c Turns on and off LEDs at to display Conway's game of life in an 8x8 matrix. Pins 0-7 are the row pins, on if HIGH. Pins 8-15 are the column pins, on if LOW. */ #include #include #include #include int start[10][10] = { {0,0,0,0,0,0,0,0,0,0}, // The starting point {0,0,0,0,0,0,0,0,0,0}, {0,0,0,1,0,0,0,0,0,0}, {0,0,1,0,1,0,0,0,0,0}, {0,1,0,0,0,1,0,0,0,0}, {0,1,1,1,1,1,0,0,0,0}, {0,1,0,0,0,1,0,0,0,0}, {0,1,0,0,0,1,0,0,0,0}, {0,1,0,0,0,1,0,0,0,0}, {0,0,0,0,0,0,0,0,0,0} }; /* int start[10][10] = { {0,0,0,0,0,0,0,0,0,0}, // The starting point {0,0,0,0,0,0,0,0,0,0}, {0,0,1,1,1,0,1,0,0,0}, {0,0,1,0,0,0,0,0,0,0}, {0,0,0,0,0,1,1,0,0,0}, {0,0,0,1,1,0,1,0,0,0}, {0,0,1,0,1,0,1,0,0,0}, {0,0,0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,0,0} }; */ int count[10][10]; //Count of adjacent live cells. int rowPin[8] = {0,1,2,3,4,5,6,7}; int columnPin[8] = {8,9,10,11,12,13,14,15}; int LED[8][8]; //8 rows, 8 columns. Used for passing data between threads. /* This thread reads the LED array and performs the multiplex. It turns on the pins for one LED at a time. Row 1 LED 1 - 2 - 3 ... Row 2 LED 1 - 2 - 3 ... The code here runs concurrently with the main program in an infinite loop. */ PI_THREAD (display) { int i,j; while(1) { for(i = 0; i < 8; i++) // Row, HIGH is on. { if(i==0) digitalWrite(rowPin[7],0); // Turn off previous row. else digitalWrite(rowPin[i-1],0); digitalWrite(columnPin[7],1); // turn off column 7 pin. digitalWrite(rowPin[i],1); // Turn on current row. for(j = 0; j < 8; j++) // Column, LOW is on. { if(j==0) digitalWrite(columnPin[7],1); // Turn off previous column. else digitalWrite(columnPin[j-1],1); if (LED[i][j]==1) { digitalWrite(columnPin[j],0); // Turn on current column. } } } } } void setup(void) { int i,j; if (geteuid() != 0) { fprintf (stderr, "You need to be root to run this program. (sudo?)\n") ; exit(0) ; } if (wiringPiSetup() == -1) { printf("wiringPiSetup error\n"); exit(1) ; } for(i = 0; i < 16; i++) { pinMode(i,OUTPUT); // Set pins 0 - 15 for output. } int x = piThreadCreate (display) ; // Start second thread. if (x != 0) printf ("It didn't start.\n"); for(i=0;i<8;i++) // Display initial sequence. { for(j=0;j<8;j++) { LED[i][j] = start[i+1][j+1]; //Move matrix to LED for display } } delay(2000); } /* The loop function. */ /*******************************************************/ void loop(void) { int i,j; for(i=0;i<10;i++) // Zero counts from previous iteration. { for(j=0;j<10;j++) { count[i][j]=0; } } /*******************************************************/ for(i=1;i<9;i++) // Count adjacent live cells. { for(j=1;j<9;j++) { if(start[i-1][j-1]==1) count[i][j]++; if(start[i-1][j]==1) count[i][j]++; if(start[i-1][j+1]==1) count[i][j]++; if(start[i][j-1]==1) count[i][j]++; if(start[i][j+1]==1) count[i][j]++; if(start[i+1][j-1]==1) count[i][j]++; if(start[i+1][j]==1) count[i][j]++; if(start[i+1][j+1]==1) count[i][j]++; } } /*******************************************************/ for(i=1;i<9;i++) // Switch on/off cells. { for(j=1;j<9;j++) { if (count[i][j]<2) start[i][j]=0; if (count[i][j]>3) start[i][j]=0; if (count[i][j]==3) start[i][j]=1; } } /*******************************************************/ for(i=0;i<8;i++) { for(j=0;j<8;j++) { LED[i][j] = start[i+1][j+1]; //Move matrix to LED for display } } delay(500); } /*******************************************************/ int main(void) { int i,j; int previous[10][10]; int endflag = 1; setup(); while(endflag > 0) { for(i=0;i<10;i++) // Save current matrix. { for(j=0;j<10;j++) { previous[i][j] = start[i][j]; } } loop(); // Perfoem one iteration. endflag = 0; for(i=0;i<10;i++) // Compare previous matrix to current. { for(j=0;j<10;j++) { if(previous[i][j] != start[i][j]) { endflag++; i = 10; j = 10; } } } } delay(2000); return(0); }