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.

116 lines
3.0KB

  1. #ifndef TRIANGLE_WEB_H__
  2. #define TRIANGLE_WEB_H__
  3. #include <Arduino.h>
  4. #include <math.h>
  5. #include "ILI9488_t3.h"
  6. #include "BaseAnimation.h"
  7. const uint_fast8_t WEB_POINT_SPACING = 64;
  8. const float WEB_POINT_RADIUS = 20.0f;
  9. class TriangleWeb : public BaseAnimation {
  10. public:
  11. TriangleWeb() : BaseAnimation() {};
  12. void init( ILI9488_t3 tft );
  13. uint_fast16_t bgColor( void );
  14. String title();
  15. void perFrame( ILI9488_t3 tft, FrameParams frameParams );
  16. private:
  17. Point getWebPoint( uint_fast8_t i, uint_fast8_t j, float phase );
  18. uint_fast8_t _ptsAcross = 0;
  19. uint_fast8_t _ptsDown = 0;
  20. float _phase = 0;
  21. uint_fast16_t _bgColor;
  22. };
  23. void TriangleWeb::init( ILI9488_t3 tft ) {
  24. uint_fast16_t w = tft.width();
  25. uint_fast16_t h = tft.height();
  26. //tft.fillRect( 0, 0, w, h, 0x0 );
  27. _ptsAcross = w / WEB_POINT_SPACING + 1;
  28. _ptsDown = h / WEB_POINT_SPACING + 1;
  29. _bgColor = tft.color565( 0x22, 0x22, 0x22 );
  30. }
  31. uint_fast16_t TriangleWeb::bgColor(){
  32. return _bgColor;
  33. }
  34. String TriangleWeb::title() {
  35. return "TriangleWeb";
  36. }
  37. Point TriangleWeb::getWebPoint( uint_fast8_t i, uint_fast8_t j, float phase ) {
  38. uint_fast8_t idx = j*_ptsAcross + i;
  39. uint_fast8_t rando = (idx ^ 37);
  40. float angle = rando * phase;
  41. return (Point){
  42. (uint_fast16_t)( (i*WEB_POINT_SPACING) + (cos(angle)*WEB_POINT_RADIUS) ),
  43. (uint_fast16_t)( (j*WEB_POINT_SPACING) + (sin(angle)*WEB_POINT_RADIUS) )
  44. };
  45. }
  46. void TriangleWeb::perFrame( ILI9488_t3 tft, FrameParams frameParams ) {
  47. //uint_fast16_t w = tft.width();
  48. //uint_fast16_t h = tft.height();
  49. // fillRect: flickers pretty bad
  50. //tft.fillRect( 0, 0, w, h, LV_RED );
  51. _phase += frameParams.timeMult * 0.0005 * (frameParams.audioMean*3.0f + 1.0f);
  52. // Prepare web color
  53. uint_fast8_t bright = (frameParams.audioPeak >> 1); // 0..512 -> 0..255
  54. // Weight brighter
  55. uint_fast16_t invBrightSquared = (0xff-bright)*(0xff-bright);
  56. bright = lerp8( bright, 0xff - (invBrightSquared>>8), 0.55f );
  57. uint_fast16_t color = tft.color565( bright, bright, bright );
  58. // When dark: Always illuminate one "lucky" line at random
  59. uint_fast16_t luckyLine = random( _ptsAcross * _ptsDown * 3 );
  60. uint_fast8_t luckyBright = random( 0x11, 0x55 );
  61. uint_fast16_t luckyColor = tft.color565( luckyBright, luckyBright, luckyBright );
  62. // It is possible to re-use some Point's here. TODO?
  63. Point nw, ne, sw;
  64. uint_fast16_t luckyIdx = 0;
  65. for( uint_fast8_t i=0; i<_ptsAcross; i++ ) {
  66. nw = getWebPoint( i, 0, _phase );
  67. for( uint_fast8_t j=0; j<_ptsDown; j++ ) {
  68. ne = getWebPoint( i+1, j, _phase );
  69. sw = getWebPoint( i, j+1, _phase );
  70. uint_fast16_t useColor;
  71. useColor = (luckyIdx==luckyLine) ? luckyColor : color;
  72. tft.drawLine( nw.x, nw.y, ne.x, ne.y, useColor );
  73. luckyIdx++;
  74. useColor = (luckyIdx==luckyLine) ? luckyColor : color;
  75. tft.drawLine( nw.x, nw.y, sw.x, sw.y, useColor );
  76. luckyIdx++;
  77. useColor = (luckyIdx==luckyLine) ? luckyColor : color;
  78. tft.drawLine( ne.x, ne.y, sw.x, sw.y, useColor );
  79. luckyIdx++;
  80. // Re-use sw point as new nw
  81. nw = sw;
  82. }
  83. }
  84. }
  85. #endif