APP 4

Dependencies:   mbed CRC16 mbed-rtos

Revision:
26:f2b37f9dfca9
Parent:
23:d41a23d8f2d7
--- a/APP.cpp	Tue Feb 23 14:50:48 2016 +0000
+++ b/APP.cpp	Tue Feb 23 20:56:47 2016 +0000
@@ -1,54 +1,55 @@
+// Vincent Bélanger et Laurent Mandrile
+// belv1802 - manl2003
+
 #include "APP.h"
 #include "Manchester.h"
 #include "Frame.h"
 #include "MEF.h"
 
+// Interfaces d'entrée-sortie
 Serial pc(USBTX, USBRX);
 DigitalOut out(p8);
 DigitalIn in(p30);
 
-bool clockTick = false;
-bitset<FRAMESIZE> message;
-bitset<MAX_DATA> decodedMessage;
-int debugMessage;
-bool debugMessageReady = false;
-bool dataReady;
-bool frameDropped; 
-int counter = 0;
-unsigned int period = 0;
-unsigned int currentClocks = 0;
-bool periodCalculated = false;
-MEF mef;
-STATES mefSTATE;
-int payloadSize = 0;
-bool swag = false;
-bool asdf = false;
-int messageLength = 4;
-bool buffer[16];
-int bufferCounter = 0;
-bool firstBit = true;
-STATES tempState = NOSTATE;
+// Variables globales
+bitset<FRAMESIZE> frameToSend;         // Contient la trame à envoyer
+bitset<MAX_DATA> decodedFrame;         // Contient la trame reçue (seulement les données utiles)
+int counter = 0;                       // Compteur pour l'envoi de la trame
+int payloadSize = 0;                   // Taille du message reçu (en octets)
+bool clockTick = false;                // Simule une horloge pour l'encodage Manchester
+bool dataReady;                        // Indique si un message complet à été reçu
+bool frameDropped;                     // Indique si un message a été ignoré à cause d'une erreur
+bool firstBit = true;                  // Indique s'il s'agit du premier bit reçu (pour calcul de la période)
 
-int benchmark(void (*function) (void))
-{
-    int count = LPC_TIM2->TC;
-    function();
-    return LPC_TIM2->TC - count;
-}
+bool periodCalculated = false;         // Indique si la période a été calculée
+unsigned int period = 0;               // Contient la période calculée
+unsigned int currentClocks = 0;        // Contient le nombre de coups d'horloge depuis le dernier interrupt
+MEF mef;                               // Machine à états finis
+STATES mefSTATE;                       // État de la MEF
 
+char* message = "Bonjour Domingo\r\n"; // Message à envoyer
+int messageLength = 17;                // Longueur en octets de ce message
+
+#if DEBUG
+bool debugBitReady = false;
+bool debugBit = false;
+STATES debugState = NOSTATE;
+#endif
+
+// Fonction appelée par l'interrupt du Timer1
 extern "C" void TIMER1_IRQHandler()
 {
     if ((LPC_TIM1->IR & 0x01) == 0x01) // if MR0 interrupt, proceed
     {
         clockTick = !clockTick;
-        out = encode(message[counter], clockTick);
+        out = encode(frameToSend[counter], clockTick);  // Encodage Manchester
 
         if (clockTick)
         {
             counter++;
         }
 
-        if (counter >= 56+messageLength*8)
+        if (counter >= 56+messageLength*8)  // Longueur totale, incluant les headers et les données utiles
         {
             counter = 0;
         }
@@ -57,12 +58,13 @@
     }
 }
 
