WolfLarsen33

arduino_oled_life

Feb 22nd, 2022 (edited)
472
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //-------------------------------------------------
  2. //
  3. // LIFE (adapté de la version ADAFRUIT)
  4. // pour arduino+OLED 128x32
  5. //
  6. //-------------------------------------------------
  7.  
  8. #define WITH_OLED
  9.  
  10. // Maximum number of generations until the screen is refreshed
  11. #define MAX_GEN_COUNT 99999
  12.  
  13. // 1 x 1 pixel cells, array size = 20480 bytes per array
  14. #define SCREEN_WIDTH 128 // adapter à l'écran
  15. #define SCREEN_HEIGHT 64 // adapter à l'écran
  16. #define CELLXY 4
  17. #define GRIDX (uint8_t)SCREEN_WIDTH / CELLXY // si je ne divise pas par 2 la largeur d'affichage, l'écran ne s'init pas... pbme de ram ?
  18. #define GRIDY (uint8_t)SCREEN_HEIGHT / CELLXY
  19.  
  20. //Current grid and newgrid arrays are needed
  21. uint8_t grid[GRIDX/8][GRIDY];
  22.  
  23. //The new grid for the next generation
  24. uint8_t newgrid[GRIDX/8][GRIDY];
  25.  
  26. unsigned long cells = 0;
  27.  
  28. #define GEN_DELAY 25 // délai entre générations
  29.  
  30. #ifdef WITH_OLED
  31. #include <SPI.h>
  32. #include <Wire.h>
  33. #include <Adafruit_GFX.h>
  34. #include <Adafruit_SSD1306.h>
  35. #define OLED_RESET     -1 //4 // Reset pin # (or -1 if sharing Arduino reset pin)
  36. #define SCREEN_ADDRESS 0x3C ///< See datasheet for Address; 0x3D for 128x64, 0x3C for 128x32
  37. Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
  38. #endif
  39.  
  40. //-------------------------------------------------
  41. void setup()   {
  42.   Serial.begin(115200);
  43.   Serial.println();
  44.   Serial.println("==== AVR OLED Life ====");
  45.  
  46. #ifdef WITH_OLED
  47.   Wire.begin();
  48.   delay(100);
  49.   for (uint8_t nbtry = 0; nbtry < 10; nbtry++) {
  50.     if (display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS)) {
  51.       break;
  52.     }
  53.     Serial.println("Erreur init OLED");
  54.     delay(250);
  55.   }
  56.   display.clearDisplay();
  57.   display.display();
  58. #endif
  59.   randomSeed(analogRead(0));
  60. }
  61.  
  62. //-------------------------------------------------
  63. void loop() {
  64.  
  65.   cells = 0;
  66.  
  67.   Serial.println(F("Automate"));
  68.   Serial.println(F("cellulaire"));
  69.   Serial.println(F("v2.0"));
  70. #ifdef WITH_OLED
  71.   display.clearDisplay();
  72.   display.setTextSize(2);
  73.   display.setTextColor(SSD1306_WHITE);
  74.   display.setCursor(0, 1);
  75.   display.println(F("Automate"));
  76.   display.println(F("cellulaire"));
  77.   display.drawRect(0, 0, SCREEN_WIDTH-1, SCREEN_HEIGHT-1, SSD1306_WHITE);
  78.   display.setTextSize(1);
  79.   display.setCursor(100, 1);
  80.   display.println(F("v2.0"));
  81.   display.display();
  82.   delay(2500);
  83.  
  84.   display.clearDisplay();
  85. #endif
  86.  
  87.   unsigned long time_start = millis();
  88.   initGrid();
  89.   uint16_t nbStandby = 0;
  90.   uint32_t nbModif_prec = 0;
  91.   uint32_t nbModif_2prec = 0;
  92.   uint32_t nbModif_3prec = 0;
  93.   float duree;
  94.  
  95.   uint32_t gen;
  96.   for (gen = 0; gen < MAX_GEN_COUNT; gen++) {
  97.     uint32_t nbm = computeCA();
  98.     if (nbm == nbModif_prec || nbm == nbModif_2prec || nbm == nbModif_3prec) {
  99.       nbStandby++;
  100. Serial.println(nbStandby);
  101.     }
  102.     else {
  103.       nbStandby = 0;
  104.     }
  105.     nbModif_3prec = nbModif_2prec;
  106.     nbModif_2prec = nbModif_prec;
  107.     nbModif_prec = nbm;
  108.  
  109.     delay(GEN_DELAY);
  110.  
  111.     drawGrid();
  112.     cells += (GRIDX - 1) * (GRIDY - 1);
  113. #ifdef WITH_OLED
  114.     display.display();
  115. #endif
  116.  
  117.     for (int16_t x = 1; x < GRIDX - 1; x++) {
  118.       for (int16_t y = 1; y < GRIDY - 1; y++) {
  119.         setGrid(x,y,getNewGrid(x,y));
  120.       }
  121.     }
  122.     if (nbStandby > 3) {
  123.       duree = (millis() - time_start - (gen * GEN_DELAY)) / 1000.0;
  124.       break;
  125.     }
  126.   }
  127.  
  128. #ifdef WITH_OLED
  129.   display.clearDisplay();
  130.   display.setTextColor(SSD1306_WHITE);
  131.   display.setTextSize(1);
  132.   display.setCursor(0, 0);
  133.   display.print(gen);
  134.   display.println(F(" generations"));
  135.   display.print(cells);
  136.   display.println(F(" cellules"));
  137.   display.print("en ");
  138.   display.print(duree);
  139.   display.println(F("sec"));
  140.   display.display();
  141.   delay(5000);
  142. #else
  143.   Serial.print(gen);
  144.   Serial.println(F(" generations"));
  145.   Serial.print(cells);
  146.   Serial.println(F(" cellules"));
  147.   Serial.print("en ");
  148.   Serial.print(duree);
  149.   Serial.println(F("sec"));
  150.   delay(5000);
  151. #endif
  152.  
  153. }
  154.  
  155. //-------------------------------------------------
  156. void drawGrid(void) {
  157.  
  158. #ifdef WITH_OLED
  159.   uint16_t color = SSD1306_WHITE;
  160.   for (int16_t x = 1; x < GRIDX - 1; x++) {
  161.     for (int16_t y = 1; y < GRIDY - 1; y++) {
  162.       if ((getGrid(x,y)) != (getNewGrid(x,y))) {
  163.         if (getNewGrid(x,y) == 1) color = SSD1306_WHITE;
  164.         else color = SSD1306_BLACK;
  165.         display.fillRect(CELLXY * x, CELLXY * y, CELLXY, CELLXY, color);
  166.       }
  167.     }
  168.   }
  169. #else
  170.   char color;
  171.   for (int il=0;il<25;il++) Serial.println();
  172.   for (int16_t y = 1; y < GRIDY - 1; y++) {
  173.     Serial.println();
  174. //Serial.print("y=");Serial.println(y);
  175.     for (int16_t x = 1; x < GRIDX - 1; x++) {
  176.       if (getNewGrid(x,y) == 1) color = 'X';
  177.       else color = '-';
  178.       Serial.print(color);
  179.     }
  180.   }
  181. #endif
  182. }
  183.  
  184. //-------------------------------------------------
  185. void setGrid(int x, int y, int valeur) {
  186.   if (valeur==0) {
  187.     grid[x/8][y] &= ~(1 << (x%8));
  188.   }
  189.   else {
  190.     grid[x/8][y] |= 1 << (x%8);
  191.   }
  192.  
  193. }
  194.  
  195. //-------------------------------------------------
  196. void setNewGrid(int x, int y, int valeur) {
  197.   if (valeur==0) {
  198.     newgrid[x/8][y] &= ~(1 << (x%8));
  199.   }
  200.   else {
  201.     newgrid[x/8][y] |= 1 << (x%8);
  202.   }
  203.  
  204. }
  205.  
  206. //-------------------------------------------------
  207. uint8_t getGrid(int x, int y) {
  208.  
  209.   if ((grid[x/8][y] & 1 << (x%8))==0) {
  210.     return 0;
  211.   }
  212.   else {
  213.     return 1;
  214.   }
  215. }
  216.  
  217. //-------------------------------------------------
  218. uint8_t getNewGrid(int x, int y) {
  219.   if ((newgrid[x/8][y] & 1 << (x%8))==0) {
  220.     return 0;
  221.   }
  222.   else {
  223.     return 1;
  224.   }
  225. }
  226.  
  227. //-------------------------------------------------
  228. void initGrid(void) {
  229.   for (int16_t x = 0; x < GRIDX; x++) {
  230.     for (int16_t y = 0; y < GRIDY; y++) {
  231.       setNewGrid(x,y,0);
  232.  
  233.       if (x == 0 || x == GRIDX - 1 || y == 0 || y == GRIDY - 1) {
  234.         setGrid(x,y,0);
  235.       }
  236.       else {
  237.         if (random(5) == 1)
  238.           setGrid(x,y,1);
  239.         else
  240.           setGrid(x,y,0);
  241.       }
  242.  
  243.     }
  244.   }
  245. }
  246.  
  247. //-------------------------------------------------
  248. uint32_t computeCA() {
  249.   uint32_t modif = 0;
  250.   for (int16_t x = 1; x < GRIDX; x++) {
  251.     for (int16_t y = 1; y < GRIDY; y++) {
  252.       int neighbors = getNumberOfNeighbors(x, y);
  253.       if (getGrid(x,y) == 1 && (neighbors == 2 || neighbors == 3 )) {
  254.         setNewGrid(x,y,1);
  255.       }
  256.       else if (getGrid(x,y) == 1) {
  257.         setNewGrid(x,y,0);
  258.         modif++;
  259.       }
  260.       if (getGrid(x,y) == 0 && (neighbors == 3)) {
  261.         setNewGrid(x,y,1);
  262.         modif++;
  263.       }
  264.       else if (getGrid(x,y) == 0) {
  265.         setNewGrid(x,y,0);
  266.       }
  267.     }
  268.   }
  269.   return modif;
  270. }
  271.  
  272. //-------------------------------------------------
  273. int getNumberOfNeighbors(int x, int y) {
  274.   return getGrid(x - 1,y) + getGrid(x - 1,y - 1) + getGrid(x,y - 1) + getGrid(x + 1,y - 1) + getGrid(x + 1,y) + getGrid(x + 1,y + 1) + getGrid(x,y + 1) + getGrid(x - 1,y + 1);
  275. }
  276.  
  277. //
Add Comment
Please, Sign In to add comment