Lumitronix_Iflex_Pro_Workshop
Library to interact with the iFlexPro
LumitronixIFlexAnimator.h
Go to the documentation of this file.
1 /*-------------------------------------------------------------------------
2 LumitronixIFlexAnimator provides animation timing support.
3 
4 Written by Michael C. Miller.
5 
6 I invest time and resources providing this open source code,
7 please support me by dontating (see https://github.com/Makuna)
8 
9 -------------------------------------------------------------------------
10 This file is part of the LUMITRONIX_iFlex_Workshop library.
11 
12 LumitronixIFlexBus is free software: you can redistribute it and/or modify
13 it under the terms of the GNU Lesser General Public License as
14 published by the Free Software Foundation, either version 3 of
15 the License, or (at your option) any later version.
16 
17 LumitronixIFlexBus is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 GNU Lesser General Public License for more details.
21 
22 You should have received a copy of the GNU Lesser General Public
23 License along with LumitronixIFlex. If not, see
24 <http://www.gnu.org/licenses/>.
25 -------------------------------------------------------------------------*/
26 
27 #pragma once
28 
29 #include <Arduino.h>
31 
33 {
37 };
38 
40 {
41  float progress;
42  uint16_t index;
44 };
45 
46 #if defined(NEOPIXEBUS_NO_STL)
47 
48 typedef void(*AnimUpdateCallback)(const AnimationParam& param);
49 
50 #else
51 
52 #undef max
53 #undef min
54 #include <functional>
55 typedef std::function<void(const AnimationParam& param)> AnimUpdateCallback;
56 
57 #endif
58 
59 
60 #define NEO_MILLISECONDS 1 // ~65 seconds max duration, ms updates
61 #define NEO_CENTISECONDS 10 // ~10.9 minutes max duration, centisecond updates
62 #define NEO_DECISECONDS 100 // ~1.8 hours max duration, decisecond updates
63 #define NEO_SECONDS 1000 // ~18.2 hours max duration, second updates
64 #define NEO_DECASECONDS 10000 // ~7.5 days, 10 second updates
65 
67 {
68 public:
69  LumitronixIFlexAnimator(uint16_t countAnimations, uint16_t timeScale = NEO_MILLISECONDS);
71 
72  bool IsAnimating() const
73  {
74  return _activeAnimations > 0;
75  }
76 
77 
78  bool NextAvailableAnimation(uint16_t* indexAvailable, uint16_t indexStart = 0);
79 
80  void StartAnimation(uint16_t indexAnimation, uint16_t duration, AnimUpdateCallback animUpdate);
81  void StopAnimation(uint16_t indexAnimation);
82  void StopAll();
83 
84  void RestartAnimation(uint16_t indexAnimation)
85  {
86  if (indexAnimation >= _countAnimations || _animations[indexAnimation]._duration == 0)
87  {
88  return;
89  }
90 
91  StartAnimation(indexAnimation, _animations[indexAnimation]._duration, (_animations[indexAnimation]._fnCallback));
92  }
93 
94  bool IsAnimationActive(uint16_t indexAnimation) const
95  {
96  if (indexAnimation >= _countAnimations)
97  {
98  return false;
99  }
100  return (IsAnimating() && _animations[indexAnimation]._remaining != 0);
101  }
102 
103  uint16_t AnimationDuration(uint16_t indexAnimation)
104  {
105  if (indexAnimation >= _countAnimations)
106  {
107  return 0;
108  }
109  return _animations[indexAnimation]._duration;
110  }
111 
112  void ChangeAnimationDuration(uint16_t indexAnimation, uint16_t newDuration);
113 
114  void UpdateAnimations();
115 
116  bool IsPaused()
117  {
118  return (!_isRunning);
119  }
120 
121  void Pause()
122  {
123  _isRunning = false;
124  }
125 
126  void Resume()
127  {
128  _isRunning = true;
129  _animationLastTick = millis();
130  }
131 
132  uint16_t getTimeScale()
133  {
134  return _timeScale;
135  }
136 
137  void setTimeScale(uint16_t timeScale)
138  {
139  _timeScale = (timeScale < 1) ? (1) : (timeScale > 32768) ? 32768 : timeScale;
140  }
141 
142 private:
143  struct AnimationContext
144  {
145  AnimationContext() :
146  _duration(0),
147  _remaining(0),
148  _fnCallback(NULL)
149  {}
150 
151  void StartAnimation(uint16_t duration, AnimUpdateCallback animUpdate)
152  {
153  _duration = duration;
154  _remaining = duration;
155  _fnCallback = animUpdate;
156  }
157 
158  void StopAnimation()
159  {
160  _remaining = 0;
161  }
162 
163  float CurrentProgress()
164  {
165  return (float)(_duration - _remaining) / (float)_duration;
166  }
167 
168  uint16_t _duration;
169  uint16_t _remaining;
170 
171  AnimUpdateCallback _fnCallback;
172  };
173 
174  uint16_t _countAnimations;
175  AnimationContext* _animations;
176  uint32_t _animationLastTick;
177  uint16_t _activeAnimations;
178  uint16_t _timeScale;
179  bool _isRunning;
180 };
std::function< void(const AnimationParam &param)> AnimUpdateCallback
Definition: LumitronixIFlexAnimator.h:55
AnimationState
Definition: LumitronixIFlexAnimator.h:33
@ AnimationState_Progress
Definition: LumitronixIFlexAnimator.h:35
@ AnimationState_Completed
Definition: LumitronixIFlexAnimator.h:36
@ AnimationState_Started
Definition: LumitronixIFlexAnimator.h:34
#define NEO_MILLISECONDS
Definition: LumitronixIFlexAnimator.h:60
Definition: LumitronixIFlexAnimator.h:67
void Resume()
Definition: LumitronixIFlexAnimator.h:126
uint16_t getTimeScale()
Definition: LumitronixIFlexAnimator.h:132
void StartAnimation(uint16_t indexAnimation, uint16_t duration, AnimUpdateCallback animUpdate)
Definition: LumitronixIFlexAnimator.cpp:71
void ChangeAnimationDuration(uint16_t indexAnimation, uint16_t newDuration)
Definition: LumitronixIFlexAnimator.cpp:170
void Pause()
Definition: LumitronixIFlexAnimator.h:121
~LumitronixIFlexAnimator()
Definition: LumitronixIFlexAnimator.cpp:41
bool IsAnimationActive(uint16_t indexAnimation) const
Definition: LumitronixIFlexAnimator.h:94
void UpdateAnimations()
Definition: LumitronixIFlexAnimator.cpp:123
void RestartAnimation(uint16_t indexAnimation)
Definition: LumitronixIFlexAnimator.h:84
bool IsPaused()
Definition: LumitronixIFlexAnimator.h:116
void StopAll()
Definition: LumitronixIFlexAnimator.cpp:113
bool IsAnimating() const
Definition: LumitronixIFlexAnimator.h:72
void StopAnimation(uint16_t indexAnimation)
Definition: LumitronixIFlexAnimator.cpp:99
bool NextAvailableAnimation(uint16_t *indexAvailable, uint16_t indexStart=0)
Definition: LumitronixIFlexAnimator.cpp:46
uint16_t AnimationDuration(uint16_t indexAnimation)
Definition: LumitronixIFlexAnimator.h:103
void setTimeScale(uint16_t timeScale)
Definition: LumitronixIFlexAnimator.h:137
LumitronixIFlexAnimator(uint16_t countAnimations, uint16_t timeScale=NEO_MILLISECONDS)
Definition: LumitronixIFlexAnimator.cpp:31
Definition: LumitronixIFlexAnimator.h:40
AnimationState state
Definition: LumitronixIFlexAnimator.h:43
float progress
Definition: LumitronixIFlexAnimator.h:41
uint16_t index
Definition: LumitronixIFlexAnimator.h:42