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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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. */
  12. #include <SPI.h>
  13. #include <RA8875.h>
  14. #define RA8875_RESET 9//any pin or 255 to disable it!
  15. const uint8_t _barWidth = 2; //width of single bar dot
  16. const uint8_t _spacer = 1; //space between bars
  17. const uint8_t _segHeight = 2; //height of every bar single dot
  18. const uint8_t _segments = 60; //how many dots per bar
  19. const unsigned long updateEvery = 40; //time between screen updates, fom 0 ...xx (in ms)
  20. #if defined(NEEDS_SET_MODULE)//Energia, this case is for stellaris/tiva
  21. RA8875 tft = RA8875(3);//select SPI module 3
  22. /*
  23. for module 3 (stellaris)
  24. SCLK: PD_0
  25. MOSI: PD_3
  26. MISO: PD_2
  27. SS: PD_1
  28. */
  29. #endif
  30. uint8_t fftdata[400];//depend of the display w and _barWidth this can be reduced.
  31. // you can put maxbars value generated from createBars here but you need to run this
  32. //sketch at list once!
  33. volatile bool started = false;
  34. unsigned int maxbars = 0;
  35. unsigned long updateDelay = 0;
  36. void setup()
  37. {
  38. // Serial.begin(38400);
  39. // long unsigned debug_start = millis ();
  40. // while (!Serial && ((millis () - debug_start) <= 5000)) ;
  41. // Serial.println("RA8875 start");
  42. tft.begin(RA8875_480x272);
  43. }
  44. void loop()
  45. {
  46. if (!started) {
  47. tft.useLayers(true);//turn on layers
  48. tft.writeTo(L1);//write colored bars to layer 1
  49. maxbars = createBars(0, 20);
  50. started = true;
  51. tft.writeTo(L2);//from this point we write on layer 2
  52. tft.layerEffect(AND);//apply AND effect between layer 1 and 2
  53. }
  54. //fill bars data with some random stuff
  55. for (uint16_t i = 0; i <= maxbars; i++) {
  56. fftdata[i] = random(0, 256);
  57. }
  58. if ((millis() - updateDelay) > updateEvery){//screen refresh every xx ms
  59. updateBars(fftdata, 0, 20, maxbars,false);
  60. updateDelay = millis();
  61. }
  62. }
  63. boolean createBar(uint16_t origX, uint16_t origY) {
  64. uint8_t i;
  65. //the real bar height
  66. uint16_t barHeight = origY + (_segHeight * _segments) + (_segments * 2);
  67. if (barHeight > tft.height()) return false;//too much, exit
  68. //with vertical bars the Y origin starts from the bottom
  69. uint16_t newOriginY = tft.height() - barHeight;
  70. uint8_t rc, gc, bc; //container for colors
  71. bc = 1;//blue always 0
  72. for (i = 0; i < _segments; i++) {
  73. gc = map(i, 0, _segments - 1, 100, 255); //calculate green
  74. rc = map(i, _segments - 1, 0, 200, 255); //calculate red
  75. if (i > 0 && i < (_segments-1)) {
  76. tft.fillRect(origX, newOriginY + ((_segHeight * i) + (i * 2)), _barWidth, _segHeight, tft.Color565(rc, gc, bc)); //MIDDLE
  77. } else if (i == 0){
  78. tft.fillRect(origX, newOriginY + ((_segHeight * i) + (i * 2)), _barWidth, _segHeight, RA8875_YELLOW);//TOP
  79. } else {
  80. tft.fillRect(origX, newOriginY + ((_segHeight * i) + (i * 2)), _barWidth, _segHeight, RA8875_WHITE);//BOTTOM
  81. }
  82. }
  83. return true;
  84. }
  85. uint16_t createBars(uint16_t origX, uint8_t origY) {
  86. uint16_t i;
  87. uint16_t currentX;
  88. uint16_t resBar;
  89. uint16_t maxW = tft.width()-(_spacer+_barWidth);
  90. for (i = 0; i < maxW; i++) { //create the bars
  91. currentX = origX + (i * _barWidth) + (i * _spacer);
  92. if (currentX >= maxW){
  93. createBar(currentX, origY);
  94. return resBar;
  95. } else {
  96. createBar(currentX, origY);
  97. resBar++;
  98. }
  99. }
  100. return resBar;
  101. }
  102. void updateBar(uint8_t val, uint16_t origX, uint16_t origY,bool single) {
  103. uint16_t barHeight = origY + (_segHeight * _segments) + (_segments * 2);
  104. uint16_t newOriginY = tft.height() - barHeight;
  105. uint16_t currentBarH = map(val, 0, 256, _segHeight, (barHeight - origY));
  106. if (single) {
  107. tft.fillRect(origX, newOriginY, _barWidth, barHeight, 0x0000); //bar negative
  108. if (_barWidth < 2){
  109. tft.drawPixel(origX,currentBarH,0xFFFF);
  110. } else {
  111. tft.fillRect(origX, currentBarH, _barWidth, _segHeight, 0xFFFF); //bar positive
  112. }
  113. } else {
  114. if (val < 255) tft.fillRect(origX, newOriginY, _barWidth, (barHeight - origY) - currentBarH, 0x0000); //bar negative
  115. tft.fillRect(origX, (barHeight - newOriginY) - (currentBarH - _segHeight), _barWidth, currentBarH, 0xFFFF); //bar positive
  116. }
  117. }
  118. void updateBars(uint8_t vals[], uint16_t origX, uint16_t origY,uint16_t maxbars,bool single) {
  119. for (uint16_t i = 0; i <= maxbars; i++) { //create the bars
  120. updateBar(vals[i], origX + (i * _barWidth) + (i * _spacer), origY,single);
  121. }
  122. }