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 10 16:51:59 2018 +0100
Revision:
14:2148b54dfdbf
Child:
19:35408d190f4e
timing

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ibiltari 14:2148b54dfdbf 1 /*
ibiltari 14:2148b54dfdbf 2 Written by Yotam Mann, The Center for New Music and Audio Technologies,
ibiltari 14:2148b54dfdbf 3 University of California, Berkeley. Copyright (c) 2012, 2013, The Regents of
ibiltari 14:2148b54dfdbf 4 the University of California (Regents).
ibiltari 14:2148b54dfdbf 5
ibiltari 14:2148b54dfdbf 6 Permission to use, copy, modify, distribute, and distribute modified versions
ibiltari 14:2148b54dfdbf 7 of this software and its documentation without fee and without a signed
ibiltari 14:2148b54dfdbf 8 licensing agreement, is hereby granted, provided that the above copyright
ibiltari 14:2148b54dfdbf 9 notice, this paragraph and the following two paragraphs appear in all copies,
ibiltari 14:2148b54dfdbf 10 modifications, and distributions.
ibiltari 14:2148b54dfdbf 11
ibiltari 14:2148b54dfdbf 12 IN NO EVENT SHALL REGENTS BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT,
ibiltari 14:2148b54dfdbf 13 SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING LOST PROFITS, ARISING
ibiltari 14:2148b54dfdbf 14 OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF REGENTS HAS
ibiltari 14:2148b54dfdbf 15 BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
ibiltari 14:2148b54dfdbf 16
ibiltari 14:2148b54dfdbf 17 REGENTS SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
ibiltari 14:2148b54dfdbf 18 THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
ibiltari 14:2148b54dfdbf 19 PURPOSE. THE SOFTWARE AND ACCOMPANYING DOCUMENTATION, IF ANY, PROVIDED
ibiltari 14:2148b54dfdbf 20 HEREUNDER IS PROVIDED "AS IS". REGENTS HAS NO OBLIGATION TO PROVIDE
ibiltari 14:2148b54dfdbf 21 MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
ibiltari 14:2148b54dfdbf 22
ibiltari 14:2148b54dfdbf 23
ibiltari 14:2148b54dfdbf 24 */
ibiltari 14:2148b54dfdbf 25
ibiltari 14:2148b54dfdbf 26 #ifndef OSCBUNDLE_h
ibiltari 14:2148b54dfdbf 27 #define OSCBUNDLE_h
ibiltari 14:2148b54dfdbf 28
ibiltari 14:2148b54dfdbf 29 #include "OSCMessage.h"
ibiltari 14:2148b54dfdbf 30 #include "OSCTiming.h"
ibiltari 14:2148b54dfdbf 31
ibiltari 14:2148b54dfdbf 32
ibiltari 14:2148b54dfdbf 33 extern osctime_t zerotime;
ibiltari 14:2148b54dfdbf 34 class OSCBundle
ibiltari 14:2148b54dfdbf 35 {
ibiltari 14:2148b54dfdbf 36
ibiltari 14:2148b54dfdbf 37 private:
ibiltari 14:2148b54dfdbf 38
ibiltari 14:2148b54dfdbf 39 /*=============================================================================
ibiltari 14:2148b54dfdbf 40 PRIVATE VARIABLES
ibiltari 14:2148b54dfdbf 41 =============================================================================*/
ibiltari 14:2148b54dfdbf 42
ibiltari 14:2148b54dfdbf 43 //the array of messages contained in the bundle
ibiltari 14:2148b54dfdbf 44 OSCMessage ** messages;
ibiltari 14:2148b54dfdbf 45
ibiltari 14:2148b54dfdbf 46 //the number of messages in the array
ibiltari 14:2148b54dfdbf 47 int numMessages;
ibiltari 14:2148b54dfdbf 48
ibiltari 14:2148b54dfdbf 49 osctime_t timetag;
ibiltari 14:2148b54dfdbf 50
ibiltari 14:2148b54dfdbf 51 //error codes
ibiltari 14:2148b54dfdbf 52 OSCErrorCode error;
ibiltari 14:2148b54dfdbf 53
ibiltari 14:2148b54dfdbf 54 /*=============================================================================
ibiltari 14:2148b54dfdbf 55 DECODING INCOMING BYTES
ibiltari 14:2148b54dfdbf 56 =============================================================================*/
ibiltari 14:2148b54dfdbf 57
ibiltari 14:2148b54dfdbf 58 //the decoding states for incoming bytes
ibiltari 14:2148b54dfdbf 59 enum DecodeState {
ibiltari 14:2148b54dfdbf 60 STANDBY,
ibiltari 14:2148b54dfdbf 61 HEADER,
ibiltari 14:2148b54dfdbf 62 TIMETAG,
ibiltari 14:2148b54dfdbf 63 MESSAGE_SIZE,
ibiltari 14:2148b54dfdbf 64 MESSAGE,
ibiltari 14:2148b54dfdbf 65 } decodeState;
ibiltari 14:2148b54dfdbf 66
ibiltari 14:2148b54dfdbf 67 //stores incoming bytes until they can be decoded
ibiltari 14:2148b54dfdbf 68 uint8_t * incomingBuffer;
ibiltari 14:2148b54dfdbf 69 int incomingBufferSize;
ibiltari 14:2148b54dfdbf 70
ibiltari 14:2148b54dfdbf 71 //the size of the incoming message
ibiltari 14:2148b54dfdbf 72 int incomingMessageSize;
ibiltari 14:2148b54dfdbf 73
ibiltari 14:2148b54dfdbf 74 //adds a byte to the buffer
ibiltari 14:2148b54dfdbf 75 void addToIncomingBuffer(uint8_t);
ibiltari 14:2148b54dfdbf 76 //clears the incoming buffer
ibiltari 14:2148b54dfdbf 77 void clearIncomingBuffer();
ibiltari 14:2148b54dfdbf 78
ibiltari 14:2148b54dfdbf 79 //decoding functions
ibiltari 14:2148b54dfdbf 80 void decode(uint8_t);
ibiltari 14:2148b54dfdbf 81 void decodeTimetag();
ibiltari 14:2148b54dfdbf 82 void decodeHeader();
ibiltari 14:2148b54dfdbf 83 void decodeMessage(uint8_t);
ibiltari 14:2148b54dfdbf 84
ibiltari 14:2148b54dfdbf 85 //just a placeholder while filling
ibiltari 14:2148b54dfdbf 86 OSCMessage & add();
ibiltari 14:2148b54dfdbf 87
ibiltari 14:2148b54dfdbf 88
ibiltari 14:2148b54dfdbf 89 public:
ibiltari 14:2148b54dfdbf 90
ibiltari 14:2148b54dfdbf 91 /*=============================================================================
ibiltari 14:2148b54dfdbf 92 CONSTRUCTORS / DESTRUCTOR
ibiltari 14:2148b54dfdbf 93 =============================================================================*/
ibiltari 14:2148b54dfdbf 94
ibiltari 14:2148b54dfdbf 95 //default timetag of
ibiltari 14:2148b54dfdbf 96 OSCBundle(osctime_t = zerotime);
ibiltari 14:2148b54dfdbf 97
ibiltari 14:2148b54dfdbf 98 //DESTRUCTOR
ibiltari 14:2148b54dfdbf 99 ~OSCBundle();
ibiltari 14:2148b54dfdbf 100
ibiltari 14:2148b54dfdbf 101 //clears all of the OSCMessages inside
ibiltari 14:2148b54dfdbf 102 OSCBundle& empty();
ibiltari 14:2148b54dfdbf 103
ibiltari 14:2148b54dfdbf 104 /*=============================================================================
ibiltari 14:2148b54dfdbf 105 SETTERS
ibiltari 14:2148b54dfdbf 106 =============================================================================*/
ibiltari 14:2148b54dfdbf 107
ibiltari 14:2148b54dfdbf 108 //start a new OSC Message in the bundle
ibiltari 14:2148b54dfdbf 109 OSCMessage & add(const char * address);
ibiltari 14:2148b54dfdbf 110 //add with nothing in it produces an invalid osc message
ibiltari 14:2148b54dfdbf 111 //copies an existing message into the bundle
ibiltari 14:2148b54dfdbf 112 OSCMessage & add(OSCMessage & msg);
ibiltari 14:2148b54dfdbf 113
ibiltari 14:2148b54dfdbf 114
ibiltari 14:2148b54dfdbf 115
ibiltari 14:2148b54dfdbf 116 template <typename T>
ibiltari 14:2148b54dfdbf 117 OSCBundle& setTimetag(T t){
ibiltari 14:2148b54dfdbf 118 timetag = (osctime_t) t;
ibiltari 14:2148b54dfdbf 119 return *this;
ibiltari 14:2148b54dfdbf 120 }
ibiltari 14:2148b54dfdbf 121 //sets the timetag from a buffer
ibiltari 14:2148b54dfdbf 122 OSCBundle& setTimetag(uint8_t * buff){
ibiltari 14:2148b54dfdbf 123 memcpy(&timetag, buff, 8);
ibiltari 14:2148b54dfdbf 124 return *this;
ibiltari 14:2148b54dfdbf 125 }
ibiltari 14:2148b54dfdbf 126
ibiltari 14:2148b54dfdbf 127 /*=============================================================================
ibiltari 14:2148b54dfdbf 128 GETTERS
ibiltari 14:2148b54dfdbf 129 =============================================================================*/
ibiltari 14:2148b54dfdbf 130
ibiltari 14:2148b54dfdbf 131 //gets the message the matches the address string
ibiltari 14:2148b54dfdbf 132 //will do regex matching
ibiltari 14:2148b54dfdbf 133 OSCMessage * getOSCMessage(char * addr);
ibiltari 14:2148b54dfdbf 134
ibiltari 14:2148b54dfdbf 135 //get message by position
ibiltari 14:2148b54dfdbf 136 OSCMessage * getOSCMessage(int position);
ibiltari 14:2148b54dfdbf 137
ibiltari 14:2148b54dfdbf 138 /*=============================================================================
ibiltari 14:2148b54dfdbf 139 MATCHING
ibiltari 14:2148b54dfdbf 140 =============================================================================*/
ibiltari 14:2148b54dfdbf 141
ibiltari 14:2148b54dfdbf 142 //if the bundle contains a message that matches the pattern,
ibiltari 14:2148b54dfdbf 143 //call the function callback on that message
ibiltari 14:2148b54dfdbf 144 bool dispatch(const char * pattern, void (*callback)(OSCMessage&), int = 0);
ibiltari 14:2148b54dfdbf 145
ibiltari 14:2148b54dfdbf 146 //like dispatch, but allows for partial matches
ibiltari 14:2148b54dfdbf 147 //the address match offset is sent as an argument to the callback
ibiltari 14:2148b54dfdbf 148 bool route(const char * pattern, void (*callback)(OSCMessage&, int), int = 0);
ibiltari 14:2148b54dfdbf 149
ibiltari 14:2148b54dfdbf 150 /*=============================================================================
ibiltari 14:2148b54dfdbf 151 SIZE
ibiltari 14:2148b54dfdbf 152 =============================================================================*/
ibiltari 14:2148b54dfdbf 153 //returns the number of messages in the bundle;
ibiltari 14:2148b54dfdbf 154 int size();
ibiltari 14:2148b54dfdbf 155
ibiltari 14:2148b54dfdbf 156 /*=============================================================================
ibiltari 14:2148b54dfdbf 157 ERROR
ibiltari 14:2148b54dfdbf 158 =============================================================================*/
ibiltari 14:2148b54dfdbf 159
ibiltari 14:2148b54dfdbf 160 bool hasError();
ibiltari 14:2148b54dfdbf 161
ibiltari 14:2148b54dfdbf 162 OSCErrorCode getError();
ibiltari 14:2148b54dfdbf 163
ibiltari 14:2148b54dfdbf 164 /*=============================================================================
ibiltari 14:2148b54dfdbf 165 SENDING
ibiltari 14:2148b54dfdbf 166 =============================================================================*/
ibiltari 14:2148b54dfdbf 167
ibiltari 14:2148b54dfdbf 168 OSCBundle& send(UDPSocket &p, const char *host, uint16_t port);
ibiltari 14:2148b54dfdbf 169
ibiltari 14:2148b54dfdbf 170 /*=============================================================================
ibiltari 14:2148b54dfdbf 171 FILLING
ibiltari 14:2148b54dfdbf 172 =============================================================================*/
ibiltari 14:2148b54dfdbf 173
ibiltari 14:2148b54dfdbf 174 OSCBundle& fill(uint8_t incomingByte);
ibiltari 14:2148b54dfdbf 175
ibiltari 14:2148b54dfdbf 176 OSCBundle& fill(const uint8_t * incomingBytes, int length);
ibiltari 14:2148b54dfdbf 177 };
ibiltari 14:2148b54dfdbf 178
ibiltari 14:2148b54dfdbf 179 #endif