-
Notifications
You must be signed in to change notification settings - Fork 138
/
OSCBundle.h
175 lines (129 loc) · 5.43 KB
/
OSCBundle.h
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
/*
Written by Yotam Mann, The Center for New Music and Audio Technologies,
University of California, Berkeley. Copyright (c) 2012, 2013, The Regents of
the University of California (Regents).
Permission to use, copy, modify, distribute, and distribute modified versions
of this software and its documentation without fee and without a signed
licensing agreement, is hereby granted, provided that the above copyright
notice, this paragraph and the following two paragraphs appear in all copies,
modifications, and distributions.
IN NO EVENT SHALL REGENTS BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT,
SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING LOST PROFITS, ARISING
OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF REGENTS HAS
BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
REGENTS SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
PURPOSE. THE SOFTWARE AND ACCOMPANYING DOCUMENTATION, IF ANY, PROVIDED
HEREUNDER IS PROVIDED "AS IS". REGENTS HAS NO OBLIGATION TO PROVIDE
MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
*/
#ifndef OSCBUNDLE_h
#define OSCBUNDLE_h
#include "OSCMessage.h"
extern osctime_t zerotime;
class OSCBundle
{
private:
/*=============================================================================
PRIVATE VARIABLES
=============================================================================*/
//the array of messages contained in the bundle
OSCMessage ** messages;
//the number of messages in the array
int numMessages;
osctime_t timetag;
//error codes
OSCErrorCode error;
/*=============================================================================
DECODING INCOMING BYTES
=============================================================================*/
//the decoding states for incoming bytes
enum DecodeState {
STANDBY,
HEADER,
TIMETAG,
MESSAGE_SIZE,
MESSAGE,
} decodeState;
//stores incoming bytes until they can be decoded
uint8_t * incomingBuffer;
int incomingBufferSize;
//the size of the incoming message
int incomingMessageSize;
//adds a byte to the buffer
void addToIncomingBuffer(uint8_t);
//clears the incoming buffer
void clearIncomingBuffer();
//decoding functions
void decode(uint8_t);
void decodeTimetag();
void decodeHeader();
void decodeMessage(uint8_t);
//just a placeholder while filling
OSCMessage & add();
public:
/*=============================================================================
CONSTRUCTORS / DESTRUCTOR
=============================================================================*/
//default timetag of
OSCBundle(osctime_t = zerotime);
//DESTRUCTOR
~OSCBundle();
//clears all of the OSCMessages inside
OSCBundle& empty();
/*=============================================================================
SETTERS
=============================================================================*/
//start a new OSC Message in the bundle
OSCMessage & add(const char * address);
//add with nothing in it produces an invalid osc message
//copies an existing message into the bundle
OSCMessage & add(OSCMessage & msg);
template <typename T>
OSCBundle& setTimetag(T t){
timetag = (osctime_t) t;
return *this;
}
//sets the timetag from a buffer
OSCBundle& setTimetag(uint8_t * buff){
memcpy(&timetag, buff, 8);
return *this;
}
/*=============================================================================
GETTERS
=============================================================================*/
//gets the message the matches the address string
//will do regex matching
OSCMessage * getOSCMessage(char * addr);
//get message by position
OSCMessage * getOSCMessage(int position);
/*=============================================================================
MATCHING
=============================================================================*/
//if the bundle contains a message that matches the pattern,
//call the function callback on that message
bool dispatch(const char * pattern, void (*callback)(OSCMessage&), int = 0);
//like dispatch, but allows for partial matches
//the address match offset is sent as an argument to the callback
bool route(const char * pattern, void (*callback)(OSCMessage&, int), int = 0);
/*=============================================================================
SIZE
=============================================================================*/
//returns the number of messages in the bundle;
int size();
/*=============================================================================
ERROR
=============================================================================*/
bool hasError();
OSCErrorCode getError();
/*=============================================================================
SENDING
=============================================================================*/
OSCBundle& send(Print &p);
/*=============================================================================
FILLING
=============================================================================*/
OSCBundle& fill(uint8_t incomingByte);
OSCBundle& fill(const uint8_t * incomingBytes, int length);
};
#endif