acid-drop- Hacking the planet from a LilyGo T-Deck using custom firmware |
git clone git://git.acid.vegas/acid-drop.git |
Log | Files | Refs | Archive | README | LICENSE |
Arduino_Life.ino (4589B)
1 //The Game of Life, also known simply as Life, is a cellular automaton 2 //devised by the British mathematician John Horton Conway in 1970. 3 // https://en.wikipedia.org/wiki/Conway's_Game_of_Life 4 5 // See license at end of file. 6 7 // Adapted by Bodmer 8 9 #include <TFT_eSPI.h> // Hardware-specific library 10 #include <SPI.h> 11 12 TFT_eSPI tft = TFT_eSPI(); // Invoke custom library 13 14 // Maximum number of generations until the screen is refreshed 15 #define MAX_GEN_COUNT 500 16 17 // The ESP8266 has plenty of memory so we can create a large array 18 // 2 x 2 pixel cells, array size = 5120 bytes per array, runs fast 19 #define GRIDX 80 20 #define GRIDY 64 21 #define CELLXY 2 22 23 // 1 x 1 pixel cells, array size = 20480 bytes per array 24 //#define GRIDX 160 25 //#define GRIDY 128 26 //#define CELLXY 1 27 28 #define GEN_DELAY 10 // Set a delay between each generation to slow things down 29 30 //Current grid and newgrid arrays are needed 31 uint8_t grid[GRIDX][GRIDY]; 32 33 //The new grid for the next generation 34 uint8_t newgrid[GRIDX][GRIDY]; 35 36 //Number of generations 37 uint16_t genCount = 0; 38 39 void setup() { 40 41 //Set up the display 42 tft.init(); 43 tft.setRotation(3); 44 tft.fillScreen(TFT_BLACK); 45 tft.setTextSize(1); 46 tft.setTextColor(TFT_WHITE); 47 tft.setCursor(0, 0); 48 49 } 50 51 void loop() { 52 53 //Display a simple splash screen 54 tft.fillScreen(TFT_BLACK); 55 tft.setTextSize(2); 56 tft.setTextColor(TFT_WHITE); 57 tft.setCursor(40, 5); 58 tft.println(F("Arduino")); 59 tft.setCursor(35, 25); 60 tft.println(F("Cellular")); 61 tft.setCursor(35, 45); 62 tft.println(F("Automata")); 63 64 delay(1000); 65 66 tft.fillScreen(TFT_BLACK); 67 68 initGrid(); 69 70 genCount = MAX_GEN_COUNT; 71 72 drawGrid(); 73 74 //Compute generations 75 for (int gen = 0; gen < genCount; gen++) 76 { 77 computeCA(); 78 drawGrid(); 79 delay(GEN_DELAY); 80 for (int16_t x = 1; x < GRIDX-1; x++) { 81 for (int16_t y = 1; y < GRIDY-1; y++) { 82 grid[x][y] = newgrid[x][y]; 83 } 84 } 85 86 } 87 } 88 89 //Draws the grid on the display 90 void drawGrid(void) { 91 92 uint16_t color = TFT_WHITE; 93 for (int16_t x = 1; x < GRIDX - 1; x++) { 94 for (int16_t y = 1; y < GRIDY - 1; y++) { 95 if ((grid[x][y]) != (newgrid[x][y])) { 96 if (newgrid[x][y] == 1) color = 0xFFFF; //random(0xFFFF); 97 else color = 0; 98 tft.fillRect(CELLXY * x, CELLXY * y, CELLXY, CELLXY, color); 99 } 100 } 101 } 102 } 103 104 //Initialise Grid 105 void initGrid(void) { 106 for (int16_t x = 0; x < GRIDX; x++) { 107 for (int16_t y = 0; y < GRIDY; y++) { 108 newgrid[x][y] = 0; 109 110 if (x == 0 || x == GRIDX - 1 || y == 0 || y == GRIDY - 1) { 111 grid[x][y] = 0; 112 } 113 else { 114 if (random(3) == 1) 115 grid[x][y] = 1; 116 else 117 grid[x][y] = 0; 118 } 119 120 } 121 } 122 } 123 124 //Compute the CA. Basically everything related to CA starts here 125 void computeCA() { 126 for (int16_t x = 1; x < GRIDX; x++) { 127 for (int16_t y = 1; y < GRIDY; y++) { 128 int neighbors = getNumberOfNeighbors(x, y); 129 if (grid[x][y] == 1 && (neighbors == 2 || neighbors == 3 )) 130 { 131 newgrid[x][y] = 1; 132 } 133 else if (grid[x][y] == 1) newgrid[x][y] = 0; 134 if (grid[x][y] == 0 && (neighbors == 3)) 135 { 136 newgrid[x][y] = 1; 137 } 138 else if (grid[x][y] == 0) newgrid[x][y] = 0; 139 } 140 } 141 } 142 143 // Check the Moore neighbourhood 144 int getNumberOfNeighbors(int x, int y) { 145 return grid[x - 1][y] + grid[x - 1][y - 1] + grid[x][y - 1] + grid[x + 1][y - 1] + grid[x + 1][y] + grid[x + 1][y + 1] + grid[x][y + 1] + grid[x - 1][y + 1]; 146 } 147 148 /* 149 The MIT License (MIT) 150 151 Copyright (c) 2016 RuntimeProjects.com 152 153 Permission is hereby granted, free of charge, to any person obtaining a copy 154 of this software and associated documentation files (the "Software"), to deal 155 in the Software without restriction, including without limitation the rights 156 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 157 copies of the Software, and to permit persons to whom the Software is 158 furnished to do so, subject to the following conditions: 159 160 The above copyright notice and this permission notice shall be included in all 161 copies or substantial portions of the Software. 162 163 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 164 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 165 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 166 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 167 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 168 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 169 SOFTWARE. 170 */