summaryrefslogtreecommitdiff
path: root/trunk/users/metalab/GCode_Interpreter_SD/GCode_Interpreter_SD.pde
blob: 892a3962300f1e6089b3abe5049cd5b4e44c8a90 (plain)
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
/* -*- mode: c++; c-basic-offset: 8; indent-tabs-mode: t -*- */

// Arduino G-code Interpreter
// v1.0 by Mike Ellery - initial software (mellery@gmail.com)
// v1.1 by Zach Hoeken - cleaned up and did lots of tweaks (hoeken@gmail.com)
// v1.2 by Chris Meighan - cleanup / G2&G3 support (cmeighan@gmail.com)
// v1.3 by Zach Hoeken - added thermocouple support and multi-sample temp readings. (hoeken@gmail.com)
// vX.X by Marius Kintel - refactored a bit to save space and clarify code (kintel@sim.no)
#include <HardwareSerial.h>

#include "_init.h"
AxisConfig axes[3] = {
	{X_STEP_PIN, X_DIR_PIN, X_MIN_PIN, X_MAX_PIN, X_ENABLE_PIN, INVERT_X_DIR, REFERENCE_X_DIR, ENDSTOP_X_MIN_ENABLED, ENDSTOP_X_MAX_ENABLED, X_STEPS_PER_INCH, X_STEPS_PER_MM, X_MOTOR_STEPS},
	{Y_STEP_PIN, Y_DIR_PIN, Y_MIN_PIN, Y_MAX_PIN, Y_ENABLE_PIN, INVERT_Y_DIR, REFERENCE_Y_DIR, ENDSTOP_Y_MIN_ENABLED, ENDSTOP_Y_MAX_ENABLED, Y_STEPS_PER_INCH, Y_STEPS_PER_MM, Y_MOTOR_STEPS},
	{Z_STEP_PIN, Z_DIR_PIN, Z_MIN_PIN, Z_MAX_PIN, Z_ENABLE_PIN, INVERT_Z_DIR, REFERENCE_Z_DIR, ENDSTOP_Z_MIN_ENABLED, ENDSTOP_Z_MAX_ENABLED, Z_STEPS_PER_INCH, Z_STEPS_PER_MM, Z_MOTOR_STEPS}
};

#include "RepRapSDCard.h"

//our command string
#define COMMAND_SIZE 128
char cmdbuffer[COMMAND_SIZE];
byte serial_count;
int no_data = 0;
long old_idle_time;
long idle_time;
int delta;
boolean comment = false;
boolean bytes_received = false;
bool use_sd_card = false;
RepRapSDCard sdcard;
File *file;
bool eof = false;

void setup()
{
	//Do startup stuff here
	Serial.begin(115200);
	Serial.println("start");
	
	//other initialization.
	init_steppers();
	init_extruder();
	clear_process_string();

	// Open first file, validate contents
	// Decide whether to read from serial or SD-card; print debug to serial
	if (sdcard.init() == RepRapSDCard::SDOK) {
		char filename[12];
		if (sdcard.getNextEntry(filename)) {
			Serial.print("First file: ");
			Serial.println(filename);
			if (!(file = sdcard.openFile(filename))) {
				Serial.println("Unable to open file.");
			}
			else {
				use_sd_card = true;
			}
                }
	}
}

void clear_process_string()
{
	//init our command buffer
	for (uint8_t i=0; i<COMMAND_SIZE; i++) cmdbuffer[i] = 0;
	serial_count = 0;
	bytes_received = false;

	idle_time = millis();
}

int input_available()
{
	if (use_sd_card) {
		if (!eof) return 1;
	}
	else {
		return Serial.available();
	}
}

uint8_t input_read()
{
	if (use_sd_card) {
		uint8_t c;
		intptr_t ret = sdcard.readFile(file, &c, 1);
		if (ret == 0) {
			eof = true;
			c = '\n';
		}
		if (ret == -1) {
			Serial.println("Read error.");
			eof = true;
			c = '\n';
		}
		return c;
	}
	else {
		return Serial.read();
	}
}

void loop()
{
	char c;

	extruder_manage_temperature();  //keep it hot!

	//read in characters if we got them.
	if (input_available() > 0)
	{
		c = input_read();
		
		// Reset no_data timer
		no_data = 0;
		idle_time = millis();
		
		if (c == '\r') {
			Serial.println("debug: linefeed ?!");
		}
		else
		//  commands end with newlines
		if (c != '\n')
		{
			// Start of comment - ignore any bytes received from now on
			if (c == '(') comment = true;
			
			// If we're not in comment mode, add it to our array.
			if (!comment)
			{
				cmdbuffer[serial_count] = c;
				serial_count++;
			}
			
			if (c == ')') comment = false; // End of comment - start listening again
		}
		
		bytes_received = true;
	}
	else
	{
		// mark no_data if nothing heard for 100 milliseconds
		delta = millis() - idle_time;
		if (delta >= 100)
// 		if ((millis() - idle_time) >= 100)
		{	
			no_data++;
			old_idle_time = idle_time;
			idle_time = millis();
		}
	}

	// if there's a pause or we got a real command, do it
	if (bytes_received && (c == '\n' || no_data))
	{
		if (no_data)
		{
			Serial.print("NODATA(");
			Serial.print(no_data);
			Serial.print(" ");
			Serial.print((long)millis());
			Serial.print(" ");
			Serial.print(old_idle_time);
			Serial.print(" ");
			Serial.print(idle_time);
			Serial.print(" ");
			Serial.print(delta);
			Serial.print("): ");
			Serial.println(cmdbuffer);
		}
#if (DEBUG == 1)
 		else
 		{
 			Serial.print("debug: ");
 			Serial.println(cmdbuffer);
 		}
#endif

		//process our command!
		process_string(cmdbuffer, serial_count);

		//clear command.
		clear_process_string();
	}

	//no data for > 1 sec -> turn off steppers
	if (no_data > 10)
		disable_steppers();
}