Advertisement
Guest User

Untitled

a guest
Jun 24th, 2019
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.49 KB | None | 0 0
  1. #include <SPI.h>
  2. #include <Adafruit_GFX.h>
  3. #include <Adafruit_ST7735.h>
  4. #include <Adafruit_seesaw.h>
  5. #include <Adafruit_TFTShield18.h>
  6.  
  7. Adafruit_TFTShield18 ss;
  8.  
  9. const int w = 20;
  10.  
  11. const int h = 16;
  12.  
  13. const int initialPopulation = 70;
  14.  
  15. byte board[2][w][h];
  16.  
  17. byte g = 0;
  18.  
  19. #define SD_CS 4
  20.  
  21. #define TFT_CS 10
  22.  
  23. #define TFT_DC 8
  24.  
  25. #define TFT_RST -1
  26.  
  27. Adafruit_ST7735 tft = Adafruit_ST7735(TFT_CS, TFT_DC, TFT_RST);
  28.  
  29. void setup(void) {
  30.  
  31. ss.begin();
  32.  
  33. ss.tftReset();
  34.  
  35. tft.initR(INITR_GREENTAB);
  36.  
  37. tft.fillScreen(0x0000);
  38.  
  39. tft.setRotation(3);
  40.  
  41. seedRandomGeneration();
  42.  
  43. Serial.begin(9600);
  44.  
  45. for (int32_t i=TFTSHIELD_BACKLIGHT_OFF; i<TFTSHIELD_BACKLIGHT_ON; i+=100) {
  46.  
  47. ss.setBacklight(i);
  48.  
  49. delay(1);
  50. }
  51. }
  52.  
  53. void seedRandomGeneration()
  54.  
  55. {
  56.  
  57. randomSeed(analogRead(A1));
  58.  
  59. for (byte i=0; i < initialPopulation; i++)
  60.  
  61. {
  62.  
  63. int x = random(w);
  64.  
  65. int y = random(h);
  66.  
  67. board[g][x][y] = 1;
  68.  
  69. }
  70.  
  71. }
  72.  
  73. void displayBoard()
  74.  
  75. {
  76.  
  77. tft.fillScreen(0x0000);
  78.  
  79. for (int x = 0; x < w; x++)
  80.  
  81. {
  82.  
  83. for (int y = 0; y < h; y++)
  84.  
  85. {
  86.  
  87. if (board[g][x][y])
  88.  
  89. {
  90.  
  91. tft.fillRect(x * 8, y * 8, 8, 8, ST7735_BLUE);
  92.  
  93. }
  94.  
  95. }
  96.  
  97. }
  98.  
  99. }
  100.  
  101. void loop()
  102.  
  103. {
  104.  
  105. if (analogRead(A3) < 512)
  106.  
  107. {
  108.  
  109. displayBoard();
  110.  
  111. updateGeneration();
  112.  
  113. g = !g;
  114.  
  115. }
  116.  
  117. delay(7000);
  118.  
  119. }
  120.  
  121. void updateGeneration()
  122.  
  123. {
  124.  
  125. for (int x = 0; x < w; x++)
  126.  
  127. {
  128.  
  129. for (int y = 0; y < h; y++)
  130.  
  131. {
  132.  
  133. board[!g][x][y] = updateCell(x, y);
  134.  
  135. }
  136.  
  137. }
  138.  
  139. }
  140.  
  141. byte updateCell(int x, int y)
  142.  
  143. {
  144.  
  145. int n = numNeighbors(x, y);
  146.  
  147. if (board[g][x][y] == 1)
  148.  
  149. {
  150.  
  151. if (n < 2) return 0;
  152.  
  153. else if (n == 2 || n == 3) return 1;
  154.  
  155. else if (n > 3) return 0;
  156.  
  157. }
  158.  
  159. else
  160.  
  161. {
  162.  
  163. return (n == 3);
  164.  
  165. }
  166.  
  167. }
  168.  
  169. int numNeighbors(int x, int y)
  170.  
  171. {
  172.  
  173. int n = 0;
  174.  
  175. for (int dx = -1; dx <= 1; dx++)
  176.  
  177. {
  178.  
  179. for (int dy = -1; dy <= 1; dy++)
  180.  
  181. {
  182.  
  183. int x1 = x + dx;
  184.  
  185. int y1 = y + dy;
  186.  
  187. boolean middle = (dx == 0 && dy == 0);
  188.  
  189. if (!middle && x1 > 0 && x1 < w && y1 > 0 && y1 < h)
  190.  
  191. {
  192.  
  193. n += board[g][x1][y1];
  194.  
  195. }
  196.  
  197. }
  198.  
  199. }
  200.  
  201. Serial.print(n);
  202.  
  203. return n;
  204.  
  205. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement