1
0
Fork 0

game of life proof of concept

main
radex 2024-04-15 23:13:11 +02:00
parent b8aa12aac5
commit e32f90d4af
Signed by: radex
SSH Key Fingerprint: SHA256:hvqRXAGG1h89yqnS+cyFTLKQbzjWD4uXIqw7Y+0ws30
2 changed files with 96 additions and 3 deletions

66
firmware/src/life.cpp Normal file
View File

@ -0,0 +1,66 @@
#include <string.h>
#include <Arduino.h>
#define CELLS_X 40
#define CELLS_Y 40
#define CELL_COUNT (CELLS_X * CELLS_Y)
bool cells[CELL_COUNT] = {0};
#define XY(x, y) (x + y * CELLS_Y)
void life_setup() {
// set up initial state
cells[XY(2, 1)] = true;
cells[XY(3, 2)] = true;
cells[XY(1, 3)] = true;
cells[XY(2, 3)] = true;
cells[XY(3, 3)] = true;
}
void life_step() {
bool new_cells[CELL_COUNT] = {0};
for (int y = 0; y < CELLS_Y; y++) {
for (int x = 0; x < CELLS_X; x++) {
int neighbors = 0;
bool wasLive = cells[XY(x, y)];
for (int dy = -1; dy <= 1; dy++) {
for (int dx = -1; dx <= 1; dx++) {
if (dx == 0 && dy == 0) {
continue;
}
int check_y = y + dy;
int check_x = x + dx;
if (check_y >= 0 && check_y < CELLS_Y) {
if (check_x >= 0 && check_x < CELLS_X) {
if (cells[XY(check_x, check_y)]) {
neighbors++;
}
}
}
}
}
if (wasLive && neighbors < 2) {
// dies by underpopulation
} else if (wasLive && (neighbors == 2 || neighbors == 3)) {
// continues to live
new_cells[XY(x, y)] = true;
} else if (wasLive && neighbors > 3) {
// dies by overpopulation
} else if (!wasLive && neighbors == 3) {
// reproduces
new_cells[XY(x, y)] = true;
} else {
// still dead, jim
}
}
}
// copy
memcpy(cells, new_cells, sizeof(cells));
}

View File

@ -52,6 +52,10 @@ uint8_t brightnessPhaseDelays[] = {1, 10, 30, 100};
uint8_t framebuffer[ROW_COUNT * COL_COUNT] = {0};
void main2();
void life_setup();
void life_step();
extern bool cells[ROW_COUNT * COL_COUNT];
void setup() {
Serial.begin(115200);
Serial.println("Hello worldd!");
@ -91,9 +95,20 @@ void setup() {
// launch core1
// NOTE: For some reason, without delay, core1 doesn't start?
delay(500);
multicore_reset_core1();
multicore_launch_core1(main2);
// delay(500);
// multicore_reset_core1();
// multicore_launch_core1(main2);
// setup_audio();
life_setup();
// copy cells to framebuffer
for (int y = 0; y < ROW_COUNT; y++) {
for (int x = 0; x < COL_COUNT; x++) {
framebuffer[y * ROW_COUNT + x] = cells[y * ROW_COUNT + x] ? 255 : 0;
}
}
}
void loop2();
@ -137,6 +152,18 @@ void loop() {
lastRenderedFrameIndex = frameIndex;
}
// game of life step
auto now = millis();
if (now - frameLastChangedAt > 100) {
frameLastChangedAt = now;
life_step();
for (int y = 0; y < ROW_COUNT; y++) {
for (int x = 0; x < COL_COUNT; x++) {
framebuffer[y * ROW_COUNT + x] = cells[y * ROW_COUNT + x] ? 255 : 0;
}
}
}
// hide output
outputEnable(ROW_OE, false);