Skip to content

Commit 4a2c8c3

Browse files
authored
Merge pull request #66 from hideakitai/feature/support-unsubscribe
Feature/support unsubscribe
2 parents 0cf20fb + b8f3555 commit 4a2c8c3

File tree

5 files changed

+230
-23
lines changed

5 files changed

+230
-23
lines changed

ArduinoOSC/ArduinoOSCCommon.h

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,45 @@ namespace osc {
6666
#endif
6767
}
6868

69+
bool unsubscribe(const uint16_t port, const String& addr) {
70+
#if defined(ARDUINOOSC_ENABLE_WIFI) && (defined(ESP_PLATFORM) || defined(ARDUINO_ARCH_RP2040))
71+
if (WiFi.getMode() != WIFI_OFF)
72+
return OscServerManager<S>::getInstance().unsubscribe(port, addr);
73+
else {
74+
LOG_ERROR(F("WiFi is not enabled. Unsubscribing OSC failed."));
75+
return false;
76+
}
77+
#else
78+
return OscServerManager<S>::getInstance().unsubscribe(port, addr);
79+
#endif
80+
}
81+
82+
bool unsubscribe(const uint16_t port) {
83+
#if defined(ARDUINOOSC_ENABLE_WIFI) && (defined(ESP_PLATFORM) || defined(ARDUINO_ARCH_RP2040))
84+
if (WiFi.getMode() != WIFI_OFF)
85+
return OscServerManager<S>::getInstance().unsubscribe(port);
86+
else {
87+
LOG_ERROR(F("WiFi is not enabled. Unsubscribing OSC failed."));
88+
return false;
89+
}
90+
#else
91+
return OscServerManager<S>::getInstance().unsubscribe(port);
92+
#endif
93+
}
94+
95+
bool unsubscribe() {
96+
#if defined(ARDUINOOSC_ENABLE_WIFI) && (defined(ESP_PLATFORM) || defined(ARDUINO_ARCH_RP2040))
97+
if (WiFi.getMode() != WIFI_OFF)
98+
return OscServerManager<S>::getInstance().unsubscribeAll();
99+
else {
100+
LOG_ERROR(F("WiFi is not enabled. Unsubscribing OSC failed."));
101+
return false;
102+
}
103+
#else
104+
return OscServerManager<S>::getInstance().unsubscribeAll();
105+
#endif
106+
}
107+
69108
void parse() {
70109
#if defined(ARDUINOOSC_ENABLE_WIFI) && (defined(ESP_PLATFORM) || defined(ARDUINO_ARCH_RP2040))
71110
if (this->isWiFiConnected() || this->isWiFiModeAP()) {

ArduinoOSC/OSCServer.h

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,23 @@ namespace osc {
204204
callbacks.insert({addr, ref});
205205
}
206206

207+
bool unsubscribe(const String& addr) {
208+
auto it = callbacks.find(addr);
209+
if (it != callbacks.end()) {
210+
callbacks.erase(it);
211+
return true;
212+
}
213+
return false;
214+
}
215+
216+
bool unsubscribeAll() {
217+
if (!callbacks.empty()) {
218+
callbacks.clear();
219+
return true;
220+
}
221+
return false;
222+
}
223+
207224
bool parse() {
208225
auto stream = UdpMapManager<S>::getInstance().getUdp(port);
209226
const size_t size = stream->parsePacket();
@@ -263,6 +280,32 @@ namespace osc {
263280
getServer(port).subscribe(addr, std::forward<Ts>(ts)...);
264281
}
265282

283+
bool unsubscribe(const uint16_t port, const String& addr) {
284+
auto it = server_map.find(port);
285+
if (it != server_map.end()) {
286+
return it->second->unsubscribe(addr);
287+
}
288+
return false;
289+
}
290+
291+
bool unsubscribe(const uint16_t port) {
292+
auto it = server_map.find(port);
293+
if (it != server_map.end()) {
294+
return it->second->unsubscribeAll();
295+
}
296+
return false;
297+
}
298+
299+
bool unsubscribeAll() {
300+
if (!server_map.empty()) {
301+
for (auto& m : server_map) {
302+
m.second->unsubscribeAll();
303+
}
304+
return true;
305+
}
306+
return false;
307+
}
308+
266309
void parse() {
267310
for (auto& m : server_map)
268311
m.second->parse();

README.md

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,131 @@ You can see the debug log when you insert following line before include `Arduino
249249
#include <ArduinoOSC.h>
250250
```
251251

252+
## APIs
253+
254+
### Main Class (`OscWiFi` / `OscEther`)
255+
256+
#### Subscribing to OSC Messages
257+
258+
```cpp
259+
// Subscribe a value to an OSC message
260+
OscWiFi.subscribe(const uint16_t port, const String& addr, T& value);
261+
// Subscribe multiple values to an OSC message
262+
OscWiFi.subscribe(const uint16_t port, const String& addr, T1& v1, T2& v2, ...);
263+
// Subscribe a lambda to an OSC message with arguments
264+
OscWiFi.subscribe(const uint16_t port, const String& addr, [](T1 arg1, T2 arg2, ...) { ... });
265+
// Subscribe a lambda to an OSC message with OscMessage argument
266+
OscWiFi.subscribe(const uint16_t port, const String& addr, [](const OscMessage& msg) { ... });
267+
// Subscribe a function to an OSC message
268+
OscWiFi.subscribe(const uint16_t port, const String& addr, onOscReceived);
269+
```
270+
271+
#### Unsubscribing from OSC Messages
272+
273+
```cpp
274+
// Unsubscribe from a specific address on a port
275+
OscWiFi.unsubscribe(const uint16_t port, const String& addr);
276+
// Unsubscribe from all addresses on a port
277+
OscWiFi.unsubscribe(const uint16_t port);
278+
// Unsubscribe from all addresses on all ports
279+
OscWiFi.unsubscribe();
280+
```
281+
282+
#### Sending OSC Messages
283+
284+
```cpp
285+
// Send an OSC message with arguments
286+
OscWiFi.send(const String& ip, const uint16_t port, const String& addr, T1 arg1, T2 arg2, ...);
287+
```
288+
289+
#### Publishing OSC Messages
290+
291+
```cpp
292+
// Publish a value periodically
293+
OscWiFi.publish(const String& ip, const uint16_t port, const String& addr, T& value)
294+
->setFrameRate(float fps);
295+
// Publish multiple values periodically
296+
OscWiFi.publish(const String& ip, const uint16_t port, const String& addr, T1& v1, T2& v2, ...)
297+
->setIntervalMsec(float ms);
298+
// Publish function results periodically
299+
OscWiFi.publish(const String& ip, const uint16_t port, const String& addr, &func1, &func2)
300+
->setIntervalSec(float sec);
301+
```
302+
303+
#### OSC Bundle Support
304+
305+
```cpp
306+
// Create and send OSC bundles
307+
OscWiFi.begin_bundle(const TimeTag& tt = TimeTag::immediate());
308+
OscWiFi.add_bundle(const String& addr, T1 arg1, T2 arg2, ...);
309+
OscWiFi.end_bundle();
310+
OscWiFi.send_bundle(const String& ip, const uint16_t port);
311+
```
312+
313+
#### Update Functions
314+
315+
```cpp
316+
// Parse incoming OSC messages (server)
317+
OscWiFi.parse();
318+
// Parse incoming OSC messages and publish outgoing messages (server + client)
319+
OscWiFi.update();
320+
// Send published OSC messages (client)
321+
OscWiFi.post();
322+
```
323+
324+
### OscMessage
325+
326+
#### Argument Getters
327+
328+
```cpp
329+
msg.arg<T>(const uint8_t index); // Get argument as type T
330+
msg.getArgAsInt32(const size_t i);
331+
msg.getArgAsInt64(const size_t i);
332+
msg.getArgAsFloat(const size_t i);
333+
msg.getArgAsDouble(const size_t i);
334+
msg.getArgAsString(const size_t i);
335+
msg.getArgAsBlob(const size_t i);
336+
msg.getArgAsBool(const size_t i);
337+
```
338+
339+
#### Type Checkers
340+
341+
```cpp
342+
msg.isBool(const size_t i);
343+
msg.isInt32(const size_t i);
344+
msg.isInt64(const size_t i);
345+
msg.isFloat(const size_t i);
346+
msg.isDouble(const size_t i);
347+
msg.isStr(const size_t i);
348+
msg.isBlob(const size_t i);
349+
```
350+
351+
#### Message Information
352+
353+
```cpp
354+
msg.address(); // Get OSC address
355+
msg.size(); // Get number of arguments
356+
msg.typeTags(); // Get type tag string
357+
msg.remoteIP(); // Get sender's IP address
358+
msg.remotePort(); // Get sender's port
359+
msg.match(const String& pattern); // Check if address matches pattern
360+
```
361+
362+
### Manual Packet Handling (for boards with limited memory)
363+
364+
```cpp
365+
// Server for receiving
366+
OscEtherServer server(recv_port);
367+
if (server.parse()) {
368+
const OscMessage* msg = server.message();
369+
// Process message...
370+
}
371+
372+
// Client for sending
373+
OscEtherClient client;
374+
client.send(host, send_port, "/addr", arg1, arg2);
375+
```
376+
252377
## Dependent Libraries
253378
254379
- [ArxTypeTraits](https://github.com/hideakitai/ArxTypeTraits)

library.json

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,24 @@
11
{
2-
"name": "ArduinoOSC",
3-
"keywords": "osc,wifi,ethernet,udp",
4-
"description": "OSC subscriber / publisher for Arduino",
5-
"repository": {
6-
"type": "git",
7-
"url": "https://github.com/hideakitai/ArduinoOSC.git"
8-
},
9-
"authors": {
10-
"name": "Hideaki Tai",
11-
"url": "https://github.com/hideakitai",
12-
"maintainer": true
13-
},
14-
"version": "0.5.2",
15-
"license": "MIT",
16-
"frameworks": "arduino",
17-
"platforms": "*",
18-
"dependencies": {
19-
"hideakitai/ArxContainer": ">=0.6.0",
20-
"hideakitai/ArxSmartPtr": "*",
21-
"hideakitai/ArxTypeTraits": "*",
22-
"hideakitai/DebugLog": ">=0.8.1"
23-
}
2+
"name": "ArduinoOSC",
3+
"keywords": "osc,wifi,ethernet,udp",
4+
"description": "OSC subscriber / publisher for Arduino",
5+
"repository": {
6+
"type": "git",
7+
"url": "https://github.com/hideakitai/ArduinoOSC.git"
8+
},
9+
"authors": {
10+
"name": "Hideaki Tai",
11+
"url": "https://github.com/hideakitai",
12+
"maintainer": true
13+
},
14+
"version": "0.6.0",
15+
"license": "MIT",
16+
"frameworks": "arduino",
17+
"platforms": "*",
18+
"dependencies": {
19+
"hideakitai/ArxContainer": ">=0.6.0",
20+
"hideakitai/ArxSmartPtr": "*",
21+
"hideakitai/ArxTypeTraits": "*",
22+
"hideakitai/DebugLog": ">=0.8.1"
23+
}
2424
}

library.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name=ArduinoOSC
2-
version=0.5.2
2+
version=0.6.0
33
author=hideakitai
44
maintainer=hideakitai
55
sentence=OSC subscriber / publisher for Arduino

0 commit comments

Comments
 (0)