-
Notifications
You must be signed in to change notification settings - Fork 18
/
impl_ps.ino
478 lines (420 loc) · 11.9 KB
/
impl_ps.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
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
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
/**
* Polargraph Server for ATMEGA1280+
* Written by Sandy Noble
* Released under GNU License version 3.
* http://www.polargraph.co.uk
* https://github.com/euphy/polargraph_server_polarshield
Specific features for Polarshield / arduino mega.
Implementation.
So this file is the interface for the extra features available in the
mega/polarshield version of the polargraph.
*/
/* Implementation of executeCommand for MEGA-sized boards that
have command storage features. */
void impl_processCommand(String com)
{
lcd_echoLastCommandToDisplay(com, "usb:");
// check for change mode commands
if (com.startsWith(CMD_MODE_STORE_COMMANDS)
|| com.startsWith(CMD_MODE_LIVE))
{
Serial.println("Changing mode.");
impl_executeCommand(com);
}
// else execute / store the command
else if (storeCommands)
{
Serial.print(F("Storing command:"));
Serial.println(com);
sd_storeCommand(com);
}
else
{
impl_executeCommand(com);
}
}
/**
* This includes the extra commands the MEGA is capable of handling.
* It tries to run the command using the core executeBasicCommand
* first, but if that doesn't work, then it will go through
* it's own decision tree to try and run one of the additional
* routines.
*/
void impl_executeCommand(String &com)
{
if (exec_executeBasicCommand(com))
{
// that's nice, it worked
//Serial.println("Executed basic.");
}
else if (com.startsWith(CMD_DRAWCIRCLEPIXEL))
curves_pixel_drawCircularPixel();
// else if (com.startsWith(CMD_TESTPATTERN))
// testPattern();
else if (com.startsWith(CMD_TESTPENWIDTHSCRIBBLE))
impl_pixel_testPenWidthScribble();
else if (com.startsWith(CMD_DRAWSAWPIXEL))
impl_pixel_drawSawtoothPixel();
else if (com.startsWith(CMD_DRAWDIRECTIONTEST))
impl_exec_drawTestDirectionSquare();
else if (com.startsWith(CMD_MODE_STORE_COMMANDS))
impl_exec_changeToStoreCommandMode();
else if (com.startsWith(CMD_MODE_LIVE))
impl_exec_changeToLiveCommandMode();
else if (com.startsWith(CMD_MODE_EXEC_FROM_STORE))
impl_exec_execFromStore();
else if (com.startsWith(CMD_RANDOM_DRAW))
drawRandom();
else if (com.startsWith(CMD_SET_ROVE_AREA))
rove_setRoveArea();
else if (com.startsWith(CMD_START_TEXT))
rove_startText();
else if (com.startsWith(CMD_DRAW_SPRITE))
sprite_drawSprite();
else if (com.startsWith(CMD_DRAW_RANDOM_SPRITE))
sprite_drawRandomPositionedSprite();
else if (com.startsWith(CMD_CHANGELENGTH_RELATIVE))
exec_changeLength();
else if (com.startsWith(CMD_SWIRLING))
rove_controlSwirling();
else if (com.startsWith(CMD_DRAW_NORWEGIAN))
rove_drawNorwegianFromFile();
else if (com.startsWith(CMD_DRAW_NORWEGIAN_OUTLINE))
rove_drawRoveAreaFittedToImage();
else if (com.startsWith(CMD_AUTO_CALIBRATE))
calibrate_doCalibration();
else if (com.startsWith(CMD_SET_DEBUGCOMMS))
impl_setDebugComms();
else
{
comms_unrecognisedCommand(com);
comms_ready();
}
inNoOfParams = 0;
}
/** Polarshield implementation of runBackgroundProcesses. This is basically stuff to do with
the screen.
*/
void impl_runBackgroundProcesses()
{
#ifdef USE_LCD
lcd_checkForInput();
lcd_updateDisplay();
#endif
long motorCutoffTime = millis() - lastOperationTime;
if ((automaticPowerDown) && (powerIsOn) && (motorCutoffTime > motorIdleTimeBeforePowerDown))
{
Serial.println("Powering down because of inactivity.");
lcd_runEndScript();
lcd_updateDisplay();
}
if (swirling)
rove_swirl();
}
void impl_loadMachineSpecFromEeprom()
{
}
void impl_exec_execFromStore()
{
String fileToExec = inParam1;
if (fileToExec != "")
{
currentlyDrawingFromFile = true;
Serial.print("Filename to read from: ");
Serial.println(fileToExec);
commandFilename = fileToExec;
impl_exec_execFromStore(commandFilename);
currentlyDrawingFromFile = true;
}
else
{
Serial.println("No filename supplied to read from.");
}
}
void impl_exec_execFromStore(String inFilename)
{
if (inFilename != "")
{
String noBlanks = "";
// remove blanks
for (int i = 0; i<inFilename.length(); i++)
{
if (inFilename[i] != ' ')
noBlanks = noBlanks + inFilename[i];
}
char filename[noBlanks.length()+1];
noBlanks.toCharArray(filename, noBlanks.length()+1);
#ifdef DEBUG_SD
Serial.print("Array to read from: ");
Serial.println(filename);
#endif
File readFile = SD.open(filename, FILE_READ);
if (readFile)
{
Serial.print("Opened file:");
Serial.println(noBlanks);
String command = "";
while (readFile.available() && currentlyDrawingFromFile)
{
#ifdef DEBUG_SD
Serial.println("Reading...");
// poll for input
#endif
char ch = readFile.read();
#ifdef DEBUG_SD
Serial.print(".");
Serial.print(ch);
Serial.print("-");
#endif
if (ch == INTERMINATOR || ch == SEMICOLON)
{
#ifdef DEBUG_SD
Serial.println("New line");
#endif
// execute the line
command.trim();
command.toCharArray(lastCommand, INLENGTH+1);
boolean commandParsed = comms_parseCommand(lastCommand);
if (commandParsed)
{
#ifdef DEBUG_SD
Serial.println("Stored command parsed.");
#endif
Serial.print(F("Executing command:"));
Serial.println(command);
if (echoingStoredCommands) lcd_echoLastCommandToDisplay(command, inFilename+": ");
impl_executeCommand(command);
}
#ifdef DEBUG_SD
else Serial.println("Stored command WAS NOT parsed.");
#endif
command = "";
lcd_checkForInput();
}
else
command += ch;
#ifdef DEBUG_SD
Serial.print("Command building:");
Serial.println(command);
#endif
}
Serial.println("Finished with the file.");
currentlyDrawingFromFile = false;
readFile.close();
}
else
{
Serial.println("Couldn't find that file, btw.");
currentlyDrawingFromFile = false;
}
}
else
{
Serial.println("No filename supplied to read from.");
currentlyDrawingFromFile = false;
}
}
void impl_exec_changeToStoreCommandMode()
{
String newfilename = inParam1;
String newFile = inParam2;
if (newfilename != "")
{
Serial.print("Filename for command store: ");
Serial.println(newfilename);
storeCommands = true;
commandFilename = newfilename;
if (newFile.equals("R"))
{
// delete file if it exists
char filename[newfilename.length()+1];
newfilename.toCharArray(filename, newfilename.length()+1);
if (SD.exists(filename))
{
// file exists
Serial.println(F("File already exists."));
boolean removed = SD.remove(filename);
if (removed)
Serial.println(F("File removed."));
}
}
}
else
{
Serial.println("No filename supplied to write to.");
}
}
void impl_exec_changeToLiveCommandMode()
{
Serial.println(F("Changing back to live mode."));
storeCommands = false;
}
void impl_pixel_testPenWidthScribble()
{
int rowWidth = multiplier(atoi(inParam1));
float startWidth = atof(inParam2);
float endWidth = atof(inParam3);
float incSize = atof(inParam4);
boolean ltr = true;
float oldPenWidth = penWidth;
int iterations = 0;
int posA = motorA.currentPosition();
int posB = motorB.currentPosition();
int startColumn = posA;
int startRow = posB;
for (float pw = startWidth; pw <= endWidth; pw+=incSize)
{
iterations++;
int column = posA;
penWidth = pw;
int maxDens = pixel_maxDensity(penWidth, rowWidth);
Serial.print(F("Penwidth test "));
Serial.print(iterations);
Serial.print(F(", pen width: "));
Serial.print(penWidth);
Serial.print(F(", max density: "));
Serial.println(maxDens);
for (int density = maxDens; density >= 0; density--)
{
pixel_drawScribblePixel(posA, posB, rowWidth, density);
posB+=rowWidth;
}
posA+=rowWidth;
posB = startRow;
}
changeLength(long(posA-(rowWidth/2)), long(startRow-(rowWidth/2)));
penWidth = oldPenWidth;
moveB(0-rowWidth);
for (int i = 1; i <= iterations; i++)
{
moveB(0-(rowWidth/2));
moveA(0-rowWidth);
moveB(rowWidth/2);
}
penWidth = oldPenWidth;
}
void impl_engageMotors()
{
motorA.enableOutputs();
motorB.enableOutputs();
powerIsOn = true;
motorA.runToNewPosition(motorA.currentPosition()+32);
motorB.runToNewPosition(motorB.currentPosition()+32);
motorA.runToNewPosition(motorA.currentPosition()-32);
motorB.runToNewPosition(motorB.currentPosition()-32);
Serial.println("Engaged motors.");
}
void impl_releaseMotors()
{
motorA.disableOutputs();
motorB.disableOutputs();
powerIsOn = false;
Serial.println("Released motors");
}
void drawRandom()
{
for (int i = 0; i < 1000; i++)
{
Serial.print("Drawing:");
Serial.println(i);
while (motorA.distanceToGo() != 0 && motorB.distanceToGo() != 0)
{
motorA.run();
motorB.run();
}
if (motorA.distanceToGo() == 0)
{
int r = random(-2,3);
motorA.move(r);
Serial.print("Chosen new A target: ");
Serial.println(r);
}
if (motorB.distanceToGo() == 0)
{
int r = random(-2,3);
motorB.move(r);
Serial.print("Chosen new B target: ");
Serial.println(r);
}
reportPosition();
}
}
void impl_exec_drawTestDirectionSquare()
{
int rowWidth = multiplier(atoi(inParam1));
int segments = atoi(inParam2);
pixel_drawSquarePixel(rowWidth, rowWidth, segments, DIR_SE);
moveA(rowWidth*2);
pixel_drawSquarePixel(rowWidth, rowWidth, segments, DIR_SW);
moveB(rowWidth*2);
pixel_drawSquarePixel(rowWidth, rowWidth, segments, DIR_NW);
moveA(0-(rowWidth*2));
pixel_drawSquarePixel(rowWidth, rowWidth, segments, DIR_NE);
moveB(0-(rowWidth*2));
}
void impl_pixel_drawSawtoothPixel()
{
long originA = multiplier(atol(inParam1));
long originB = multiplier(atol(inParam2));
int size = multiplier(atoi(inParam3));
int density = atoi(inParam4);
int halfSize = size / 2;
long startPointA;
long startPointB;
long endPointA;
long endPointB;
int calcFullSize = halfSize * 2; // see if there's any rounding errors
int offsetStart = size - calcFullSize;
if (globalDrawDirectionMode == DIR_MODE_AUTO)
globalDrawDirection = pixel_getAutoDrawDirection(originA, originB, motorA.currentPosition(), motorB.currentPosition());
if (globalDrawDirection == DIR_SE)
{
// Serial.println(F("d: SE"));
startPointA = originA - halfSize;
startPointA += offsetStart;
startPointB = originB;
endPointA = originA + halfSize;
endPointB = originB;
}
else if (globalDrawDirection == DIR_SW)
{
// Serial.println(F("d: SW"));
startPointA = originA;
startPointB = originB - halfSize;
startPointB += offsetStart;
endPointA = originA;
endPointB = originB + halfSize;
}
else if (globalDrawDirection == DIR_NW)
{
// Serial.println(F("d: NW"));
startPointA = originA + halfSize;
startPointA -= offsetStart;
startPointB = originB;
endPointA = originA - halfSize;
endPointB = originB;
}
else //(drawDirection == DIR_NE)
{
// Serial.println(F("d: NE"));
startPointA = originA;
startPointB = originB + halfSize;
startPointB -= offsetStart;
endPointA = originA;
endPointB = originB - halfSize;
}
density = pixel_scaleDensity(density, 255, pixel_maxDensity(penWidth, size));
changeLength(startPointA, startPointB);
if (density > 1)
{
pixel_drawWavePixel(size, size, density, globalDrawDirection, SAW_SHAPE);
}
changeLength(endPointA, endPointB);
}
void impl_setDebugComms() {
int debugCommsValue = atoi(inParam1);
switch (debugCommsValue) {
case 0: debugComms = false; break;
case 1: debugComms = true; break;
}
}