PlatformIO package of the Teensy core framework compatible with GCC 10 & C++20
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

bubbles.ino 4.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. /*
  2. This example was adapted from ugfx http://ugfx.org
  3. It's a great example of many 2d objects in a 3d space (matrix transformations)
  4. and show the capabilities of RA8875 chip.
  5. Tested and worked with:
  6. Teensy3,Teensy3.1,Arduino UNO,Arduino YUN,Arduino Leonardo,Stellaris
  7. Works with Arduino 1.0.6 IDE, Arduino 1.5.8 IDE, Energia 0013 IDE
  8. */
  9. #ifdef __AVR__
  10. #define sinf sin
  11. #endif
  12. #define BLACK 0x0000
  13. #define BLUE 0x001F
  14. #define RED 0xF800
  15. #define GREEN 0x07E0
  16. #define CYAN 0x07FF
  17. #define MAGENTA 0xF81F
  18. #define YELLOW 0xFFE0
  19. #define WHITE 0xFFFF
  20. #define NDOTS 512 // Number of dots 512
  21. #define SCALE 4096//4096
  22. #define INCREMENT 512//512
  23. #define PI2 6.283185307179586476925286766559
  24. #define RED_COLORS (32)
  25. #define GREEN_COLORS (64)
  26. #define BLUE_COLORS (32)
  27. #include <SPI.h>
  28. #include <Adafruit_GFX.h>
  29. #include <TFT_ILI9163C.h>
  30. /*
  31. Teensy3.x and Arduino's
  32. You are using 4 wire SPI here, so:
  33. MOSI: 11//Teensy3.x/Arduino UNO (for MEGA/DUE refere to arduino site)
  34. MISO: 12//Teensy3.x/Arduino UNO (for MEGA/DUE refere to arduino site)
  35. SCK: 13//Teensy3.x/Arduino UNO (for MEGA/DUE refere to arduino site)
  36. the rest of pin below:
  37. */
  38. TFT_ILI9163C tft = TFT_ILI9163C(10, 9);
  39. int16_t sine[SCALE+(SCALE/4)];
  40. int16_t *cosi = &sine[SCALE/4];
  41. int16_t angleX = 0, angleY = 0, angleZ = 0;
  42. int16_t speedX = 0, speedY = 0, speedZ = 0;
  43. int16_t xyz[3][NDOTS];
  44. uint16_t col[NDOTS];
  45. int pass = 0;
  46. void initialize (void){
  47. uint16_t i;
  48. /* if you change the SCALE*1.25 back to SCALE, the program will
  49. * occassionally overrun the cosi array -- however this actually
  50. * produces some interesting effects as the BUBBLES LOSE CONTROL!!!!
  51. */
  52. for (i = 0; i < SCALE+(SCALE/4); i++)
  53. //sine[i] = (-SCALE/2) + (int)(sinf(PI2 * i / SCALE) * sinf(PI2 * i / SCALE) * SCALE);
  54. sine[i] = (int)(sinf(PI2 * i / SCALE) * SCALE);
  55. }
  56. void setup()
  57. {
  58. tft.begin();
  59. initialize();
  60. }
  61. void matrix (int16_t xyz[3][NDOTS], uint16_t col[NDOTS]){
  62. static uint32_t t = 0;
  63. int16_t x = -SCALE, y = -SCALE;
  64. uint16_t i, s, d;
  65. uint8_t red,grn,blu;
  66. for (i = 0; i < NDOTS; i++)
  67. {
  68. xyz[0][i] = x;
  69. xyz[1][i] = y;
  70. d = sqrt(x * x + y * y); /* originally a fastsqrt() call */
  71. s = sine[(t * 30) % SCALE] + SCALE;
  72. xyz[2][i] = sine[(d + s) % SCALE] * sine[(t * 10) % SCALE] / SCALE / 2;
  73. red = (cosi[xyz[2][i] + SCALE / 2] + SCALE) * (RED_COLORS - 1) / SCALE / 2;
  74. grn = (cosi[(xyz[2][i] + SCALE / 2 + 2 * SCALE / 3) % SCALE] + SCALE) * (GREEN_COLORS - 1) / SCALE / 2;
  75. blu = (cosi[(xyz[2][i] + SCALE / 2 + SCALE / 3) % SCALE] + SCALE) * (BLUE_COLORS - 1) / SCALE / 2;
  76. col[i] = ((red << 11) + (grn << 5) + blu);
  77. x += INCREMENT;
  78. if (x >= SCALE) x = -SCALE, y += INCREMENT;
  79. }
  80. t++;
  81. }
  82. void rotate (int16_t xyz[3][NDOTS], uint16_t angleX, uint16_t angleY, uint16_t angleZ){
  83. uint16_t i;
  84. int16_t tmpX, tmpY;
  85. int16_t sinx = sine[angleX], cosx = cosi[angleX];
  86. int16_t siny = sine[angleY], cosy = cosi[angleY];
  87. int16_t sinz = sine[angleZ], cosz = cosi[angleZ];
  88. for (i = 0; i < NDOTS; i++)
  89. {
  90. tmpX = (xyz[0][i] * cosx - xyz[2][i] * sinx) / SCALE;
  91. xyz[2][i] = (xyz[0][i] * sinx + xyz[2][i] * cosx) / SCALE;
  92. xyz[0][i] = tmpX;
  93. tmpY = (xyz[1][i] * cosy - xyz[2][i] * siny) / SCALE;
  94. xyz[2][i] = (xyz[1][i] * siny + xyz[2][i] * cosy) / SCALE;
  95. xyz[1][i] = tmpY;
  96. tmpX = (xyz[0][i] * cosz - xyz[1][i] * sinz) / SCALE;
  97. xyz[1][i] = (xyz[0][i] * sinz + xyz[1][i] * cosz) / SCALE;
  98. xyz[0][i] = tmpX;
  99. }
  100. }
  101. void draw(int16_t xyz[3][NDOTS], uint16_t col[NDOTS]){
  102. static uint16_t oldProjX[NDOTS] = { 0 };
  103. static uint16_t oldProjY[NDOTS] = { 0 };
  104. static uint8_t oldDotSize[NDOTS] = { 0 };
  105. uint16_t i, projX, projY, projZ, dotSize;
  106. for (i = 0; i < NDOTS; i++)
  107. {
  108. projZ = SCALE - (xyz[2][i] + SCALE) / 4;
  109. projX = tft.width() / 2 + (xyz[0][i] * projZ / SCALE) / 25;
  110. projY = tft.height() / 2 + (xyz[1][i] * projZ / SCALE) / 25;
  111. dotSize = 3 - (xyz[2][i] + SCALE) * 2 / SCALE;
  112. tft.drawCircle (oldProjX[i], oldProjY[i], oldDotSize[i], BLACK);
  113. if (projX > dotSize && projY > dotSize && projX < tft.width() - dotSize && projY < tft.height() - dotSize)
  114. {
  115. tft.drawCircle (projX, projY, dotSize, col[i]);
  116. oldProjX[i] = projX;
  117. oldProjY[i] = projY;
  118. oldDotSize[i] = dotSize;
  119. }
  120. }
  121. }
  122. void loop()
  123. {
  124. matrix(xyz, col);
  125. rotate(xyz, angleX, angleY, angleZ);
  126. draw(xyz, col);
  127. angleX += speedX;
  128. angleY += speedY;
  129. angleZ += speedZ;
  130. if (pass > 400) speedY = 1;
  131. if (pass > 800) speedX = 1;
  132. if (pass > 1200) speedZ = 1;
  133. pass++;
  134. if (angleX >= SCALE) {
  135. angleX -= SCALE;
  136. }
  137. else if (angleX < 0) {
  138. angleX += SCALE;
  139. }
  140. if (angleY >= SCALE) {
  141. angleY -= SCALE;
  142. }
  143. else if (angleY < 0) {
  144. angleY += SCALE;
  145. }
  146. if (angleZ >= SCALE) {
  147. angleZ -= SCALE;
  148. }
  149. else if (angleZ < 0) {
  150. angleZ += SCALE;
  151. }
  152. }