Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <Adafruit_NeoPixel.h>
- // ===============================
- // ========== Constants ==========
- // ===============================
- #define IO_PIN 10
- #define NUM_PIXEL 100
- #define BRIGHTNESS 255
- #define TIME_DELAY 1500
- #define MATRIX_SIZE 10
- #define MATRIX_EMPTY 0
- #define MATRIX_SET 1
- #define RED 0x00FF0000
- #define GREEN 0x0000FF00
- #define BLUE 0x000000FF
- // =============================
- // ========== CLASSES ==========
- // =============================
- class Map {
- private:
- bool matrix[MATRIX_SIZE][MATRIX_SIZE];
- public:
- Map(){
- doEmpty();
- }
- ~Map(){
- }
- bool get(uint8_t x, uint8_t y){
- return matrix[y][x];
- }
- void set(uint8_t x, uint8_t y, bool value){
- matrix[y][x] = value;
- }
- bool isEmpty(){
- for (uint8_t i = 0; i < MATRIX_SIZE; i++){
- for (uint8_t j = 0; j < MATRIX_SIZE; j++){
- if (matrix[i][j] == true){
- return false;
- }
- }
- }
- return true;
- }
- void doEmpty(){
- for (uint8_t i = 0; i < MATRIX_SIZE; i++){
- for (uint8_t j = 0; j < MATRIX_SIZE; j++){
- matrix[i][j] = false;
- }
- }
- }
- bool coordsInRange(int8_t x, int8_t y){
- if ((x < 0) || (y < 0)){
- return false;
- }
- if ((x >= MATRIX_SIZE) || (y >= MATRIX_SIZE)){
- return false;
- }
- return true;
- }
- uint8_t countNeighbors(uint8_t x, uint8_t y){
- uint8_t count = 0;
- for (int8_t i = x - 1; i < x + 2; i++){
- for (int8_t j = y - 1; j < y + 2; j++){
- if (coordsInRange(i, j) && (get(i, j) == true)){
- count++;
- } else {
- }
- }
- }
- if (get(x, y) == true){
- count--;
- }
- return count;
- }
- };
- // ==========
- class GameOfLife {
- private:
- Map map_a;
- Map map_b;
- Map history_1;
- Map history_2;
- Map *currentMap;
- Map *otherMap;
- public:
- GameOfLife(){
- map_a = Map();
- map_b = Map();
- history_1 = Map();
- history_2 = Map();
- currentMap = &map_a;
- otherMap = &map_b;
- }
- ~GameOfLife(){
- }
- void switchMaps(){
- if (currentMap == &map_a){
- currentMap = &map_b;
- otherMap = &map_a;
- } else {
- currentMap = &map_a;
- otherMap = &map_b;
- }
- }
- void setHistory(Map &map){
- copy(history_1, history_2);
- copy(map, history_1);
- }
- void copy(Map &map_1, Map &map_2){
- for (uint8_t i = 0; i < MATRIX_SIZE; i++){
- for (uint8_t j = 0; j < MATRIX_SIZE; j++){
- map_2.set(i, j, map_1.get(i, j));
- }
- }
- }
- void rnd(){
- for (uint8_t i = 0; i < MATRIX_SIZE; i++){
- for (uint8_t j = 0; j < MATRIX_SIZE; j++){
- if (random(4) == 0){
- currentMap->set(i, j, true);
- } else {
- currentMap->set(i, j, false);
- }
- }
- }
- }
- bool isEmpty(){
- return currentMap->isEmpty();
- }
- void doEmpty(){
- currentMap->doEmpty();
- }
- bool get(uint8_t x, uint8_t y){
- return currentMap->get(x, y);
- }
- void iterate(){
- setHistory(*otherMap);
- otherMap->doEmpty();
- uint8_t count;
- for (uint8_t i = 0; i < MATRIX_SIZE; i++){
- for (uint8_t j = 0; j < MATRIX_SIZE; j++){
- count = currentMap->countNeighbors(i, j);
- if (count == 3){
- otherMap->set(i, j, true);
- }
- if ((count == 2) && (currentMap->get(i, j) == true)){
- otherMap->set(i, j, true);
- }
- }
- }
- switchMaps();
- }
- bool bothEqual(Map &map_1, Map &map_2){
- for (uint8_t i = 0; i < MATRIX_SIZE; i++){
- for (uint8_t j = 0; j < MATRIX_SIZE; j++){
- if (map_1.get(i, j) != map_2.get(i, j)){
- return false;
- }
- }
- }
- return true;
- }
- bool isLoop(){
- bool isLoop_bool = false;
- isLoop_bool |= bothEqual(*currentMap, *otherMap);
- isLoop_bool |= bothEqual(*currentMap, history_1);
- isLoop_bool |= bothEqual(*currentMap, history_2);
- return isLoop_bool;
- }
- };
- // =============================
- // ========== GLOBALS ==========
- // =============================
- long periodEnd;
- GameOfLife game = GameOfLife();
- Adafruit_NeoPixel strip = Adafruit_NeoPixel(NUM_PIXEL, IO_PIN, NEO_GRBW + NEO_KHZ800);
- // ====================================
- // ========== Main-Functions ==========
- // ====================================
- void setup() {
- randomSeed(analogRead(0));
- Serial.begin(9600);
- while (!Serial) {
- ;
- }
- strip.begin(); // This initializes the NeoPixel library.
- strip.setBrightness(BRIGHTNESS); // set brightness
- strip.show(); // Initialize all pixels to 'off'
- }
- void loop() {
- if (game.isEmpty()){
- game.rnd();
- }
- if (game.isLoop()){
- game.doEmpty();
- }
- game.iterate();
- while (periodEnd > millis()) {
- ; // Wait
- }
- periodEnd = millis() + TIME_DELAY;
- renderToScreen();
- }
- void renderToScreen(){
- uint8_t pos = 0;
- uint32_t pointColor = strip.Color(0, 0, 255);
- for (uint8_t i = 0; i < MATRIX_SIZE; i++){
- for (uint8_t j = 0; j < MATRIX_SIZE; j++){
- pos = calcLedPos(i, j);
- if (game.get(i, j) == true){
- strip.setPixelColor(pos, BLUE);
- } else {
- strip.setPixelColor(pos, 0);
- }
- }
- }
- strip.show();
- }
- /**
- Calculates the position of a coordinate on the led strip
- */
- uint8_t calcLedPos(uint8_t x, uint8_t y) {
- uint8_t pos = y * MATRIX_SIZE;
- if ((y % 2) == 0) {
- return pos + x;
- } else {
- return pos + MATRIX_SIZE - x - 1;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement