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

Dependents:   CVtoOSCConverter

Fork of osc-cnmat by Asperius van Hansen

Committer:
aspeteRakete
Date:
Fri May 16 21:50:03 2014 +0000
Revision:
0:36705c54059a
Child:
1:18009e3041ea
init, ported OSCData:; - removed arduino specific headers.; - removed duplicated method (uint32_t vs int)

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 OSCData(){};
aspeteRakete 0:36705c54059a 70
aspeteRakete 0:36705c54059a 71 //an error flag
aspeteRakete 0:36705c54059a 72 OSCErrorCode error;
aspeteRakete 0:36705c54059a 73
aspeteRakete 0:36705c54059a 74 //the size (in bytes) of the data
aspeteRakete 0:36705c54059a 75 int bytes;
aspeteRakete 0:36705c54059a 76
aspeteRakete 0:36705c54059a 77 //the type of the data
aspeteRakete 0:36705c54059a 78 int type;
aspeteRakete 0:36705c54059a 79
aspeteRakete 0:36705c54059a 80 //the data
aspeteRakete 0:36705c54059a 81 union {
aspeteRakete 0:36705c54059a 82 char * s; //string
aspeteRakete 0:36705c54059a 83 int32_t i; //int
aspeteRakete 0:36705c54059a 84 float f; //float
aspeteRakete 0:36705c54059a 85 double d; //double
aspeteRakete 0:36705c54059a 86 uint64_t l; //long
aspeteRakete 0:36705c54059a 87 uint8_t * b; //blob
aspeteRakete 0:36705c54059a 88 } data;
aspeteRakete 0:36705c54059a 89
aspeteRakete 0:36705c54059a 90 //overload the constructor to account for all the types and sizes
aspeteRakete 0:36705c54059a 91 OSCData(const char * s);
aspeteRakete 0:36705c54059a 92 OSCData (int);
aspeteRakete 0:36705c54059a 93 OSCData (float);
aspeteRakete 0:36705c54059a 94 OSCData (double);
aspeteRakete 0:36705c54059a 95 OSCData (uint8_t *, int);
aspeteRakete 0:36705c54059a 96 //accepts another OSCData objects and clones it
aspeteRakete 0:36705c54059a 97 OSCData (OSCData *);
aspeteRakete 0:36705c54059a 98 OSCData (bool);
aspeteRakete 0:36705c54059a 99 OSCData (uint64_t);
aspeteRakete 0:36705c54059a 100
aspeteRakete 0:36705c54059a 101 //destructor
aspeteRakete 0:36705c54059a 102 ~OSCData();
aspeteRakete 0:36705c54059a 103
aspeteRakete 0:36705c54059a 104 //GETTERS
aspeteRakete 0:36705c54059a 105 int32_t getInt();
aspeteRakete 0:36705c54059a 106 float getFloat();
aspeteRakete 0:36705c54059a 107 double getDouble();
aspeteRakete 0:36705c54059a 108 int getString(char *, int);
aspeteRakete 0:36705c54059a 109 int getBlob(uint8_t *, int);
aspeteRakete 0:36705c54059a 110 bool getBoolean();
aspeteRakete 0:36705c54059a 111 uint64_t getTime();
aspeteRakete 0:36705c54059a 112
aspeteRakete 0:36705c54059a 113 //constructor from byte array with type and length
aspeteRakete 0:36705c54059a 114 OSCData(char, uint8_t *, int);
aspeteRakete 0:36705c54059a 115 //fill the passed in buffer with the data
aspeteRakete 0:36705c54059a 116 //uint8_t * asByteArray();
aspeteRakete 0:36705c54059a 117
aspeteRakete 0:36705c54059a 118 };
aspeteRakete 0:36705c54059a 119
aspeteRakete 0:36705c54059a 120
aspeteRakete 0:36705c54059a 121 //## Cortex-M is little Endian
aspeteRakete 0:36705c54059a 122 /*
aspeteRakete 0:36705c54059a 123 based on http://stackoverflow.com/questions/809902/64-bit-ntohl-in-c
aspeteRakete 0:36705c54059a 124
aspeteRakete 0:36705c54059a 125 if the system is little endian, it will flip the bits
aspeteRakete 0:36705c54059a 126 if the system is big endian, it'll do nothing
aspeteRakete 0:36705c54059a 127 */
aspeteRakete 0:36705c54059a 128 template<typename T>
aspeteRakete 0:36705c54059a 129 static inline T BigEndian(const T& x)
aspeteRakete 0:36705c54059a 130 {
aspeteRakete 0:36705c54059a 131 const int one = 1;
aspeteRakete 0:36705c54059a 132 const char sig = *(char*)&one;
aspeteRakete 0:36705c54059a 133 if (sig == 0) return x; // for big endian machine just return the input
aspeteRakete 0:36705c54059a 134 T ret;
aspeteRakete 0:36705c54059a 135 int size = sizeof(T);
aspeteRakete 0:36705c54059a 136 char* src = (char*)&x + sizeof(T) - 1;
aspeteRakete 0:36705c54059a 137 char* dst = (char*)&ret;
aspeteRakete 0:36705c54059a 138 while (size-- > 0){
aspeteRakete 0:36705c54059a 139 *dst++ = *src--;
aspeteRakete 0:36705c54059a 140 }
aspeteRakete 0:36705c54059a 141 return ret;
aspeteRakete 0:36705c54059a 142 }
aspeteRakete 0:36705c54059a 143
aspeteRakete 0:36705c54059a 144 #endif