Created
April 2, 2020 17:26
-
-
Save dflemstr/b797aa935aaf26d8c971c3c466bb148e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <RGBmatrixPanel.h> | |
#include <SPI.h> | |
#include <WiFi101.h> | |
#include <WiFi101OTA.h> | |
#define CLK 13 | |
#define OE 1 // TX | |
#define LAT 0 // RX | |
#define A A5 | |
#define B A4 | |
#define C A3 | |
#define D A2 | |
uint8_t rgbpins[] = { 6,5,9,11,10,12 }; | |
RGBmatrixPanel matrix(A, B, C, D, CLK, LAT, OE, false, 64, rgbpins); | |
const char ssid[] = "dflemstr-network"; | |
const char pass[] = "RcN5MEdxdav3"; | |
int status = WL_IDLE_STATUS; | |
enum { | |
GAME_WIDTH = 64, | |
GAME_HEIGHT = 64, | |
NUM_GENERATIONS = 1024, | |
}; | |
struct Cell { | |
uint16_t hue; | |
bool alive() const { | |
return this->hue > 0; | |
} | |
}; | |
typedef Cell Matrix[GAME_WIDTH][GAME_HEIGHT]; | |
size_t generation = 0; | |
Matrix cellsA = {0}; | |
Matrix cellsB = {0}; | |
void setup() { | |
Serial.begin(9600); | |
setupWifi(); | |
printWifiStatus(); | |
matrix.begin(); | |
randomSeed(analogRead(0)); | |
randomize(cellsA); | |
} | |
void loop() { | |
WiFiOTA.poll(); | |
if (generation > NUM_GENERATIONS) { | |
randomize(cellsA); | |
generation = 0; | |
} | |
if (generation % 2 == 0) { | |
computeNextGeneration(cellsA, cellsB); | |
render(cellsB); | |
} else { | |
computeNextGeneration(cellsB, cellsA); | |
render(cellsA); | |
} | |
generation++; | |
delay(16); | |
} | |
void randomize(Matrix &cells) { | |
for (size_t x = 0; x < GAME_WIDTH; x++) { | |
for (size_t y = 0; y < GAME_HEIGHT; y++) { | |
if (random(2)) { | |
cells[x][y].hue = random(-1535, 1536); | |
} else { | |
cells[x][y].hue = 0; | |
} | |
} | |
} | |
} | |
void addCell(const Cell &cell, size_t &numAlive, long &hueSum) { | |
numAlive += cell.alive(); | |
hueSum += cell.hue; | |
} | |
void computeNextGeneration(const Matrix &input, Matrix &output) { | |
for (size_t x = 0; x < GAME_WIDTH; x++) { | |
for (size_t y = 0; y < GAME_HEIGHT; y++) { | |
size_t numAlive = 0; | |
long hueSum = 0; | |
size_t x0 = x > 0 ? x - 1 : GAME_WIDTH - 1; | |
size_t x1 = x < GAME_WIDTH - 1 ? x + 1 : 0; | |
size_t y0 = y > 0 ? y - 1 : GAME_HEIGHT - 1; | |
size_t y1 = y < GAME_HEIGHT - 1 ? y + 1 : 0; | |
addCell(input[x0][y0], numAlive, hueSum); | |
addCell(input[x0][y], numAlive, hueSum); | |
addCell(input[x0][y1], numAlive, hueSum); | |
addCell(input[x][y0], numAlive, hueSum); | |
// Skip x, y since that is the current cell | |
addCell(input[x][y1], numAlive, hueSum); | |
addCell(input[x1][y0], numAlive, hueSum); | |
addCell(input[x1][y], numAlive, hueSum); | |
addCell(input[x1][y1], numAlive, hueSum); | |
if (input[x][y].alive() && (numAlive == 2 || numAlive == 3) || numAlive == 3) { | |
addCell(input[x][y], numAlive, hueSum); | |
output[x][y].hue = hueSum / numAlive; | |
} else { | |
output[x][y].hue = 0; | |
} | |
} | |
} | |
} | |
void render(const Matrix &cells) { | |
for (size_t x = 0; x < GAME_WIDTH; x++) { | |
for (size_t y = 0; y < GAME_HEIGHT; y++) { | |
if (cells[x][y].alive()) { | |
matrix.drawPixel(x, y, matrix.ColorHSV(cells[x][y].hue, 0xff, 0xff, false)); | |
} else { | |
matrix.drawPixel(x, y, matrix.Color888(0x00, 0x00, 0x00)); | |
} | |
} | |
} | |
} | |
void setupWifi() { | |
WiFi.setPins(8,7,4,2); | |
// check for the presence of the shield: | |
if (WiFi.status() == WL_NO_SHIELD) { | |
Serial.println("WiFi shield not present"); | |
// don't continue: | |
while (true); | |
} | |
// attempt to connect to Wifi network: | |
while ( status != WL_CONNECTED) { | |
Serial.print("Attempting to connect to SSID: "); | |
Serial.println(ssid); | |
// Connect to WPA/WPA2 network. Change this line if using open or WEP network: | |
status = WiFi.begin(ssid, pass); | |
} | |
// start the WiFi OTA library with internal (flash) based storage | |
WiFiOTA.begin("dflemstr", "aBZ67j%38nWwNk%4Mx2QB1Mb4yg1jm", InternalStorage); | |
} | |
void printWifiStatus() { | |
// print the SSID of the network you're attached to: | |
Serial.print("SSID: "); | |
Serial.println(WiFi.SSID()); | |
// print your WiFi shield's IP address: | |
IPAddress ip = WiFi.localIP(); | |
Serial.print("IP Address: "); | |
Serial.println(ip); | |
// print the received signal strength: | |
long rssi = WiFi.RSSI(); | |
Serial.print("signal strength (RSSI):"); | |
Serial.print(rssi); | |
Serial.println(" dBm"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment