-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathArduinoPainter.ino
306 lines (275 loc) · 6.1 KB
/
ArduinoPainter.ino
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
// Based on:
// 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])
// Downloaded from http://sourceforge.net/projects/reprap/files/Arduino%20Firmware/v1.3/
// Modified by: Santiago Castro (@sccastrillon)
// Downloaded from https://github.com/santicastro/ArduinoPainter
#include "Configuration.h"
#include <SD.h>
#include <SPI.h>
#ifdef USE_LCD
#include <Wire.h>
#include <LiquidCrystal.h>
#endif
//#ifdef ENABLE_SERVO_TOOL
#include <Servo.h>
//#endif
//our command string
#define COMMAND_SIZE 96
char command[COMMAND_SIZE+1];
byte char_count=0;
//#ifdef ENABLE_SERVO_TOOL
Servo toolsServo;
//#endif
#ifdef USE_LCD
LiquidCrystal lcd(0x0);
#endif
void setup()
{
Serial.begin(57600);
// On the Ethernet Shield, CS is pin 4. It's set as an output by default.
// Note that even if it's not used as the CS pin, the hardware SS pin
// (10 on most Arduino boards, 53 on the Mega) must be left as an output
// or the SD library functions will not work.
pinMode(SD_SELECT, OUTPUT);
pinMode(BUTTON1, INPUT);
pinMode(BUTTON2, INPUT);
#ifdef USE_INTERNAL_PULLUPS
digitalWrite(BUTTON1, HIGH);
digitalWrite(BUTTON2, HIGH);
#endif
#ifdef USE_LCD
lcd.begin(20, 4);
lcd.setCursor(0,0);
lcd.print("Init..");
#endif
//#ifdef ENABLE_SERVO_TOOL
toolsServo.attach(TOOLS_SERVO_PIN);
changeTool(NO_TOOL);
//#endif
//other initialization.
init_steppers();
drawFullMenu();
//#ifdef POLAR_PAINTER
// set_position(-CANVAS_PADDING, -CANVAS_PADDING);
//#endif
}
void waitButtonRelease(int pin){
while(digitalRead(pin)==LOW)
delay(10);
}
void printFullLine(int line, char* text){
#ifdef USE_LCD
lcd.setCursor(0, line);
if(strlen(text)>20){
char tmp = text[20];
text[20]=0;
lcd.print(text);
text[20] = tmp;
}
else{
lcd.print(text);
for(int i=strlen(text); i<20; i++){
lcd.print(' ');
}
}
#else
Serial.println(text);
#endif
}
////////////////
// MENU CONTROL
////////////////
const int TOTAL_MENU_COUNT = 3;
const char menus[TOTAL_MENU_COUNT][21]={
"1.Serial comm.","2.SD file", "3.Move home (0,0)"};
int selectedMenu=0;
void drawFullMenu(){
#ifdef USE_LCD
lcd.clear();
printFullLine(0, "# Select mode:");
drawMenu();
#endif
}
void drawMenu(){
#ifdef USE_LCD
printFullLine(1, (char *)menus[selectedMenu]);
#endif
}
void executeSelectedMenu(){
#ifdef USE_LCD
lcd.clear();
lcd.setCursor(0,0);
lcd.print("# ");
lcd.print(menus[selectedMenu]+2);
#endif
switch(selectedMenu){
case 0:
loopSerial();
break;
case 1:
loopFile();
break;
case 2:
gotoHome();
break;
}
waitButtonRelease(BUTTON1);
waitButtonRelease(BUTTON2);
}
////////////////
// FILE
////////////////
File root;
boolean sd_loaded=false;
void loopFile(){
if(root){
root.close();
}
printFullLine(1,"SD Init..");
delay(100);
if(!sd_loaded){
if (!SD.begin(SD_SELECT)) {
printFullLine(1, "SD failed!" );
delay(1000);
return;
}
}
sd_loaded=true;
printFullLine(1, "File:" );
root = SD.open("/");
File entry;
boolean fileSelected=false;
while(true) {
entry = root.openNextFile();
if (! entry) {
break;
}
printFullLine(2, entry.name());
while(digitalRead(BUTTON1)==HIGH){
delay(20);
if(digitalRead(BUTTON2)==LOW){
waitButtonRelease(BUTTON2);
fileSelected=true;
break;
}
}
if(fileSelected){
break;
}
waitButtonRelease(BUTTON1);
entry.close();
}
#ifdef USE_LCD
printFullLine(1, "Print: ");
lcd.setCursor(7, 1);
lcd.print(entry.name());
printFullLine(2, "Printing");
#endif
int startTime=millis()/1000;
if(entry){
int linecount = 0;
char chr;
while (entry.available()) {
if(digitalRead(BUTTON2)==LOW){
entry.close();
return;
}
chr = entry.read();
if(char_count && (chr == '\n')){
command[char_count]=0;
// printFullLine(3, command);
process_string(command, char_count);
char_count=0;
}
else
if(char_count<COMMAND_SIZE){
command[char_count++] = chr;
}
}
if(char_count && char_count<COMMAND_SIZE){
command[char_count]=0;
// printFullLine(3, command);
process_string(command, char_count);
char_count=0;
}
entry.close();
#ifdef USE_LCD
Serial.println("Finished ");
printFullLine(3, " ");
printFullLine(2, "Finished ");
lcd.setCursor(9,2);
lcd.print( millis() / 1000 - startTime );
lcd.print("sec");
#endif
while(digitalRead(BUTTON1)==HIGH && digitalRead(BUTTON2)==HIGH){
delay(20);
}
waitButtonRelease(BUTTON1);
waitButtonRelease(BUTTON2);
}
}
////////////////
// SERIAL
////////////////
void loopSerial(){
printFullLine(1, "Listening");
Serial.println("Listening");
byte no_data = 0;
char c;
while(true){
if (Serial.available() > 0)
{
c = Serial.read();
no_data = 0;
//newlines are ends of commands.
if (c != '\n')
{
command[char_count++] = c;
}
}
else
{
if(no_data<127)
no_data++;
delay(10);
}
//if theres a pause (1 second) or we got a real command, do it
if (char_count && (c == '\n' || no_data > 100))
{
// printFullLine(3, command);
process_string(command, char_count);
char_count=0;
}
if(digitalRead(BUTTON2)==LOW){
return;
}
}
}
void gotoHome(){
process_string("G21",3);
process_string("G90",3);
process_string("G1 X0.0 Y0.0",12);
}
void loop()
{
#ifndef USE_LCD
loopSerial();
#endif
if(digitalRead(BUTTON1)==LOW){
selectedMenu = (selectedMenu+1) % TOTAL_MENU_COUNT;
drawMenu();
waitButtonRelease(BUTTON1);
}
else if(digitalRead(BUTTON2)==LOW){
waitButtonRelease(BUTTON2);
executeSelectedMenu();
drawFullMenu();
}
else{
delay(30);
}
}