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.

Checkerboard.h 2.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. #ifndef CHECKERBOARD_H__
  2. #define CHECKERBOARD_H__
  3. #include <Arduino.h>
  4. #include "ILI9341_t3.h"
  5. #include "MathUtil.h"
  6. #include "BaseAnimation.h"
  7. const uint_fast8_t CHECKERBOARD_SIZE_MIN = 20;
  8. const uint_fast8_t CHECKERBOARD_SIZE_MAX = 60;
  9. const float CHECKERBOARD_ZOOM_SPEED = 0.027;
  10. const float CHECKERBOARD_STRAFE_DISTANCE = 5; // in squares
  11. const float CHECKERBOARD_STRAFE_SPEED_X = 0.015;
  12. const float CHECKERBOARD_STRAFE_SPEED_Y = 0.0095;
  13. class Checkerboard : public BaseAnimation {
  14. public:
  15. Checkerboard() : BaseAnimation() {};
  16. void init( ILI9341_t3 tft );
  17. uint_fast16_t bgColor( void );
  18. String title();
  19. void perFrame( ILI9341_t3 tft, FrameParams frameParams );
  20. private:
  21. float _phase = 0;
  22. uint_fast16_t _bgColor;
  23. };
  24. void Checkerboard::init( ILI9341_t3 tft ) {
  25. _bgColor = tft.color565( 0xff, 0xbb, 0xbb );
  26. }
  27. uint_fast16_t Checkerboard::bgColor(){
  28. return _bgColor;
  29. }
  30. String Checkerboard::title() {
  31. return "Checkerboard";
  32. }
  33. void Checkerboard::perFrame( ILI9341_t3 tft, FrameParams frameParams ) {
  34. int_fast16_t w = (int_fast16_t)tft.width();
  35. int_fast16_t h = (int_fast16_t)tft.height();
  36. _phase += frameParams.timeMult;
  37. float sizer = (cos(_phase*CHECKERBOARD_ZOOM_SPEED) + 1.0) * 0.5; // Range 0..1
  38. float size = lerp8( CHECKERBOARD_SIZE_MIN, CHECKERBOARD_SIZE_MAX, sizer );
  39. // Zoom in & out from center
  40. int_fast16_t strafeX = sin(_phase*CHECKERBOARD_STRAFE_SPEED_X) * CHECKERBOARD_STRAFE_DISTANCE * size;
  41. uint_fast8_t sqLeftOfCenter = ceil( (w/2 + strafeX) / (float)size );
  42. float startX = (w/2) - sqLeftOfCenter * size + strafeX;
  43. int_fast16_t strafeY = sin(_phase*CHECKERBOARD_STRAFE_SPEED_Y) * CHECKERBOARD_STRAFE_DISTANCE * size;
  44. uint_fast8_t sqAboveCenter = ceil( (h/2 + strafeY) / (float)size );
  45. float startY = (h/2) - sqAboveCenter * size + strafeY;
  46. // First square is light or 8dark?
  47. boolean startLight = (sqLeftOfCenter%2) ^ (sqAboveCenter%2);
  48. // Modulate colors based on audio input
  49. float ease = (1.0f-frameParams.audioMean);
  50. ease *= ease; // Stronger reactions, please
  51. uint_fast8_t bright = (1.0f-ease) * 0xff;
  52. uint_fast16_t audioColor = tft.color565( bright, bright>>1, 0 );
  53. int_fast8_t sqX = -sqLeftOfCenter;
  54. for( float x=startX; x<w; x+=size ) {
  55. boolean isLight = startLight;
  56. int16_t drawX = max( x, 0 );
  57. int16_t drawW = (x >= 0) ? size : (size+x);
  58. int_fast8_t sqY = -sqAboveCenter;
  59. for( float y=startY; y<h; y+=size ) {
  60. int16_t drawY = max( y, 0 );
  61. int16_t drawH = (y >= 0) ? size : (size+y);
  62. uint_fast16_t color = tft.color565( (sqX * sqY * 57), (isLight ? 0xff : 0), (isLight ? 0xff : 0) );
  63. color ^= audioColor;
  64. tft.fillRect( drawX, drawY, drawW, drawH, color );
  65. isLight = !isLight;
  66. sqY++;
  67. }
  68. startLight = !startLight;
  69. sqX++;
  70. }
  71. }
  72. #endif