app4

Dependencies:   mbed-rtos mbed CRC16

Fork of S5info_APP2 by Éric Bisson

Revision:
16:f2661759714c
Parent:
15:7c2e70c36b98
Child:
17:2b4b3f3a0489
--- a/main.cpp	Tue Mar 07 04:54:45 2017 +0000
+++ b/main.cpp	Tue Mar 07 05:11:35 2017 +0000
@@ -163,20 +163,50 @@
 
 void read()
 {
-    char byte;
+    char byte = 0;
     vector<char> bytes;
     char shift = 0;
+    char totalsize = 7;
     while (true)
     {
         ThreadLecture.signal_wait(1);
         
-        byte = (byte << 1) + in;
+        byte = (byte << 1) + !in; // inversion car 2e période
         
         shift++;
         if (shift == 8)
         {
+            shift = 0;
+            
+            // à partir d'ici, je travaille en byte et non bit
             bytes.push_back(byte);
-            shift = 0;
+            
+            // Validations de base
+            if ((bytes.size() == 1 && bytes[0] != PREAMBULE) ||
+                (bytes.size() == 2 && bytes[1] != START))
+            {
+                bytes.clear();
+            }
+            if (bytes.size() == 3)
+            {
+                totalsize = 7 + bytes[2];
+            }
+            
+            // fin
+            if (totalsize == bytes.size())
+            {
+                // Calcul du CRC
+                unsigned short currentCRC = bytes[bytes.size()-2] + bytes[bytes.size()-3]<<8;
+                
+                vector<char> charge_utile(&bytes[4], &bytes[bytes.size()-4]);
+                
+                if (currentCRC == mycrc16.calculateCRC16(&charge_utile[0], charge_utile.size()) && bytes.back() == END)
+                {
+                    // Affiche à l'écran le message valide
+                    pc.printf(&charge_utile[0], charge_utile.size());
+                }
+                bytes.clear();
+            }
         }
     }
 }