-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRGBPlayer.cpp
More file actions
118 lines (86 loc) · 2.78 KB
/
RGBPlayer.cpp
File metadata and controls
118 lines (86 loc) · 2.78 KB
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
/*
* RGBPlayer.cpp
*
* Copyright (c) 2024 Yang D.L.
*
* License: GPL-2.0-or-later
*/
#include "RGBPlayer.h"
// #include "log.h"
RGBPlayer::RGBPlayer(RGBLEDControllerBase *ledController, size_t start, const RGBActionSeries *actions, std::initializer_list<std::initializer_list<size_t> > orders)
: ledController(ledController), start(start), actions(actions), state(RGBPlayerState::STOP), actionOrderSelect(0), actionOrderIndex(0) {
this->orders.reserve(orders.size());
for (auto &order : orders) {
this->orders.push_back(std::vector<size_t>(order));
}
unsigned int maxn = 0;
for (size_t i = 0; i < actions->size(); ++i) {
maxn = std::max(maxn, actions->at(i)->argsCount());
}
action_args = new int[maxn];
stop();
}
RGBPlayer::~RGBPlayer() {
delete[] action_args;
}
void RGBPlayer::stop() {
state = RGBPlayerState::STOP;
for (size_t i = 0; i < actions->size(); ++i) {
actions->at(i)->act(nullptr, action_args);
}
// actionOrderSelect = 0;
actionOrderIndex = 0;
ledController->disable();
}
void RGBPlayer::pause() {
state = RGBPlayerState::PAUSE;
}
void RGBPlayer::resume() {
state = RGBPlayerState::PLAY;
ledController->enable();
}
int RGBPlayer::play() {
if (orders[actionOrderSelect].empty()) {
return -1;
}
if (state != RGBPlayerState::PLAY) {
return -2;
}
int index = 0;
int select = 0;
select = orders[actionOrderSelect][actionOrderIndex];
RGBActionBase *action = actions->at(select);
index = action->act(ledController->getLEDs() + start, action_args);
// log_i("Action %d: %d by order %d", select, index, actionOrderIndex);
if (index < 0) {
action->act(nullptr, action_args);
actionOrderIndex = (actionOrderIndex + 1) % orders[actionOrderSelect].size();
select = orders[actionOrderSelect][actionOrderIndex];
action = actions->at(select);
action->act(nullptr, action_args);
index = action->act(ledController->getLEDs() + start, action_args);
// log_i("Action %d: %d by order %d", select, index, actionOrderIndex);
}
return index;
}
void RGBPlayer::show() {
ledController->show();
}
void RGBPlayer::next() {
actions->at(orders[actionOrderSelect][actionOrderIndex])->act(nullptr, action_args);
actionOrderSelect = (actionOrderSelect + 1) % orders.size();
actionOrderIndex = 0;
actions->at(orders[actionOrderSelect][actionOrderIndex])->act(nullptr, action_args);
}
void RGBPlayer::prev() {
actions->at(orders[actionOrderSelect][actionOrderIndex])->act(nullptr, action_args);
actionOrderSelect = (actionOrderSelect + orders.size() - 1) % orders.size();
actionOrderIndex = 0;
actions->at(orders[actionOrderSelect][actionOrderIndex])->act(nullptr, action_args);
}
void RGBPlayer::setBrightness(uint8_t brightness) {
ledController->setBrightness(brightness);
}
uint8_t RGBPlayer::getBrightness() const {
return ledController->getBrightness();
}