RobOmega - PSL RoboCup / Mbed OS nRF24L01P_L432KC_CarteBlanche

Dependencies:   nRF24L01P_Hello_World nRF24L01P

Revision:
4:5caf9e1dc16c
Child:
5:668dd9395ca5
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/protocol.cpp	Fri Jan 08 08:51:10 2021 +0000
@@ -0,0 +1,106 @@
+#include "protocol.h"
+#include "bufferCirculaire.h"
+
+//------------------------------------------------Encodage/Decodage des trames------------------------------------------------//
+//!!! La taille de "msg[]" doit être [msgPayloadLength + 6]
+int encodeMessage(int msgFunction, int msgPayloadLength, char* msgPayload, char* msg)
+{
+    int msgLength = 0;
+    msg[msgLength++] = 0xFE;                                                                //Start of Frame
+    msg[msgLength++] = (char)(msgFunction >> 8);                                            //Message Function MSB
+    msg[msgLength++] = (char)(msgFunction);                                                 //Message Function LSB
+    msg[msgLength++] = (char)(msgPayloadLength >> 8);                                       //Message Payload Length MSB
+    msg[msgLength++] = (char)(msgPayloadLength);                                            //Message Payload Length LSB
+    int j;
+    for (j = 0 ; j < msgPayloadLength; j++)
+        msg[msgLength++] = msgPayload[j];                                                   //MessagePayload
+    msg[msgLength++] = calculateChecksum(msgFunction, msgPayloadLength, msgPayload);        //Checksum
+    
+    return msgLength;
+}
+
+char calculateChecksum(int msgFunction, int msgPayloadLength, char* msgPayload)
+{
+    char checksum = 0xFE;
+    checksum ^= (char)(msgFunction >> 8);
+    checksum ^= (char)(msgFunction);
+    checksum ^= (char)(msgPayloadLength >> 8);
+    checksum ^= (char)(msgPayloadLength);
+    int i;
+    for(i = 0; i < msgPayloadLength; i++)
+        checksum ^= msgPayload[i];
+    
+    return checksum;
+}
+
+unsigned char rcvState = WAITING;
+int decodedMsgFunction;
+int decodedPayloadLength;
+char decodedPayload[BUFFER_SIZE];
+int decodedPayloadIndex = 0;
+
+void decodeMessage(char receivedByte)
+{
+    switch(rcvState)
+    {
+        case WAITING:
+            if (receivedByte == 0xFE)
+                rcvState = FUNCTION_MSB;
+            break;
+            
+        case FUNCTION_MSB:
+            decodedMsgFunction = receivedByte << 8;
+            rcvState = FUNCTION_LSB;
+            break;
+            
+        case FUNCTION_LSB:
+            decodedMsgFunction += receivedByte;
+            rcvState = LENGTH_MSB;
+            break;
+            
+        case LENGTH_MSB:
+            decodedPayloadLength = receivedByte << 8;
+            rcvState = LENGTH_LSB;
+            break;
+            
+        case LENGTH_LSB:
+            decodedPayloadLength += receivedByte;
+            rcvState = PAYLOAD;
+            break;
+            
+        case PAYLOAD:
+            if(decodedPayloadLength >= BUFFER_SIZE)
+            {
+                rcvState = WAITING;
+                decodedPayloadLength = 0;
+                decodedMsgFunction = 0;
+            }
+            else if (decodedPayloadLength > 0)
+            {
+                decodedPayload[decodedPayloadIndex++] = receivedByte;
+                if (decodedPayloadIndex >= decodedPayloadLength)
+                {
+                    rcvState = CHECKSUM;
+                    decodedPayloadIndex = 0;
+                }
+            }
+            else
+            {
+                rcvState = CHECKSUM;
+            }
+            break;
+            
+        case CHECKSUM:
+        {    
+            char calculatedChecksum = calculateChecksum(decodedMsgFunction, decodedPayloadLength, decodedPayload);
+            if (calculatedChecksum == receivedByte)
+            {
+                //Victoire, la trame recu correspond à la trame envoyé
+                //TODO: Faire une fonction qui traite la charge utile apres décodage.
+            }
+            
+            rcvState = WAITING;
+            break;
+        }
+    }
+}
\ No newline at end of file