Problematique

Dependencies:   mbed mbed-rtos

Revision:
2:db7c8378b324
Parent:
1:261bb31628e8
Child:
3:65845faafbb1
--- a/main.cpp	Mon Feb 10 15:47:25 2014 +0000
+++ b/main.cpp	Tue Feb 11 15:15:23 2014 +0000
@@ -1,48 +1,217 @@
 #include "mbed.h"
 #include "rtos.h"
-#include <bitset>
+#include "trame.hpp"
 
 Serial pc(USBTX, USBRX);
 
-//const bool preambule[8] = {false, true, false, true, false, true, false, true};
-//const bool start_end[8] = {false, true, true, true, true, true, true, false};
+int tickPerPeriod = 9600;
+
+Queue<string, 16> textToSend;
+Mail<trame, 16> trameToSend;
+
+Mail<trame, 16> trameToDecode;
+Queue<string, 16> textToPrint;
+
+void getText(void const *args)
+{
+    int compteur = 0;
+    char c;
+    string message = "";
+    
+    while (true)
+    {
+        c = pc.getc();
+        
+        if (c == 0xD)
+        {
+            if (!message.empty())
+            {
+                pc.putc(0xA);
+                pc.putc(0xD);
+                textToSend.put(new string(message));
+                message = "";
+                compteur = 0;
+            }
+        }
+        else if (c == 0x8)
+        {
+            pc.putc(c);
+            pc.putc(0x20);
+            pc.putc(c);
+            message = message.substr(0, message.length() - 1);
+            compteur--;
+        }
+        else
+        {
+            if (compteur < 80)
+            {
+                pc.putc(c);
+                message += c;
+                compteur++;
+            }
+        }
+    }
+}
+
+void leTrameur(void const *args)
+{
+    while (true)
+    {
+        osEvent evt = textToSend.get();
+
+        if (evt.status == osEventMessage) {
+            string *msg = (string*)evt.value.p;
+            
+            //pc.printf("message envoye :\n\r%s\n\r", msg->c_str());
+            trameToSend.put(new trame(*msg));
+            
+            delete msg;
+        }
+    }
+}
 
-bool Trame[16] = {false, true, false, true, true, true, true, false, false, false, true, false, false, true, true, true};
+void setPwmMatch(bitset<8> bitst)
+{
+    for (int a = 0; a < 8; a++)
+    {
+        if (bitst.test(a))
+        {
+            LPC_PWM1->MR1 = tickPerPeriod/2;
+            LPC_PWM1->MR2 = 1;
+        }
+        else
+        {
+            LPC_PWM1->MR1 = 1;
+            LPC_PWM1->MR2 = tickPerPeriod/2;
+        }
+        
+        while(LPC_PWM1->IR != 0x01);
+        LPC_PWM1->IR = 0xFF;
+    }
+}
+
+void sendTrame(trame *trm)
+{
+    LPC_PWM1->TCR |= (1 << 0);  // Enable counter
+    
+    setPwmMatch(trm->preambule);
+    setPwmMatch(trm->start_end);
+    setPwmMatch(trm->type_flag);
+    setPwmMatch(trm->charge_utile);
+    
+    for (int a = 0; a < trm->length; a++)
+    {
+        if (trm->message.test(a))
+        {
+            LPC_PWM1->MR1 = tickPerPeriod/2;
+            LPC_PWM1->MR2 = 1;
+        }
+        else
+        {
+            LPC_PWM1->MR1 = 1;
+            LPC_PWM1->MR2 = tickPerPeriod/2;
+        }
+        
+        while(LPC_PWM1->IR != 0x01);
+        LPC_PWM1->IR = 0xFF;
+    }
+    
+    for (int a = 0; a < 16; a++)
+    {
+        if (trm->crc16.test(a))
+        {
+            LPC_PWM1->MR1 = tickPerPeriod/2;
+            LPC_PWM1->MR2 = 1;
+        }
+        else
+        {
+            LPC_PWM1->MR1 = 1;
+            LPC_PWM1->MR2 = tickPerPeriod/2;
+        }
+        
+        while(LPC_PWM1->IR != 0x01);
+        LPC_PWM1->IR = 0xFF;
+    }
+    
+    setPwmMatch(trm->start_end);
+    
+    LPC_PWM1->TCR = 0x0;    // Disable counter
+}
+
+void sender(void const *args)
+{
+    while (true)
+    {
+        osEvent evt = trameToSend.get();
+
+        if (evt.status == osEventMail) {
+            trame *trm = (trame*)evt.value.p;
+
+            pc.printf("trame shooter :\n\r%s", trm->trameToString().c_str());
+            sendTrame(trm);
+            
+            trameToSend.free(trm);
+        }
+    }
+}
 
 void readTrame()
 {
-    pc.printf("Duree impulsion : %d microsecondes.\n\r", LPC_TIM2->CR1/(SystemCoreClock/1000000));
+    //LPC_TIM2->IR = 0xFF;
+}
 
