Problematique

Dependencies:   mbed mbed-rtos

Revision:
3:65845faafbb1
Parent:
2:db7c8378b324
Child:
4:2af360b178d2
--- a/main.cpp	Tue Feb 11 15:15:23 2014 +0000
+++ b/main.cpp	Tue Feb 11 21:58:34 2014 +0000
@@ -4,14 +4,43 @@
 
 Serial pc(USBTX, USBRX);
 
-int tickPerPeriod = 9600;
+DigitalOut led1(LED1);
+DigitalOut led2(LED2);
+DigitalOut led3(LED3);
+DigitalOut led4(LED4);
+
+const int tickPerPeriod = 960000;
+
+struct message_t
+{
+    char msg[696];
+    
+    /*message_t (char bla[696])
+    {
+        strcpy(msg, bla);
+    }*/
+};
 
 Queue<string, 16> textToSend;
 Mail<trame, 16> trameToSend;
 
+Queue<message_t, 16> messagesQueue;
 Mail<trame, 16> trameToDecode;
 Queue<string, 16> textToPrint;
 
+unsigned long tc_periods[8] = {0};
+unsigned long period = 0;
+bool type_bit = false;
+int synchrone = 0;
+int count = 0;
+const char end[8] = {0, 1, 1, 1, 1, 1, 1, 0};
+
+bool good = true;
+
+char messages[696] = {0};
+message_t msgs;
+const unsigned long offset = 5000;
+
 void getText(void const *args)
 {
     int compteur = 0;
@@ -92,6 +121,7 @@
 
 void sendTrame(trame *trm)
 {
+    LPC_TIM2->TC = 0;
     LPC_PWM1->TCR |= (1 << 0);  // Enable counter
     
     setPwmMatch(trm->preambule);
@@ -155,31 +185,122 @@
     }
 }
 
-void readTrame()
-{
-    //LPC_TIM2->IR = 0xFF;
-}
-
-void bla()
+void pushTrame(void const *args)
 {
-    string temp = "01010101011111100000000000000011011000110110011000100000011001101101101001111110";
-    bitset<696> bit(temp);
-    
-    trame *trm = new trame(bit);
-    if (trm->checkCRC16())
+    while (true)
     {
-        //pc.printf("trame recu :\n\r%s\n\r", trm->trameToString().c_str());
-        trameToDecode.put(trm);
-    }
-    else
-    {
-        pc.printf("Mauvais CRC16");
+        osEvent evt = messagesQueue.get();
+
+        if (evt.status == osEventMessage) {
+            message_t *msg = (message_t*)evt.value.p;
+            
+            bitset<696> bit(string(const_cast<const char*>(msg->msg)));
+            trame *trm = new trame(bit);
+            
+            pc.printf("trame recu :\n\r%s\n\r", trm->trameToString().c_str());
+            if (trm->checkCRC16())
+            {
+                trameToDecode.put(trm);
+            }
+            else
+            {
+                for (int a=0;a < 8; a++)
+                {
+                    pc.printf("tc_periods: %d\n\r", tc_periods[a]);
+                }
+                pc.printf("period: %d\n\r", period);
+                pc.printf("Mauvais CRC16\n\r");
+            }
+        }
     }
 }
 
-void receiver(void const *args)
+void readTrame()
 {
-    bla();
+    if (synchrone < 8)
+    {
+        tc_periods[synchrone] = LPC_TIM2->CR1;
+        synchrone++;
+        
+        if (synchrone == 8)
+        {
+            for (int i = 0; i < 8; i++)
+            {
+               period += tc_periods[i]; 
+            }
+            
+            period = period/8;
+        }
+    }
+    else
+    {   
+        unsigned long tc_count = LPC_TIM2->CR1;
+        if (tc_count > (period*2 - offset) && tc_count < (period*2 + offset))
+        {
+            type_bit = !type_bit;
+            good = true;
+            
+            if (type_bit)
+            {
+                messages[count] = '1';
+                led1 = 1;
+                led2 = 0;
+            }
+            else
+            {
+                messages[count] = '0';
+                led1 = 0;
+                led2 = 1;
+            }
+                
+            count++;
+        }
+        
+        good = !good;
+        if (good)
+        {
+            if (type_bit)
+            {
+                messages[count] = '1';
+                led1 = 1;
+                led2 = 0;
+            }
+            else
+            {
+                messages[count] = '0';
+                led1 = 0;
+                led2 = 1;
+            }
+                
+            count++;
+        }
+    }
+    
+    if (count >= 48)
+    {
+        char temp[8] = {0};
+        for (int a = 0; a < 8; a++)
+        {
+            temp[a] = messages[count-a];
+        }
+        
+        if (strcmp(temp, end) == 0)
+        {
+            count = 0;
+            synchrone = 0;
+            good = true;
+            strcpy(msgs.msg, messages);
+            fill(messages, messages+696, 0);
+            messagesQueue.put(&msgs);
+            LPC_TIM2->TCR |= (1 << 1);  // Reset Timer Control Register
+            LPC_TIM2->TCR = 0x01;
+            led3 = 1;
+        }
+    }
+
+    LPC_TIM2->TC = 0;
+    LPC_TIM2->IR = 0xFF;
+    led4 = 1;
 }
 
 void decoder(void const *args)
@@ -193,7 +314,7 @@
 
             textToPrint.put(new string(trm->text));
             
-            trameToSend.free(trm);
+            trameToDecode.free(trm);
         }
     }
 }
@@ -227,6 +348,7 @@
 
     //Initialize Timer2 for capture
     NVIC_SetVector(TIMER2_IRQn, (uint32_t)&readTrame);
+    NVIC_SetPriority(TIMER2_IRQn, 255);
     NVIC_EnableIRQ(TIMER2_IRQn);
 
     LPC_TIM2->TC = 0;           // Initialize Time Counter
@@ -235,7 +357,7 @@
     LPC_TIM2->TCR |= (1 << 1);  // Reset Timer Control Register
     LPC_TIM2->IR = 0xFF;        // Reset Interrupt Register
     LPC_TIM2->CCR |=  (1 << 5) | (1 << 4) | (1 << 3);   // Initialize Capture Control Register
-    LPC_TIM2->CTCR |= (1 << 0); // TC is incremented on rising edge
+    LPC_TIM2->CTCR = 0x0;
 
     LPC_TIM2->TCR = 0x01;       // Start Timer Control Register
     
@@ -259,12 +381,14 @@
     thread2.set_priority(osPriorityAboveNormal);
     Thread thread3(sender);
     thread3.set_priority(osPriorityHigh);
-    Thread thread4(receiver);
-    thread4.set_priority(osPriorityRealtime);
-    Thread thread5(decoder);
-    thread5.set_priority(osPriorityHigh);
-    Thread thread6(printer);
-    thread6.set_priority(osPriorityAboveNormal);
+    
+    Thread thread(pushTrame);
+    thread.set_priority(osPriorityRealtime);
+    
+    Thread thread4(decoder);
+    thread4.set_priority(osPriorityHigh);
+    Thread thread5(printer);
+    thread5.set_priority(osPriorityAboveNormal);
     
     while(true);
 }