Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/send bundle #58

Merged
merged 4 commits into from
Aug 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 17 additions & 3 deletions ArduinoOSC/ArduinoOSCCommon.h
Original file line number Diff line number Diff line change
Expand Up @@ -96,19 +96,33 @@ namespace osc {
OscClientManager<S>::getInstance().send(ip, port, addr, std::forward<Ts>(ts)...);
#endif
}

#ifndef ARDUINOOSC_DISABLE_BUNDLE

void begin_bundle(const TimeTag &tt) {
OscClientManager<S>::getInstance().begin_bundle(tt);
}
template <typename... Ts>
void send(const String& ip, const uint16_t port, const TimeTag& tt, const String& addr, Ts&&... ts) {
void add_bundle(const String &addr, Ts&&... ts) {
OscClientManager<S>::getInstance().add_bundle(addr, std::forward<Ts>(ts)...);
}
void end_bundle() {
OscClientManager<S>::getInstance().end_bundle();
}
void send_bundle(const String& ip, const uint16_t port) {
#if defined(ARDUINOOSC_ENABLE_WIFI) && (defined(ESP_PLATFORM) || defined(ARDUINO_ARCH_RP2040))
if (this->isWiFiConnected() || this->isWiFiModeAP()) {
OscClientManager<S>::getInstance().send(ip, port, tt, addr, std::forward<Ts>(ts)...);
OscClientManager<S>::getInstance().send_bundle(ip, port);
} else {
LOG_ERROR(F("WiFi is not connected. Please connected to WiFi"));
}
#else
OscClientManager<S>::getInstance().send(ip, port, tt, addr, std::forward<Ts>(ts)...);
OscClientManager<S>::getInstance().send_bundle(ip, port);
#endif
}

#endif // ARDUINOOSC_DISABLE_BUNDLE

void post() {
#if defined(ARDUINOOSC_ENABLE_WIFI) && (defined(ESP_PLATFORM) || defined(ARDUINO_ARCH_RP2040))
if (this->isWiFiConnected() || this->isWiFiModeAP()) {
Expand Down
52 changes: 44 additions & 8 deletions ArduinoOSC/OSCClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -172,24 +172,50 @@ namespace osc {
msg.init(addr);
send(ip, port, msg, std::forward<Rest>(rest)...);
}
template <typename... Rest>
void send(const String& ip, const uint16_t port, const TimeTag& tt, const String& addr, Rest&&... rest) {
msg.init(addr, tt);
send(ip, port, msg, std::forward<Rest>(rest)...);
}
template <typename First, typename... Rest>
void send(const String& ip, const uint16_t port, Message& m, First&& first, Rest&&... rest) {
m.push(first);
send(ip, port, m, std::forward<Rest>(rest)...);
}
void send(const String& ip, const uint16_t port, Message& m) {
auto stream = UdpMapManager<S>::getInstance().getUdp(local_port);
this->writer.init().encode(m);
this->send(ip, port);
}
void send(const String &ip, const uint16_t port)
{
auto stream = UdpMapManager<S>::getInstance().getUdp(local_port);
stream->beginPacket(ip.c_str(), port);
stream->write(this->writer.data(), this->writer.size());
stream->endPacket();
}

#ifndef ARDUINOOSC_DISABLE_BUNDLE

void begin_bundle(const TimeTag &tt) {
this->writer.init().begin_bundle(tt);
}
template <typename... Rest>
void add_bundle(const String& addr, Rest&&... rest) {
this->msg.init(addr);
this->add_bundle(this->msg, std::forward<Rest>(rest)...);
}
template <typename First, typename... Rest>
void add_bundle(Message& m, First&& first, Rest&&... rest)
{
m.push(first);
add_bundle(m, std::forward<Rest>(rest)...);
}
void add_bundle(Message& m)
{
this->writer.encode(m);
}
void end_bundle()
{
this->writer.end_bundle();
}

#endif // ARDUINOOSC_DISABLE_BUNDLE

void send(const Destination& dest, ElementRef elem) {
elem->init(msg, dest.addr);
elem->encodeTo(msg);
Expand Down Expand Up @@ -227,9 +253,19 @@ namespace osc {
void send(const String& ip, const uint16_t port, const String& addr, Ts&&... ts) {
client.send(ip, port, addr, std::forward<Ts>(ts)...);
}

void begin_bundle(const TimeTag &tt) {
client.begin_bundle(tt);
}
template <typename... Ts>
void send(const String& ip, const uint16_t port, const TimeTag& tt, const String& addr, Ts&&... ts) {
client.send(ip, port, tt, addr, std::forward<Ts>(ts)...);
void add_bundle(const String& addr, Ts&&... ts) {
client.add_bundle(addr, std::forward<Ts>(ts)...);
}
void end_bundle() {
client.end_bundle();
}
void send_bundle(const String& ip, const uint16_t port) {
client.send(ip, port);
}

void post() {
Expand Down
17 changes: 9 additions & 8 deletions ArduinoOSC/OSCServer.h
Original file line number Diff line number Diff line change
Expand Up @@ -213,21 +213,22 @@ namespace osc {
stream->read(data, size);

decoder.init(data, size);
if (Message* msg = decoder.decode()) {
while (Message* msg = decoder.decode()) {
if (msg->available()) {
msg->remoteIP(stream->S::remoteIP());
msg->remotePort((uint16_t)stream->S::remotePort());
for (auto& c : this->callbacks)
if (msg->match(c.first))
for (auto& c : this->callbacks) {
if (msg->match(c.first)) {
c.second->decodeFrom(*msg);

}
}
msg_ptr = msg;
return true;
} else {
LOG_ERROR(F("osc message parsing failed"));
msg_ptr = nullptr;
}
}
LOG_ERROR(F("osc message parsing failed"));
msg_ptr = nullptr;
return false;
return msg_ptr != nullptr;
}

const OscMessage* message() const { return msg_ptr; }
Expand Down
3 changes: 2 additions & 1 deletion ArduinoOSC/OscDecoder.h
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,9 @@ namespace osc {
LOG_ERROR(F("bundle header was corrupted"));
return false;
}
} else
} else {
messages.push_back(Message(beg, end - beg, time_tag));
}

return true;
}
Expand Down
2 changes: 1 addition & 1 deletion ArduinoOSC/OscMessage.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ namespace osc {
#endif

class Message {
TimeTag time_tag;
TimeTag time_tag; // Used only for the received msg in the bundle
String address_str;
String type_tags;
Storage storage;
Expand Down
1 change: 1 addition & 0 deletions ArduinoOSC/OscTypes.h
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@ namespace osc {
explicit TimeTag(const uint64_t w)
: v(w) {}
operator uint64_t() const { return v; }
uint64_t value() const { return v; }
static TimeTag immediate() { return TimeTag(1); }
};

Expand Down
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ If you have already installed this library, please follow:
- s (`string`)
- b (`bundle`)
- support pattern-matching (wildcards)
- does NOT support timestamp values.

## Usage

Expand Down
74 changes: 74 additions & 0 deletions examples/arduino/OscWiFiBundle/OscWiFiBundle.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
#if !defined(ARDUINO_ARCH_ESP32)
#error "This example is for ESP32 only"
#endif

// #define ARDUINOOSC_DEBUGLOG_ENABLE

#include <ArduinoOSCWiFi.h>

// WiFi stuff
const char* ssid = "your-ssid";
const char* pwd = "your-password";
const IPAddress ip(192, 168, 0, 201);
const IPAddress gateway(192, 168, 0, 1);
const IPAddress subnet(255, 255, 255, 0);

// for ArduinoOSC
const char* host = "127.0.0.1";
const int port = 54321;

void onBundleReceived(const OscMessage& m) {
Serial.print(m.remoteIP());
Serial.print(" ");
Serial.print(m.remotePort());
Serial.print(" ");
Serial.print(m.size());
Serial.print(" ");
Serial.print(m.address());
Serial.print(" ");
Serial.print(m.timeTag().value());
Serial.print(" ");
Serial.print(m.arg<int>(0));
Serial.println();
}

void setup() {
Serial.begin(115200);
delay(2000);

// WiFi stuff (no timeout setting for WiFi)
WiFi.disconnect(true, true); // disable wifi, erase ap info
delay(1000);
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, pwd);
WiFi.config(ip, gateway, subnet);

while (WiFi.status() != WL_CONNECTED) {
Serial.print(".");
delay(500);
}
Serial.print("WiFi connected, IP = ");
Serial.println(WiFi.localIP());

// subscribe osc messages
OscWiFi.subscribe(port, "/bundle/foo", onBundleReceived);
OscWiFi.subscribe(port, "/bundle/bar", onBundleReceived);
}

void loop() {
OscWiFi.update(); // should be called to receive + send osc

static int counter = 0;

const uint64_t now = esp_timer_get_time();
const OscTimeTag tt(now);
OscWiFi.begin_bundle(tt);
OscWiFi.add_bundle("/bundle/foo", millis());
OscWiFi.add_bundle("/bundle/bar", millis() / 1000);
OscWiFi.end_bundle();
OscWiFi.send_bundle(host, port);

counter++;

delay(500);
}
Loading