Post by Greg Kovacs on Jul 28, 2018 21:07:12 GMT -8
I was trying a quick port of Conway's Game of Life (which works on the Adafruit 3.5" LCD and several other LCD's, with appropriate changes, of course). With a Teensy 3.6 connected to the Gameduino 3, there should be enough memory to have *almost* all of the pixels active. The code below is set for 280 X 272 "cells" but in debugging I had changed the size (xmax, ymax) to 32 X 32...
In any case, even with this tiny array size, or with the "current generation display" code always setting a fixed pixel on (e.g., GD.Vertex2ii(10, 10), it comes up with "ERROR COPROCESSOR EXCEPTION" when run. Now I had earlier discovered that one can only write 2023 pixels (not one more!) before doing "GD.swap();" or this comes up (presumably due to buffer overflow). However, in this case, even writing a 32 X 32 matrix (which is only 1024 points), it causes the error.
I'm not the world's best programmer, but I could not see the issue. Any help would be appreciated!
Thanks,
Greg
#include <EEPROM.h>
#include <SPI.h>
#include <GD2.h>
//Gameduino X=480, y=272 pixels;
//Try to port to Gameduino 3 on 7/27/18, G. Kovacs
int xmax=32;
int ymax=32;
//int x,y;
//int i,j;
int generation = 0;
int maxgen = 20; //number of generations before reseeding randomly
/*
* Conway's "Life"
*
* Adapted from the Life example
* on the Processing.org site
*/
byte world[280][272][2];
long density = 12;
void setup() {
GD.begin(~GD_STORAGE);
randomSeed(analogRead(A5));
for (int i = 0; i < xmax; i++) {
for (int j = 0; j < ymax; j++) {
if (random(100) < density) {
world[j][0] = 1;
}
else {
world[j][0] = 0;
}
world[j][1] = 0;
}
}
}
void loop() {
if (generation == 0) {
randomSeed(analogRead(A5));
for (int i = 0; i < xmax; i++) {
for (int j = 0; j < ymax; j++) {
if (random(100) < density) {
world[j][0] = 1;
}
else {
world[j][0] = 0;
}
world[j][1] = 0;
}
}
}
GD.ClearColorRGB(0x103000);
GD.Clear();
GD.ColorRGB(0xFFFFFF); // white
GD.Begin(POINTS);
GD.Vertex2ii(0,0);
GD.cmd_text(100, 136, 31, OPT_CENTER, "Life Demo");
GD.swap();
delay(1000);
// Display current generation
for (int i = 0; i < xmax; i++) {
for (int j = 0; j < ymax; j++) {
if (world[j][0] == 1)
{
GD.ColorRGB(0x00FF00); // bright green
}
else
{
GD.ColorRGB(0x000000); // black
}
GD.Vertex2ii(i, j);
}
}
delay(0);
GD.swap();
// Birth and death cycle
for (int x = 0; x < xmax; x++) {
for (int y = 0; y < ymax; y++) {
// Default is for cell to stay the same
world[x][y][1] = world[x][y][0];
int count = neighbors(x, y);
if (count == 3 && world[x][y][0] == 0) {
// A new cell is born
world[x][y][1] = 1;
}
if ((count < 2 || count > 3) && world[x][y][0] == 1) {
// Cell dies
world[x][y][1] = 0;
}
}
}
// Copy next generation into place
for (int x = 0; x < xmax; x++) {
for (int y = 0; y < ymax; y++) {
world[x][y][0] = world[x][y][1];
}
}
generation++;
if (generation >= maxgen)
generation=0;
}
int neighbors(int x, int y) {
return world[(x + 1) % xmax][y][0] +
world[x][(y + 1) % ymax][0] +
world[(x + xmax - 1) % xmax][y][0] +
world[x][(y + ymax - 1) % ymax][0] +
world[(x + 1) % xmax][(y + 1) % ymax][0] +
world[(x + xmax - 1) % xmax][(y + 1) % ymax][0] +
world[(x + xmax - 1) % xmax][(y + ymax - 1) % ymax][0] +
world[(x + 1) % xmax][(y + ymax - 1) % ymax][0];
}