modded version CNMAT/OSC https://github.com/CNMAT/OSC

Dependents:   CVtoOSCConverter

Fork of osc-cnmat by Asperius van Hansen

Committer:
casiotone401
Date:
Sat Feb 13 11:29:14 2016 +0000
Revision:
4:107c23eb31b6
Parent:
1:18009e3041ea
OSCMessage::send & OSCBundle::send supports return OSC buffer

Who changed what in which revision?

UserRevisionLine numberNew contents of line
aspeteRakete 0:36705c54059a 1 /*
aspeteRakete 0:36705c54059a 2 Written by Yotam Mann, The Center for New Music and Audio Technologies,
aspeteRakete 0:36705c54059a 3 University of California, Berkeley. Copyright (c) 2013, The Regents of
aspeteRakete 0:36705c54059a 4 the University of California (Regents).
aspeteRakete 0:36705c54059a 5
aspeteRakete 0:36705c54059a 6 Permission to use, copy, modify, distribute, and distribute modified versions
aspeteRakete 0:36705c54059a 7 of this software and its documentation without fee and without a signed
aspeteRakete 0:36705c54059a 8 licensing agreement, is hereby granted, provided that the above copyright
aspeteRakete 0:36705c54059a 9 notice, this paragraph and the following two paragraphs appear in all copies,
aspeteRakete 0:36705c54059a 10 modifications, and distributions.
aspeteRakete 0:36705c54059a 11
aspeteRakete 0:36705c54059a 12 IN NO EVENT SHALL REGENTS BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT,
aspeteRakete 0:36705c54059a 13 SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING LOST PROFITS, ARISING
aspeteRakete 0:36705c54059a 14 OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF REGENTS HAS
aspeteRakete 0:36705c54059a 15 BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
aspeteRakete 0:36705c54059a 16
aspeteRakete 0:36705c54059a 17 REGENTS SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
aspeteRakete 0:36705c54059a 18 THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
aspeteRakete 0:36705c54059a 19 PURPOSE. THE SOFTWARE AND ACCOMPANYING DOCUMENTATION, IF ANY, PROVIDED
aspeteRakete 0:36705c54059a 20 HEREUNDER IS PROVIDED "AS IS". REGENTS HAS NO OBLIGATION TO PROVIDE
aspeteRakete 0:36705c54059a 21 MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
aspeteRakete 0:36705c54059a 22
aspeteRakete 0:36705c54059a 23 For bug reports and feature requests please email me at yotam@cnmat.berkeley.edu
aspeteRakete 0:36705c54059a 24 */
aspeteRakete 0:36705c54059a 25
aspeteRakete 0:36705c54059a 26 #ifndef OSCDATA_h
aspeteRakete 0:36705c54059a 27 #define OSCDATA_h
aspeteRakete 0:36705c54059a 28
aspeteRakete 0:36705c54059a 29 /*
aspeteRakete 0:36705c54059a 30
aspeteRakete 0:36705c54059a 31 #include <inttypes.h>
aspeteRakete 0:36705c54059a 32 */
aspeteRakete 0:36705c54059a 33 #include <stdint.h>
aspeteRakete 0:36705c54059a 34 #include <stdlib.h>
aspeteRakete 0:36705c54059a 35 #include <string.h>
aspeteRakete 0:36705c54059a 36
aspeteRakete 0:36705c54059a 37 //#include "mbed.h"
aspeteRakete 0:36705c54059a 38
aspeteRakete 0:36705c54059a 39 /*#if defined(CORE_TEENSY)|| defined(__AVR_ATmega32U4__) || defined(__SAM3X8E__) || (defined(_USB) && defined(_USE_USB_FOR_SERIAL_)) || defined(BOARD_maple_mini)*/
aspeteRakete 0:36705c54059a 40 // mbed boards should all have usbSerial.
aspeteRakete 0:36705c54059a 41 #define BOARD_HAS_USB_SERIAL
aspeteRakete 0:36705c54059a 42 /*#endif*/
aspeteRakete 0:36705c54059a 43
aspeteRakete 0:36705c54059a 44 /*#if defined(__SAM3X8E__)*/
aspeteRakete 0:36705c54059a 45 #define thisBoardsSerialUSB SerialUSB
aspeteRakete 0:36705c54059a 46 /*#else
aspeteRakete 0:36705c54059a 47 #define thisBoardsSerialUSB Serial
aspeteRakete 0:36705c54059a 48 #endif*/
aspeteRakete 0:36705c54059a 49
aspeteRakete 0:36705c54059a 50 //ERRORS/////////////////////////////////////////////////
aspeteRakete 0:36705c54059a 51 typedef enum { OSC_OK = 0,
aspeteRakete 0:36705c54059a 52 BUFFER_FULL, INVALID_OSC, ALLOCFAILED, INDEX_OUT_OF_BOUNDS
aspeteRakete 0:36705c54059a 53 } OSCErrorCode;
aspeteRakete 0:36705c54059a 54
aspeteRakete 0:36705c54059a 55 class OSCData
aspeteRakete 0:36705c54059a 56 {
aspeteRakete 0:36705c54059a 57
aspeteRakete 0:36705c54059a 58 private:
aspeteRakete 0:36705c54059a 59
aspeteRakete 0:36705c54059a 60 //friends
aspeteRakete 0:36705c54059a 61 friend class OSCMessage;
aspeteRakete 0:36705c54059a 62
aspeteRakete 0:36705c54059a 63 //should only be used while decoding
aspeteRakete 0:36705c54059a 64 //leaves an invalid OSCMessage with a type, but no data
aspeteRakete 0:36705c54059a 65 OSCData(char t);
aspeteRakete 0:36705c54059a 66
aspeteRakete 0:36705c54059a 67 public:
aspeteRakete 0:36705c54059a 68
aspeteRakete 0:36705c54059a 69 //an error flag
aspeteRakete 0:36705c54059a 70 OSCErrorCode error;
aspeteRakete 0:36705c54059a 71
aspeteRakete 0:36705c54059a 72 //the size (in bytes) of the data
aspeteRakete 0:36705c54059a 73 int bytes;
aspeteRakete 0:36705c54059a 74
aspeteRakete 0:36705c54059a 75 //the type of the data
aspeteRakete 0:36705c54059a 76 int type;
aspeteRakete 0:36705c54059a 77
aspeteRakete 0:36705c54059a 78 //the data
aspeteRakete 0:36705c54059a 79 union {
aspeteRakete 0:36705c54059a 80 char * s; //string
aspeteRakete 0:36705c54059a 81 int32_t i; //int
aspeteRakete 0:36705c54059a 82 float f; //float
aspeteRakete 0:36705c54059a 83 double d; //double
aspeteRakete 0:36705c54059a 84 uint64_t l; //long
aspeteRakete 0:36705c54059a 85 uint8_t * b; //blob
aspeteRakete 0:36705c54059a 86 } data;
aspeteRakete 0:36705c54059a 87
aspeteRakete 0:36705c54059a 88 //overload the constructor to account for all the types and sizes
aspeteRakete 0:36705c54059a 89 OSCData(const char * s);
aspeteRakete 0:36705c54059a 90 OSCData (int);
aspeteRakete 0:36705c54059a 91 OSCData (float);
aspeteRakete 0:36705c54059a 92 OSCData (double);
aspeteRakete 0:36705c54059a 93 OSCData (uint8_t *, int);
aspeteRakete 0:36705c54059a 94 //accepts another OSCData objects and clones it
aspeteRakete 0:36705c54059a 95 OSCData (OSCData *);
aspeteRakete 0:36705c54059a 96 OSCData (bool);
aspeteRakete 0:36705c54059a 97 OSCData (uint64_t);
aspeteRakete 0:36705c54059a 98
aspeteRakete 0:36705c54059a 99 //destructor
aspeteRakete 0:36705c54059a 100 ~OSCData();
aspeteRakete 0:36705c54059a 101
aspeteRakete 0:36705c54059a 102 //GETTERS
aspeteRakete 0:36705c54059a 103 int32_t getInt();
aspeteRakete 0:36705c54059a 104 float getFloat();
aspeteRakete 0:36705c54059a 105 double getDouble();
aspeteRakete 0:36705c54059a 106 int getString(char *, int);
aspeteRakete 0:36705c54059a 107 int getBlob(uint8_t *, int);
aspeteRakete 0:36705c54059a 108 bool getBoolean();
aspeteRakete 0:36705c54059a 109 uint64_t getTime();
aspeteRakete 0:36705c54059a 110
aspeteRakete 0:36705c54059a 111 //constructor from byte array with type and length
aspeteRakete 0:36705c54059a 112 OSCData(char, uint8_t *, int);
aspeteRakete 0:36705c54059a 113 //fill the passed in buffer with the data
aspeteRakete 0:36705c54059a 114 //uint8_t * asByteArray();
aspeteRakete 0:36705c54059a 115
aspeteRakete 0:36705c54059a 116 };
aspeteRakete 0:36705c54059a 117
aspeteRakete 0:36705c54059a 118
aspeteRakete 0:36705c54059a 119 //## Cortex-M is little Endian
aspeteRakete 0:36705c54059a 120 /*
aspeteRakete 0:36705c54059a 121 based on http://stackoverflow.com/questions/809902/64-bit-ntohl-in-c
aspeteRakete 0:36705c54059a 122
aspeteRakete 0:36705c54059a 123 if the system is little endian, it will flip the bits
aspeteRakete 0:36705c54059a 124 if the system is big endian, it'll do nothing
aspeteRakete 0:36705c54059a 125 */
aspeteRakete 0:36705c54059a 126 template<typename T>
aspeteRakete 0:36705c54059a 127 static inline T BigEndian(const T& x)
aspeteRakete 0:36705c54059a 128 {
aspeteRakete 0:36705c54059a 129 const int one = 1;
aspeteRakete 0:36705c54059a 130 const char sig = *(char*)&one;
aspeteRakete 0:36705c54059a 131 if (sig == 0) return x; // for big endian machine just return the input
aspeteRakete 0:36705c54059a 132 T ret;
aspeteRakete 0:36705c54059a 133 int size = sizeof(T);
aspeteRakete 0:36705c54059a 134 char* src = (char*)&x + sizeof(T) - 1;
aspeteRakete 0:36705c54059a 135 char* dst = (char*)&ret;
aspeteRakete 0:36705c54059a 136 while (size-- > 0){
aspeteRakete 0:36705c54059a 137 *dst++ = *src--;
aspeteRakete 0:36705c54059a 138 }
aspeteRakete 0:36705c54059a 139 return ret;
aspeteRakete 0:36705c54059a 140 }
aspeteRakete 0:36705c54059a 141
aspeteRakete 0:36705c54059a 142 #endif