loop.cpp 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. #include <chrono>
  2. #include "simple/motion/movement.hpp"
  3. #include "simple/motion/melody.hpp"
  4. #include "simple/motion/symphony.hpp"
  5. #include "simple/motion/algorithm.hpp"
  6. using namespace std::literals;
  7. // using duration = std::chrono::duration<float>;
  8. using duration = std::chrono::steady_clock::duration;
  9. using simple::support::way;
  10. // TODO: use rational, can't rely on float
  11. using movement = simple::motion::movement<duration,float,float>;
  12. void BasicLoop()
  13. {
  14. auto justamo = movement{1250ms, 0,1};
  15. float x = 0.0f;
  16. assert( 0 == loop(x, justamo, 500ms) );
  17. assert( x == way(0.f,1.f, 0.5f / 1.25f) );
  18. assert( 0 == loop(x, justamo, 500ms) );
  19. assert( x == way(0.f,1.f, 1.0f / 1.25f) );
  20. assert( 1 == loop(x, justamo, 500ms) );
  21. assert( x == way(0.f,1.f, 0.25f / 1.25f) );
  22. }
  23. void LoopDontSkip()
  24. {
  25. {
  26. auto sequence = simple::motion::melody(
  27. movement{10ms, 0,1},
  28. movement{10ms, 0,1},
  29. movement{10ms, 0,1},
  30. movement{10ms, 0,1}
  31. );
  32. std::array<float,4> values{};
  33. assert( 1 == loop(values, sequence, 60ms) ); // TODO: do 55ms with rational
  34. assert(( values == std::array{1.f, 1.f, 1.f, 1.f} ));
  35. assert( 2 == loop(values, sequence, 90ms) );
  36. assert(( values == std::array{1.f, 1.f, 1.f, 1.f} ));
  37. }
  38. {
  39. auto sequence = simple::motion::symphony(std::array{
  40. movement{10ms, 0,1},
  41. movement{10ms, 0,1},
  42. movement{10ms, 0,1},
  43. movement{10ms, 0,1}
  44. });
  45. std::array<float,4> values{};
  46. assert( 1 == loop(values, sequence, 60ms) ); // TODO: do 55ms with rational
  47. assert(( values == std::array{1.f, 1.f, 1.f, 1.f} ));
  48. assert( 2 == loop(values, sequence, 90ms) );
  49. assert(( values == std::array{1.f, 1.f, 1.f, 1.f} ));
  50. }
  51. }
  52. int main()
  53. {
  54. BasicLoop();
  55. LoopDontSkip();
  56. return 0;
  57. }