-    LPC_TIM2->IR = 0xFF;
+void bla()
+{
+    string temp = "01010101011111100000000000000011011000110110011000100000011001101101101001111110";
+    bitset<696> bit(temp);
+    
+    trame *trm = new trame(bit);
+    if (trm->checkCRC16())
+    {
+        //pc.printf("trame recu :\n\r%s\n\r", trm->trameToString().c_str());
+        trameToDecode.put(trm);
+    }
+    else
+    {
+        pc.printf("Mauvais CRC16");
+    }
+}
+
+void receiver(void const *args)
+{
+    bla();
 }
 
-void sendTrame(bool *trame)
+void decoder(void const *args)
 {
-    for (int a = 0; a < 5; a++)
+    while(true)
     {
-        if (trame[a])
-        {
-            LPC_PWM1->MR1 = 1;
-            LPC_PWM1->MR2 = SystemCoreClock/2;
+        osEvent evt = trameToDecode.get();
+
+        if (evt.status == osEventMail) {
+            trame *trm = (trame*)evt.value.p;
+
+            textToPrint.put(new string(trm->text));
+            
+            trameToSend.free(trm);
         }
-        else
-        {
-            LPC_PWM1->MR1 = SystemCoreClock/2;
-            LPC_PWM1->MR2 = 1;
-        }
-        
-        pc.printf("trame: %d\n\r", trame[a]);
-        
-        while(LPC_PWM1->IR != 0x01);
-        
-        LPC_PWM1->IR = 0xFF;
     }
-    
-    LPC_PWM1->TCR = 0x0;
 }
 
-void send(void const *args) {
-    sendTrame(Trame);
+void printer(void const *args)
+{
+    while (true)
+    {
+        osEvent evt = textToPrint.get();
+
+        if (evt.status == osEventMessage) {
+            string *msg = (string*)evt.value.p;
+            
+            pc.printf("Sender says :\n\r%s\n\r", msg->c_str());
+            
+            delete msg;
+        }
+    }
 }
 
 void initialize()
@@ -74,9 +243,8 @@
     LPC_PWM1->MCR |= (1 << 1) | (1 << 0);   // Initialize Match Control Register Interrupt/Reset
     LPC_PWM1->PCR |= (1 << 10) | (1 << 2);  // Initialize PWM Control Register Output/Double-edge
     
-    LPC_PWM1->MR0 = SystemCoreClock;        // Period
+    LPC_PWM1->MR0 = tickPerPeriod;        // Period
     LPC_PWM1->LER |= (1 << 2) | (1 << 1);   // Initialize Latch Enable Register
-    LPC_PWM1->TCR |= (1 << 0);              // Enable counter
     
     LPC_PWM1->IR = 0xFF; // Reset Interrupt Registe
 }
@@ -85,21 +253,18 @@
 {
     initialize();
     
-    Thread thread1(send);
-    //thread1.set_priority(osPriorityHigh);
+    Thread thread1(getText);
+    //thread1.set_priority(osPriorityAboveNormal);
+    Thread thread2(leTrameur);
+    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);
     
-    while(true) {
-        while (LPC_TIM2->IR != 0x0);
-        pc.printf("borne 1\n\r");
-        
-        LPC_TIM2->IR = 0xFF;
-        pc.printf("borne 2\n\r");
-        NVIC_EnableIRQ(TIMER2_IRQn);
-        pc.printf("borne 3\n\r");
-        NVIC_DisableIRQ(TIMER2_IRQn);
-        pc.printf("borne 4\n\r");
-        //NVIC_DisableIRQ(TIMER2_IRQn);
-        
-        pc.printf("borne 5\n\r");
-    }
+    while(true);
 }