-
Notifications
You must be signed in to change notification settings - Fork 1
/
AdaGcode_interpreter.pde
404 lines (309 loc) · 6.71 KB
/
AdaGcode_interpreter.pde
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
#include <PString.h>
// Arduino G-code Interpreter
// v1.0 by Mike Ellery - initial software ([email protected])
// v1.1 by Zach Hoeken - cleaned up and did lots of tweaks ([email protected])
// v1.2 by Chris Meighan - cleanup / G2&G3 support ([email protected])
// v1.3 by Zach Hoeken - added thermocouple support and multi-sample temp readings. ([email protected])
// Adafruit motor board version - Dirty and fast mod, need revision. ([email protected])
#include <HardwareSerial.h>
#include <WProgram.h>
#include <SimplePacket.h>
#include <RepRapSDCard.h>
#include <WString.h>
//our command string
#define COMMAND_SIZE 25
RepRapSDCard card;
File f;
int startpos=0;
int actualpos=0;
//our buffer of bytes.
#define BUFFSIZE 100
char buffer[BUFFSIZE];
byte bufferIndex = 0;
byte error = 0;
byte serial_count;
int no_data = 0;
int enable_power=14;
char filename1[50]="test.TXT";
char command[COMMAND_SIZE];
char clean[COMMAND_SIZE];
char dirty[COMMAND_SIZE];
PString cleanstr(clean, sizeof(clean));
PString dirtystr(dirty, sizeof(dirty));
int startcom=0;
int cont=0;
void setup()
{
pinMode(enable_power,OUTPUT);
digitalWrite(enable_power,LOW);
//Do startup stuff here
Serial.begin(19200);
Serial.println("start");
//other initialization.
init_process_string();
init_steppers();
//init_extruder();
delay(500);
init_sd_card();
open_gfile("test.txt");
//read_gfile();
cleanstr.begin();
dirtystr.begin();
}
void loop()
{
//read 8 bytes of sdcar into buffer.
uint8_t result = card.read_file(f, (uint8_t *)buffer, 8);
if(result==0){
Serial.println("End of file");
digitalWrite(enable_power,HIGH);
delay(10000);
digitalWrite(enable_power,LOW);
//some tests to check the gcode commands.
while(digitalRead(20) == LOW)
{
process_string("G0 X-30",80);
}
delay(500);
process_string("G90",80);
process_string("G92",80);
process_string("G0 X5",80);
digitalWrite(enable_power,HIGH);
while(1){}
}
else{
//take a look in buffer.
cont=0;
while(cont<8)
{
if(buffer[cont]=='G') startcom=1;
else if(buffer[cont]=='\n'){
Serial.println(clean);
process_string(clean, 80);
cleanstr.begin();
startcom=0;
}
if(startcom==1)
{
cleanstr.print(buffer[cont]);
}
cont++;
}
}
//countline_gfile();
//process_string(command, 80);
//loads the buffer in filebuf.
//filebuf.append(buffer);
//delay(500);
//Serial.println("calling fun count...");
//countline_gfile();
//delay(20000);
//int size=0;
//size_command();
//Serial.println("something");
//load_command("comandoG!!!!");
//mystring.print("comandog");
//Serial.println(buffer1);
//delay(1000);
//Serial.println("Starts the counter");
//countline_gfile();
//countline_gfile();
//
//print_gfile();
//delay(5000);
/*
char c;
//keep it hot!
//extruder_manage_temperature();
//read in characters if we got them.
if (Serial.available() > 0)
{
c = Serial.read();
no_data = 0;
Serial.println("inside serial avaliable");
//newlines are ends of commands.
if (c != '\n')
{
wordo[serial_count] = c;
serial_count++;
}
}
//mark no data.
else
{
no_data++;
delayMicroseconds(100);
}
//if theres a pause or we got a real command, do it
if (serial_count && (c == '\n' || no_data > 100))
{
//process our command!
Serial.println(wordo);
process_string(wordo, serial_count);
//clear command.
init_process_string();
}
//no data? turn off steppers
if (no_data > 1000)
disable_steppers();
*/
}
void init_sd_card()
{
if (!card.init_card())
{
if (!card.isAvailable())
{
Serial.println("No card present");
error = 1;
}
else
{
Serial.println("Card init failed");
error = 2;
}
}
else if (!card.open_partition())
{
Serial.println("No partition");
error = 3;
}
else if (!card.open_filesys())
{
Serial.println("Can't open filesys");
error = 4;
}
else if (!card.open_dir("/"))
{
Serial.println("Can't open /");
error = 5;
}
else if (card.isLocked())
{
Serial.println("Card is locked");
error = 6;
}
}
/*
void countline_gfile()
{
actualpos=0;
startpos=0;
Serial.println("Starts the count");
while(actualpos<= 40)
{
commandstr.print(buffer[actualpos]);
//End of line means end of command.
if(buffer[actualpos]==10)
{
startpos=actualpos; //update.
Serial.println(command); //execute the command.
commandstr.begin(); //reset the buffer command.
}
++actualpos;
}
}
*/
void open_gfile(char filename[50])
{
strcpy(buffer, filename);
f = card.open_file(buffer);
if (!f)
{
Serial.print("error opening: ");
error = 8;
}
}
void read_gfile()
{
Serial.print("reading from: ");
Serial.println(buffer);
uint8_t result = card.read_file(f, (uint8_t *)buffer, 8000);
}
void close_gfile()
{
card.close_file(f);
}
void print_gfile()
{
Serial.println(buffer);
}
void open_file()
{
open_new_file();
if (error == 0)
{
bufferIndex = 0;
for (char c = 'a'; c<= 'z'; c++)
{
buffer[bufferIndex] = c;
bufferIndex++;
}
card.write_file(f, (uint8_t *) buffer, bufferIndex);
card.close_file(f);
}
read_first_file();
}
void open_new_file()
{
strcpy(buffer, filename1);
for (buffer[5] = '0'; buffer[5] <= '9'; buffer[5]++)
{
for (buffer[6] = '0'; buffer[6] <= '9'; buffer[6]++)
{
f = card.open_file(buffer);
if (!f)
break; // found a file!
card.close_file(f);
}
if (!f)
break;
}
if(!card.create_file(buffer))
{
Serial.print("couldnt create: ");
Serial.println(buffer);
error = 7;
}
else
{
f = card.open_file(buffer);
if (!f)
{
Serial.print("error opening: ");
Serial.println(buffer);
error = 8;
}
else
{
Serial.print("writing to: ");
Serial.println(buffer);
}
}
}
void read_first_file()
{
strcpy(buffer, "test.txt");
f = card.open_file(buffer);
if (!f)
{
Serial.print("error opening: ");
Serial.println(buffer);
error = 8;
}
else
{
Serial.print("reading from: ");
Serial.println(buffer);
uint8_t result = card.read_file(f, (uint8_t *)buffer, 8);
if (result >0)
Serial.print(buffer);
else if (result == 0)
Serial.println("end of file.");
else
{
Serial.print("read error: ");
Serial.println(result, DEC);
}
}
}