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.

186 lines
4.5KB

  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. Stellaris
  7. Works with Energia 0013 IDE
  8. */
  9. #ifdef __AVR__
  10. #define sinf sin
  11. #endif
  12. #define NDOTS 512 // Number of dots
  13. #define SCALE 4096
  14. #define INCREMENT 512
  15. #define PI2 6.283185307179586476925286766559
  16. #define RED_COLORS (32)
  17. #define GREEN_COLORS (64)
  18. #define BLUE_COLORS (32)
  19. #include <SPI.h>
  20. #include <RA8875.h>
  21. #define RA8875_RESET 9//any pin or nothing!
  22. #if defined(NEEDS_SET_MODULE)//Energia, this case is for stellaris/tiva
  23. RA8875 tft = RA8875(3);//select SPI module 3
  24. /*
  25. for module 3 (stellaris)
  26. SCLK: PD_0
  27. MOSI: PD_3
  28. MISO: PD_2
  29. SS: PD_1
  30. */
  31. #endif
  32. int16_t sine[SCALE+(SCALE/4)];
  33. int16_t *cosi = &sine[SCALE/4];
  34. int16_t angleX = 0, angleY = 0, angleZ = 0;
  35. int16_t speedX = 0, speedY = 0, speedZ = 0;
  36. int16_t xyz[3][NDOTS];
  37. uint16_t col[NDOTS];
  38. int pass = 0;
  39. void initialize (void){
  40. uint16_t i;
  41. /* if you change the SCALE*1.25 back to SCALE, the program will
  42. * occassionally overrun the cosi array -- however this actually
  43. * produces some interesting effects as the BUBBLES LOSE CONTROL!!!!
  44. */
  45. for (i = 0; i < SCALE+(SCALE/4); i++)
  46. //sine[i] = (-SCALE/2) + (int)(sinf(PI2 * i / SCALE) * sinf(PI2 * i / SCALE) * SCALE);
  47. sine[i] = (int)(sinf(PI2 * i / SCALE) * SCALE);
  48. }
  49. void setup()
  50. {
  51. Serial.begin(9600);
  52. //while (!Serial) {;}
  53. Serial.println("RA8875 start");
  54. tft.begin(RA8875_480x272);
  55. initialize();
  56. }
  57. void matrix (int16_t xyz[3][NDOTS], uint16_t col[NDOTS]){
  58. static uint32_t t = 0;
  59. int16_t x = -SCALE, y = -SCALE;
  60. uint16_t i, s, d;
  61. uint8_t red,grn,blu;
  62. for (i = 0; i < NDOTS; i++)
  63. {
  64. xyz[0][i] = x;
  65. xyz[1][i] = y;
  66. d = sqrt(x * x + y * y); /* originally a fastsqrt() call */
  67. s = sine[(t * 30) % SCALE] + SCALE;
  68. xyz[2][i] = sine[(d + s) % SCALE] * sine[(t * 10) % SCALE] / SCALE / 2;
  69. red = (cosi[xyz[2][i] + SCALE / 2] + SCALE) * (RED_COLORS - 1) / SCALE / 2;
  70. grn = (cosi[(xyz[2][i] + SCALE / 2 + 2 * SCALE / 3) % SCALE] + SCALE) * (GREEN_COLORS - 1) / SCALE / 2;
  71. blu = (cosi[(xyz[2][i] + SCALE / 2 + SCALE / 3) % SCALE] + SCALE) * (BLUE_COLORS - 1) / SCALE / 2;
  72. col[i] = ((red << 11) + (grn << 5) + blu);
  73. x += INCREMENT;
  74. if (x >= SCALE) x = -SCALE, y += INCREMENT;
  75. }
  76. t++;
  77. }
  78. void rotate (int16_t xyz[3][NDOTS], uint16_t angleX, uint16_t angleY, uint16_t angleZ){
  79. uint16_t i;
  80. int16_t tmpX, tmpY;
  81. int16_t sinx = sine[angleX], cosx = cosi[angleX];
  82. int16_t siny = sine[angleY], cosy = cosi[angleY];
  83. int16_t sinz = sine[angleZ], cosz = cosi[angleZ];
  84. for (i = 0; i < NDOTS; i++)
  85. {
  86. tmpX = (xyz[0][i] * cosx - xyz[2][i] * sinx) / SCALE;
  87. xyz[2][i] = (xyz[0][i] * sinx + xyz[2][i] * cosx) / SCALE;
  88. xyz[0][i] = tmpX;
  89. tmpY = (xyz[1][i] * cosy - xyz[2][i] * siny) / SCALE;
  90. xyz[2][i] = (xyz[1][i] * siny + xyz[2][i] * cosy) / SCALE;
  91. xyz[1][i] = tmpY;
  92. tmpX = (xyz[0][i] * cosz - xyz[1][i] * sinz) / SCALE;
  93. xyz[1][i] = (xyz[0][i] * sinz + xyz[1][i] * cosz) / SCALE;
  94. xyz[0][i] = tmpX;
  95. }
  96. }
  97. void draw(int16_t xyz[3][NDOTS], uint16_t col[NDOTS]){
  98. static uint16_t oldProjX[NDOTS] = { 0 };
  99. static uint16_t oldProjY[NDOTS] = { 0 };
  100. static uint8_t oldDotSize[NDOTS] = { 0 };
  101. uint16_t i, projX, projY, projZ, dotSize;
  102. for (i = 0; i < NDOTS; i++)
  103. {
  104. projZ = SCALE - (xyz[2][i] + SCALE) / 4;
  105. projX = tft.width() / 2 + (xyz[0][i] * projZ / SCALE) / 25;
  106. projY = tft.height() / 2 + (xyz[1][i] * projZ / SCALE) / 25;
  107. dotSize = 3 - (xyz[2][i] + SCALE) * 2 / SCALE;
  108. tft.drawCircle (oldProjX[i], oldProjY[i], oldDotSize[i], RA8875_BLACK);
  109. if (projX > dotSize && projY > dotSize && projX < tft.width() - dotSize && projY < tft.height() - dotSize)
  110. {
  111. tft.drawCircle (projX, projY, dotSize, col[i]);
  112. oldProjX[i] = projX;
  113. oldProjY[i] = projY;
  114. oldDotSize[i] = dotSize;
  115. }
  116. }
  117. }
  118. void loop()
  119. {
  120. matrix(xyz, col);
  121. rotate(xyz, angleX, angleY, angleZ);
  122. draw(xyz, col);
  123. angleX += speedX;
  124. angleY += speedY;
  125. angleZ += speedZ;
  126. if (pass > 400) speedY = 1;
  127. if (pass > 800) speedX = 1;
  128. if (pass > 1200) speedZ = 1;
  129. pass++;
  130. if (angleX >= SCALE) {
  131. angleX -= SCALE;
  132. }
  133. else if (angleX < 0) {
  134. angleX += SCALE;
  135. }
  136. if (angleY >= SCALE) {
  137. angleY -= SCALE;
  138. }
  139. else if (angleY < 0) {
  140. angleY += SCALE;
  141. }
  142. if (angleZ >= SCALE) {
  143. angleZ -= SCALE;
  144. }
  145. else if (angleZ < 0) {
  146. angleZ += SCALE;
  147. }
  148. }