Skip to content

Commit c6ae15b

Browse files
authored
Merge pull request #58 from hideakitai/feature/send-bundle
Feature/send bundle
2 parents a369f78 + ce5bcb3 commit c6ae15b

File tree

8 files changed

+148
-22
lines changed

8 files changed

+148
-22
lines changed

ArduinoOSC/ArduinoOSCCommon.h

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -96,19 +96,33 @@ namespace osc {
9696
OscClientManager<S>::getInstance().send(ip, port, addr, std::forward<Ts>(ts)...);
9797
#endif
9898
}
99+
100+
#ifndef ARDUINOOSC_DISABLE_BUNDLE
101+
102+
void begin_bundle(const TimeTag &tt) {
103+
OscClientManager<S>::getInstance().begin_bundle(tt);
104+
}
99105
template <typename... Ts>
100-
void send(const String& ip, const uint16_t port, const TimeTag& tt, const String& addr, Ts&&... ts) {
106+
void add_bundle(const String &addr, Ts&&... ts) {
107+
OscClientManager<S>::getInstance().add_bundle(addr, std::forward<Ts>(ts)...);
108+
}
109+
void end_bundle() {
110+
OscClientManager<S>::getInstance().end_bundle();
111+
}
112+
void send_bundle(const String& ip, const uint16_t port) {
101113
#if defined(ARDUINOOSC_ENABLE_WIFI) && (defined(ESP_PLATFORM) || defined(ARDUINO_ARCH_RP2040))
102114
if (this->isWiFiConnected() || this->isWiFiModeAP()) {
103-
OscClientManager<S>::getInstance().send(ip, port, tt, addr, std::forward<Ts>(ts)...);
115+
OscClientManager<S>::getInstance().send_bundle(ip, port);
104116
} else {
105117
LOG_ERROR(F("WiFi is not connected. Please connected to WiFi"));
106118
}
107119
#else
108-
OscClientManager<S>::getInstance().send(ip, port, tt, addr, std::forward<Ts>(ts)...);
120+
OscClientManager<S>::getInstance().send_bundle(ip, port);
109121
#endif
110122
}
111123

124+
#endif // ARDUINOOSC_DISABLE_BUNDLE
125+
112126
void post() {
113127
#if defined(ARDUINOOSC_ENABLE_WIFI) && (defined(ESP_PLATFORM) || defined(ARDUINO_ARCH_RP2040))
114128
if (this->isWiFiConnected() || this->isWiFiModeAP()) {

ArduinoOSC/OSCClient.h

Lines changed: 44 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -172,24 +172,50 @@ namespace osc {
172172
msg.init(addr);
173173
send(ip, port, msg, std::forward<Rest>(rest)...);
174174
}
175-
template <typename... Rest>
176-
void send(const String& ip, const uint16_t port, const TimeTag& tt, const String& addr, Rest&&... rest) {
177-
msg.init(addr, tt);
178-
send(ip, port, msg, std::forward<Rest>(rest)...);
179-
}
180175
template <typename First, typename... Rest>
181176
void send(const String& ip, const uint16_t port, Message& m, First&& first, Rest&&... rest) {
182177
m.push(first);
183178
send(ip, port, m, std::forward<Rest>(rest)...);
184179
}
185180
void send(const String& ip, const uint16_t port, Message& m) {
186-
auto stream = UdpMapManager<S>::getInstance().getUdp(local_port);
187181
this->writer.init().encode(m);
182+
this->send(ip, port);
183+
}
184+
void send(const String &ip, const uint16_t port)
185+
{
186+
auto stream = UdpMapManager<S>::getInstance().getUdp(local_port);
188187
stream->beginPacket(ip.c_str(), port);
189188
stream->write(this->writer.data(), this->writer.size());
190189
stream->endPacket();
191190
}
192191

192+
#ifndef ARDUINOOSC_DISABLE_BUNDLE
193+
194+
void begin_bundle(const TimeTag &tt) {
195+
this->writer.init().begin_bundle(tt);
196+
}
197+
template <typename... Rest>
198+
void add_bundle(const String& addr, Rest&&... rest) {
199+
this->msg.init(addr);
200+
this->add_bundle(this->msg, std::forward<Rest>(rest)...);
201+
}
202+
template <typename First, typename... Rest>
203+
void add_bundle(Message& m, First&& first, Rest&&... rest)
204+
{
205+
m.push(first);
206+
add_bundle(m, std::forward<Rest>(rest)...);
207+
}
208+
void add_bundle(Message& m)
209+
{
210+
this->writer.encode(m);
211+
}
212+
void end_bundle()
213+
{
214+
this->writer.end_bundle();
215+
}
216+
217+
#endif // ARDUINOOSC_DISABLE_BUNDLE
218+
193219
void send(const Destination& dest, ElementRef elem) {
194220
elem->init(msg, dest.addr);
195221
elem->encodeTo(msg);
@@ -227,9 +253,19 @@ namespace osc {
227253
void send(const String& ip, const uint16_t port, const String& addr, Ts&&... ts) {
228254
client.send(ip, port, addr, std::forward<Ts>(ts)...);
229255
}
256+
257+
void begin_bundle(const TimeTag &tt) {
258+
client.begin_bundle(tt);
259+
}
230260
template <typename... Ts>
231-
void send(const String& ip, const uint16_t port, const TimeTag& tt, const String& addr, Ts&&... ts) {
232-
client.send(ip, port, tt, addr, std::forward<Ts>(ts)...);
261+
void add_bundle(const String& addr, Ts&&... ts) {
262+
client.add_bundle(addr, std::forward<Ts>(ts)...);
263+
}
264+
void end_bundle() {
265+
client.end_bundle();
266+
}
267+
void send_bundle(const String& ip, const uint16_t port) {
268+
client.send(ip, port);
233269
}
234270

235271
void post() {

ArduinoOSC/OSCServer.h

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -213,21 +213,22 @@ namespace osc {
213213
stream->read(data, size);
214214

215215
decoder.init(data, size);
216-
if (Message* msg = decoder.decode()) {
216+
while (Message* msg = decoder.decode()) {
217217
if (msg->available()) {
218218
msg->remoteIP(stream->S::remoteIP());
219219
msg->remotePort((uint16_t)stream->S::remotePort());
220-
for (auto& c : this->callbacks)
221-
if (msg->match(c.first))
220+
for (auto& c : this->callbacks) {
221+
if (msg->match(c.first)) {
222222
c.second->decodeFrom(*msg);
223-
223+
}
224+
}
224225
msg_ptr = msg;
225-
return true;
226+
} else {
227+
LOG_ERROR(F("osc message parsing failed"));
228+
msg_ptr = nullptr;
226229
}
227230
}
228-
LOG_ERROR(F("osc message parsing failed"));
229-
msg_ptr = nullptr;
230-
return false;
231+
return msg_ptr != nullptr;
231232
}
232233

233234
const OscMessage* message() const { return msg_ptr; }

ArduinoOSC/OscDecoder.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,9 @@ namespace osc {
7676
LOG_ERROR(F("bundle header was corrupted"));
7777
return false;
7878
}
79-
} else
79+
} else {
8080
messages.push_back(Message(beg, end - beg, time_tag));
81+
}
8182

8283
return true;
8384
}

ArduinoOSC/OscMessage.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ namespace osc {
1919
#endif
2020

2121
class Message {
22-
TimeTag time_tag;
22+
TimeTag time_tag; // Used only for the received msg in the bundle
2323
String address_str;
2424
String type_tags;
2525
Storage storage;

ArduinoOSC/OscTypes.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,7 @@ namespace osc {
165165
explicit TimeTag(const uint64_t w)
166166
: v(w) {}
167167
operator uint64_t() const { return v; }
168+
uint64_t value() const { return v; }
168169
static TimeTag immediate() { return TimeTag(1); }
169170
};
170171

README.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ If you have already installed this library, please follow:
3131
- s (`string`)
3232
- b (`bundle`)
3333
- support pattern-matching (wildcards)
34-
- does NOT support timestamp values.
3534

3635
## Usage
3736

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
#if !defined(ARDUINO_ARCH_ESP32)
2+
#error "This example is for ESP32 only"
3+
#endif
4+
5+
// #define ARDUINOOSC_DEBUGLOG_ENABLE
6+
7+
#include <ArduinoOSCWiFi.h>
8+
9+
// WiFi stuff
10+
const char* ssid = "your-ssid";
11+
const char* pwd = "your-password";
12+
const IPAddress ip(192, 168, 0, 201);
13+
const IPAddress gateway(192, 168, 0, 1);
14+
const IPAddress subnet(255, 255, 255, 0);
15+
16+
// for ArduinoOSC
17+
const char* host = "127.0.0.1";
18+
const int port = 54321;
19+
20+
void onBundleReceived(const OscMessage& m) {
21+
Serial.print(m.remoteIP());
22+
Serial.print(" ");
23+
Serial.print(m.remotePort());
24+
Serial.print(" ");
25+
Serial.print(m.size());
26+
Serial.print(" ");
27+
Serial.print(m.address());
28+
Serial.print(" ");
29+
Serial.print(m.timeTag().value());
30+
Serial.print(" ");
31+
Serial.print(m.arg<int>(0));
32+
Serial.println();
33+
}
34+
35+
void setup() {
36+
Serial.begin(115200);
37+
delay(2000);
38+
39+
// WiFi stuff (no timeout setting for WiFi)
40+
WiFi.disconnect(true, true); // disable wifi, erase ap info
41+
delay(1000);
42+
WiFi.mode(WIFI_STA);
43+
WiFi.begin(ssid, pwd);
44+
WiFi.config(ip, gateway, subnet);
45+
46+
while (WiFi.status() != WL_CONNECTED) {
47+
Serial.print(".");
48+
delay(500);
49+
}
50+
Serial.print("WiFi connected, IP = ");
51+
Serial.println(WiFi.localIP());
52+
53+
// subscribe osc messages
54+
OscWiFi.subscribe(port, "/bundle/foo", onBundleReceived);
55+
OscWiFi.subscribe(port, "/bundle/bar", onBundleReceived);
56+
}
57+
58+
void loop() {
59+
OscWiFi.update(); // should be called to receive + send osc
60+
61+
static int counter = 0;
62+
63+
const uint64_t now = esp_timer_get_time();
64+
const OscTimeTag tt(now);
65+
OscWiFi.begin_bundle(tt);
66+
OscWiFi.add_bundle("/bundle/foo", millis());
67+
OscWiFi.add_bundle("/bundle/bar", millis() / 1000);
68+
OscWiFi.end_bundle();
69+
OscWiFi.send_bundle(host, port);
70+
71+
counter++;
72+
73+
delay(500);
74+
}

0 commit comments

Comments
 (0)