WolfLarsen33

AIP1640 / TM1640 matrix Life

Mar 22nd, 2021 (edited)
161
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.  * AiP1640 / TM1640 (matrix) Life
  3.  *
  4. **/
  5.  
  6. #include <TM1640.h>
  7. #include <TM16xxMatrix.h>
  8.  
  9. //TM1640 module(9, 10);    // DIN=9, CLK=10
  10. TM1640 module(A4, A5);    // For ESP8266/WeMos D1-mini: DIN=D7/13/MOSI, CLK=D5/14/SCK
  11. // Maximum number of generations until the screen is refreshed
  12. #define MAX_GEN_COUNT 100
  13.  
  14. #define GRIDX 16  // nb colonnes
  15. #define GRIDY 8   // nb lignes
  16.  
  17. #define GEN_DELAY 150
  18.  
  19. TM16xxMatrix matrix(&module, GRIDX, GRIDY);
  20.  
  21. uint8_t grid[GRIDX][GRIDY];
  22. uint8_t newgrid[GRIDX][GRIDY];
  23.  
  24. void setup()
  25. {
  26.  
  27. }
  28.  
  29. void loop()
  30. {
  31.  
  32.   unsigned long cells = 0;
  33.   unsigned long time_start = millis();
  34.  
  35.   // Un petit flash au demarrage
  36.   module.setupDisplay(true, 0);
  37.   matrix.setAll(true);
  38.   for (int intensity = 0; intensity < 8; intensity++) {
  39.     module.setupDisplay(true, intensity);
  40.     delay(40);
  41.   }
  42.   for (int intensity = 7; intensity >= 0; intensity--) {
  43.     module.setupDisplay(true, intensity);
  44.     delay(40);
  45.   }
  46.   matrix.setAll(false);    // Note: module.clearDisplay() doesn't clear the offscreen bitmap!
  47.   module.setupDisplay(true, 7);
  48.   delay(500);
  49.  
  50.   initGrid();
  51.   // MAX_GEN_COUNT générations
  52.   for (int gen = 0; gen < MAX_GEN_COUNT; gen++) {
  53.     if ( ! computeCA()) { // s'il n'y a plus de mouvement on reinit
  54.       delay(1000);
  55.       break;
  56.     }
  57.     drawGrid();
  58.     cells+=(GRIDX-1)*(GRIDY - 1);
  59.     delay(GEN_DELAY);
  60.     for (int16_t x = 1; x < GRIDX-1; x++) {
  61.       for (int16_t y = 1; y < GRIDY-1; y++) {
  62.         grid[x][y] = newgrid[x][y];
  63.       }
  64.     }
  65.   }
  66.  
  67.   float duree = (millis() - time_start) / 1000.0;
  68. }
  69.  
  70. // Dessine la grille
  71. void drawGrid(void) {
  72.  
  73.   bool color = true;
  74.   for (int16_t x = 1; x < GRIDX - 1; x++) {
  75.     for (int16_t y = 1; y < GRIDY - 1; y++) {
  76.       if ((grid[x][y]) != (newgrid[x][y])) {
  77.         if (newgrid[x][y] == 1) color = true;
  78.         else color = false;
  79.           matrix.setPixel(x, y, color);
  80.       }
  81.     }
  82.   }
  83. }
  84.  
  85. // Initialise la grille
  86. void initGrid(void) {
  87.   for (int16_t x = 0; x < GRIDX; x++) {
  88.     for (int16_t y = 0; y < GRIDY; y++) {
  89.       newgrid[x][y] = 0;
  90.  
  91.       if (x == 0 || x == GRIDX - 1 || y == 0 || y == GRIDY - 1) {
  92.         grid[x][y] = 0;
  93.       }
  94.       else {
  95.         if (random(5) <= 2)
  96.           grid[x][y] = 1;
  97.         else
  98.           grid[x][y] = 0;
  99.       }
  100.  
  101.     }
  102.   }
  103. }
  104.  
  105. // Calcule la prochaine generation
  106. bool computeCA() {
  107.   bool change_occured=false;
  108.  
  109.   for (int16_t x = 1; x < GRIDX; x++) {
  110.     for (int16_t y = 1; y < GRIDY; y++) {
  111.       int neighbors = getNumberOfNeighbors(x, y);
  112.       if (grid[x][y] == 1 && (neighbors == 2 || neighbors == 3 ))
  113.       {
  114.         newgrid[x][y] = 1;
  115.       }
  116.       else {
  117.         if (grid[x][y] == 1) {
  118.           newgrid[x][y] = 0;
  119.         }
  120.       }
  121.       if (grid[x][y] == 0 && (neighbors == 3))
  122.       {
  123.         newgrid[x][y] = 1;
  124.       }
  125.       else {
  126.         if (grid[x][y] == 0) {
  127.           newgrid[x][y] = 0;
  128.         }
  129.       }
  130.       if (newgrid[x][y] != grid[x][y]) {
  131.         change_occured = true;
  132.       }
  133.     }
  134.   }
  135.   return change_occured;
  136. }
  137.  
  138. // entourage de la cellule
  139. int getNumberOfNeighbors(int x, int y) {
  140.   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];
  141. }
  142.  
Add Comment
Please, Sign In to add comment