Make Gcode from .txt data. It is 2D array, for Robot Path.

Dependents:   WRS_mechanamu_test WRS2019_master

Revision:
4:1e606e2301f9
Parent:
3:09ee560a3007
Child:
5:9b4fcc87af6a
--- a/MakeSequencer.cpp	Thu Oct 11 11:23:53 2018 +0000
+++ b/MakeSequencer.cpp	Wed Oct 17 08:39:42 2018 +0000
@@ -1,31 +1,29 @@
 /*
-sample
-
+SAMPLE
 #include "mbed.h"
 #include "MakeSequencer.h"
 
+#define SIZE 5
+#define ArraySize(array) (sizeof(array) / sizeof(array[0]))
+
 //A row is from 1
 
 LocalFileSystem local("local");
-int array[5] = {};
+int array[SIZE] = {};
 
 int main()
 {
     FILE *fp = fopen( "/local/out.txt", "r");
     MakeSequencer code(fp);
-    code.getGcode(3,sizeof(array)/sizeof(int),array);        //getGcode(int row,int aSize, int* array)
-                                                             //array will get Gcode of a "int row" line
-    for(int i = 0;i < sizeof(array)/sizeof(int);i++)
+    code.getGcode(2,ArraySize(array),array);//getGcode(int row,int aSize, int* array)
+                                            //array will get Gcode of a "int row" line
+    for(int i = 0;i < ArraySize(array);i++)
         printf("%d,",array[i]);
-    printf("\n\r%d",code.getGcodeSize());                    //getGcodeSize()
-                                                             //return Gcode row
+    printf("\n\r%d\n\r",code.getGcodeSize());   //getGcodeSize()
+                                               //return Gcode row
 }
 */
-
-
 #include "MakeSequencer.h"
-
-
 MakeSequencer::MakeSequencer(FILE *fp)
 {
     _fp=fp;
@@ -50,7 +48,7 @@
 {
     for(int i = 0;i < aSize;i++)
     {
-        array[i] = _data[i][row-1];
+        array[i] = _data[i][row];
     }
 }
 
@@ -61,27 +59,46 @@
 
 bool MakeSequencer::ReadGcode()
 {
+    char * _buf     = new char[BUF];
+    int  *_tempnum  = new int[ARRAY_EREMENT];
+    char **_element = new char*[ARRAY_EREMENT];
+    bool **_check   = new bool*[ARRAY_EREMENT];
+    for (int y = 0; y < ARRAY_EREMENT; y++) 
+    {
+        _element[y] = new char[CODE_LENGTH];
+        _check[y]   = new bool[CODE_LENGTH];
+    }
+    
+    for(int b = 0;b < BUF;b++)
+        _buf[b] = 0;
+    
+    for(int x = 0;x < ARRAY_EREMENT;x++)
+    {
+        _tempnum[x] = 0;
+        for(int y = 0;y < CODE_LENGTH;y++)
+        {
+            _data   [x][y] = 0;
+            _element[x][y] = ' ';
+            _check  [x][y] = true;
+        }
+    }
     _ele = 0;
-    for(int i = 0;fgets(_buf,sizeof(_buf), _fp) != NULL;i++)
+    for(int i = 0;fgets(_buf,BUF, _fp) != NULL;i++)
     {
         _codesize = i;
-        
         if(i > CODE_LENGTH)
             return false;
         else
         {
-            string _tempstr[ARRAY_EREMENT];
+            string *_tempstr = new string[ARRAY_EREMENT];
+            for(int x = 0;x < ARRAY_EREMENT;x++)
+                _tempstr[x] = " ";
+                
             _count = -1;
             
-            for(int n = 0;n < ARRAY_EREMENT;n++)
-            {
-                for(int p = 0;p < CODE_LENGTH;p++)
-                    _check[n][p] = true;
-            }
-            
             for(int n = 0;_buf[n] != '\0';n++)
             {
-                _char = _buf[n];
+                char _char = _buf[n];
                 if((_char >= 'a'&& _char <= 'z') || (_char >= 'A' && _char <= 'Z'))
                 {
                     _count++;                    
@@ -98,30 +115,49 @@
                             }
                         }
                     }
-                    _element[_count][i] = _char;
+                    else
+                        _element[_count][i] = _char;
                     if(i == 0)
                         _ele++;
                 }
-                else if(_char >= '0'&& _char <= '9')
+                else if(_char >= '0'&& _char <= '9' || _char <= '-')
                 {
                     _tempstr[_count] = _tempstr[_count] + _char;
                 }   
                 
-                else if('\n' || '\r')
+                else if(_char == '\n' || _char == '\r')
                 {
                 }
                 else
                     return false;
             }
-            
+            //printf("#(%d) ",i);
             for(int n = 0;n < _ele;n++)
             {
                 _tempnum[n] = atoi(_tempstr[n].c_str());
                 _data[n][i] = (i != 0 && _check[n][i] && _element[n][i] != _element[n][0])
                             ? _data[n][i-1]
                             : _tempnum[n];
+                            
+                _element[n][i] = (i != 0 && _check[n][i] && _element[n][i] != _element[n][0])
+                            ? _element[n][0]
+                            : _element[n][i];
+                //printf("%c:%d",_element[n][i],_data[n][i]);
             }
+            //printf("\n\r");
+            delete[] _tempstr;
         }
     }
+    
+    for (int y = 0; y < ARRAY_EREMENT;y++) 
+    {
+        delete[] _check[y];
+        delete[] _element[y];
+    }
+    delete[] _tempnum;
+    delete[] _buf;
+    delete[] _check;
+    delete[] _element;
+    
     return true;
 }
\ No newline at end of file