/
buttons_actions.pde
399 lines (351 loc) · 12.6 KB
/
buttons_actions.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
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
//------------------------------------------------------------------------------------
void addButtonsInSetup() {
controlP5 = new ControlP5(this);
controlP5.setFont(createFont("Quantico-Regular",12));
// debug things
controlP5.enableShortcuts();
myTextarea = controlP5.addTextarea("txt")
.setPosition(230, 400)
.setSize(600, 290)
.setFont(createFont("", 10))
.setLineHeight(14)
.setColor(color(200))
.setColorBackground(color(50, 55, 100))
.setColorForeground(color(255));
;
myTextarea.setVisible(false);
console = controlP5.addConsole(myTextarea);//
debugButton = controlP5.addButton("Debug", 4, 40, 600, 100, 30).setId(19);
controlP5.addButton("Open", 4, 855, 45, 70, 30).setId(3);
controlP5.addToggle("Repeating pattern mode", true, 855, 210, 20, 20).setId(4);
//controlP5.addToggle("UDP live pattern mode", true, 855, 255, 20, 20).setId(8);
controlP5.addButton("Go to row", 4, 855, 90, 110, 30).setId(5);
controlP5.addButton("Move pattern", 4, 855, 130, 130, 30).setId(6);
controlP5.addButton("Start edit image", 4, 855, 170, 160, 30).setId(7);
parametricSweaterButton = controlP5.addButton("Open parametric sweater", 4, 855, 460, 205, 30).setId(10);
startOpenKnit = controlP5.addButton("Start knitting", 4, 855, 500, 120, 30).setId(14);
startOpenKnit.setVisible(false);
setupGUIParametricSweater();
controlP5.addButton("Refresh", 4, 855, 270, 80, 25).setId(17);
controlP5.addButton("Close", 4, 955, 270, 80, 25).setId(18);
usbList = controlP5.addScrollableList("usbList", 855, 300, 200, 300).setId(8);
fillListUSB(usbList);
machineList = controlP5.addScrollableList("machine", 855, 380, 200, 300).setId(9);
fillListMachines(machineList);
machineList.update();
knittingTypeList = controlP5.addScrollableList("knittingType", 855, 550, 200, 300).setId(16);
fillListKnittingType(knittingTypeList);
// last id : 19
}
//------------------------------------------------------------------------------------
void fillListUSB(ScrollableList ddl) {
ddl.setBackgroundColor(color(190));
ddl.setItemHeight(20);
ddl.setBarHeight(30);
ddl.clear();
if (Serial.list().length==0) {
ddl.setCaptionLabel("No devices connected");
}
else if (Serial.list().length==1) {
ddl.addItem(Serial.list()[0], 0);
ddl.setValue(0);
}
else if (Serial.list().length>1) {
for (int i=0;i<Serial.list().length;i++) {
ddl.addItem(Serial.list()[i], i);
}
// try to found in list one usb selected
Boolean usbSelected = false;
for (int i=0;i<Serial.list().length;i++) { ///////////// to preselect usb uncomment that block
if (Serial.list()[i].equals(getUSBSelected())) {
ddl.setValue(i);
usbSelected = true;
}
}
if (!usbSelected) ddl.setCaptionLabel("Select usb port");
}
ddl.getCaptionLabel().getStyle().setMarginTop(3);
ddl.getCaptionLabel().getStyle().setMarginLeft(3);
ddl.getCaptionLabel().getStyle().setMarginTop(3);
ddl.setColorBackground(color(60));
ddl.setColorActive(color(255, 128));
ddl.setHeight(400 );
ddl.close();
}
//------------------------------------------------------------------------------------
void fillListMachines(ScrollableList ddl) {
ddl.setBackgroundColor(color(190));
ddl.setItemHeight(20);
ddl.setBarHeight(30);
ddl.clear();
machinesListName.add("Brother 930 / 940");
machinesListName.add("Openknit");
//usbListName.add("Brother 910");
//usbListName.add("Brother 950");
for (int i=0;i<machinesListName.size();i++) {
ddl.addItem(machinesListName.get(i), i);
}
Boolean machineSelected = false;
for (int i=0;i<machinesListName.size();i++) { ///////////// to preselect machine uncomment that block
if (machinesListName.get(i).equals(getMachineMode())) {
ddl.setValue(i);
machineSelected = true;
}
}
if (!machineSelected) ddl.setCaptionLabel("Select Machine");
ddl.getCaptionLabel().getStyle().setMarginTop(3);
ddl.getCaptionLabel().getStyle().setMarginLeft(3);
ddl.getCaptionLabel().getStyle().setMarginTop(3);
ddl.setColorBackground(color(60));
ddl.setColorActive(color(255, 128));
ddl.setHeight(400 );
ddl.close();
}
//------------------------------------------------------------------------------------
void fillListKnittingType(ScrollableList ddl) {
ddl.setBackgroundColor(color(190));
ddl.setItemHeight(20);
ddl.setBarHeight(30);
ddl.clear();
for (int i=0;i< my_brother.knittingTypeListName.size();i++) {
ddl.addItem( my_brother.knittingTypeListName.get(i), i);
}
Boolean knittingTypeSelected = false;
for (int i=0;i< my_brother.knittingTypeListName.size();i++) {
if ( my_brother.knittingTypeListName.get(i).equals(getKnittingType())) {
ddl.setValue(i);
knittingTypeSelected = true;
}
}
if (!knittingTypeSelected) ddl.setCaptionLabel("Select kind machine");
ddl.getCaptionLabel().getStyle().setMarginTop(3);
ddl.getCaptionLabel().getStyle().setMarginLeft(3);
ddl.getCaptionLabel().getStyle().setMarginTop(3);
ddl.setColorBackground(color(60));
ddl.setColorActive(color(255, 128));
ddl.setHeight(400 );
ddl.close();
}
//------------------------------------------------------------------------------------
void controlEvent(ControlEvent theEvent) {
if (theEvent.isGroup()) {
// check if the Event was triggered from a ControlGroup
println("event from group : "+theEvent.getGroup().getId()+" from "+theEvent.getGroup());
}
else if (theEvent.isController()) {
println(theEvent.controller().getId());
if (theEvent.controller().getId()==3) openknittingPattern();
if (theEvent.controller().getId()==4) repedPatternMode = !repedPatternMode;
if (theEvent.controller().getId()==5) jumpToRow();
if (theEvent.controller().getId()==6) howMuchPatternToLeft("");
if (theEvent.controller().getId()==7) changeEditPixels();
if (theEvent.controller().getId()==8) {
String devicePath = Serial.list()[(int)theEvent.getValue()];
saveUSBSelected(devicePath);
setupSerialConnection(devicePath);
}
if (theEvent.controller().getId()==9) {
String machineType=machinesListName.get((int)theEvent.getValue());
saveModelSelected(machineType);
setupTypeMachine(machineType);
showHideFeaturesOpenKnit(machineType);
}
if (theEvent.controller().getId()==10) {
createParametricSweater();
}
if (theEvent.controller().getId()==11)saveImagePattern();
if (theEvent.controller().getId()==12)applyParametricSweater();
if (theEvent.controller().getId()==13)saveSweaterAsInputImage();
if (theEvent.controller().getId()==14) {
if (nowKnitting_openKnit) {
startOpenKnit.setLabel("Start knitting");
stitch = 0;
current_row = 0;
status="r";
endLineStarted = true;
lastChangeHead = "left";
}
else {
startOpenKnit.setLabel("Pause");
}
nowKnitting_openKnit =!nowKnitting_openKnit;
}
if (theEvent.controller().getId()==16) {
saveKnittingType((String)my_brother.knittingTypeListName.get((int)theEvent.getValue()));
}
if (theEvent.controller().getId()==17) fillListUSB(usbList); //refresh
if (theEvent.controller().getId()==18) {
myPort.clear();
myPort.stop();
}
if (theEvent.controller().getId()==19) {
showHideFeaturesDebug();
}
}
if (theEvent.isAssignableFrom(Textfield.class)) {
println("controlEvent: accessing a string from controller '"
+theEvent.getName()+"': "
+theEvent.getStringValue()
);
//ns.generateSweater();
}
}
//------------------------------------------------------------------------------------
public void input(String theText) {
// automatically receives results from controller input
println("a textfield event for controller 'input' : "+theText);
}
//------------------------------------------------------------------------------------
void setupTypeMachine(String machineType) {
}
//------------------------------------------------------------------------------------
void jumpToRow() {
String new_current_row = JOptionPane.showInputDialog(frame, "To whish row you want to jump ?", Integer.toString(current_row));
if ( !Integer.toString(current_row).equals(new_current_row) ) {
current_row = Integer.valueOf(new_current_row);
sendtoKnittingMachine();
}
my_brother.jumpToRow();
}
//------------------------------------------------------------------------------------
void changeEditPixels() {
editPixels =! editPixels;
if (editPixels) {
cursor(CROSS);
controlP5.getController("Start edit image").setCaptionLabel("Stop edit image");
}
else {
controlP5.getController("Start edit image").setCaptionLabel("Start edit image");
cursor(ARROW);
}
}
//------------------------------------------------------------------------------------
void updateEditPixels() {
if (editPixels) {
}
else {
}
}
//------------------------------------------------------------------------------------
void openknittingPattern() {
selectInput("Select a file to process:", "fileSelected"); // Opens file chooser
}
//------------------------------------------------------------------------------------
void fileSelected(File selection) {
try {
if (selection != null) {
fillArrayWithImagePath(selection.getAbsolutePath());
}
}
catch(Exception e) {
}
}
//------------------------------------------------------------------------------------
void fillArrayWithImagePath(String imgPath) {
noLoop();
try {
PImage imgTemp = loadImage(imgPath);
fillArrayWithImage(imgTemp);
}
catch(Exception e) {
}
loop();
}
//------------------------------------------------------------------------------------
void fillArrayWithImage(PImage imgTemp) {
noLoop();
try {
img = imgTemp;
cols = img.width;
if (cols>200) {
JOptionPane.showMessageDialog(frame, "The image have more than 200 pixels", "Alert from Knitic", 2);
}
else {
laststitch = -1;
section = -1;
rows = img.height;
/*if (img.height>750) { //not in use??
posYOffSetPattern = (img.height*sizePixel)-750;
}
else {
posYOffSetPattern = 0;
}*/
endLineStarted = false;
lastEndLineStarted = false;
if (cols>0 && rows>0) loadPattern = true;
pixelArray = new int[cols][rows];
int restPixels = (200-cols);
leftStick = (100-(restPixels/2));
rightStick = 100-(restPixels/2);
if ( (100-leftStick)+cols+(100-rightStick) !=200) {
rightStick +=1;
}
if (cols!=200) {
howMuchPatternToLeft("");
}
img.loadPixels();
for (int y = 0; y <rows; y++) {
for (int x = 0; x < cols; x++) {
int loc = (cols-1)-x + y*cols;
if (brightness(img.pixels[loc]) > threshold ) { //&& alpha(img.pixels[loc])==1
pixelArray[x][y] = 0;
}
else {//if (alpha(img.pixels[loc])==1)
pixelArray[x][y] = 1;
}
/*
else {
pixelArray[x][y] = 2;
}
*/
}
}
status = "r";
// send first line
sendtoKnittingMachine();
my_brother.resetPassDoubleBed();
}
}
catch(Exception e) {
println("error filling array");
}
loop();
}
//------------------------------------------------------------------------------------
void howMuchPatternToLeft(String message) {
try {
String userStartStick="";
if (message=="") {
userStartStick = JOptionPane.showInputDialog(frame, "Do you want to start from left " +Integer.toString(leftStick)+"?", Integer.toString(leftStick));
}
else {
userStartStick = JOptionPane.showInputDialog(frame, message, Integer.toString(cols-100));
}
if (!userStartStick.equals(Integer.toString(leftStick))) {
if ((100-(Integer.valueOf(userStartStick)))+cols>200 ) {
howMuchPatternToLeft("Is not possible to put that right. The maxium is "+Integer.toString((cols-100)));
}
else {
leftStick = Integer.valueOf(userStartStick);
rightStick = (cols+(100-leftStick))-100;
}
}
sendtoKnittingMachine();
}
catch(Exception e) {
}
}
//------------------------------------------------------------------------------------
void showHideFeaturesDebug() {
if (debugMode == false) {
myTextarea.setVisible(true);
debugButton.setLabel("Close Debug");
debugMode = true;
}
else {
myTextarea.setVisible(false);
debugButton.setLabel("Debug");
debugMode = false;
}
}
//------------------------------------------------------------------------------------