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.

140 lines
4.7KB

  1. /*
  2. This example create an hudge vertical bars gauge display with more than 230 bars!
  3. Perfect for FFT, etc.
  4. The sketch shows the power of layers!
  5. First it will create the full bars background in a layer, then it draws
  6. 2 rectangles for each bar (one black will cover the background, the other white
  7. will reveal the background) in the layer 2 by using AND function. The result its a really
  8. fast hudge vertical bars gauge that it's not possible to do with a tiny microcontroller
  9. and if you have a Teensy 3.1 you still have resources for a complete FFT and more!
  10. Created by Max MC Costa as a demo for RA8875 library
  11. For 800x480 see the 800x480 folder!
  12. */
  13. #include <SPI.h>
  14. #include <RA8875.h>
  15. #define RA8875_CS 10
  16. #define RA8875_RESET 9//any pin or 255 to disable it!
  17. const uint8_t _barWidth = 2; //width of single bar dot
  18. const uint8_t _spacer = 1; //space between bars
  19. const uint8_t _segHeight = 2; //height of every bar single dot
  20. const uint8_t _segments = 60; //how many dots per bar
  21. const unsigned long updateEvery = 40; //time between screen updates, fom 0 ...xx (in ms)
  22. RA8875 tft = RA8875(RA8875_CS, RA8875_RESET);
  23. uint8_t fftdata[400];//depend of the display w and _barWidth this can be reduced.
  24. // you can put maxbars value generated from createBars here but you need to run this
  25. //sketch at list once!
  26. volatile bool started = false;
  27. unsigned int maxbars = 0;
  28. unsigned long updateDelay = 0;
  29. void setup()
  30. {
  31. // Serial.begin(38400);
  32. // long unsigned debug_start = millis ();
  33. // while (!Serial && ((millis () - debug_start) <= 5000)) ;
  34. // Serial.println("RA8875 start");
  35. tft.begin(RA8875_480x272);
  36. }
  37. void loop()
  38. {
  39. if (!started) {
  40. tft.useLayers(true);//turn on layers
  41. tft.writeTo(L1);//write colored bars to layer 1
  42. maxbars = createBars(0, 20);
  43. started = true;
  44. tft.writeTo(L2);//from this point we write on layer 2
  45. tft.layerEffect(AND);//apply AND effect between layer 1 and 2
  46. }
  47. //fill bars data with some random stuff
  48. for (uint16_t i = 0; i <= maxbars; i++) {
  49. fftdata[i] = random(0, 256);
  50. }
  51. if ((millis() - updateDelay) > updateEvery){//screen refresh every xx ms
  52. updateBars(fftdata, 0, 20, maxbars,false);
  53. updateDelay = millis();
  54. }
  55. }
  56. boolean createBar(uint16_t origX, uint16_t origY) {
  57. uint8_t i;
  58. //the real bar height
  59. uint16_t barHeight = origY + (_segHeight * _segments) + (_segments * 2);
  60. if (barHeight > tft.height()) return false;//too much, exit
  61. //with vertical bars the Y origin starts from the bottom
  62. uint16_t newOriginY = tft.height() - barHeight;
  63. uint8_t rc, gc, bc; //container for colors
  64. bc = 1;//blue always 0
  65. for (i = 0; i < _segments; i++) {
  66. gc = map(i, 0, _segments - 1, 100, 255); //calculate green
  67. rc = map(i, _segments - 1, 0, 200, 255); //calculate red
  68. if (i > 0 && i < (_segments-1)) {
  69. tft.fillRect(origX, newOriginY + ((_segHeight * i) + (i * 2)), _barWidth, _segHeight, tft.Color565(rc, gc, bc)); //MIDDLE
  70. } else if (i == 0){
  71. tft.fillRect(origX, newOriginY + ((_segHeight * i) + (i * 2)), _barWidth, _segHeight, RA8875_YELLOW);//TOP
  72. } else {
  73. tft.fillRect(origX, newOriginY + ((_segHeight * i) + (i * 2)), _barWidth, _segHeight, RA8875_WHITE);//BOTTOM
  74. }
  75. }
  76. return true;
  77. }
  78. uint16_t createBars(uint16_t origX, uint8_t origY) {
  79. uint16_t i;
  80. uint16_t currentX;
  81. uint16_t resBar;
  82. uint16_t maxW = tft.width()-(_spacer+_barWidth);
  83. for (i = 0; i < maxW; i++) { //create the bars
  84. currentX = origX + (i * _barWidth) + (i * _spacer);
  85. if (currentX >= maxW){
  86. createBar(currentX, origY);
  87. return resBar;
  88. } else {
  89. createBar(currentX, origY);
  90. resBar++;
  91. }
  92. }
  93. return resBar;
  94. }
  95. void updateBar(uint8_t val, uint16_t origX, uint16_t origY,bool single) {
  96. uint16_t barHeight = origY + (_segHeight * _segments) + (_segments * 2);
  97. uint16_t newOriginY = tft.height() - barHeight;
  98. uint16_t currentBarH = map(val, 0, 256, _segHeight, (barHeight - origY));
  99. if (single) {
  100. tft.fillRect(origX, newOriginY, _barWidth, barHeight, 0x0000); //bar negative
  101. if (_barWidth < 2){
  102. tft.drawPixel(origX,currentBarH,0xFFFF);
  103. } else {
  104. tft.fillRect(origX, currentBarH, _barWidth, _segHeight, 0xFFFF); //bar positive
  105. }
  106. } else {
  107. if (val < 255) tft.fillRect(origX, newOriginY, _barWidth, (barHeight - origY) - currentBarH, 0x0000); //bar negative
  108. tft.fillRect(origX, (barHeight - newOriginY) - (currentBarH - _segHeight), _barWidth, currentBarH, 0xFFFF); //bar positive
  109. }
  110. }
  111. void updateBars(uint8_t vals[], uint16_t origX, uint16_t origY,uint16_t maxbars,bool single) {
  112. for (uint16_t i = 0; i <= maxbars; i++) { //create the bars
  113. updateBar(vals[i], origX + (i * _barWidth) + (i * _spacer), origY,single);
  114. }
  115. }