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.

88 lines
2.6KB

  1. #ifndef TRANSITION_SQUARES_H__
  2. #define TRANSITION_SQUARES_H__
  3. #include <Arduino.h>
  4. #include "ILI9488_t3.h"
  5. #include "MathUtil.h"
  6. #include "BaseTransition.h"
  7. const float TRANSITION_SQUARES_SPEED = 0.06f;
  8. const float TRANSITION_SQUARES_SLOPE = 0.08f;
  9. const uint_fast8_t TRANSITION_SQ_SIZE = 30; // multiple of 2, please
  10. class TransitionSquares : public BaseTransition {
  11. public:
  12. TransitionSquares() : BaseTransition() {};
  13. void init( ILI9488_t3 tft );
  14. void restart( ILI9488_t3 tft, uint_fast16_t color );
  15. void perFrame( ILI9488_t3 tft, FrameParams frameParams );
  16. boolean isComplete();
  17. private:
  18. float _phase = 0;
  19. uint_fast16_t _color;
  20. boolean _isComplete = false;
  21. };
  22. void TransitionSquares::init( ILI9488_t3 tft ) {
  23. }
  24. void TransitionSquares::restart( ILI9488_t3 tft, uint_fast16_t inColor ) {
  25. //uint_fast16_t w = tft.width();
  26. //uint_fast16_t h = tft.height();
  27. _phase = 0;
  28. _color = inColor;
  29. _isComplete = false;
  30. }
  31. float easeInOutSine( float p ) {
  32. if( p < 0.5f ) return (1.0f - cos( p * M_PI )) * 0.5f;
  33. return 0.5f + ( sin( ( p - 0.5f ) * M_PI ) * 0.5f );
  34. }
  35. void TransitionSquares::perFrame( ILI9488_t3 tft, FrameParams frameParams ) {
  36. uint_fast16_t w = (uint_fast16_t)tft.width();
  37. uint_fast16_t h = (uint_fast16_t)tft.height();
  38. float prevPhase = _phase;
  39. _phase += frameParams.timeMult * TRANSITION_SQUARES_SPEED;
  40. boolean anySmallSquares = false;
  41. // Draw squares, offset so squares grow out from center
  42. uint_fast8_t across = w / TRANSITION_SQ_SIZE + 1;
  43. uint_fast8_t down = h / TRANSITION_SQ_SIZE + 1;
  44. for( uint_fast8_t i=0; i<across; i++ ) {
  45. for( uint_fast8_t j=0; j<down; j++ ) {
  46. uint_fast8_t prevSize = TRANSITION_SQ_SIZE * easeInOutSine( constrain( prevPhase - (i+j)*TRANSITION_SQUARES_SLOPE, 0.0f, 1.0f ) );
  47. prevSize = (prevSize>>1) << 1; // Round down to even number
  48. uint_fast8_t size = TRANSITION_SQ_SIZE * easeInOutSine( constrain( _phase - (i+j)*TRANSITION_SQUARES_SLOPE, 0.0f, 1.0f ) );
  49. size = (size>>1) << 1; // Round down to even number
  50. if( size < TRANSITION_SQ_SIZE ) anySmallSquares = true;
  51. // Squares should grow out from center.
  52. // Draw hollow squares, so we only redraw pixels that change.
  53. for( uint_fast8_t s=prevSize+2; s<=size; s+=2 ) {
  54. tft.drawRect( (i+0.5f)*TRANSITION_SQ_SIZE - (s>>1), (j+0.5f)*TRANSITION_SQ_SIZE - (s>>1), s, s, _color );
  55. }
  56. //tft.fillRect( (i+0.5f)*TRANSITION_SQ_SIZE - (size>>1), (j+0.5f)*TRANSITION_SQ_SIZE - (size>>1), size, size, _color );
  57. }
  58. }
  59. // When all squares are full-size, the transition is done!
  60. _isComplete = !anySmallSquares;
  61. }
  62. boolean TransitionSquares::isComplete() {
  63. return _isComplete;
  64. }
  65. #endif