-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathutils.pde
278 lines (198 loc) · 5.84 KB
/
utils.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
// Return the [x,y] of the motor position in pixels
int[] getMotorPixelPos() {
int[] out = {
int (float (MotorX) / MotorStepsPerPixel) + MousePaperLeft,
int (float (MotorY) / MotorStepsPerPixel) + MousePaperTop + yBrushRestPositionPixels
};
return out;
}
// Get float distance between two non-encoded (x,y) positions.
float getDistance(int x1, int y1, int x2, int y2)
{
int xdiff = abs(x2 - x1);
int ydiff = abs(y2 - y1);
return sqrt(pow(xdiff, 2) + pow(ydiff, 2));
}
void scanSerial()
{
// Serial port search string:
int PortCount = 0;
String portName;
String str1, str2;
int j;
int OpenPortList[];
OpenPortList = new int[0];
SerialOnline = false;
boolean serialErr = false;
try {
PortCount = Serial.list().length;
}
catch (Exception e) {
e.printStackTrace();
serialErr = true;
}
if (serialErr == false)
{
println("\nI found "+PortCount+" serial ports, which are:");
println(Serial.list());
String os=System.getProperty("os.name").toLowerCase();
boolean isMacOs = os.startsWith("mac os x");
boolean isWin = os.startsWith("win");
if (isMacOs)
{
str1 = "/dev/tty.usbmodem"; // Can change to be the name of the port you want, e.g., COM5.
// The default value is "/dev/cu.usbmodem"; which works on Macs.
str1 = str1.substring(0, 14);
j = 0;
while (j < PortCount) {
str2 = Serial.list()[j].substring(0, 14);
if (str1.equals(str2) == true)
OpenPortList = append(OpenPortList, j);
j++;
}
} else if (isWin)
{
// All available ports will be listed.
j = 0;
while (j < PortCount) {
OpenPortList = append(OpenPortList, j);
j++;
}
} else {
// Assume linux
str1 = "/dev/ttyACM";
str1 = str1.substring(0, 11);
j = 0;
while (j < PortCount) {
if (Serial.list()[j].length() > 11) {
str2 = Serial.list()[j].substring(0, 11);
if (str1.equals(str2) == true)
OpenPortList = append(OpenPortList, j);
}
j++;
}
}
boolean portErr;
j = 0;
while (j < OpenPortList.length) {
portErr = false;
portName = Serial.list()[OpenPortList[j]];
try
{
myPort = new Serial(this, portName, 38400);
}
catch (Exception e)
{
SerialOnline = false;
portErr = true;
println("Serial port "+portName+" could not be activated.");
}
if (portErr == false)
{
myPort.buffer(1);
myPort.clear();
println("Serial port "+portName+" found and activated.");
String inBuffer = "";
myPort.write("v\r"); //Request version number
delay(50); // Delay for EBB to respond!
while (myPort.available () > 0) {
inBuffer = myPort.readString();
if (inBuffer != null) {
println("Version Number: "+inBuffer);
}
}
str1 = "EBB";
if (inBuffer.length() > 2)
{
str2 = inBuffer.substring(0, 3);
if (str1.equals(str2) == true)
{
// EBB Identified!
SerialOnline = true; // confirm that this port is good
j = OpenPortList.length; // break out of loop
println("Serial port "+portName+" confirmed to have EBB.");
} else
{
myPort.clear();
myPort.stop();
println("Serial port "+portName+": No EBB detected.");
}
}
}
j++;
}
}
}
// Only need to redraw if hovering or changing state
void redrawButtons() {
offScreen.beginDraw();
offScreen.background(0, 0);
DrawButtons(offScreen);
offScreen.endDraw();
imgButtons = offScreen.get(0, 0, offScreen.width, offScreen.height);
}
// Only need to redraw if hovering or change select on specific items
void redrawHighlight() {
offScreen.beginDraw();
offScreen.background(0, 0);
offScreen.endDraw();
imgHighlight = offScreen.get(0, 0, offScreen.width, offScreen.height);
// TODO: Remove this section?
}
// Draw the locator crosshair to the offscreen buffer and fill imgLocator with it
// Only need to redraw this when it changes color
void redrawLocator() {
offScreen.beginDraw();
offScreen.background(0, 0);
offScreen.stroke(0, 0, 0, 128);
offScreen.strokeWeight(2);
int x0 = 10;
int y0 = 10;
if (BrushDown)
offScreen.fill(PenColor);
else
offScreen.noFill();
offScreen.ellipse(x0, y0, 10, 10);
offScreen.line(x0 + 5, y0, x0 + 10, y0);
offScreen.line(x0 - 5, y0, x0 - 10, y0);
offScreen.line(x0, y0 + 5, x0, y0 + 10);
offScreen.line(x0, y0 - 5, x0, y0 - 10);
offScreen.endDraw();
imgLocator = offScreen.get(0, 0, 25, 25);
}
void zero()
{
// Mark current location as (0,0) in motor coordinates.
// Manually move the motor carriage to the left-rear (upper left) corner before executing this command.
MotorX = 0;
MotorY = 0;
moveStatus = -1;
MoveDestX = -1;
MoveDestY = -1;
// Calculate and animate position location cursor
int[] pos = getMotorPixelPos();
float sec = .25;
Ani.to(this, sec, "MotorLocatorX", pos[0]);
Ani.to(this, sec, "MotorLocatorY", pos[1]);
// if (debugMode) println("Motor X: " + MotorX + " Motor Y: " + MotorY);
}
void clearall()
{ // ***** CLEAR ALL *****
ToDoList = new PVector[0];
ToDoList = (PVector[]) append(ToDoList, new PVector(-30, 0)); //Command 30 (Raise pen)
ToDoList = (PVector[]) append(ToDoList, new PVector(-35, 0)); //Command 35 (Go home)
indexDone = -1; // Index in to-do list of last action performed
indexDrawn = -1; // Index in to-do list of last to-do element drawn to screen
drawToDoList();
Paused = false;
pause();
}
void quitApp()
{ // ***** QUIT *****
if (SerialOnline)
{
myPort.clear();
myPort.stop();
}
exit();
}