Shih-Ho Hsieh / Mbed 2 deprecated pololu5mag_with_platform

Dependencies:   mbed

Fork of Motor_XYZ_UI_SPI_I2C_5mag by Shih-Ho Hsieh

Branch:
envelope
Revision:
8:33d34a775873
Parent:
7:ee0569d49c52
--- a/parse_array/envelopetracker.cpp	Fri Dec 08 06:30:58 2017 +0000
+++ b/parse_array/envelopetracker.cpp	Tue Dec 12 08:47:56 2017 +0000
@@ -1,5 +1,8 @@
 #include "envelopetracker.h"
-//#include <QDebug>
+#include "mbed.h"
+
+extern Serial pc;
+extern DigitalOut mag_test;
 EnvelopeTracker::EnvelopeTracker()
 {
     currentIndex = 0;
@@ -7,12 +10,18 @@
     envelopeDataLength = 0;
     currentDataIndex = 0;
     bufferTailIndex = -1;
+    envelopeListIndex = 0;
+    envelopeListLength = 100;
+    envelopeListIndexFront = -1;
+    envelopeList = new Envelope*[envelopeListLength];
 }
 
 void EnvelopeTracker::parse(char * buf, int bufLength)
 {
     int byteAmountToAddInBuffer = 0;
-    int bufOffset = 0;/*
+    int bufOffset = 0;
+    char * data;
+    Envelope* newEnvelope;/*
     int bufIndex;
     if(bufferTailIndex < 0){
         bufIndex = 0;
@@ -31,7 +40,6 @@
         bufferTailIndex %= bufferLength;
 
         int numToParse=(currentIndex>bufferTailIndex)?(bufferTailIndex + bufferLength - currentIndex):(bufferTailIndex - currentIndex);
-//        qDebug()<<"num to parse: " << numToParse << bufferTailIndex << currentIndex << byteAmountToAddInBuffer << endl;
         for(;numToParse>=envelopeLength-1;numToParse--)
         {
             if(testHeader()&&testFooter()&&testXOR()){
@@ -40,18 +48,18 @@
                 currentHeaderIndex = currentIndex;
                 currentDataIndex = currentIndex + headerLength;
 
-                char * data = new char[envelopeDataLength];
+                data = new char[envelopeDataLength];
                 for(int i = 0; i < envelopeDataLength; i++){
                     data[i] = bufferArray[(i+currentDataIndex)%bufferLength];
-//                    if(bufIndex>=-2) buf[i+bufIndex+1] = 'J';
                 }
-                Envelope* newEnvelope = new Envelope(envelope.setEnvelopeData(data, envelopeDataLength));
-                envelopeList.push_back(newEnvelope);
-//                qDebug()<<"get 1 env, bufIndex: "<<bufIndex << endl;
+                newEnvelope = &(envelope.setEnvelopeData(data, envelopeDataLength));
+                if(envelopeListIndexFront == -1) envelopeListIndex = 0;
+                envelopeList[envelopeListIndex] = newEnvelope;
+                envelopeListIndex++;
+                envelopeListIndex %= envelopeListLength;
+                if(envelopeListIndexFront == -1) envelopeListIndexFront = 0;
             }
-//            else if(testHeader()&&testFooter()&&!testXOR()) qDebug()<<"fail xor\n";
             ++currentIndex %= bufferLength;
-//            bufIndex++;
 
         }
         if(byteAmountToAddInBuffer != bufLength){
@@ -79,6 +87,7 @@
 void EnvelopeTracker::setEnvelope(const Envelope &value)
 {
     envelope = value;
+//    printf("envelope assigned\n");
     envelopeLength = envelope.length();
     envelopeDataLength = envelope.getPayloadLength();
     headerLength = envelope.getHeaderLength();
@@ -101,7 +110,6 @@
     int footerIndex = envelope.getFooterIndex();
     for(int i = 0; i < envelope.getFooterLength(); i++)
         if(footer[i] != bufferArray[(i+currentIndex+footerIndex)%bufferLength]){
-//            qDebug()<<"footer fail";
             return false;
         }
     return true;
@@ -141,7 +149,7 @@
 
 EnvelopeTracker::~EnvelopeTracker()
 {
-    if(!bufferArray) delete bufferArray;
+    if(bufferArray) delete bufferArray;
 }