Advertisement
Guest User

Untitled

a guest
Jun 24th, 2019
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.83 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 = 30;
  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)
  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. buf[0] = x;
  67. buf[1]= y;
  68.  
  69. Serial.println(x);
  70. Serial.println(y);
  71.  
  72.  
  73. }
  74.  
  75. void displayBoard()
  76.  
  77. {
  78.  
  79. tft.fillScreen(0x0000);
  80.  
  81. for (int x = 0; x < w; x++)
  82.  
  83. {
  84.  
  85. for (int y = 0; y < h; y++)
  86.  
  87. {
  88.  
  89. if (board[g][x][y])
  90.  
  91. {
  92.  
  93. tft.fillRect(x * 8, y * 8, 8, 8, ST7735_BLUE);
  94.  
  95. }
  96.  
  97. }
  98.  
  99. }
  100.  
  101. }
  102.  
  103. int x1,y1;
  104.  
  105.  
  106. void loop()
  107. {
  108.  
  109. uint32_t buttons = ss.readButtons();
  110.  
  111.  
  112. if(! (buttons & TFTSHIELD_BUTTON_2)){
  113.  
  114.  
  115.  
  116.  
  117. char arr[2] = {0};
  118.  
  119. seedRandomGeneration(arr);
  120.  
  121. x1=arr[0];
  122. y1=arr[1];
  123. Serial.println(x1);
  124. Serial.println(y1);
  125.  
  126. displayBoard();
  127.  
  128. }
  129. if(! (buttons & TFTSHIELD_BUTTON_DOWN)){
  130.  
  131.  
  132. board[g][x1][y1] = 0;
  133. y1=y1-1;
  134. board[g][x1][y1] = 1;
  135. displayBoard();
  136.  
  137.  
  138. }
  139.  
  140.  
  141. if(! (buttons & TFTSHIELD_BUTTON_LEFT)){
  142. board[g][x1][y1] = 0;
  143. x1=x1+1;
  144. board[g][x1][y1] = 1;
  145. displayBoard();
  146.  
  147. }
  148.  
  149. if(! (buttons & TFTSHIELD_BUTTON_UP)){
  150. board[g][x1][y1] = 0;
  151. y1=y1+1;
  152. board[g][x1][y1] = 1;
  153. displayBoard();
  154. }
  155.  
  156. if(! (buttons & TFTSHIELD_BUTTON_RIGHT)){
  157. board[g][x1][y1] = 0;
  158. x1=x1-1;
  159. board[g][x1][y1] = 1;
  160. displayBoard();
  161.  
  162. }
  163.  
  164.  
  165.  
  166.  
  167. if(! (buttons & TFTSHIELD_BUTTON_1)){
  168.  
  169.  
  170. displayBoard();
  171.  
  172. updateGeneration();
  173.  
  174. g = !g;
  175.  
  176. }
  177.  
  178. delay(100);
  179.  
  180. }
  181.  
  182. void updateGeneration()
  183.  
  184. {
  185.  
  186. for (int x = 0; x < w; x++)
  187.  
  188. {
  189.  
  190. for (int y = 0; y < h; y++)
  191.  
  192. {
  193.  
  194. board[!g][x][y] = updateCell(x, y);
  195.  
  196. }
  197.  
  198. }
  199.  
  200. }
  201.  
  202. byte updateCell(int x, int y)
  203.  
  204. {
  205.  
  206. int n = numNeighbors(x, y);
  207.  
  208. if (board[g][x][y] == 1)
  209.  
  210. {
  211.  
  212. if (n < 2) return 0;
  213.  
  214. else if (n == 2 || n == 3) return 1;
  215.  
  216. else if (n > 3) return 0;
  217.  
  218. }
  219.  
  220. else
  221.  
  222. {
  223.  
  224. return (n == 3);
  225.  
  226. }
  227.  
  228. }
  229.  
  230. int numNeighbors(int x, int y)
  231.  
  232. {
  233.  
  234. int n = 0;
  235.  
  236. for (int dx = -1; dx <= 1; dx++)
  237.  
  238. {
  239.  
  240. for (int dy = -1; dy <= 1; dy++)
  241.  
  242. {
  243.  
  244. int x1 = x + dx;
  245.  
  246. int y1 = y + dy;
  247.  
  248. boolean middle = (dx == 0 && dy == 0);
  249.  
  250. if (!middle && x1 > 0 && x1 < w && y1 > 0 && y1 < h)
  251.  
  252. {
  253.  
  254. n += board[g][x1][y1];
  255.  
  256. }
  257.  
  258. }
  259.  
  260. }
  261.  
  262. Serial.print(n);
  263.  
  264. return n;
  265.  
  266. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement