forked from euphy/polargraph_server_a1
-
Notifications
You must be signed in to change notification settings - Fork 1
/
pixel.ino
424 lines (361 loc) · 11.4 KB
/
pixel.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
/**
* Polargraph Server. - CORE
* Written by Sandy Noble
* Released under GNU License version 3.
* http://www.polargraph.co.uk
* https://github.com/euphy/polargraph_server_a1
Pixel.
This is one of the core files for the polargraph server program.
This is a biggie, and has the routines necessary for generating and drawing
the squarewave and scribble pixel styles.
*/
#ifdef PIXEL_DRAWING
void pixel_changeDrawingDirection()
{
globalDrawDirectionMode = atoi(inParam1);
globalDrawDirection = atoi(inParam2);
// Serial.print(F("Changed draw direction mode to be "));
// Serial.print(globalDrawDirectionMode);
// Serial.print(F(" and direction is "));
// Serial.println(globalDrawDirection);
}
void pixel_drawSquarePixel()
{
long originA = multiplier(atol(inParam1));
long originB = multiplier(atol(inParam2));
int size = multiplier(atoi(inParam3));
int density = atoi(inParam4);
/* Here density is accepted as a recording of BRIGHTNESS, where 0 is black and 255 is white.
Later on, density gets scaled to the range that is available for this particular
pixel+pentip combination, and also inverted so that it becomes a recording of DARKNESS,
where 0 is white and the higher values are darker.
(Using the same variable to save on space, really.)
This is because paper is white, and ink is black, and this density value is used to
control how many waves are drawn.
O waves means no ink, so a very light pixel.
50 waves means lots of ink, so a much darker pixel.
*/
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;
}
/* pixel_scaleDensity takes it's input value as a BRIGHTNESS value (ie 255 = white),
but returns a DARKNESS value (ie 0 = white).
Here I'm using the same variable to hold both, save space in memory. */
density = pixel_scaleDensity(density, 255, pixel_maxDensity(penWidth, size));
// Serial.print(F("Start point: "));
// Serial.print(startPointA);
// Serial.print(COMMA);
// Serial.print(startPointB);
// Serial.print(F(". end point: "));
// Serial.print(endPointA);
// Serial.print(COMMA);
// Serial.print(endPointB);
// Serial.println(F("."));
changeLength(startPointA, startPointB);
if (density > 1)
{
pixel_drawSquarePixel(size, size, density, globalDrawDirection);
}
changeLength(endPointA, endPointB);
//outputAvailableMemory();
}
byte pixel_getRandomDrawDirection()
{
return random(1, 5);
}
byte pixel_getAutoDrawDirection(long targetA, long targetB, long sourceA, long sourceB)
{
byte dir = DIR_SE;
if (targetA<sourceA && targetB<sourceA)
dir = DIR_NW;
else if (targetA>sourceA && targetB>sourceB)
dir = DIR_SE;
else if (targetA<sourceA && targetB>sourceB)
dir = DIR_SW;
else if (targetA>sourceA && targetB<sourceB)
dir = DIR_NE;
else if (targetA==sourceA && targetB<sourceB)
dir = DIR_NE;
else if (targetA==sourceA && targetB>sourceB)
dir = DIR_SW;
else if (targetA<sourceA && targetB==sourceB)
dir = DIR_NW;
else if (targetA>sourceA && targetB==sourceB)
dir = DIR_SE;
else
{
// Serial.println("Not calculated - default SE");
}
return dir;
}
void pixel_drawScribblePixel()
{
long originA = multiplier(atol(inParam1));
long originB = multiplier(atol(inParam2));
int size = multiplier(atoi(inParam3));
int density = atoi(inParam4);
int maxDens = pixel_maxDensity(penWidth, size);
density = pixel_scaleDensity(density, 255, maxDens);
pixel_drawScribblePixel(originA, originB, size*1.1, density);
// outputAvailableMemory();
}
void pixel_drawScribblePixel(long originA, long originB, int size, int density)
{
// int originA = motorA.currentPosition();
// int originB = motorB.currentPosition();
long lowLimitA = originA-(size/2);
long highLimitA = lowLimitA+size;
long lowLimitB = originB-(size/2);
// long highLimitB = lowLimitB+size;
int randA;
int randB;
int inc = 0;
int currSize = size;
for (int i = 0; i <= density; i++)
{
randA = random(0, currSize);
randB = random(0, currSize);
changeLength(lowLimitA+randA, lowLimitB+randB);
lowLimitA-=inc;
highLimitA+=inc;
currSize+=inc*2;
}
}
int pixel_minSegmentSizeForPen(float penSize)
{
float penSizeInSteps = penSize * stepsPerMM;
int minSegSize = 1;
if (penSizeInSteps >= 2.0)
minSegSize = int(penSizeInSteps);
// Serial.print(F("Min segment size for penSize "));
// Serial.print(penSize);
// Serial.print(F(": "));
// Serial.print(minSegSize);
// Serial.print(F(" steps."));
// Serial.println();
return minSegSize;
}
int pixel_maxDensity(float penSize, int rowSize)
{
float rowSizeInMM = mmPerStep * rowSize;
#ifdef DEBUG_PIXEL
Serial.print(F("MSG,D,rowsize in mm: "));
Serial.print(rowSizeInMM);
Serial.print(F(", mmPerStep: "));
Serial.print(mmPerStep);
Serial.print(F(", rowsize: "));
Serial.println(rowSize);
#endif
float numberOfSegments = rowSizeInMM / penSize;
int maxDens = 1;
if (numberOfSegments >= 2.0)
maxDens = int(numberOfSegments);
if (maxDens <= 2)
{
Serial.print("num of segments float:");
Serial.println(numberOfSegments);
Serial.print(F("MSG,I,Max density for penSize: "));
Serial.print(penSize);
Serial.print(F(", rowSize: "));
Serial.print(rowSize);
Serial.print(F(" is "));
Serial.println(maxDens);
Serial.println(F("MSG,I,You probably won't get any detail in this."));
}
//
//
return maxDens;
}
int pixel_scaleDensity(int inDens, int inMax, int outMax)
{
float reducedDens = (float(inDens) / float(inMax)) * float(outMax);
reducedDens = outMax-reducedDens;
// Serial.print(F("inDens:"));
// Serial.print(inDens);
// Serial.print(F(", inMax:"));
// Serial.print(inMax);
// Serial.print(F(", outMax:"));
// Serial.print(outMax);
// Serial.print(F(", reduced:"));
// Serial.println(reducedDens);
// round up if bigger than .5
int result = int(reducedDens);
if (reducedDens - (result) > 0.5)
result ++;
return result;
}
void pixel_drawSquarePixel(int length, int width, int density, byte drawDirection)
{
// work out how wide each segment should be
int segmentLength = 0;
if (density > 0)
{
// work out some segment widths
int basicSegLength = length / density;
int basicSegRemainder = length % density;
float remainderPerSegment = float(basicSegRemainder) / float(density);
float totalRemainder = 0.0;
int lengthSoFar = 0;
// Serial.print("Basic seg length:");
// Serial.print(basicSegLength);
// Serial.print(", basic seg remainder:");
// Serial.print(basicSegRemainder);
// Serial.print(", remainder per seg");
// Serial.println(remainderPerSegment);
for (int i = 0; i <= density; i++)
{
totalRemainder += remainderPerSegment;
if (totalRemainder >= 1.0)
{
totalRemainder -= 1.0;
segmentLength = basicSegLength+1;
}
else
{
segmentLength = basicSegLength;
}
if (drawDirection == DIR_SE) {
pixel_drawSquareWaveAlongAxis(motorA, motorB, width, segmentLength, density, i);
}
else if (drawDirection == DIR_SW) {
pixel_drawSquareWaveAlongAxis(motorB, motorA, width, segmentLength, density, i);
}
else if (drawDirection == DIR_NW) {
segmentLength = 0 - segmentLength; // reverse
pixel_drawSquareWaveAlongAxis(motorA, motorB, width, segmentLength, density, i);
}
else if (drawDirection == DIR_NE) {
segmentLength = 0 - segmentLength; // reverse
pixel_drawSquareWaveAlongAxis(motorB, motorA, width, segmentLength, density, i);
}
lengthSoFar += segmentLength;
// Serial.print("distance so far:");
// Serial.print(distanceSoFar);
reportPosition();
} // end of loop
}
}
void pixel_drawSquareWaveAlongAxis(AccelStepper &longAxis, AccelStepper &wideAxis, int waveAmplitude, int waveLength, int totalWaves, int waveNo)
{
if (waveNo == 0)
{
// first one, half a line and an along
Serial.println(F("First wave half"));
if (lastWaveWasTop) {
moveAxis(wideAxis, waveAmplitude/2);
moveAxis(longAxis, waveLength);
}
else {
moveAxis(wideAxis, 0-(waveAmplitude/2));
moveAxis(longAxis, waveLength);
}
pixel_flipWaveDirection();
}
else if (waveNo == totalWaves)
{
// last one, half a line with no along
if (lastWaveWasTop) {
moveAxis(wideAxis, waveAmplitude/2);
}
else {
moveAxis(wideAxis, 0-(waveAmplitude/2));
}
}
else
{
// intervening lines - full lines, and an along
if (lastWaveWasTop) {
moveAxis(wideAxis, waveAmplitude);
moveAxis(longAxis, waveLength);
}
else {
moveAxis(wideAxis, 0-waveAmplitude);
moveAxis(longAxis, waveLength);
}
pixel_flipWaveDirection();
}
}
void pixel_flipWaveDirection()
{
if (lastWaveWasTop)
lastWaveWasTop = false;
else
lastWaveWasTop = true;
}
void pixel_testPenWidth()
{
int rowWidth = multiplier(atoi(inParam1));
float startWidth = atof(inParam2);
float endWidth = atof(inParam3);
float incSize = atof(inParam4);
int tempDirectionMode = globalDrawDirectionMode;
globalDrawDirectionMode = DIR_MODE_PRESET;
float oldPenWidth = penWidth;
int iterations = 0;
for (float pw = startWidth; pw <= endWidth; pw+=incSize)
{
iterations++;
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);
pixel_drawSquarePixel(rowWidth, rowWidth, maxDens, DIR_SE);
}
penWidth = oldPenWidth;
moveAxis(motorB, 0-rowWidth);
for (int i = 1; i <= iterations; i++)
{
moveAxis(motorB, 0-(rowWidth/2));
moveAxis(motorA, 0-rowWidth);
moveAxis(motorB, rowWidth/2);
}
penWidth = oldPenWidth;
globalDrawDirectionMode = tempDirectionMode;
}
#endif