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.

vbarsGauge.ino 4.8KB

3 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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. // begin display: Choose from: RA8875_480x272, RA8875_800x480, RA8875_800x480ALT, Adafruit_480x272, Adafruit_800x480
  36. tft.begin(RA8875_480x272);
  37. }
  38. void loop()
  39. {
  40. if (!started) {
  41. tft.useLayers(true);//turn on layers
  42. tft.writeTo(L1);//write colored bars to layer 1
  43. maxbars = createBars(0, 20);
  44. started = true;
  45. tft.writeTo(L2);//from this point we write on layer 2
  46. tft.layerEffect(AND);//apply AND effect between layer 1 and 2
  47. }
  48. //fill bars data with some random stuff
  49. for (uint16_t i = 0; i <= maxbars; i++) {
  50. fftdata[i] = random(0, 256);
  51. }
  52. if ((millis() - updateDelay) > updateEvery){//screen refresh every xx ms
  53. updateBars(fftdata, 0, 20, maxbars,false);
  54. updateDelay = millis();
  55. }
  56. }
  57. boolean createBar(uint16_t origX, uint16_t origY) {
  58. uint8_t i;
  59. //the real bar height
  60. uint16_t barHeight = origY + (_segHeight * _segments) + (_segments * 2);
  61. if (barHeight > tft.height()) return false;//too much, exit
  62. //with vertical bars the Y origin starts from the bottom
  63. uint16_t newOriginY = tft.height() - barHeight;
  64. uint8_t rc, gc, bc; //container for colors
  65. bc = 1;//blue always 0
  66. for (i = 0; i < _segments; i++) {
  67. gc = map(i, 0, _segments - 1, 100, 255); //calculate green
  68. rc = map(i, _segments - 1, 0, 200, 255); //calculate red
  69. if (i > 0 && i < (_segments-1)) {
  70. tft.fillRect(origX, newOriginY + ((_segHeight * i) + (i * 2)), _barWidth, _segHeight, tft.Color565(rc, gc, bc)); //MIDDLE
  71. } else if (i == 0){
  72. tft.fillRect(origX, newOriginY + ((_segHeight * i) + (i * 2)), _barWidth, _segHeight, RA8875_YELLOW);//TOP
  73. } else {
  74. tft.fillRect(origX, newOriginY + ((_segHeight * i) + (i * 2)), _barWidth, _segHeight, RA8875_WHITE);//BOTTOM
  75. }
  76. }
  77. return true;
  78. }
  79. uint16_t createBars(uint16_t origX, uint8_t origY) {
  80. uint16_t i;
  81. uint16_t currentX;
  82. uint16_t resBar=0;
  83. uint16_t maxW = tft.width()-(_spacer+_barWidth);
  84. for (i = 0; i < maxW; i++) { //create the bars
  85. currentX = origX + (i * _barWidth) + (i * _spacer);
  86. if (currentX >= maxW){
  87. createBar(currentX, origY);
  88. return resBar;
  89. } else {
  90. createBar(currentX, origY);
  91. resBar++;
  92. }
  93. }
  94. return resBar;
  95. }
  96. void updateBar(uint8_t val, uint16_t origX, uint16_t origY,bool single) {
  97. uint16_t barHeight = origY + (_segHeight * _segments) + (_segments * 2);
  98. uint16_t newOriginY = tft.height() - barHeight;
  99. uint16_t currentBarH = map(val, 0, 256, _segHeight, (barHeight - origY));
  100. if (single) {
  101. tft.fillRect(origX, newOriginY, _barWidth, barHeight, 0x0000); //bar negative
  102. if (_barWidth < 2){
  103. tft.drawPixel(origX,currentBarH,0xFFFF);
  104. } else {
  105. tft.fillRect(origX, currentBarH, _barWidth, _segHeight, 0xFFFF); //bar positive
  106. }
  107. } else {
  108. if (val < 255) tft.fillRect(origX, newOriginY, _barWidth, (barHeight - origY) - currentBarH, 0x0000); //bar negative
  109. tft.fillRect(origX, (barHeight - newOriginY) - (currentBarH - _segHeight), _barWidth, currentBarH, 0xFFFF); //bar positive
  110. }
  111. }
  112. void updateBars(uint8_t vals[], uint16_t origX, uint16_t origY,uint16_t maxbars,bool single) {
  113. for (uint16_t i = 0; i <= maxbars; i++) { //create the bars
  114. updateBar(vals[i], origX + (i * _barWidth) + (i * _spacer), origY,single);
  115. }
  116. }