Reading Gcode in LocalFiileSystem and encodeing int array. but this program has bug.probably

Fork of MakeSequencer by yuki sato

Committer:
satoyuki1111
Date:
Tue Oct 09 12:19:02 2018 +0000
Revision:
0:359eba7592ca
Child:
1:4ebb30d54be7
Gcode Sequencer used by LocalFileSystem;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
satoyuki1111 0:359eba7592ca 1 /*
satoyuki1111 0:359eba7592ca 2 sample code
satoyuki1111 0:359eba7592ca 3
satoyuki1111 0:359eba7592ca 4 LocalFileSystem local("local");
satoyuki1111 0:359eba7592ca 5 int array[3] = {};
satoyuki1111 0:359eba7592ca 6
satoyuki1111 0:359eba7592ca 7 int main()
satoyuki1111 0:359eba7592ca 8 {
satoyuki1111 0:359eba7592ca 9 FILE *fp = fopen( "/local/out.txt", "r");
satoyuki1111 0:359eba7592ca 10 MakeSequencer code(fp);
satoyuki1111 0:359eba7592ca 11
satoyuki1111 0:359eba7592ca 12 code.getGcode(1,array);
satoyuki1111 0:359eba7592ca 13 }
satoyuki1111 0:359eba7592ca 14 */
satoyuki1111 0:359eba7592ca 15
satoyuki1111 0:359eba7592ca 16
satoyuki1111 0:359eba7592ca 17
satoyuki1111 0:359eba7592ca 18 #include "MakeSequencer.h"
satoyuki1111 0:359eba7592ca 19
satoyuki1111 0:359eba7592ca 20
satoyuki1111 0:359eba7592ca 21 MakeSequencer::MakeSequencer(FILE *fp)
satoyuki1111 0:359eba7592ca 22 {
satoyuki1111 0:359eba7592ca 23 _fp=fp;
satoyuki1111 0:359eba7592ca 24 FileOpen();
satoyuki1111 0:359eba7592ca 25 }
satoyuki1111 0:359eba7592ca 26
satoyuki1111 0:359eba7592ca 27 bool MakeSequencer::inherit(int _rows,int _columns,int _data[][CODE_LENGTH])
satoyuki1111 0:359eba7592ca 28 {
satoyuki1111 0:359eba7592ca 29 _data[_rows][_columns] = (_columns == 0) ? 0 : _data[_rows][_columns-1];
satoyuki1111 0:359eba7592ca 30 return false;
satoyuki1111 0:359eba7592ca 31 }
satoyuki1111 0:359eba7592ca 32
satoyuki1111 0:359eba7592ca 33 bool MakeSequencer::FileOpen()
satoyuki1111 0:359eba7592ca 34 {
satoyuki1111 0:359eba7592ca 35 if ( _fp == NULL )
satoyuki1111 0:359eba7592ca 36 {
satoyuki1111 0:359eba7592ca 37 printf("Exception");
satoyuki1111 0:359eba7592ca 38 return false;
satoyuki1111 0:359eba7592ca 39 }
satoyuki1111 0:359eba7592ca 40 else
satoyuki1111 0:359eba7592ca 41 ReadGcode();
satoyuki1111 0:359eba7592ca 42
satoyuki1111 0:359eba7592ca 43 fclose(_fp);
satoyuki1111 0:359eba7592ca 44 return true;
satoyuki1111 0:359eba7592ca 45 }
satoyuki1111 0:359eba7592ca 46
satoyuki1111 0:359eba7592ca 47 void MakeSequencer::getGcode(int _seq,int* _array)
satoyuki1111 0:359eba7592ca 48 {
satoyuki1111 0:359eba7592ca 49 for(int _i = 0;_i < ARRAY_EREMENT;_i++)
satoyuki1111 0:359eba7592ca 50 {
satoyuki1111 0:359eba7592ca 51 _array[_i] = data[_i][_seq];
satoyuki1111 0:359eba7592ca 52 }
satoyuki1111 0:359eba7592ca 53 }
satoyuki1111 0:359eba7592ca 54
satoyuki1111 0:359eba7592ca 55 bool MakeSequencer::ReadGcode()
satoyuki1111 0:359eba7592ca 56 {
satoyuki1111 0:359eba7592ca 57 for(int i = 0;fgets(buf,sizeof(buf), _fp) != NULL;i++)
satoyuki1111 0:359eba7592ca 58 {
satoyuki1111 0:359eba7592ca 59 if(i > CODE_LENGTH)
satoyuki1111 0:359eba7592ca 60 return false;
satoyuki1111 0:359eba7592ca 61
satoyuki1111 0:359eba7592ca 62 for(int n = 0;buf[n] != '\0';n++)
satoyuki1111 0:359eba7592ca 63 code[0][i] = code[0][i]+buf[n];
satoyuki1111 0:359eba7592ca 64
satoyuki1111 0:359eba7592ca 65 int pos[3] = {code[0][i].find("x"),code[0][i].find("y"),code[0][i].find("z")};
satoyuki1111 0:359eba7592ca 66
satoyuki1111 0:359eba7592ca 67 for(int p = 0;p < sizeof(pos)/sizeof(int);p++)
satoyuki1111 0:359eba7592ca 68 find[p] = (pos[p] != code[0][i].npos) ? true:inherit(p,i,data);
satoyuki1111 0:359eba7592ca 69
satoyuki1111 0:359eba7592ca 70 for(int p = 0;p < sizeof(pos)/sizeof(int);p++)
satoyuki1111 0:359eba7592ca 71 {
satoyuki1111 0:359eba7592ca 72 char trans[1] = {};
satoyuki1111 0:359eba7592ca 73 string sw_code = find[p] ? code[0][i].substr(pos[p],1) : "I";
satoyuki1111 0:359eba7592ca 74 sw_code.copy(trans,1);
satoyuki1111 0:359eba7592ca 75 switch(trans[0])
satoyuki1111 0:359eba7592ca 76 {
satoyuki1111 0:359eba7592ca 77 case 'x':
satoyuki1111 0:359eba7592ca 78 if(find[1])
satoyuki1111 0:359eba7592ca 79 data[0][i] = atoi(code[0][i].substr(pos[0]+1,pos[1] - 1).c_str());
satoyuki1111 0:359eba7592ca 80 else if(find[2])
satoyuki1111 0:359eba7592ca 81 data[0][i] = atoi(code[0][i].substr(pos[0]+1,pos[2] - 1).c_str());
satoyuki1111 0:359eba7592ca 82 else
satoyuki1111 0:359eba7592ca 83 data[0][i] = atoi(code[0][i].substr(pos[0]+1,code[0][i].size() - 1).c_str());
satoyuki1111 0:359eba7592ca 84 break;
satoyuki1111 0:359eba7592ca 85
satoyuki1111 0:359eba7592ca 86 case 'y':
satoyuki1111 0:359eba7592ca 87 if(find[2])
satoyuki1111 0:359eba7592ca 88 data[1][i] = atoi(code[0][i].substr(pos[1]+1,pos[2] - 1).c_str());
satoyuki1111 0:359eba7592ca 89 else
satoyuki1111 0:359eba7592ca 90 data[1][i] = atoi(code[0][i].substr(pos[1]+1,code[0][i].size() - 1).c_str());
satoyuki1111 0:359eba7592ca 91 break;
satoyuki1111 0:359eba7592ca 92
satoyuki1111 0:359eba7592ca 93 case 'z':
satoyuki1111 0:359eba7592ca 94 data[2][i] = atoi(code[0][i].substr(pos[2]+1,code[0][i].size() - 1).c_str());
satoyuki1111 0:359eba7592ca 95 break;
satoyuki1111 0:359eba7592ca 96
satoyuki1111 0:359eba7592ca 97 default:
satoyuki1111 0:359eba7592ca 98 break;
satoyuki1111 0:359eba7592ca 99 }
satoyuki1111 0:359eba7592ca 100 }
satoyuki1111 0:359eba7592ca 101 printf("x=%d ,y=%d ,z=%d \n\r",data[0][i],data[1][i],data[2][i]);
satoyuki1111 0:359eba7592ca 102 }
satoyuki1111 0:359eba7592ca 103 return true;
satoyuki1111 0:359eba7592ca 104 }