APP 4

Dependencies:   mbed CRC16 mbed-rtos

Revision:
25:a3e06778c54b
Parent:
23:d41a23d8f2d7
--- a/APP.cpp	Tue Feb 23 14:50:48 2016 +0000
+++ b/APP.cpp	Tue Feb 23 15:58:49 2016 +0000
@@ -21,22 +21,25 @@
 MEF mef;
 STATES mefSTATE;
 int payloadSize = 0;
-bool swag = false;
-bool asdf = false;
-int messageLength = 4;
-bool buffer[16];
-int bufferCounter = 0;
+#if DEBUG
+bool debugBitReady = false;
+bool debugBit = false;
+#endif
+int messageLength = 17;
 bool firstBit = true;
 STATES tempState = NOSTATE;
+int sendTime = 0;
+int readTime = 0;
+int mainTime = 0;
 
 int benchmark(void (*function) (void))
 {
-    int count = LPC_TIM2->TC;
+    int count = LPC_TIM1->TC;
     function();
-    return LPC_TIM2->TC - count;
+    return LPC_TIM1->TC - count;
 }
 
-extern "C" void TIMER1_IRQHandler()
+void send()
 {
     if ((LPC_TIM1->IR & 0x01) == 0x01) // if MR0 interrupt, proceed
     {
@@ -57,7 +60,7 @@
     }
 }
 
-extern "C" void TIMER2_IRQHandler()
+void read()
 {
     unsigned int clocks = LPC_TIM2->CR0;
     bool inputValue = in.read();
@@ -71,8 +74,10 @@
     
     if (firstBit)
     {
-        swag = true;
-        asdf = !inputValue;
+        #if DEBUG
+        debugBitReady = true;
+        debugBit = !inputValue;
+        #endif
         mef.ReceiveBit(!inputValue);
         firstBit = false;
     }
@@ -81,9 +86,11 @@
     {
         if (clocks >= period*1.5 || (currentClocks + clocks) >= period*1.5)
         {
+            #if DEBUG
+            debugBitReady = true;
+            debugBit = !inputValue;
+            #endif
             currentClocks = 0;
-            swag = true;
-            asdf = !inputValue;
             mef.ReceiveBit(!inputValue);
         }
         else
@@ -97,6 +104,16 @@
     LPC_TIM2->IR |= 0xFFFFFFFF;     // clear Timer interrupt register
 }
 
+extern "C" void TIMER1_IRQHandler()
+{
+    sendTime = benchmark(send);
+}
+
+extern "C" void TIMER2_IRQHandler()
+{
+    readTime = benchmark(read);
+}
+
 void initTimers()
 {
     //Timer 1 (match)
@@ -123,34 +140,38 @@
     LPC_TIM2->TCR = 1;                      // start Timer
 }
 
+void mainLoop()
+{
+    if (dataReady)
+    {
+        for (int i = 0; i < payloadSize * 8; i++)
+        {
+            if((i + 1) % 8 == 0)
+            {
+                char tempChar = (decodedMessage[i] << 0) | (decodedMessage[i - 1] << 1) | (decodedMessage[i - 2] << 2) | (decodedMessage[i - 3] << 3) | (decodedMessage[i - 4] << 4) | (decodedMessage[i - 5] << 5) | (decodedMessage[i - 6] << 6) | (decodedMessage[i - 7] << 7);
+                pc.printf("%c", tempChar);        
+            }
+        }
+        dataReady = false;
+    }
+
+    #if DEBUG
+    debugPrint();
+    #endif
+    
+    wait(0.01);
+}
+
 int main()
 {
-    message = buildFrame(convertToBits("BLUB", messageLength), messageLength);    
+    message = buildFrame(convertToBits("Bonjour Domingo\r\n", messageLength), messageLength);    
 
     LPC_PINCON->PINSEL0 |= (3 << 8);   // P0.4 = CAP2.0
     initTimers();
 
     while (true)
     {
-        if (dataReady)
-        {
-            for (int i = 0; i < payloadSize * 8; i++)
-            {
-                
-                if((i + 1) % 8 == 0)
-                {
-                    char tempChar = (decodedMessage[i] << 0) | (decodedMessage[i - 1] << 1) | (decodedMessage[i - 2] << 2) | (decodedMessage[i - 3] << 3) | (decodedMessage[i - 4] << 4) | (decodedMessage[i - 5] << 5) | (decodedMessage[i - 6] << 6) | (decodedMessage[i - 7] << 7);
-                    pc.printf("%c", tempChar);        
-                }
-            }
-            dataReady = false;
-        }
-        
-        
-        
-        #if DEBUG
-        debugPrint();
-        #endif
+        mainTime = benchmark(mainLoop);
     }
 }
 
@@ -184,6 +205,7 @@
     debugMessageReady = true;
 }
 
+#if DEBUG
 void debugPrint()
 {
     if (tempState != mefSTATE)
@@ -202,9 +224,15 @@
         debugMessageReady = false;
     }
     
-    if (swag)
+    if (debugBitReady)
     {
-        pc.printf("%i ", asdf);
-        swag = false;
+        pc.printf("%i ", debugBit);
+        debugBitReady = false;
     }
+    
+    #if PROFILE
+    float percent = 100*(readTime / (period/2)) + 100*(sendTime / (CLOCKS_TO_SECOND / 100)) + 100*(mainTime / (CLOCKS_TO_SECOND / 100));
+    pc.printf("Pourcent CPU = %f \r\n", percent);
+    #endif
 }
+#endif