Fork of, triyin to rewrite for mbed 5. WIP not yet finished but working. ----------------------- An Open Sound Control library for the mbed, created to be compatible with Recotana's OSCClass library (http://recotana.com) for the Arduino with Ethernet shield. It also uses parts of the OSC Transceiver(Sender/Receiver) code by xshige written by: Alvaro Cassinelli, October 2011 tweaked by: Toby Harris / *spark audio-visual, March 2012

Committer:
Ibiltari
Date:
Mon Dec 03 20:27:07 2018 +0100
Revision:
7:98280cef1c4f
Child:
9:e4528f622bcc
Cambio de libreria base a CNMAT OSC Arduino

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Ibiltari 7:98280cef1c4f 1 /* mbed OSC Library
Ibiltari 7:98280cef1c4f 2 This is an Open Sound Control library for the mbed, created to be compatible with Recotana's OSCClass library (http://recotana.com) for the
Ibiltari 7:98280cef1c4f 3 Arduino with Ethernet shield. It also uses parts of the OSC Transceiver(Sender/Receiver) code by xshige
Ibiltari 7:98280cef1c4f 4 written by: Alvaro Cassinelli, October 2011
Ibiltari 7:98280cef1c4f 5 tweaked by: Toby Harris / *spark audio-visual, March 2012
Ibiltari 7:98280cef1c4f 6
Ibiltari 7:98280cef1c4f 7 This library is free software; you can redistribute it and/or
Ibiltari 7:98280cef1c4f 8 modify it under the terms of the GNU Lesser General Public
Ibiltari 7:98280cef1c4f 9 License version 2.1 as published by the Free Software Foundation.
Ibiltari 7:98280cef1c4f 10 Open Sound Control http://opensoundcontrol.org/
Ibiltari 7:98280cef1c4f 11 */
Ibiltari 7:98280cef1c4f 12
Ibiltari 7:98280cef1c4f 13 #ifndef mbedOSC_h
Ibiltari 7:98280cef1c4f 14 #define mbedOSC_h
Ibiltari 7:98280cef1c4f 15
Ibiltari 7:98280cef1c4f 16 #include "mbed.h"
Ibiltari 7:98280cef1c4f 17 #include "EthernetInterface.h"
Ibiltari 7:98280cef1c4f 18 #include "UDPSocket.h"
Ibiltari 7:98280cef1c4f 19 #include "OSCData.h"
Ibiltari 7:98280cef1c4f 20
Ibiltari 7:98280cef1c4f 21 class OSCMessage
Ibiltari 7:98280cef1c4f 22 {
Ibiltari 7:98280cef1c4f 23
Ibiltari 7:98280cef1c4f 24 private:
Ibiltari 7:98280cef1c4f 25
Ibiltari 7:98280cef1c4f 26 //friends
Ibiltari 7:98280cef1c4f 27 friend class OSCBundle;
Ibiltari 7:98280cef1c4f 28
Ibiltari 7:98280cef1c4f 29
Ibiltari 7:98280cef1c4f 30 /*=============================================================================
Ibiltari 7:98280cef1c4f 31 PRIVATE VARIABLES
Ibiltari 7:98280cef1c4f 32 =============================================================================*/
Ibiltari 7:98280cef1c4f 33
Ibiltari 7:98280cef1c4f 34 //the address
Ibiltari 7:98280cef1c4f 35 char * address;
Ibiltari 7:98280cef1c4f 36
Ibiltari 7:98280cef1c4f 37 //the data
Ibiltari 7:98280cef1c4f 38 OSCData ** data;
Ibiltari 7:98280cef1c4f 39
Ibiltari 7:98280cef1c4f 40 //the number of OSCData in the data array
Ibiltari 7:98280cef1c4f 41 int dataCount;
Ibiltari 7:98280cef1c4f 42
Ibiltari 7:98280cef1c4f 43 //error codes for potential runtime problems
Ibiltari 7:98280cef1c4f 44 OSCErrorCode error;
Ibiltari 7:98280cef1c4f 45
Ibiltari 7:98280cef1c4f 46 /*=============================================================================
Ibiltari 7:98280cef1c4f 47 DECODING INCOMING BYTES
Ibiltari 7:98280cef1c4f 48 =============================================================================*/
Ibiltari 7:98280cef1c4f 49
Ibiltari 7:98280cef1c4f 50 //the decoding states for incoming bytes
Ibiltari 7:98280cef1c4f 51 enum DecodeState {
Ibiltari 7:98280cef1c4f 52 STANDBY,
Ibiltari 7:98280cef1c4f 53 ADDRESS,
Ibiltari 7:98280cef1c4f 54 ADDRESS_PADDING,
Ibiltari 7:98280cef1c4f 55 TYPES,
Ibiltari 7:98280cef1c4f 56 TYPES_PADDING,
Ibiltari 7:98280cef1c4f 57 DATA,
Ibiltari 7:98280cef1c4f 58 DATA_PADDING,
Ibiltari 7:98280cef1c4f 59 DONE,
Ibiltari 7:98280cef1c4f 60 } decodeState;
Ibiltari 7:98280cef1c4f 61
Ibiltari 7:98280cef1c4f 62 //stores incoming bytes until they can be decoded
Ibiltari 7:98280cef1c4f 63 uint8_t * incomingBuffer;
Ibiltari 7:98280cef1c4f 64 int incomingBufferSize; // how many bytes are stored
Ibiltari 7:98280cef1c4f 65 int incomingBufferFree; // how many bytes are allocated but unused
Ibiltari 7:98280cef1c4f 66
Ibiltari 7:98280cef1c4f 67 //adds a byte to the buffer
Ibiltari 7:98280cef1c4f 68 void addToIncomingBuffer(uint8_t);
Ibiltari 7:98280cef1c4f 69 //clears the incoming buffer
Ibiltari 7:98280cef1c4f 70 void clearIncomingBuffer();
Ibiltari 7:98280cef1c4f 71
Ibiltari 7:98280cef1c4f 72 //decoding function
Ibiltari 7:98280cef1c4f 73 void decode(uint8_t);
Ibiltari 7:98280cef1c4f 74 void decodeAddress();
Ibiltari 7:98280cef1c4f 75 void decodeType(uint8_t);
Ibiltari 7:98280cef1c4f 76 void decodeData(uint8_t);
Ibiltari 7:98280cef1c4f 77
Ibiltari 7:98280cef1c4f 78 /*=============================================================================
Ibiltari 7:98280cef1c4f 79 HELPER FUNCTIONS
Ibiltari 7:98280cef1c4f 80 =============================================================================*/
Ibiltari 7:98280cef1c4f 81
Ibiltari 7:98280cef1c4f 82 void setupMessage();
Ibiltari 7:98280cef1c4f 83
Ibiltari 7:98280cef1c4f 84 //compares the OSCData's type char to a test char
Ibiltari 7:98280cef1c4f 85 bool testType(int position, char type);
Ibiltari 7:98280cef1c4f 86
Ibiltari 7:98280cef1c4f 87 //returns the number of bytes to pad to make it 4-bit aligned
Ibiltari 7:98280cef1c4f 88 int padSize(int bytes);
Ibiltari 7:98280cef1c4f 89
Ibiltari 7:98280cef1c4f 90 public:
Ibiltari 7:98280cef1c4f 91
Ibiltari 7:98280cef1c4f 92 //returns the OSCData at that position
Ibiltari 7:98280cef1c4f 93 OSCData * getOSCData(int);
Ibiltari 7:98280cef1c4f 94
Ibiltari 7:98280cef1c4f 95 /*=============================================================================
Ibiltari 7:98280cef1c4f 96 CONSTRUCTORS / DESTRUCTOR
Ibiltari 7:98280cef1c4f 97 =============================================================================*/
Ibiltari 7:98280cef1c4f 98
Ibiltari 7:98280cef1c4f 99 //new constructor needs an address
Ibiltari 7:98280cef1c4f 100 OSCMessage (const char * _address);
Ibiltari 7:98280cef1c4f 101 //no address
Ibiltari 7:98280cef1c4f 102 //placeholder since it's invalide OSC
Ibiltari 7:98280cef1c4f 103 OSCMessage();
Ibiltari 7:98280cef1c4f 104
Ibiltari 7:98280cef1c4f 105 //can optionally accept all of the data after the address
Ibiltari 7:98280cef1c4f 106 //OSCMessage(const char * _address, char * types, ... );
Ibiltari 7:98280cef1c4f 107 //created from another OSCMessage
Ibiltari 7:98280cef1c4f 108 OSCMessage (OSCMessage *);
Ibiltari 7:98280cef1c4f 109
Ibiltari 7:98280cef1c4f 110 //DESTRUCTOR
Ibiltari 7:98280cef1c4f 111 ~OSCMessage();
Ibiltari 7:98280cef1c4f 112
Ibiltari 7:98280cef1c4f 113 //empties all of the data
Ibiltari 7:98280cef1c4f 114 OSCMessage& empty();
Ibiltari 7:98280cef1c4f 115
Ibiltari 7:98280cef1c4f 116 /*=============================================================================
Ibiltari 7:98280cef1c4f 117 SETTING DATA
Ibiltari 7:98280cef1c4f 118 =============================================================================*/
Ibiltari 7:98280cef1c4f 119
Ibiltari 7:98280cef1c4f 120 //returns the OSCMessage so that multiple 'add's can be strung together
Ibiltari 7:98280cef1c4f 121 template <typename T>
Ibiltari 7:98280cef1c4f 122 OSCMessage& add(T datum){
Ibiltari 7:98280cef1c4f 123 //make a piece of data
Ibiltari 7:98280cef1c4f 124 OSCData * d = new OSCData(datum);
Ibiltari 7:98280cef1c4f 125 //check if it has any errors
Ibiltari 7:98280cef1c4f 126 if (d->error == ALLOCFAILED){
Ibiltari 7:98280cef1c4f 127 error = ALLOCFAILED;
Ibiltari 7:98280cef1c4f 128 } else {
Ibiltari 7:98280cef1c4f 129 //resize the data array
Ibiltari 7:98280cef1c4f 130 OSCData ** dataMem = (OSCData **) realloc(data, sizeof(OSCData *) * (dataCount + 1));
Ibiltari 7:98280cef1c4f 131 if (dataMem == NULL){
Ibiltari 7:98280cef1c4f 132 error = ALLOCFAILED;
Ibiltari 7:98280cef1c4f 133 } else {
Ibiltari 7:98280cef1c4f 134 data = dataMem;
Ibiltari 7:98280cef1c4f 135 //add data to the end of the array
Ibiltari 7:98280cef1c4f 136 data[dataCount] = d;
Ibiltari 7:98280cef1c4f 137 //increment the data size
Ibiltari 7:98280cef1c4f 138 dataCount++;
Ibiltari 7:98280cef1c4f 139 }
Ibiltari 7:98280cef1c4f 140 }
Ibiltari 7:98280cef1c4f 141 return *this;
Ibiltari 7:98280cef1c4f 142 }
Ibiltari 7:98280cef1c4f 143 /*
Ibiltari 7:98280cef1c4f 144 //blob specific add
Ibiltari 7:98280cef1c4f 145 OSCMessage& add(uint8_t * blob, int length){
Ibiltari 7:98280cef1c4f 146 //make a piece of data
Ibiltari 7:98280cef1c4f 147 OSCData * d = new OSCData(blob, length);
Ibiltari 7:98280cef1c4f 148 //check if it has any errors
Ibiltari 7:98280cef1c4f 149 if (d->error == ALLOCFAILED){
Ibiltari 7:98280cef1c4f 150 error = ALLOCFAILED;
Ibiltari 7:98280cef1c4f 151 } else {
Ibiltari 7:98280cef1c4f 152 //resize the data array
Ibiltari 7:98280cef1c4f 153 OSCData ** dataMem = (OSCData **) realloc(data, sizeof(OSCData *) * (dataCount + 1));
Ibiltari 7:98280cef1c4f 154 if (dataMem == NULL){
Ibiltari 7:98280cef1c4f 155 error = ALLOCFAILED;
Ibiltari 7:98280cef1c4f 156 } else {
Ibiltari 7:98280cef1c4f 157 data = dataMem;
Ibiltari 7:98280cef1c4f 158 //add data to the end of the array
Ibiltari 7:98280cef1c4f 159 data[dataCount] = d;
Ibiltari 7:98280cef1c4f 160 //increment the data size
Ibiltari 7:98280cef1c4f 161 dataCount++;
Ibiltari 7:98280cef1c4f 162 }
Ibiltari 7:98280cef1c4f 163 }
Ibiltari 7:98280cef1c4f 164 return *this;
Ibiltari 7:98280cef1c4f 165 }
Ibiltari 7:98280cef1c4f 166 */
Ibiltari 7:98280cef1c4f 167 //sets the data at a position
Ibiltari 7:98280cef1c4f 168 template <typename T>
Ibiltari 7:98280cef1c4f 169 OSCMessage& set(int position, T datum){
Ibiltari 7:98280cef1c4f 170 if (position < dataCount){
Ibiltari 7:98280cef1c4f 171 //replace the OSCData with a new one
Ibiltari 7:98280cef1c4f 172 OSCData * oldDatum = getOSCData(position);
Ibiltari 7:98280cef1c4f 173 //destroy the old one
Ibiltari 7:98280cef1c4f 174 delete oldDatum;
Ibiltari 7:98280cef1c4f 175 //make a new one
Ibiltari 7:98280cef1c4f 176 OSCData * newDatum = new OSCData(datum);
Ibiltari 7:98280cef1c4f 177 //test if there was an error
Ibiltari 7:98280cef1c4f 178 if (newDatum->error == ALLOCFAILED){
Ibiltari 7:98280cef1c4f 179 error = ALLOCFAILED;
Ibiltari 7:98280cef1c4f 180 } else {
Ibiltari 7:98280cef1c4f 181 //otherwise, put it in the data array
Ibiltari 7:98280cef1c4f 182 data[position] = newDatum;
Ibiltari 7:98280cef1c4f 183 }
Ibiltari 7:98280cef1c4f 184 } else if (position == (dataCount)){
Ibiltari 7:98280cef1c4f 185 //add the data to the end
Ibiltari 7:98280cef1c4f 186 add(datum);
Ibiltari 7:98280cef1c4f 187 } else {
Ibiltari 7:98280cef1c4f 188 //else out of bounds error
Ibiltari 7:98280cef1c4f 189 error = INDEX_OUT_OF_BOUNDS;
Ibiltari 7:98280cef1c4f 190 }
Ibiltari 7:98280cef1c4f 191 return *this;
Ibiltari 7:98280cef1c4f 192 }
Ibiltari 7:98280cef1c4f 193 /*
Ibiltari 7:98280cef1c4f 194 //blob specific setter
Ibiltari 7:98280cef1c4f 195 OSCMessage& set(int position, uint8_t * blob, int length){
Ibiltari 7:98280cef1c4f 196 if (position < dataCount){
Ibiltari 7:98280cef1c4f 197 //replace the OSCData with a new one
Ibiltari 7:98280cef1c4f 198 OSCData * oldDatum = getOSCData(position);
Ibiltari 7:98280cef1c4f 199 //destroy the old one
Ibiltari 7:98280cef1c4f 200 delete oldDatum;
Ibiltari 7:98280cef1c4f 201 //make a new one
Ibiltari 7:98280cef1c4f 202 OSCData * newDatum = new OSCData(blob, length);
Ibiltari 7:98280cef1c4f 203 //test if there was an error
Ibiltari 7:98280cef1c4f 204 if (newDatum->error == ALLOCFAILED){
Ibiltari 7:98280cef1c4f 205 error = ALLOCFAILED;
Ibiltari 7:98280cef1c4f 206 } else {
Ibiltari 7:98280cef1c4f 207 //otherwise, put it in the data array
Ibiltari 7:98280cef1c4f 208 data[position] = newDatum;
Ibiltari 7:98280cef1c4f 209 }
Ibiltari 7:98280cef1c4f 210 } else if (position == (dataCount)){
Ibiltari 7:98280cef1c4f 211 //add the data to the end
Ibiltari 7:98280cef1c4f 212 add(blob, length);
Ibiltari 7:98280cef1c4f 213 } else {
Ibiltari 7:98280cef1c4f 214 //else out of bounds error
Ibiltari 7:98280cef1c4f 215 error = INDEX_OUT_OF_BOUNDS;
Ibiltari 7:98280cef1c4f 216 }
Ibiltari 7:98280cef1c4f 217 return *this;
Ibiltari 7:98280cef1c4f 218 }
Ibiltari 7:98280cef1c4f 219 */
Ibiltari 7:98280cef1c4f 220 OSCMessage& setAddress(const char *);
Ibiltari 7:98280cef1c4f 221
Ibiltari 7:98280cef1c4f 222 /*=============================================================================
Ibiltari 7:98280cef1c4f 223 GETTING DATA
Ibiltari 7:98280cef1c4f 224
Ibiltari 7:98280cef1c4f 225 getters take a position as an argument
Ibiltari 7:98280cef1c4f 226 =============================================================================*/
Ibiltari 7:98280cef1c4f 227
Ibiltari 7:98280cef1c4f 228 int32_t getInt(int);
Ibiltari 7:98280cef1c4f 229 // osctime_t getTime(int); // TODO: implement timming
Ibiltari 7:98280cef1c4f 230
Ibiltari 7:98280cef1c4f 231 float getFloat(int);
Ibiltari 7:98280cef1c4f 232 double getDouble(int);
Ibiltari 7:98280cef1c4f 233 bool getBoolean(int);
Ibiltari 7:98280cef1c4f 234
Ibiltari 7:98280cef1c4f 235 //return the copied string's length
Ibiltari 7:98280cef1c4f 236 int getString(int, char *);
Ibiltari 7:98280cef1c4f 237 //check that it won't overflow the passed buffer's size with a third argument
Ibiltari 7:98280cef1c4f 238 int getString(int, char *, int);
Ibiltari 7:98280cef1c4f 239 //offset and size can be defined in order to only query a part of the string
Ibiltari 7:98280cef1c4f 240 int getString(int, char *, int, int, int);
Ibiltari 7:98280cef1c4f 241
Ibiltari 7:98280cef1c4f 242 //returns the number of unsigned int8's copied into the buffer
Ibiltari 7:98280cef1c4f 243 int getBlob(int, uint8_t *);
Ibiltari 7:98280cef1c4f 244 //check that it won't overflow the passed buffer's size with a third argument
Ibiltari 7:98280cef1c4f 245 int getBlob(int, uint8_t *, int);
Ibiltari 7:98280cef1c4f 246 //offset and size can be defined in order to only query a part of the blob's content
Ibiltari 7:98280cef1c4f 247 int getBlob(int, uint8_t *, int, int, int);
Ibiltari 7:98280cef1c4f 248
Ibiltari 7:98280cef1c4f 249
Ibiltari 7:98280cef1c4f 250 // returns the length of blob
Ibiltari 7:98280cef1c4f 251 uint32_t getBlobLength(int position);
Ibiltari 7:98280cef1c4f 252
Ibiltari 7:98280cef1c4f 253 //returns the number of bytes of the data at that position
Ibiltari 7:98280cef1c4f 254 int getDataLength(int);
Ibiltari 7:98280cef1c4f 255
Ibiltari 7:98280cef1c4f 256 //returns the type at the position
Ibiltari 7:98280cef1c4f 257 char getType(int);
Ibiltari 7:98280cef1c4f 258
Ibiltari 7:98280cef1c4f 259 //put the address in the buffer
Ibiltari 7:98280cef1c4f 260 const char * getAddress( int offset = 0);
Ibiltari 7:98280cef1c4f 261 int getAddress(char * buffer, int offset, int len);
Ibiltari 7:98280cef1c4f 262
Ibiltari 7:98280cef1c4f 263 // TODO: int getAddressLength(int offset = 0);
Ibiltari 7:98280cef1c4f 264
Ibiltari 7:98280cef1c4f 265
Ibiltari 7:98280cef1c4f 266 /*=============================================================================
Ibiltari 7:98280cef1c4f 267 TESTING DATA
Ibiltari 7:98280cef1c4f 268
Ibiltari 7:98280cef1c4f 269 testers take a position as an argument
Ibiltari 7:98280cef1c4f 270 =============================================================================*/
Ibiltari 7:98280cef1c4f 271
Ibiltari 7:98280cef1c4f 272 bool isInt(int);
Ibiltari 7:98280cef1c4f 273 bool isFloat(int);
Ibiltari 7:98280cef1c4f 274 bool isBlob(int);
Ibiltari 7:98280cef1c4f 275 bool isChar(int);
Ibiltari 7:98280cef1c4f 276 bool isString(int);
Ibiltari 7:98280cef1c4f 277 bool isDouble(int);
Ibiltari 7:98280cef1c4f 278 bool isBoolean(int);
Ibiltari 7:98280cef1c4f 279 bool isTime(int);
Ibiltari 7:98280cef1c4f 280
Ibiltari 7:98280cef1c4f 281 /*=============================================================================
Ibiltari 7:98280cef1c4f 282 PATTERN MATCHING
Ibiltari 7:98280cef1c4f 283 =============================================================================*/
Ibiltari 7:98280cef1c4f 284 /*
Ibiltari 7:98280cef1c4f 285 //match the pattern against the address
Ibiltari 7:98280cef1c4f 286 //returns true only for a complete match
Ibiltari 7:98280cef1c4f 287 bool fullMatch( const char * pattern, int = 0);
Ibiltari 7:98280cef1c4f 288
Ibiltari 7:98280cef1c4f 289 //returns the number of characters matched in the address
Ibiltari 7:98280cef1c4f 290 int match( const char * pattern, int = 0);
Ibiltari 7:98280cef1c4f 291
Ibiltari 7:98280cef1c4f 292 //calls the function with the message as the arg if it was a full match
Ibiltari 7:98280cef1c4f 293 bool dispatch(const char * pattern, void (*callback)(OSCMessage &), int = 0);
Ibiltari 7:98280cef1c4f 294
Ibiltari 7:98280cef1c4f 295 //like dispatch, but allows for partial matches
Ibiltari 7:98280cef1c4f 296 //the address match offset is sent as an argument to the callback
Ibiltari 7:98280cef1c4f 297 //also room for an option address offset to allow for multiple nested routes
Ibiltari 7:98280cef1c4f 298 bool route(const char * pattern, void (*callback)(OSCMessage &, int), int = 0);
Ibiltari 7:98280cef1c4f 299
Ibiltari 7:98280cef1c4f 300 */
Ibiltari 7:98280cef1c4f 301
Ibiltari 7:98280cef1c4f 302 /*=============================================================================
Ibiltari 7:98280cef1c4f 303 SIZE
Ibiltari 7:98280cef1c4f 304 =============================================================================*/
Ibiltari 7:98280cef1c4f 305
Ibiltari 7:98280cef1c4f 306 //the number of data that the message contains
Ibiltari 7:98280cef1c4f 307 int size();
Ibiltari 7:98280cef1c4f 308
Ibiltari 7:98280cef1c4f 309 //computes the number of bytes the OSCMessage occupies if everything is 32-bit aligned
Ibiltari 7:98280cef1c4f 310 int bytes();
Ibiltari 7:98280cef1c4f 311
Ibiltari 7:98280cef1c4f 312 /*=============================================================================
Ibiltari 7:98280cef1c4f 313 TRANSMISSION
Ibiltari 7:98280cef1c4f 314 =============================================================================*/
Ibiltari 7:98280cef1c4f 315
Ibiltari 7:98280cef1c4f 316 //send the message
Ibiltari 7:98280cef1c4f 317 // OSCMessage& send(Print &p);
Ibiltari 7:98280cef1c4f 318
Ibiltari 7:98280cef1c4f 319 //fill the message from a byte stream
Ibiltari 7:98280cef1c4f 320 OSCMessage& fill(uint8_t);
Ibiltari 7:98280cef1c4f 321 OSCMessage& fill(uint8_t *, int);
Ibiltari 7:98280cef1c4f 322
Ibiltari 7:98280cef1c4f 323 /*=============================================================================
Ibiltari 7:98280cef1c4f 324 ERROR
Ibiltari 7:98280cef1c4f 325 =============================================================================*/
Ibiltari 7:98280cef1c4f 326
Ibiltari 7:98280cef1c4f 327 bool hasError();
Ibiltari 7:98280cef1c4f 328
Ibiltari 7:98280cef1c4f 329 OSCErrorCode getError();
Ibiltari 7:98280cef1c4f 330
Ibiltari 7:98280cef1c4f 331 };
Ibiltari 7:98280cef1c4f 332
Ibiltari 7:98280cef1c4f 333
Ibiltari 7:98280cef1c4f 334 #endif