Skip to content

Commit beb68a0

Browse files
committed
KARadioProtocol
1 parent 9cb80b7 commit beb68a0

File tree

1 file changed

+139
-73
lines changed

1 file changed

+139
-73
lines changed

src/AudioTools/AudioLibs/KARadioProtocol.h

Lines changed: 139 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -19,19 +19,130 @@ namespace audio_tools {
1919

2020
class KARadioProtocol {
2121
public:
22-
/// Default constructor
23-
KARadioProtocol(AudioPlayer& player) { setPlayer(player); }
24-
2522
/// Empty constructor: call setPlayer to define the player
26-
KARadioProtocol() = default;
23+
KARadioProtocol() {
24+
addCommand("play", [](AudioPlayer& player, Str& cmd, Str& par, Print& out,
25+
KARadioProtocol* self) {
26+
if (!par.isEmpty()) {
27+
int idx = par.toInt();
28+
player.setIndex(idx);
29+
}
30+
return true;
31+
});
32+
addCommand("instant", [](AudioPlayer& player, Str& cmd, Str& par,
33+
Print& out, KARadioProtocol* self) {
34+
player.setPath(par.c_str());
35+
return true;
36+
});
37+
addCommand("volume", [](AudioPlayer& player, Str& cmd, Str& par, Print& out,
38+
KARadioProtocol* self) {
39+
if (!par.isEmpty()) {
40+
int volume = par.toInt();
41+
player.setVolume(static_cast<float>(volume) / 254.0f);
42+
}
43+
return true;
44+
});
45+
addCommand("volume+", [](AudioPlayer& player, Str& cmd, Str& par,
46+
Print& out, KARadioProtocol* self) {
47+
int volume = player.volume() * 254.0f;
48+
volume += 5;
49+
if (volume > 245) {
50+
volume = 254;
51+
}
52+
player.setVolume(static_cast<float>(volume) / 254.0f);
53+
return true;
54+
});
55+
addCommand("volume-", [](AudioPlayer& player, Str& cmd, Str& par,
56+
Print& out, KARadioProtocol* self) {
57+
int volume = player.volume() * 254.0f;
58+
volume -= 5;
59+
if (volume < 0) {
60+
volume = 0;
61+
}
62+
player.setVolume(static_cast<float>(volume) / 254.0f);
63+
return true;
64+
});
65+
addCommand("pause", [](AudioPlayer& player, Str& cmd, Str& par, Print& out,
66+
KARadioProtocol* self) {
67+
player.setActive(false);
68+
return true;
69+
});
70+
addCommand("resume", [](AudioPlayer& player, Str& cmd, Str& par, Print& out,
71+
KARadioProtocol* self) {
72+
player.setActive(true);
73+
return true;
74+
});
75+
addCommand("stop", [](AudioPlayer& player, Str& cmd, Str& par, Print& out,
76+
KARadioProtocol* self) {
77+
player.setActive(false);
78+
return true;
79+
});
80+
addCommand("start", [](AudioPlayer& player, Str& cmd, Str& par, Print& out,
81+
KARadioProtocol* self) {
82+
player.setActive(true);
83+
return true;
84+
});
85+
addCommand("next", [](AudioPlayer& player, Str& cmd, Str& par, Print& out,
86+
KARadioProtocol* self) {
87+
player.next();
88+
return true;
89+
});
90+
addCommand("prev", [](AudioPlayer& player, Str& cmd, Str& par, Print& out,
91+
KARadioProtocol* self) {
92+
player.previous();
93+
return true;
94+
});
95+
addCommand("mute", [](AudioPlayer& player, Str& cmd, Str& par, Print& out,
96+
KARadioProtocol* self) {
97+
if (!par.isEmpty()) {
98+
player.setActive(!(par.toInt() == 1));
99+
}
100+
return true;
101+
});
102+
addCommand("infos", [](AudioPlayer& player, Str& cmd, Str& par,
103+
Print& result, KARadioProtocol* self) {
104+
result.print("vol: ");
105+
result.println(self->volume);
106+
result.print("num: ");
107+
result.println(self->index());
108+
result.print("stn: "); // station
109+
result.println(self->stationName());
110+
result.print("tit: "); // title
111+
result.println(self->title());
112+
result.print("sts: "); // status
113+
result.println(player.isActive());
114+
return true;
115+
});
116+
addCommand("version", [](AudioPlayer& player, Str& cmd, Str& par,
117+
Print& result, KARadioProtocol* self) {
118+
result.print("version: ");
119+
result.println(KA_VERSION);
120+
return true;
121+
});
122+
addCommand("list",
123+
[](AudioPlayer& player, Str& cmd, Str& par, Print& result,
124+
KARadioProtocol* self) { // arg: 0 to 254
125+
if (!par.isEmpty()) {
126+
player.setIndex(par.toInt());
127+
}
128+
result.println(self->stationName());
129+
return true;
130+
});
131+
}
132+
133+
/// Default constructor
134+
KARadioProtocol(AudioPlayer& player) : KARadioProtocol() {
135+
setPlayer(player);
136+
}
27137

28138
/// Defines the player
29139
void setPlayer(AudioPlayer& player) {
30140
p_player = &player;
31141
volume = player.volume() * 254.0f;
32142
}
33143

34-
/// processes the commands and returns the result output via the Print object
144+
/// processes the commands and returns the result output via the Print
145+
/// object
35146
bool processCommand(const char* input, Print& result) {
36147
if (p_player == nullptr) {
37148
LOGE("player not set");
@@ -72,76 +183,16 @@ class KARadioProtocol {
72183
/// Processes a single command
73184
bool processCommand(Str& name, Str& arg, Print& result) {
74185
LOGI("command: %s (%s)", name.c_str(), arg.c_str());
75-
if (p_player == nullptr) {
76-
LOGE("No player set");
77-
return false;
78-
}
79-
if (name == "play") {
80-
if (!arg.isEmpty()) {
81-
int idx = arg.toInt();
82-
p_player->setIndex(idx);
83-
}
84-
} else if (name == "instant") {
85-
p_player->setPath(arg.c_str());
86-
} else if (name == "volume") {
87-
if (!arg.isEmpty()) {
88-
volume = arg.toInt();
89-
p_player->setVolume(static_cast<float>(volume) / 254.0f);
90-
}
91-
} else if (name == "volume+") {
92-
volume += 5;
93-
if (volume > 245) {
94-
volume = 254;
95-
}
96-
p_player->setVolume(static_cast<float>(volume) / 254.0f);
97-
} else if (name == "volume-") {
98-
volume -= 5;
99-
if (volume < 0) {
100-
volume = 0;
101-
}
102-
p_player->setVolume(static_cast<float>(volume) / 254.0f);
103-
} else if (name == "pause") {
104-
p_player->setActive(false);
105-
} else if (name == "resume") {
106-
p_player->setActive(true);
107-
} else if (name == "stop") {
108-
p_player->setActive(false);
109-
} else if (name == "start") {
110-
p_player->setActive(true);
111-
} else if (name == "next") {
112-
p_player->next();
113-
} else if (name == "prev") {
114-
p_player->previous();
115-
} else if (name == "version") {
116-
result.print("version: ");
117-
result.println(KA_VERSION);
118-
} else if (name == "mute") {
119-
if (!arg.isEmpty()) {
120-
p_player->setActive(!(arg.toInt() == 1));
121-
}
122-
} else if (name == "infos") {
123-
result.print("vol: ");
124-
result.println(volume);
125-
result.print("num: ");
126-
result.println(index());
127-
result.print("stn: "); // station
128-
result.println(stationName());
129-
result.print("tit: "); // title
130-
result.println(title());
131-
result.print("sts: "); // status
132-
result.println(p_player->isActive());
133-
} else if (name == "list") {
134-
// arg: 0 to 254
135-
if (!arg.isEmpty()) {
136-
p_player->setIndex(arg.toInt());
186+
assert(p_player != nullptr);
187+
188+
for (Action& act : actions) {
189+
if (name.equals(act.cmd)) {
190+
return act.callback(*p_player, name, arg, result, this);
137191
}
138-
result.println(stationName());
139-
} else {
140-
LOGE("Invalid command:", name.c_str());
141-
return false;
142192
}
143-
result.flush();
144-
return true;
193+
194+
LOGE("Invalid command:", name.c_str());
195+
return false;
145196
}
146197

147198
/// Provides the actual index
@@ -150,10 +201,25 @@ class KARadioProtocol {
150201
/// Provides the actual title
151202
const char* title() { return title_str.c_str(); }
152203

204+
/// Add a new command
205+
void addCommand(const char* cmd,
206+
bool (*cb)(AudioPlayer& player, Str& cmd, Str& par,
207+
Print& out, KARadioProtocol* self)) {
208+
Action act;
209+
act.cmd = cmd;
210+
act.callback = cb;
211+
}
212+
153213
protected:
154214
AudioPlayer* p_player = nullptr;
155215
int volume = 0;
156216
Str title_str = "n/a";
217+
struct Action {
218+
const char* cmd;
219+
bool (*callback)(AudioPlayer& player, Str& cmd, Str& par, Print& out,
220+
KARadioProtocol* self) = nullptr;
221+
};
222+
Vector<Action> actions;
157223

158224
int getEndPos(StrView& line, int start) {
159225
int endPos = line.indexOf('&', start);

0 commit comments

Comments
 (0)