.

Dependencies:   L432KC_SPI_Pey_Lal

Revision:
115:156b8234f2de
Parent:
111:f11575e7c79b
Child:
116:6dfcafa00e42
diff -r c1f7be27aa5d -r 156b8234f2de protocol.cpp
--- a/protocol.cpp	Wed May 18 16:12:59 2022 +0000
+++ b/protocol.cpp	Wed May 18 18:07:09 2022 +0000
@@ -3,24 +3,24 @@
 char newDataAvailable = 0;
 
 //Bytewise XOR
-char calculateChecksum(uint32_t propulsion, uint32_t direction)
+char calculateChecksum(char command, char* payload, char payloadLength)
 {
     char checksum = 0xff;
-    checksum ^= propulsion >> 8;
-    checksum ^= propulsion & (0b11111111);
-    checksum ^= direction >> 8;
-    checksum ^= direction & (0b11111111);
-    
+    checksum ^= command;
+    for (int i = 0; i < payloadLength; i++)
+        checksum ^= payload[i];
     return checksum;
 }
 
 char receiveState = STATE_START_OF_FRAME;
 char receiveIndex = 0;
-uint32_t receivedPropulsion;
-uint32_t receivedDirection;
+char receiveIndexLimit;
+char receivedCommand;
+char receivedPayload[PAYLOAD_MAX_SIZE] = {0};
 
-uint32_t verifiedPropulsion = 1500;
-uint32_t verifiedDirection = 1150;
+char verifiedPayload[PAYLOAD_MAX_SIZE] = {0};
+char verifiedIndexLimit;
+char verifiedCommand;
 
 //Decode the bytes received according to their order of reception.
 void decodeMessage(char c)
@@ -31,41 +31,49 @@
             //Checking if we received the start Byte
             if (c == 0xff)
             {
-                receiveState = STATE_PROPULSION;
-                receiveIndex = 0;
-            }
-            break;
-            
-        case STATE_PROPULSION:
-            //Retrieving the propulsion PWM bytes (1. MSB; 2. LSB)
-            if (receiveIndex++ == 0)
-                receivedPropulsion = c << 8;
-            else
-            {
-                receivedPropulsion += c;
-                receiveState = STATE_DIRECTION;
+                receiveState = STATE_COMMAND_TYPE;
                 receiveIndex = 0;
             }
             break;
             
-        case STATE_DIRECTION:
-            //Retrieving the direction PWM bytes (1. MSB; 2. LSB)
-            if (receiveIndex++ == 0)
-                receivedDirection = c << 8;
-            else
+        case STATE_COMMAND_TYPE:
+            receivedCommand = c;
+            receiveState = STATE_PAYLOAD;
+            switch(receivedCommand)
             {
-                receivedDirection += c;
-                receiveState = STATE_CHECKSUM;
+                case COMMAND_PWM:
+                    receiveIndexLimit = 4;
+                    break;
+                    
+                case COMMAND_ASSERVISSEMENT:
+                    receiveIndexLimit = 6;
+                    break;
+                    
+                case COMMAND_PARAMETRES:
+                    receiveIndexLimit = 8;
+                    break;
+                
+                default:
+                    receiveState = STATE_START_OF_FRAME;
+                    break;
             }
             break;
             
+        case STATE_PAYLOAD:
+            receivedPayload[receiveIndex++] = c;
+            if (receiveIndex >= receiveIndexLimit)
+                receiveState = STATE_CHECKSUM;
+            break;
+                
         case STATE_CHECKSUM:
             //Checksum
-            if (c == calculateChecksum(receivedPropulsion, receivedDirection))
+            if (c == calculateChecksum(receivedCommand, receivedPayload, receiveIndexLimit))
             {
                 //The frame is correct, we can use the values retrieved.
-                verifiedPropulsion = receivedPropulsion;
-                verifiedDirection = receivedDirection;
+                for (int i = 0; i < receiveIndexLimit; i++)
+                    verifiedPayload[i] = receivedPayload[i];
+                verifiedIndexLimit = receiveIndexLimit;
+                verifiedCommand = receivedCommand;
                 newDataAvailable = 1;
             }
             receiveState = STATE_START_OF_FRAME;
@@ -78,11 +86,12 @@
 }
 
 //Return the values decoded
-void getVerifiedPWMValues(uint32_t *pwmPropulsion, uint32_t *pwmDirection)
+char getVerifiedPayload(char *payloadOutput)
 {
-    *pwmPropulsion = verifiedPropulsion;
-    *pwmDirection = verifiedDirection;
+    for (int i = 0; i < verifiedIndexLimit; i++)
+        payloadOutput[i] = verifiedPayload[i];
     newDataAvailable = 0;
+    return verifiedCommand;
 }
 
 //Return a value telling if new informations are available.