-
Notifications
You must be signed in to change notification settings - Fork 7
/
looper.cpp
84 lines (63 loc) · 1.67 KB
/
looper.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#include "looper.h"
namespace pixl {
Looper* Looper::looper_ = 0;
void Looper::addAnimation(Animation* animation) {
Log.Info("Adding animation\n");
animations_[num_anim_++] = animation;
}
void Looper::addInput(Input* input) {
Log.Info("Adding input\n");
inputs_[num_input_++] = input;
}
void Looper::addVisualization(Visualization* visualization) {
Log.Info("Adding visualization\n");
visualizations_[num_viz_++] = visualization;
}
void Looper::clearAll() {
Log.Info("Clearing animations, inputs, and visualizations\n");
num_anim_ = 0;
num_input_ = 0;
num_viz_ = 0;
}
void Looper::clearVisualizations() {
Log.Info("Clearing visualizations\n");
num_viz_ = 0;
}
void Looper::loop() {
Log.Debug("Looping..\n");
unsigned long current_time = millis();
if (current_time > next_update_tick_) {
next_update_tick_ = current_time + update_millis_per_tick_;
update_();
}
if (current_time > next_draw_tick_) {
next_draw_tick_ = current_time + draw_millis_per_tick_;
draw_(draw_millis_per_tick_);
}
FastLED.show();
}
void Looper::setUpdatesPerSecond(int updates) {
update_millis_per_tick_ = 1000.0 / updates;
}
void Looper::setFramesPerSecond(int frames) {
draw_millis_per_tick_ = 1000.0 / frames;
}
void Looper::update_() {
Log.Debug("Updating..\n");
for (int i = 0; i < num_anim_; i++) {
animations_[i]->update();
}
for (int i = 0; i < num_input_; i++) {
inputs_[i]->update();
}
for (int i = 0; i < num_viz_; i++) {
visualizations_[i]->update();
}
}
void Looper::draw_(float interp) {
Log.Debug("Drawing..\n");
for (int i = 0; i < num_anim_; i++) {
animations_[i]->draw(interp);
}
}
} // end namespace pixl