+// Fonction appelée par l'interrupt du Timer2
 extern "C" void TIMER2_IRQHandler()
 {
     unsigned int clocks = LPC_TIM2->CR0;
     bool inputValue = in.read();
 
-    // Discard first bit
+    // Ignorer le premier bit pour la période
     if (!periodCalculated && !firstBit)
     {
         period = clocks / 2;
@@ -71,20 +73,27 @@
     
     if (firstBit)
     {
-        swag = true;
-        asdf = !inputValue;
+        #if DEBUG
+        debugBitReady = true;
+        debugBit = !inputValue;
+        #endif
+        // Envoi à la MEF
         mef.ReceiveBit(!inputValue);
         firstBit = false;
     }
 
     if (periodCalculated)
     {
+        // Si une ou deux périodes se sont écoulées depuis le dernier interrupt
         if (clocks >= period*1.5 || (currentClocks + clocks) >= period*1.5)
         {
+            #if DEBUG
+            debugBitReady = true;
+            debugBit = !inputValue;
+            #endif
+            // Envoi à la MEF
+            mef.ReceiveBit(!inputValue);
             currentClocks = 0;
-            swag = true;
-            asdf = !inputValue;
-            mef.ReceiveBit(!inputValue);
         }
         else
         {
@@ -92,11 +101,11 @@
         }
     }
 
-    LPC_TIM2->TC = 0;
-
+    LPC_TIM2->TC = 0;               // clear Timer counter
     LPC_TIM2->IR |= 0xFFFFFFFF;     // clear Timer interrupt register
 }
 
+// Fonction d'initialisation des Timers
 void initTimers()
 {
     //Timer 1 (match)
@@ -123,49 +132,47 @@
     LPC_TIM2->TCR = 1;                      // start Timer
 }
 
+// Fonction principale
 int main()
 {
-    message = buildFrame(convertToBits("BLUB", messageLength), messageLength);    
+    // Trame à envoyer
+    frameToSend = buildFrame(convertToBits(message, messageLength), messageLength);    
 
-    LPC_PINCON->PINSEL0 |= (3 << 8);   // P0.4 = CAP2.0
+    LPC_PINCON->PINSEL0 |= (3 << 8);   // P0.4 = CAP2.0, correspond à la pin30
     initTimers();
 
     while (true)
     {
         if (dataReady)
         {
+            // Affichage en console des données reçues
             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);        
+                    char ascii = (decodedFrame[i] << 0) | (decodedFrame[i - 1] << 1) | (decodedFrame[i - 2] << 2) | (decodedFrame[i - 3] << 3) | (decodedFrame[i - 4] << 4) | (decodedFrame[i - 5] << 5) | (decodedFrame[i - 6] << 6) | (decodedFrame[i - 7] << 7);
+                    pc.printf("%c", ascii);        
                 }
             }
             dataReady = false;
         }
-        
-        
-        
+
         #if DEBUG
+        // Affichage des informations de debug
         debugPrint();
         #endif
     }
 }
 
-void bSToChar(bitset<8> bs)
-{
-       
-}
-
+// Fonction de callback pour le décodage des messages
 void _decodeCallback(bitset<MAX_DATA> decMessage, int size)
 {
-    decodedMessage = decMessage;
+    decodedFrame = decMessage;
     payloadSize = size;
     dataReady = true;
 }
 
+// Fonction de callback dans le cas d'une erreur de trame
 void _decodeError()
 {
     frameDropped = true;
@@ -173,23 +180,19 @@
     period = 0;
 }
 
+// Fonction de mise à jour de l'état de la MEF
 void _updateState(STATES state)
 {
     mefSTATE = state;
 }
 
-void _mefDebug(int blu)
-{
-    debugMessage = blu;
-    debugMessageReady = true;
-}
-
+#if DEBUG
 void debugPrint()
 {
-    if (tempState != mefSTATE)
+    if (debugState != mefSTATE)
     {
         pc.printf("\r\nNew state: %i \r\n", mefSTATE);
-        tempState = mefSTATE;
+        debugState = mefSTATE;
     }
     if(frameDropped)
     {
@@ -202,9 +205,10 @@
         debugMessageReady = false;
     }
     
-    if (swag)
+    if (debugBitReady)
     {
-        pc.printf("%i ", asdf);
-        swag = false;
+        pc.printf("%i ", debugBit);
+        debugBitReady = false;
     }
 }
+#endif