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

Dependents:   WRS_mechanamu_test WRS2019_master

Revision:
2:4fc4593498d7
Parent:
1:4ebb30d54be7
Child:
3:09ee560a3007
diff -r 4ebb30d54be7 -r 4fc4593498d7 MakeSequencer.cpp
--- a/MakeSequencer.cpp	Wed Oct 10 05:34:43 2018 +0000
+++ b/MakeSequencer.cpp	Thu Oct 11 11:07:31 2018 +0000
@@ -1,20 +1,28 @@
 /*
-sample code
+sample
+
+#include "mbed.h"
+#include "MakeSequencer.h"
+
+//A row is from 1
 
 LocalFileSystem local("local");
-int array[3] = {};
+int array[5] = {};
 
 int main()
 {
     FILE *fp = fopen( "/local/out.txt", "r");
     MakeSequencer code(fp);
-    
-    code.getGcode(1,array);
+    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++)
+        printf("%d,",array[i]);
+    printf("\n\r%d",code.getGcodeSize());                    //getGcodeSize()
+                                                             //return Gcode row
 }
 */
 
 
-
 #include "MakeSequencer.h"
 
 
@@ -24,12 +32,6 @@
     FileOpen();
 }
 
-bool MakeSequencer::inherit(int _rows,int _columns,int _data[][CODE_LENGTH])
-{
-    _data[_rows][_columns] = (_columns == 0) ? 0 : _data[_rows][_columns-1];
-    return false;
-}
-
 bool MakeSequencer::FileOpen()
 {
     if ( _fp == NULL )
@@ -44,67 +46,96 @@
     return true;
 }
 
-void MakeSequencer::getGcode(int _seq,int* _array)
+void MakeSequencer::getGcode(int row,int aSize, int* array)
 {
-    for(int _i = 0;_i < ARRAY_EREMENT;_i++)
+    for(int i = 0;i < aSize;i++)
     {
-        _array[_i] = data[_i][_seq];   
+        array[i] = _data[i][row-1];
     }
 }
 
 int MakeSequencer::getGcodeSize()
 {
-    return codesize;
+    return _codesize+1;
 }
 
 bool MakeSequencer::ReadGcode()
 {
-    for(int i = 0;fgets(buf,sizeof(buf), _fp) != NULL;i++)
+    int _ele = 0;
+    
+    for(int i = 0;fgets(_buf,sizeof(_buf), _fp) != NULL;i++)
     {
-        codesize = i+1;
+        _codesize = i;
+        
         if(i > CODE_LENGTH)
             return false;
-        
-        for(int n = 0;buf[n] != '\0';n++)
-            code[0][i] = code[0][i]+buf[n];
+        else
+        {
+            int    _tempnum[ARRAY_EREMENT];
+            string _tempstr[ARRAY_EREMENT];
+            char   _element[ARRAY_EREMENT][CODE_LENGTH];
+            char   _char;
+            int    _count = -1;
+            bool   _check[ARRAY_EREMENT][CODE_LENGTH];
             
-        int pos[3] = {code[0][i].find("x"),code[0][i].find("y"),code[0][i].find("z")};
-        
-        for(int p = 0;p < sizeof(pos)/sizeof(int);p++)
-            find[p] = (pos[p] != code[0][i].npos) ? true:inherit(p,i,data);
-
-        for(int p = 0;p < sizeof(pos)/sizeof(int);p++)
-        {
-            char trans[1] = {};
-            string sw_code = find[p] ? code[0][i].substr(pos[p],1) : "I";
-            sw_code.copy(trans,1);
-            switch(trans[0])
+            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++)
             {
-                case 'x':
-                    if(find[1])
-                        data[0][i] = atoi(code[0][i].substr(pos[0]+1,pos[1] - 1).c_str());   
-                    else if(find[2])
-                        data[0][i] = atoi(code[0][i].substr(pos[0]+1,pos[2] - 1).c_str());
-                    else
-                        data[0][i] = atoi(code[0][i].substr(pos[0]+1,code[0][i].size() - 1).c_str());
-                break;
+                _char = _buf[n];
+                if((_char >= 'a'&& _char <= 'z') || (_char >= 'A' && _char <= 'Z'))
+                {
+                    _count++;                    
+                    if(_char != _element[_count][0] && i != 0)
+                    {
+                        for(int p = 0;p < _ele;p++)
+                        {
+                            if(_char == _element[p][0])
+                            {
+                                _element[p][i] = _char;
+                                _count = p;
+                                _check[p][i] = false;
+                                break;
+                            }
+                        }
+                    }
+                    _element[_count][i] = _char;
+                    if(i == 0)
+                        _ele++;
+                }
+                else if(_char >= '0'&& _char <= '9')
+                {
+                    _tempstr[_count] = _tempstr[_count] + _char;
+                }   
                 
-                case 'y':
-                    if(find[2])
-                        data[1][i] = atoi(code[0][i].substr(pos[1]+1,pos[2] - 1).c_str());
-                    else
-                        data[1][i] = atoi(code[0][i].substr(pos[1]+1,code[0][i].size() - 1).c_str());
-                break;
+                else if('\n' || '\r')
+                {
+                }
+                else
+                    return false;
+            }
+            
+            for(int n = 0;n < _ele;n++)
+            {
+                _tempnum[n] = atoi(_tempstr[n].c_str());
                 
-                case 'z':
-                    data[2][i] = atoi(code[0][i].substr(pos[2]+1,code[0][i].size() - 1).c_str());
-                break;
-                
-                default:
-                break;
-            }    
+                if(i != 0 && _check[n][i] && _element[n][i] != _element[n][0])
+                {
+                    //printf("{%d}-{%d},",n,i);
+                    _data[n][i] = _data[n][i-1];
+                }
+                else
+                {
+                    _data[n][i] = _tempnum[n];
+                }        
+                printf("%c:%d ",_element[n][0],_data[n][i]);    
+            }
+            printf("\n\r");
         }
-        printf("x=%d ,y=%d ,z=%d \n\r",data[0][i],data[1][i],data[2][i]);
     }
     return true;
 }
\ No newline at end of file