Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //-------------------------------------------------
- //
- // LIFE (adapté de la version ADAFRUIT)
- // pour arduino+OLED 128x32
- //
- //-------------------------------------------------
- #define WITH_OLED
- // Maximum number of generations until the screen is refreshed
- #define MAX_GEN_COUNT 99999
- // 1 x 1 pixel cells, array size = 20480 bytes per array
- #define SCREEN_WIDTH 128 // adapter à l'écran
- #define SCREEN_HEIGHT 64 // adapter à l'écran
- #define CELLXY 4
- #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 ?
- #define GRIDY (uint8_t)SCREEN_HEIGHT / CELLXY
- //Current grid and newgrid arrays are needed
- uint8_t grid[GRIDX/8][GRIDY];
- //The new grid for the next generation
- uint8_t newgrid[GRIDX/8][GRIDY];
- unsigned long cells = 0;
- #define GEN_DELAY 25 // délai entre générations
- #ifdef WITH_OLED
- #include <SPI.h>
- #include <Wire.h>
- #include <Adafruit_GFX.h>
- #include <Adafruit_SSD1306.h>
- #define OLED_RESET -1 //4 // Reset pin # (or -1 if sharing Arduino reset pin)
- #define SCREEN_ADDRESS 0x3C ///< See datasheet for Address; 0x3D for 128x64, 0x3C for 128x32
- Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
- #endif
- //-------------------------------------------------
- void setup() {
- Serial.begin(115200);
- Serial.println();
- Serial.println("==== AVR OLED Life ====");
- #ifdef WITH_OLED
- Wire.begin();
- delay(100);
- for (uint8_t nbtry = 0; nbtry < 10; nbtry++) {
- if (display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS)) {
- break;
- }
- Serial.println("Erreur init OLED");
- delay(250);
- }
- display.clearDisplay();
- display.display();
- #endif
- randomSeed(analogRead(0));
- }
- //-------------------------------------------------
- void loop() {
- cells = 0;
- Serial.println(F("Automate"));
- Serial.println(F("cellulaire"));
- Serial.println(F("v2.0"));
- #ifdef WITH_OLED
- display.clearDisplay();
- display.setTextSize(2);
- display.setTextColor(SSD1306_WHITE);
- display.setCursor(0, 1);
- display.println(F("Automate"));
- display.println(F("cellulaire"));
- display.drawRect(0, 0, SCREEN_WIDTH-1, SCREEN_HEIGHT-1, SSD1306_WHITE);
- display.setTextSize(1);
- display.setCursor(100, 1);
- display.println(F("v2.0"));
- display.display();
- delay(2500);
- display.clearDisplay();
- #endif
- unsigned long time_start = millis();
- initGrid();
- uint16_t nbStandby = 0;
- uint32_t nbModif_prec = 0;
- uint32_t nbModif_2prec = 0;
- uint32_t nbModif_3prec = 0;
- float duree;
- uint32_t gen;
- for (gen = 0; gen < MAX_GEN_COUNT; gen++) {
- uint32_t nbm = computeCA();
- if (nbm == nbModif_prec || nbm == nbModif_2prec || nbm == nbModif_3prec) {
- nbStandby++;
- Serial.println(nbStandby);
- }
- else {
- nbStandby = 0;
- }
- nbModif_3prec = nbModif_2prec;
- nbModif_2prec = nbModif_prec;
- nbModif_prec = nbm;
- delay(GEN_DELAY);
- drawGrid();
- cells += (GRIDX - 1) * (GRIDY - 1);
- #ifdef WITH_OLED
- display.display();
- #endif
- for (int16_t x = 1; x < GRIDX - 1; x++) {
- for (int16_t y = 1; y < GRIDY - 1; y++) {
- setGrid(x,y,getNewGrid(x,y));
- }
- }
- if (nbStandby > 3) {
- duree = (millis() - time_start - (gen * GEN_DELAY)) / 1000.0;
- break;
- }
- }
- #ifdef WITH_OLED
- display.clearDisplay();
- display.setTextColor(SSD1306_WHITE);
- display.setTextSize(1);
- display.setCursor(0, 0);
- display.print(gen);
- display.println(F(" generations"));
- display.print(cells);
- display.println(F(" cellules"));
- display.print("en ");
- display.print(duree);
- display.println(F("sec"));
- display.display();
- delay(5000);
- #else
- Serial.print(gen);
- Serial.println(F(" generations"));
- Serial.print(cells);
- Serial.println(F(" cellules"));
- Serial.print("en ");
- Serial.print(duree);
- Serial.println(F("sec"));
- delay(5000);
- #endif
- }
- //-------------------------------------------------
- void drawGrid(void) {
- #ifdef WITH_OLED
- uint16_t color = SSD1306_WHITE;
- for (int16_t x = 1; x < GRIDX - 1; x++) {
- for (int16_t y = 1; y < GRIDY - 1; y++) {
- if ((getGrid(x,y)) != (getNewGrid(x,y))) {
- if (getNewGrid(x,y) == 1) color = SSD1306_WHITE;
- else color = SSD1306_BLACK;
- display.fillRect(CELLXY * x, CELLXY * y, CELLXY, CELLXY, color);
- }
- }
- }
- #else
- char color;
- for (int il=0;il<25;il++) Serial.println();
- for (int16_t y = 1; y < GRIDY - 1; y++) {
- Serial.println();
- //Serial.print("y=");Serial.println(y);
- for (int16_t x = 1; x < GRIDX - 1; x++) {
- if (getNewGrid(x,y) == 1) color = 'X';
- else color = '-';
- Serial.print(color);
- }
- }
- #endif
- }
- //-------------------------------------------------
- void setGrid(int x, int y, int valeur) {
- if (valeur==0) {
- grid[x/8][y] &= ~(1 << (x%8));
- }
- else {
- grid[x/8][y] |= 1 << (x%8);
- }
- }
- //-------------------------------------------------
- void setNewGrid(int x, int y, int valeur) {
- if (valeur==0) {
- newgrid[x/8][y] &= ~(1 << (x%8));
- }
- else {
- newgrid[x/8][y] |= 1 << (x%8);
- }
- }
- //-------------------------------------------------
- uint8_t getGrid(int x, int y) {
- if ((grid[x/8][y] & 1 << (x%8))==0) {
- return 0;
- }
- else {
- return 1;
- }
- }
- //-------------------------------------------------
- uint8_t getNewGrid(int x, int y) {
- if ((newgrid[x/8][y] & 1 << (x%8))==0) {
- return 0;
- }
- else {
- return 1;
- }
- }
- //-------------------------------------------------
- void initGrid(void) {
- for (int16_t x = 0; x < GRIDX; x++) {
- for (int16_t y = 0; y < GRIDY; y++) {
- setNewGrid(x,y,0);
- if (x == 0 || x == GRIDX - 1 || y == 0 || y == GRIDY - 1) {
- setGrid(x,y,0);
- }
- else {
- if (random(5) == 1)
- setGrid(x,y,1);
- else
- setGrid(x,y,0);
- }
- }
- }
- }
- //-------------------------------------------------
- uint32_t computeCA() {
- uint32_t modif = 0;
- for (int16_t x = 1; x < GRIDX; x++) {
- for (int16_t y = 1; y < GRIDY; y++) {
- int neighbors = getNumberOfNeighbors(x, y);
- if (getGrid(x,y) == 1 && (neighbors == 2 || neighbors == 3 )) {
- setNewGrid(x,y,1);
- }
- else if (getGrid(x,y) == 1) {
- setNewGrid(x,y,0);
- modif++;
- }
- if (getGrid(x,y) == 0 && (neighbors == 3)) {
- setNewGrid(x,y,1);
- modif++;
- }
- else if (getGrid(x,y) == 0) {
- setNewGrid(x,y,0);
- }
- }
- }
- return modif;
- }
- //-------------------------------------------------
- int getNumberOfNeighbors(int x, int y) {
- 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);
- }
- //
Add Comment
Please, Sign In to add comment