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 3.51 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 = 50;
  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.  
  42. Serial.begin(9600);
  43.  
  44. for (int32_t i=TFTSHIELD_BACKLIGHT_OFF; i<TFTSHIELD_BACKLIGHT_ON; i+=100) {
  45.  
  46. ss.setBacklight(i);
  47.  
  48. delay(1);
  49. }
  50. }
  51.  
  52. void seedRandomGeneration(char *buf,int count)
  53.  
  54. {
  55.  
  56. randomSeed(analogRead(A1));
  57.  
  58.  
  59.  
  60. int x = random(w);
  61.  
  62. int y = random(h);
  63.  
  64. board[g][x][y] = 1;
  65.  
  66. int tab[2];
  67. tab[0]=x;
  68. tab[1]=y;
  69.  
  70. Serial.println(x);
  71. Serial.println(y);
  72.  
  73. return tab;
  74. }
  75.  
  76. void displayBoard()
  77.  
  78. {
  79.  
  80. tft.fillScreen(0x0000);
  81.  
  82. for (int x = 0; x < w; x++)
  83.  
  84. {
  85.  
  86. for (int y = 0; y < h; y++)
  87.  
  88. {
  89.  
  90. if (board[g][x][y])
  91.  
  92. {
  93.  
  94. tft.fillRect(x * 8, y * 8, 8, 8, ST7735_BLUE);
  95.  
  96. }
  97.  
  98. }
  99.  
  100. }
  101.  
  102. }
  103.  
  104. void loop()
  105. {
  106.  
  107. uint32_t buttons = ss.readButtons();
  108.  
  109. if(! (buttons & TFTSHIELD_BUTTON_2)){
  110.  
  111.  
  112.  
  113. displayBoard();
  114.  
  115. int x1,y1;
  116. int p[2];
  117. p=seedRandomGeneration();
  118. x1=p(0);
  119. y1=p(1);
  120. Serial.println(x1);
  121. Serial.println(y1);
  122.  
  123. if(! (buttons & TFTSHIELD_BUTTON_DOWN)){
  124.  
  125.  
  126. if (board[g][x1][y1])
  127. {
  128. board[g][x1][y1] = 0;
  129. board[g][x1][y1-8] = 1;
  130. displayBoard();
  131.  
  132. }
  133.  
  134.  
  135. }
  136.  
  137.  
  138. if(! (buttons & TFTSHIELD_BUTTON_LEFT)){
  139.  
  140. }
  141.  
  142. if(! (buttons & TFTSHIELD_BUTTON_UP)){
  143.  
  144. }
  145.  
  146. if(! (buttons & TFTSHIELD_BUTTON_RIGHT)){
  147.  
  148.  
  149. }
  150.  
  151.  
  152. }
  153.  
  154. if(! (buttons & TFTSHIELD_BUTTON_1)){
  155.  
  156.  
  157. displayBoard();
  158.  
  159. updateGeneration();
  160.  
  161. g = !g;
  162.  
  163. }
  164.  
  165. delay(100);
  166.  
  167. }
  168.  
  169. void updateGeneration()
  170.  
  171. {
  172.  
  173. for (int x = 0; x < w; x++)
  174.  
  175. {
  176.  
  177. for (int y = 0; y < h; y++)
  178.  
  179. {
  180.  
  181. board[!g][x][y] = updateCell(x, y);
  182.  
  183. }
  184.  
  185. }
  186.  
  187. }
  188.  
  189. byte updateCell(int x, int y)
  190.  
  191. {
  192.  
  193. int n = numNeighbors(x, y);
  194.  
  195. if (board[g][x][y] == 1)
  196.  
  197. {
  198.  
  199. if (n < 2) return 0;
  200.  
  201. else if (n == 2 || n == 3) return 1;
  202.  
  203. else if (n > 3) return 0;
  204.  
  205. }
  206.  
  207. else
  208.  
  209. {
  210.  
  211. return (n == 3);
  212.  
  213. }
  214.  
  215. }
  216.  
  217. int numNeighbors(int x, int y)
  218.  
  219. {
  220.  
  221. int n = 0;
  222.  
  223. for (int dx = -1; dx <= 1; dx++)
  224.  
  225. {
  226.  
  227. for (int dy = -1; dy <= 1; dy++)
  228.  
  229. {
  230.  
  231. int x1 = x + dx;
  232.  
  233. int y1 = y + dy;
  234.  
  235. boolean middle = (dx == 0 && dy == 0);
  236.  
  237. if (!middle && x1 > 0 && x1 < w && y1 > 0 && y1 < h)
  238.  
  239. {
  240.  
  241. n += board[g][x1][y1];
  242.  
  243. }
  244.  
  245. }
  246.  
  247. }
  248.  
  249. Serial.print(n);
  250.  
  251. return n;
  252.  
  253. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement