support OSC-string

Dependents:   OSCtoCVConverter

Fork of OSC by Toby Harris

Revision:
5:5d585d5107da
Parent:
4:601f6a1141fb
Child:
6:a47004fb44f5
--- a/mbedOSC.cpp	Sat Jan 09 10:54:31 2016 +0000
+++ b/mbedOSC.cpp	Tue Feb 16 11:30:55 2016 +0000
@@ -40,8 +40,7 @@
     
     host.setIp(IpAddr(_ip[0], _ip[1], _ip[2], _ip[3]));
 }
- 
- 
+
  
 void OSCMessage::setIp( uint8_t _ip1,
                         uint8_t _ip2,
@@ -155,7 +154,6 @@
 }
  
  
- 
 void OSCMessage::setAddress(char *_topAddress,char *_subAddress) {
     
     setTopAddress(_topAddress);
@@ -197,17 +195,25 @@
         typeTag[i] = types[i];
         
         switch(types[i]) {
+            
+            case 's':
+                args[i].s = va_arg(argList, char *);
+                break;
  
             case 'i':
-                arg[i] = va_arg(argList, int *);
+                args[i].i = va_arg(argList, uint32_t);
                 break;
  
-            case 'f':
-                arg[i] = va_arg(argList, float *);
+            case 'f': // variadic function float is promoted to double
+                
+                args[i].f = (va_arg(argList, double));
                 break;
- 
-            case 's':
-                arg[i] = va_arg(argList, char *);
+            
+            case 'd':
+                args[i].d = va_arg(argList, double);
+                break;
+                
+            default:
                 break;
         }   
     }
@@ -396,8 +402,7 @@
     }
 }
  
- 
- 
+
 OSCMessage * OSCClass::getMessage() {
     
     newMessage = false; // this indicate the user READ the message
@@ -407,15 +412,11 @@
  
 void OSCClass::sendOsc( OSCMessage *_mes ) {
     uint8_t i, j = 0;
-    uint8_t lengthEnd;
-    uint8_t lengthStart;    
-    char buff[128];
-    bool strCheck = false;
+    uint8_t lengthStart, lengthEnd;   
+    char buff[128] = {0};
     
     sendContainer = _mes;
     
-    buff[0] = '\0';
-    
     //1) Add name spaces:
     for (i = 0; i < sendContainer->addressNum; ++i) {
         
@@ -440,7 +441,6 @@
     for (i = 0; i < sendContainer->argNum; ++i) {
         
         buff[lengthEnd++] = sendContainer->typeTag[i];
-        
     }
  
     // pad with 0s to align in multiples of 4:
@@ -458,23 +458,9 @@
     
     for (i = 0; i < sendContainer->argNum; ++i) {
         
-        if (sendContainer->typeTag[i] == 's') {
-            
-            strCheck = true;
-        }
-        
-        if (!strCheck) {
-            
-            v = (uint8_t *)&sendContainer->arg[i];
+        if (sendContainer->typeTag[i] == 's') {       // strings
             
-            buff[lengthEnd++] = v[3];
-            buff[lengthEnd++] = v[2];
-            buff[lengthEnd++] = v[1];
-            buff[lengthEnd++] = v[0];
-            
-        } else {
- 
-            v = (uint8_t *)sendContainer->arg[i];
+            v = (uint8_t *)sendContainer->args[i].s;
             
             while (v[j] != '\0') {
                 
@@ -491,8 +477,39 @@
                 buff[i] = '\0';
             }
             
-            strCheck = false;
+        } else if (sendContainer->typeTag[i] == 'd') { // double
+            
+            v = (uint8_t *)&sendContainer->args[i].d;
+            
+            buff[lengthEnd++] = v[7];
+            buff[lengthEnd++] = v[6];
+            buff[lengthEnd++] = v[5];
+            buff[lengthEnd++] = v[4];
+            buff[lengthEnd++] = v[3];
+            buff[lengthEnd++] = v[2];
+            buff[lengthEnd++] = v[1];
+            buff[lengthEnd++] = v[0];
+            
+        }  if (sendContainer->typeTag[i] == 'f') {     // float                                    
+            
+            v = (uint8_t *)&sendContainer->args[i].f;
+            
+            buff[lengthEnd++] = v[3];
+            buff[lengthEnd++] = v[2];
+            buff[lengthEnd++] = v[1];
+            buff[lengthEnd++] = v[0];
+        
+        } else {                                       // int
+            
+            v = (uint8_t *)&sendContainer->args[i].i;
+            
+            buff[lengthEnd++] = v[3];
+            buff[lengthEnd++] = v[2];
+            buff[lengthEnd++] = v[1];
+            buff[lengthEnd++] = v[0];
+            
         }
+
     }
     
     //4) Send udp packet: