Advertisement
Guest User

Untitled

a guest
Jun 25th, 2019
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.25 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. int ster=0;
  105.  
  106. void loop()
  107. {
  108. if (ster>0)
  109. {
  110.  
  111. displayBoard();
  112.  
  113. updateGeneration();
  114.  
  115. g = !g;
  116.  
  117.  
  118. }
  119.  
  120.  
  121. uint32_t buttons = ss.readButtons();
  122.  
  123.  
  124. if(! (buttons & TFTSHIELD_BUTTON_IN)){
  125.  
  126.  
  127.  
  128.  
  129. char arr[2] = {0};
  130.  
  131. seedRandomGeneration(arr);
  132.  
  133. x1=arr[0];
  134. y1=arr[1];
  135. displayBoard();
  136. delay(100);
  137.  
  138. }
  139. if(! (buttons & TFTSHIELD_BUTTON_DOWN)){
  140.  
  141. if (board[g][x1][y1-1] == 1)
  142. {
  143.  
  144. }
  145. else
  146. {
  147.  
  148. board[g][x1][y1] = 0;
  149. y1=y1-1;
  150. board[g][x1][y1] = 1;
  151. displayBoard();
  152. }
  153.  
  154. }
  155.  
  156.  
  157. if(! (buttons & TFTSHIELD_BUTTON_LEFT)){
  158. board[g][x1][y1] = 0;
  159. x1=x1+1;
  160. board[g][x1][y1] = 1;
  161. displayBoard();
  162.  
  163. }
  164.  
  165. if(! (buttons & TFTSHIELD_BUTTON_UP)){
  166. board[g][x1][y1] = 0;
  167. y1=y1+1;
  168. board[g][x1][y1] = 1;
  169. displayBoard();
  170. }
  171.  
  172. if(! (buttons & TFTSHIELD_BUTTON_RIGHT)){
  173. board[g][x1][y1] = 0;
  174. x1=x1-1;
  175. board[g][x1][y1] = 1;
  176. displayBoard();
  177.  
  178. }
  179.  
  180.  
  181.  
  182.  
  183. if(! (buttons & TFTSHIELD_BUTTON_1)){
  184.  
  185.  
  186. ster=2;
  187. }
  188.  
  189.  
  190. if(! (buttons & TFTSHIELD_BUTTON_2)){
  191.  
  192.  
  193. ster=0;
  194. }
  195. if(! (buttons & TFTSHIELD_BUTTON_3)){
  196.  
  197.  
  198.  
  199. displayBoard();
  200.  
  201. updateGeneration();
  202.  
  203. g = !g;
  204.  
  205. }
  206.  
  207.  
  208. }
  209.  
  210. void updateGeneration()
  211.  
  212. {
  213.  
  214. for (int x = 0; x < w; x++)
  215.  
  216. {
  217.  
  218. for (int y = 0; y < h; y++)
  219.  
  220. {
  221.  
  222. board[!g][x][y] = updateCell(x, y);
  223.  
  224. }
  225.  
  226. }
  227.  
  228. }
  229.  
  230. byte updateCell(int x, int y)
  231.  
  232. {
  233.  
  234. int n = numNeighbors(x, y);
  235.  
  236. if (board[g][x][y] == 1)
  237.  
  238. {
  239.  
  240. if (n < 2) return 0;
  241.  
  242. else if (n == 2 || n == 3) return 1;
  243.  
  244. else if (n > 3) return 0;
  245.  
  246. }
  247.  
  248. else
  249.  
  250. {
  251.  
  252. return (n == 3);
  253.  
  254. }
  255.  
  256. }
  257.  
  258. int numNeighbors(int x, int y)
  259.  
  260. {
  261.  
  262. int n = 0;
  263.  
  264. for (int dx = -1; dx <= 1; dx++)
  265.  
  266. {
  267.  
  268. for (int dy = -1; dy <= 1; dy++)
  269.  
  270. {
  271.  
  272. int x1 = x + dx;
  273.  
  274. int y1 = y + dy;
  275.  
  276. boolean middle = (dx == 0 && dy == 0);
  277.  
  278. if (!middle && x1 > 0 && x1 < w && y1 > 0 && y1 < h)
  279.  
  280. {
  281.  
  282. n += board[g][x1][y1];
  283.  
  284. }
  285.  
  286. }
  287.  
  288. }
  289.  
  290. Serial.print(n);
  291.  
  292. return n;
  293.  
  294. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement