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.

TransitionDither.h 2.1KB

3 years ago
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. #ifndef TRANSITION_DITHER_H__
  2. #define TRANSITION_DITHER_H__
  3. #include <Arduino.h>
  4. #include "ILI9488_t3.h"
  5. #include "MathUtil.h"
  6. #include "BaseTransition.h"
  7. const float TRANSITION_DITHER_SPEED = 0.015f;
  8. class TransitionDither : public BaseTransition {
  9. public:
  10. TransitionDither() : BaseTransition() {};
  11. void init( ILI9488_t3 tft );
  12. void restart( ILI9488_t3 tft, uint_fast16_t color );
  13. void perFrame( ILI9488_t3 tft, FrameParams frameParams );
  14. boolean isComplete();
  15. private:
  16. float _phase = 0;
  17. uint_fast16_t _color;
  18. uint_fast8_t _step;
  19. };
  20. void TransitionDither::init( ILI9488_t3 tft ) {
  21. }
  22. void TransitionDither::restart( ILI9488_t3 tft, uint_fast16_t inColor ) {
  23. //uint_fast16_t w = tft.width();
  24. //uint_fast16_t h = tft.height();
  25. _phase = 0;
  26. _color = inColor;
  27. _step = 0;
  28. }
  29. void TransitionDither::perFrame( ILI9488_t3 tft, FrameParams frameParams ) {
  30. uint_fast16_t w = (uint_fast16_t)tft.width();
  31. uint_fast16_t h = (uint_fast16_t)tft.height();
  32. _phase += frameParams.timeMult * TRANSITION_DITHER_SPEED;
  33. // Apply some easing. Linear speed feels dull
  34. float easeOutQuad = 1.0f - (1.0f-_phase)*(1.0f-_phase);
  35. // Calculate destination
  36. float dest_f = min( easeOutQuad, 1.0f ) * 0xff;
  37. uint_fast8_t dest = floor(dest_f);
  38. // Draw dither dots until _step reaches the destination
  39. while( _step <= dest ) {
  40. // Bayer matrix. See: https://en.wikipedia.org/wiki/Ordered_dithering
  41. // Recreate Bayer matrix pattern, basically a recursive sequence of 2D moves: [ 0, 3,
  42. // 2, 1 ]
  43. uint_fast16_t start_i = 0;
  44. uint_fast16_t start_j = 0;
  45. uint_fast8_t move = (1<<3);
  46. uint_fast8_t s = _step;
  47. while( s > 0 ) {
  48. if( s&0b01 ) start_i += move; // 1 & 3: move horizontal
  49. if( (s&0b01) ^ ((s&0b10)>>1) ) start_j += move; // 1 & 2: move vertical
  50. move >>= 1;
  51. s >>= 2;
  52. }
  53. for( uint_fast16_t i=start_i; i<w; i+=0x10 ) {
  54. for( uint_fast16_t j=start_j; j<h; j+=0x10 ) {
  55. tft.drawPixel( i, j, _color );
  56. }
  57. }
  58. _step++;
  59. }
  60. }
  61. boolean TransitionDither::isComplete() {
  62. return _phase >= 1.0f;
  63. }
  64. #endif