Jules Thuillier / Mbed 2 deprecated OldRemote

Dependencies:   BLE_API mbed nRF51822

Files at this revision

API Documentation at this revision

Comitter:
JulesThuillier
Date:
Mon Feb 09 16:25:56 2015 +0000
Parent:
2:61e196a0eaf2
Child:
4:688a955562ad
Commit message:
parsing send

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/main.cpp	Mon Feb 09 07:21:43 2015 +0000
+++ b/main.cpp	Mon Feb 09 16:25:56 2015 +0000
@@ -39,9 +39,6 @@
 enum States {DISCONNECTED, OFF, ADVERTISING, CONNECTED};
 States currentState = DISCONNECTED;
 
-enum commands {RECORD=0, SEND=1, SENDSIZE=2, REPEAT=3};
-
-
 // Timer pour connaitre la duree de l'enfoncement
 Timer timer;
 Timer timerUp;
@@ -57,7 +54,9 @@
 uint8_t indexi = 0, indexj = 0;
 
 uint16_t sendBuffer [100];
-uint8_t sendBufferSize = 0;
+uint8_t sendBufferIndex = 0;
+
+bool receivingDatas = false;
 
 // Convertit un tableau de char en int
 int char_to_int(char *data)
@@ -74,9 +73,9 @@
  * every odd index : duration of HIGH
  * every even index : duration of LOW
  */
-void sendIR(uint16_t *data, uint8_t size)
+void sendIR(uint16_t *data)
 {
-
+    uint8_t size = data[0];
     // Start at index 1, as index 0 is buffer length
     for(int i=1; i<size; i++)
     {
@@ -102,7 +101,18 @@
  */
 void repeat()
 {
-    sendIR(sendBuffer, sendBufferSize);    
+    if(sendBuffer[0] != 0)
+    {
+        sendIR(sendBuffer); 
+    }   
+}
+
+void prepareForReception()
+{
+    for(int i=0; i<100; i++)
+    {
+     sendBuffer[i] = 0;   
+    }    
 }
 
 
@@ -254,6 +264,7 @@
         uint16_t bytesRead = params->len;
 
         DEBUG("Data received : %*s\n\r", params->len, params->data);
+        DEBUG("Data length : %d\n\r", bytesRead);
         
         // get all the data in one array
         uint8_t  buf[bytesRead];
@@ -286,11 +297,57 @@
             
         }
         
+        // Get ready to receive the datas to send
+        if(strncmp(option, "send", split) == 0)
+        {   
+            
+            prepareForReception();
+            sendBuffer[0] = char_to_int(arg); 
+            receivingDatas = true;  
+            DEBUG("Receiving datas\r\n");
+        }
+        
+        
         // Set the frequency
-        if(strcmp(option, "setfreq") == 0)
+        else if(strncmp(option, "setfreq", split) == 0)
         {   
             setFrequency(char_to_int(arg));    
         }
+        
+        // Repeat the last emission
+        else if(strncmp(option, "repeat", split) == 0)
+        {   
+            repeat();    
+        }
+        
+        
+        else if(receivingDatas)
+        {  
+            int duration = char_to_int(option);
+            DEBUG("data : %u\r\n", duration);
+            
+            if(duration != 0)
+            {
+                sendBufferIndex++;
+                sendBuffer[sendBufferIndex] = duration;
+                
+                if(sendBufferIndex == sendBuffer[0]-1)
+                {
+                    DEBUG("Reception Over\r\n"); 
+                    receivingDatas = false;    
+                }
+            }   
+            else {
+                sendUART("FAIL:send", 9);
+                DEBUG("FAIL:send\r\n"); 
+                receivingDatas = false;   
+ 
+            }
+        }
+        else
+        {
+            DEBUG("UNPARSED : %s\r\n", option);
+        }
     }    
 }