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
Dependencies: NetServices mbed
Diff: mbedOSC.cpp
- Revision:
- 1:ab7dc9550de6
- Parent:
- 0:49cdaebd52d5
--- a/mbedOSC.cpp Tue Mar 13 22:42:25 2012 +0000 +++ b/mbedOSC.cpp Sun Apr 15 12:06:01 2012 +0000 @@ -11,29 +11,17 @@ // host=new Host(IpAddr(10, 0, 0, 1), DEFAULT_RECEIVE_PORT, NULL); } -/* - Set PortNo for the OSC Message - @param[in] _port PortNo (unsigned int) - @return None - */ void OSCMessage::setPort(uint16_t _port){ host.setPort(_port); } -/* - Set IP Address of the OSC Message (for SENDING messages - for receiving this will be done when receiving something ) - param[in] <-- _ip pointer of IP Address array (byte *) - Example: IP=192.168.0.99, then we have to do: ip[]={192,168,0,1}, then setIp(ip) - */ + void OSCMessage::setIp(uint8_t *_ip){ host.setIp(IpAddr(_ip[0], _ip[1], _ip[2], _ip[3])); } -/*! - Set IP Address to the OSC Message container (not through pointer) - Example: IP=192.168.0.99 => setIp(192,168,0,99) - */ + void OSCMessage::setIp( uint8_t _ip1, uint8_t _ip2, uint8_t _ip3, @@ -52,45 +40,20 @@ } -/* - Gets the number of the OSC message address - param[in] None - return number of the OSC message address (byte) - Examples: "/ard" --> the number of the addresses is 1 - "/ard/test" --> the number of the addresses is 2 - Attention: the maximum number of addresses is 2 (MAX_ADDRESS) -*/ + uint8_t OSCMessage::getAddressNum(){ return addressNum; } -/* - Gets the number of the OSC message args - param[in] None - return number of the args (byte) - Example: "i" 123 --> number of the OSC message args is 1 - "if" 123 54.24 --> number of the OSC message args is 2 - Attention: the maximum number of args is 2 (MAX_ARG) - */ uint8_t OSCMessage::getArgNum(){ return argNum; } -/* - Gets the address string of the OSC message - param [in] <-- _index is the index of the address string (byte) - return pointer of the address string (char *) - @note ex. "/ard/test"<br> - getAddress(0) = "/ard"<br> - getAddress(1) = "/test" - @attention It is maximum number of the addresses is 2<br> - In this case "/ard/test1/test2"<br> - ignore it after "/test2" - */ + char * OSCMessage::getAddress(uint8_t _index){ if(_index>MAX_ADDRESS) _index=MAX_ADDRESS-1; return address[_index]; @@ -98,50 +61,27 @@ } -/* - Gets the TopAddress string of the OSC message (this is just the address with index 0) - param[in] None - return pointer of the TopAddress string (char *), i.e. address[0] - Example: In the case "/ard/test", getTopAddress() = "/ard" (WITH the slash "/") - */ + char * OSCMessage::getTopAddress(){ return getAddress(0); } -/* - Gets the "SubAddress" string of the OSC message (this is just the address with index 1) - param[in] None - return pointer of the SubAddress string (char *), i.e. address[1] - Example: in the case "/ard/test", getSubAddress() = "/test" (WITH the slash "/") - */ + char * OSCMessage::getSubAddress(){ return getAddress(1); } -/* - Gets the TypeTag string (with index) of the OSC message - param[in] <--_index is the index of the TypeTag string (byte) - return: TypeTag char (char) - Example: in the case of a total typetag string equal to "if", getTypeTag(0) = 'i' and getTypeTag(1) = 'f' - Attention: MAX_ARG is maximum number of the args, if the index argument is larger, it will be constrained to this max. - */ + char OSCMessage::getTypeTag(uint8_t _index){ if(_index>MAX_ARG) _index=MAX_ARG-1; return typeTag[_index]; } -/* - Get the args of the OSC message with an integer value - param[in] <--_index is (an int, or uint8_t), corresponding to the index of the args (byte) - return: integer value (long, or int32_t) - Example: in the case "if" 123 54.24, getArgInt(0) = 123 - Noe: "i" is integer, but the return type is "long" - Note: When a index is bigger than the number of the args, it is set to the number of the args - */ + int32_t OSCMessage::getArgInt(uint8_t _index){ int32_t *value; if(_index > argNum) _index=argNum; @@ -149,14 +89,7 @@ return *value; } -/* - Get the args of the OSC message with a float value - param[in] <--_index is the index of the args - return: float value (double) - note: In this case "if" 123 54.24, getArgFloat(1) = 54.24 - attention: arg declared as float, but return value cast as "double" - attention: When index is bigger than the number of the args, it is set to the number of the args - */ + double OSCMessage::getArgFloat(uint8_t _index){ double *value; if(_index > argNum) _index=argNum; @@ -164,48 +97,28 @@ return *value; } -/* - Set TopAddress string of OSC Message - param[in] <-- _address is a string pointer for the TopAddress String (char *). NOTE: is this a good idea? why not pass as const, and do allocation here? - return: None - Example: if the complete address string is "/ard/test", we set the topaddress as follows: char top[]="/ard" (allocation done here!), then setTopAddress(top) - */ + void OSCMessage::setTopAddress(char *_address){ address[0]=_address; address[1]=0; addressNum=1; // Note: this "erases" the subaddress! (is this a good idea?) } -/* - Set SubAddress string of the OSC Message - param[in] <-- _address is a string pointer for the SubAddress String (char *) - return: None - Example: if the complete address string is "/ard/test", we set the subaddress as follows: char sub[]="/test" (allocation done here!), then setSubAddress(sub) - Attention: we should call first setTopAddress, and then setSubAddress. The order is important. This does not seems like a good idea... - */ + void OSCMessage::setSubAddress(char *_address){ address[1]=_address; addressNum=2; // Note: this assumes the top address was already set! } -/* - Set the complete Address string of the OSC Message (top and sub addresses) - param[in] <-- _topAddress and _subAddress are the string pointers to top and sub addresses (char *) - return: None - Example: in the case "/ard/test", we need to do: char top[]="/ard", char sub[]="/test", and then setAddress(top,sub) - Reminder: in this implementation, the maximum number of addresses is MAX_ADDRESS=2 - */ + void OSCMessage::setAddress(char *_topAddress,char *_subAddress){ setTopAddress(_topAddress); setSubAddress(_subAddress); addressNum=2; // (unnecessary...) } -/* - Set address string using index (here 0 or 1) - Example: "/ard/test", char adr[]="/ard", setAddress(0,adr), char adr2[]="/test", setAddress(1,adr) - */ + void OSCMessage::setAddress(uint8_t _index, char *_address){ if(_index>MAX_ADDRESS) _index=MAX_ADDRESS-1; address[_index]=_address; @@ -213,19 +126,7 @@ } -/* - Set TypeTag and args to the OSC Message container - @param[in] types TypeTag string "i"(integer) or"f"(float) (char *) - @param[in] ... Pointer of the Args(variable argument) .. - @return None - @Example: - (1) integer 123: (NOTE: integers are LONG) - long v1=123; sendMes.setArgs("i",&v1) - (2)integer:123 and float:52.14 - long v1=123; double v2=52.14; sendMes.setArgs("if",&v1,&v2) - Attention: in this implementation, the maximum number of the args is 2 - (if setArgs("iff",&v1,&v2,&v3), data is ignored after &v3) - */ + void OSCMessage::setArgs(char *types,...){ va_list argList; @@ -261,32 +162,19 @@ newMessage=false; } -/* - This sets "binds" the received message to the receiver container of the communication object - param[in]<--_mes is a pointer to the "receiveing" OSC message (OSCMessage *) - */ OSCClass::OSCClass(OSCMessage *_mes){ udpRec.setOnEvent(this, &OSCClass::onUDPSocketEvent); receiverMessage = _mes; // note: receiverMessage MUST be a pointer to the message, because we will modify things in it newMessage=false; } -/* - This initializes the OSC communication object with default receiving port (DEFAULT_REC_PORT) - param[in]: None - return: None - */ void OSCClass::begin() { // setup receiver udp socket: udpRec.bind(receiverMessage->host); } -/* - Initialize an OSC object with arbitrary listening port - param[in] <-- _recievePort, is the listening ("receiving") Port No (unsigned int) - return: None - */ + void OSCClass::begin(uint16_t _recievePort) { receiverMessage->host.setPort(_recievePort); @@ -294,18 +182,11 @@ udpRec.bind(receiverMessage->host); } -/* - Set a OSC receive message container - param[in] _mes Pointer to the OSC receive message container (OSCMessage *) - return None - */ + void OSCClass::setReceiveMessage(OSCMessage *_mes){ receiverMessage = _mes; } -/* - callback function when an upd message arrives (it will be transformed as OSC message) - */ void OSCClass::onUDPSocketEvent(UDPSocketEvent e) { switch(e) @@ -440,21 +321,13 @@ } -/* - Get the received OSC message (note: this is another way to access the message directly from the OSCClass object). - The advantage is that we will signal that we read the message, and will be able to query if a NEW message arrived - (Alternatively, one could have a function pointer to pass to the OSC object, that will be called each time a new packet is received: TO DO) - */ + OSCMessage * OSCClass::getMessage(){ newMessage=false; // this indicate the user READ the message return receiverMessage; } -/* - Send an OSC Message (message contain the host ip and port where the message data has to be sent) - param[in] _mes Pointer to the OSC message container (OSCMessage *) - return None - */ + void OSCClass::sendOsc( OSCMessage *_mes ) { uint8_t lengthEnd; @@ -521,9 +394,7 @@ } */ -/* - Stop OSC communication (in fact, only the receiver - the server side) - */ + void OSCClass::stop() { //close( socketNo ); udpSend.resetOnEvent(); // disables callback