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

Cellular_Automata.ino (4180B)

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