-
Notifications
You must be signed in to change notification settings - Fork 62
/
polargraphcontroller.pde
3041 lines (2591 loc) · 86.2 KB
/
polargraphcontroller.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
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
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
Polargraph controller
Copyright Sandy Noble 2018.
This file is part of Polargraph Controller.
Polargraph Controller is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Polargraph Controller is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Polargraph Controller. If not, see <http://www.gnu.org/licenses/>.
Requires the excellent ControlP5 GUI library available from http://www.sojamo.de/libraries/controlP5/.
Requires the excellent Geomerative library available from http://www.ricardmarxer.com/geomerative/.
This is an application for controlling a polargraph machine, communicating using ASCII command language over a serial link.
sandy.noble@gmail.com
http://www.polargraph.co.uk
https://github.com/euphy/polargraphcontroller
*/
//import processing.video.*;
import diewald_CV_kit.libraryinfo.*;
import diewald_CV_kit.utility.*;
import diewald_CV_kit.blobdetection.*;
import geomerative.*;
import java.util.zip.CRC32;
// for OSX
import java.text.*;
import java.util.*;
import java.io.*;
import java.util.logging.*;
import javax.swing.*;
import processing.serial.*;
import controlP5.*;
import java.awt.event.KeyEvent;
import java.awt.event.*;
import java.awt.Frame;
import java.awt.BorderLayout;
import java.lang.reflect.Method;
int majorVersionNo = 2;
int minorVersionNo = 6;
int buildNo = 0;
String programTitle = "Polargraph Controller v" + majorVersionNo + "." + minorVersionNo + " build " + buildNo;
ControlP5 cp5;
Map<String, ControlP5> cp5s = new HashMap<String, ControlP5>();
boolean drawbotReady = false;
boolean drawbotConnected = false;
static final int HARDWARE_VER_UNO = 1;
static final int HARDWARE_VER_MEGA = 100;
static final int HARDWARE_VER_MEGA_POLARSHIELD = 200;
static final int HARDWARE_VER_POLARPRO = 300;
int currentHardware = HARDWARE_VER_MEGA_POLARSHIELD;
final int HARDWARE_ATMEGA328_SRAM = 2048;
final int HARDWARE_ATMEGA1280_SRAM = 8096;
int currentSram = HARDWARE_ATMEGA328_SRAM;
String newMachineName = "PGXXABCD";
PVector machinePosition = new PVector(130.0, 50.0);
float machineScaling = 1.0;
DisplayMachine displayMachine = null;
int homeALengthMM = 400;
int homeBLengthMM = 400;
// preset sizes - these can be referred to in the properties file
// and will be automatically converted to numbers when loaded.
final String PRESET_A3_SHORT = "A3SHORT";
final String PRESET_A3_LONG = "A3LONG";
final String PRESET_A2_SHORT = "A2SHORT";
final String PRESET_A2_LONG = "A2LONG";
final String PRESET_A2_IMP_SHORT = "A2+SHORT";
final String PRESET_A2_IMP_LONG = "A2+LONG";
final String PRESET_A1_SHORT = "A1SHORT";
final String PRESET_A1_LONG = "A1LONG";
final int A3_SHORT = 297;
final int A3_LONG = 420;
final int A2_SHORT = 418;
final int A2_LONG = 594;
final int A2_IMP_SHORT = 450;
final int A2_IMP_LONG = 640;
final int A1_SHORT = 594;
final int A1_LONG = 841;
int leftEdgeOfQueue = 800;
int rightEdgeOfQueue = 1100;
int topEdgeOfQueue = 0;
int bottomEdgeOfQueue = 0;
int queueRowHeight = 15;
int baudRate = 57600;
Serial myPort; // The serial port
int[] serialInArray = new int[1]; // Where we'll put what we receive
int serialCount = 0; // A count of how many bytes we receive
boolean[] keys = new boolean[526];
final JFileChooser chooser = new JFileChooser();
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yy hh:mm:ss");
String commandStatus = "Waiting for a click.";
float sampleArea = 10;
float gridSize = 75.0;
float currentPenWidth = 0.8;
float penIncrement = 0.05;
int penLiftDownPosition = 90;
int penLiftUpPosition = 180;
// this variable controls how big the pixels are scaled when drawn.
// 1.0 represents full size, 2.0 would be twice as big as the grid size,
// 0.5 would be half the grid size.
float pixelScalingOverGridSize = 1.0;
float currentMachineMaxSpeed = 600.0;
float currentMachineAccel = 400.0;
float MACHINE_ACCEL_INCREMENT = 25.0;
float MACHINE_MAXSPEED_INCREMENT = 25.0;
List<String> commandQueue = new ArrayList<String>();
List<String> realtimeCommandQueue = new ArrayList<String>();
List<String> commandHistory = new ArrayList<String>();
List<String> machineMessageLog = new ArrayList<String>();
List<PreviewVector> previewCommandList = new ArrayList<PreviewVector>();
long lastCommandQueueHash = 0L;
File lastImageDirectory = null;
File lastPropertiesDirectory = null;
String lastCommand = "";
String lastDrawingCommand = "";
Boolean commandQueueRunning = false;
static final int DRAW_DIR_NE = 1;
static final int DRAW_DIR_SE = 2;
static final int DRAW_DIR_SW = 3;
static final int DRAW_DIR_NW = 4;
static final int DRAW_DIR_N = 5;
static final int DRAW_DIR_E = 6;
static final int DRAW_DIR_S = 7;
static final int DRAW_DIR_W = 8;
static final int DRAW_DIR_MODE_AUTO = 1;
static final int DRAW_DIR_MODE_PRESET = 2;
static final int DRAW_DIR_MODE_RANDOM = 3;
static int pixelDirectionMode = DRAW_DIR_MODE_PRESET;
static final int PIXEL_STYLE_SQ_FREQ = 0;
static final int PIXEL_STYLE_SQ_SIZE = 1;
static final int PIXEL_STYLE_SQ_SOLID = 2;
static final int PIXEL_STYLE_SCRIBBLE = 3;
static final int PIXEL_STYLE_CIRCLE = 4;
static final int PIXEL_STYLE_SAW = 5;
PVector currentMachinePos = new PVector();
PVector currentCartesianMachinePos = new PVector();
int machineAvailMem = 0;
int machineUsedMem = 0;
int machineMinAvailMem = 2048;
//String testPenWidthCommand = "TESTPENWIDTHSCRIBBLE,";
String testPenWidthCommand = CMD_TESTPENWIDTHSQUARE;
float testPenWidthStartSize = 0.5;
float testPenWidthEndSize = 2.0;
float testPenWidthIncrementSize = 0.5;
int machineStepMultiplier = 8;
int maxSegmentLength = 2;
static final String MODE_BEGIN = "button_mode_begin";
static final String MODE_DRAW_OUTLINE_BOX = "button_mode_drawOutlineBox";
static final String MODE_DRAW_OUTLINE_BOX_ROWS = "button_mode_drawOutlineBoxRows";
static final String MODE_DRAW_SHADE_BOX_ROWS_PIXELS = "button_mode_drawShadeBoxRowsPixels";
static final String MODE_RENDER_SQUARE_PIXELS = "button_mode_renderSquarePixel";
static final String MODE_RENDER_SAW_PIXELS = "button_mode_renderSawPixel";
static final String MODE_RENDER_CIRCLE_PIXELS = "button_mode_renderCirclePixel";
static final String MODE_RENDER_PIXEL_DIALOG = "button_mode_drawPixelsDialog";
static final String MODE_INPUT_ROW_START = "button_mode_inputRowStart";
static final String MODE_INPUT_ROW_END = "button_mode_inputRowEnd";
static final String MODE_DRAW_TESTPATTERN = "button_mode_drawTestPattern";
static final String MODE_DRAW_GRID = "button_mode_drawGrid";
static final String MODE_PLACE_IMAGE = "button_mode_placeImage";
static final String MODE_LOAD_IMAGE = "button_mode_loadImage";
static final String MODE_PAUSE_QUEUE = "button_mode_pauseQueue";
static final String MODE_RUN_QUEUE = "button_mode_runQueue";
static final String MODE_SET_POSITION_HOME = "button_mode_setPositionHome";
static final String MODE_RETURN_TO_HOME = "button_mode_returnToHome";
static final String MODE_INPUT_SINGLE_PIXEL = "button_mode_inputSinglePixel";
static final String MODE_DRAW_TEST_PENWIDTH = "button_mode_drawTestPenWidth";
static final String MODE_RENDER_SCALED_SQUARE_PIXELS = "button_mode_renderScaledSquarePixels";
static final String MODE_RENDER_SOLID_SQUARE_PIXELS = "button_mode_renderSolidSquarePixels";
static final String MODE_RENDER_SCRIBBLE_PIXELS = "button_mode_renderScribblePixels";
static final String MODE_CHANGE_MACHINE_SPEC = "button_mode_changeMachineSpec";
static final String MODE_REQUEST_MACHINE_SIZE = "button_mode_requestMachineSize";
static final String MODE_RESET_MACHINE = "button_mode_resetMachine";
static final String MODE_SAVE_PROPERTIES = "button_mode_saveProperties";
static final String MODE_SAVE_AS_PROPERTIES = "button_mode_saveAsProperties";
static final String MODE_LOAD_PROPERTIES = "button_mode_loadProperties";
static final String MODE_INC_SAMPLE_AREA = "button_mode_incSampleArea";
static final String MODE_DEC_SAMPLE_AREA = "button_mode_decSampleArea";
static final String MODE_INPUT_IMAGE = "button_mode_inputImage";
static final String MODE_IMAGE_PIXEL_BRIGHT_THRESHOLD = "numberbox_mode_pixelBrightThreshold";
static final String MODE_IMAGE_PIXEL_DARK_THRESHOLD = "numberbox_mode_pixelDarkThreshold";
static final String MODE_CONVERT_BOX_TO_PICTUREFRAME = "button_mode_convertBoxToPictureframe";
static final String MODE_SELECT_PICTUREFRAME = "button_mode_selectPictureframe";
static final String MODE_EXPORT_QUEUE = "button_mode_exportQueue";
static final String MODE_IMPORT_QUEUE = "button_mode_importQueue";
static final String MODE_CLEAR_QUEUE = "button_mode_clearQueue";
static final String MODE_FIT_IMAGE_TO_BOX = "button_mode_fitImageToBox";
static final String MODE_RESIZE_IMAGE = "numberbox_mode_resizeImage";
static final String MODE_RENDER_COMMAND_QUEUE = "button_mode_renderCommandQueue";
static final String MODE_MOVE_IMAGE = "toggle_mode_moveImage";
static final String MODE_SET_POSITION = "toggle_mode_setPosition";
static final String MODE_INPUT_BOX_TOP_LEFT = "toggle_mode_inputBoxTopLeft";
static final String MODE_INPUT_BOX_BOT_RIGHT = "toggle_mode_inputBoxBotRight";
static final String MODE_DRAW_TO_POSITION = "toggle_mode_drawToPosition";
static final String MODE_DRAW_DIRECT = "toggle_mode_drawDirect";
static final String MODE_CHANGE_SAMPLE_AREA = "numberbox_mode_changeSampleArea";
static final String MODE_CHANGE_GRID_SIZE = "numberbox_mode_changeGridSize";
static final String MODE_SHOW_DENSITY_PREVIEW = "minitoggle_mode_showDensityPreview";
static final String MODE_SHOW_IMAGE = "minitoggle_mode_showImage";
static final String MODE_SHOW_QUEUE_PREVIEW = "minitoggle_mode_showQueuePreview";
static final String MODE_SHOW_VECTOR = "minitoggle_mode_showVector";
static final String MODE_SHOW_GUIDES = "minitoggle_mode_showGuides";
static final String MODE_CHANGE_MACHINE_WIDTH = "numberbox_mode_changeMachineWidth";
static final String MODE_CHANGE_MACHINE_HEIGHT = "numberbox_mode_changeMachineHeight";
static final String MODE_CHANGE_MM_PER_REV = "numberbox_mode_changeMMPerRev";
static final String MODE_CHANGE_STEPS_PER_REV = "numberbox_mode_changeStepsPerRev";
static final String MODE_CHANGE_STEP_MULTIPLIER = "numberbox_mode_changeStepMultiplier";
static final String MODE_CHANGE_PAGE_WIDTH = "numberbox_mode_changePageWidth";
static final String MODE_CHANGE_PAGE_HEIGHT = "numberbox_mode_changePageHeight";
static final String MODE_CHANGE_PAGE_OFFSET_X = "numberbox_mode_changePageOffsetX";
static final String MODE_CHANGE_PAGE_OFFSET_Y = "numberbox_mode_changePageOffsetY";
static final String MODE_CHANGE_PAGE_OFFSET_X_CENTRE = "button_mode_changePageOffsetXCentre";
static final String MODE_CHANGE_HOMEPOINT_X = "numberbox_mode_changeHomePointX";
static final String MODE_CHANGE_HOMEPOINT_Y = "numberbox_mode_changeHomePointY";
static final String MODE_CHANGE_HOMEPOINT_X_CENTRE = "button_mode_changeHomePointXCentre";
static final String MODE_CHANGE_PEN_WIDTH = "numberbox_mode_changePenWidth";
static final String MODE_SEND_PEN_WIDTH = "button_mode_sendPenWidth";
static final String MODE_CHANGE_PEN_TEST_START_WIDTH = "numberbox_mode_changePenTestStartWidth";
static final String MODE_CHANGE_PEN_TEST_END_WIDTH = "numberbox_mode_changePenTestEndWidth";
static final String MODE_CHANGE_PEN_TEST_INCREMENT_SIZE = "numberbox_mode_changePenTestIncrementSize";
static final String MODE_CHANGE_MACHINE_MAX_SPEED = "numberbox_mode_changeMachineMaxSpeed";
static final String MODE_CHANGE_MACHINE_ACCELERATION = "numberbox_mode_changeMachineAcceleration";
static final String MODE_SEND_MACHINE_SPEED = "button_mode_sendMachineSpeed";
static final String MODE_SEND_MACHINE_SPEED_PERSIST = "button_mode_sendMachineSpeedPersist";
static final String MODE_RENDER_VECTORS = "button_mode_renderVectors";
static final String MODE_LOAD_VECTOR_FILE = "button_mode_loadVectorFile";
static final String MODE_CHANGE_MIN_VECTOR_LINE_LENGTH = "numberbox_mode_changeMinVectorLineLength";
static final String MODE_CHANGE_SERIAL_PORT = "button_mode_serialPortDialog";
static final String MODE_SEND_MACHINE_STORE_MODE = "button_mode_machineStoreDialog";
static final String MODE_SEND_MACHINE_LIVE_MODE = "button_mode_sendMachineLiveMode";
static final String MODE_SEND_MACHINE_EXEC_MODE = "button_mode_machineExecDialog";
static final String MODE_RESIZE_VECTOR = "numberbox_mode_resizeVector";
static final String MODE_MOVE_VECTOR = "toggle_mode_moveVector";
static final String MODE_CHOOSE_CHROMA_KEY_COLOUR = "toggle_mode_chooseChromaKeyColour";
static final String MODE_CHANGE_PIXEL_SCALING = "numberbox_mode_changePixelScaling";
static final String MODE_PEN_LIFT_UP = "button_mode_penUp";
static final String MODE_PEN_LIFT_DOWN = "button_mode_penDown";
static final String MODE_PEN_LIFT_POS_UP = "numberbox_mode_penUpPos";
static final String MODE_PEN_LIFT_POS_DOWN = "numberbox_mode_penDownPos";
static final String MODE_SEND_PEN_LIFT_RANGE = "button_mode_sendPenliftRange";
static final String MODE_SEND_PEN_LIFT_RANGE_PERSIST = "button_mode_sendPenliftRangePersist";
static final String MODE_SEND_ROVE_AREA = "button_mode_sendRoveArea";
static final String MODE_SELECT_ROVE_IMAGE_SOURCE = "button_mode_selectRoveImageSource";
static final String MODE_SEND_START_TEXT = "toggle_mode_sendStartText";
// controls to do with text start
static final String MODE_CHANGE_TEXT_ROW_SIZE = "numberbox_mode_changeTextRowSize";
static final String MODE_CHANGE_TEXT_ROW_SPACING = "numberbox_mode_changeTextRowSize";
static final String MODE_SHOW_WRITING_DIALOG = "button_mode_drawWritingDialog";
static final String MODE_START_SWIRLING = "button_mode_startSwirling";
static final String MODE_STOP_SWIRLING = "button_mode_stopSwirling";
static final String MODE_START_MARKING = "button_mode_startMarking";
static final String MODE_STOP_MARKING = "button_mode_stopMarking";
static final String MODE_START_SPRITE = "button_mode_drawSpriteDialog";
static final String MODE_START_RANDOM_SPRITES = "button_mode_startRandomSprite";
static final String MODE_STOP_RANDOM_SPRITES = "button_mode_stopRandomSprites";
static final String MODE_DRAW_NORWEGIAN_DIALOG = "button_mode_drawNorwegianDialog";
static final String MODE_LIVE_BLUR_VALUE = "numberbox_mode_liveBlurValue";
static final String MODE_LIVE_SIMPLIFICATION_VALUE = "numberbox_mode_liveSimplificationValue";
static final String MODE_LIVE_POSTERISE_VALUE = "numberbox_mode_livePosteriseValue";
static final String MODE_LIVE_CAPTURE_FROM_LIVE = "button_mode_liveCaptureFromLive";
static final String MODE_LIVE_CANCEL_CAPTURE = "button_mode_liveClearCapture";
static final String MODE_LIVE_ADD_CAPTION = "button_mode_liveAddCaption";
static final String MODE_LIVE_CONFIRM_DRAW = "button_mode_liveConfirmDraw";
static final String MODE_VECTOR_PATH_LENGTH_HIGHPASS_CUTOFF = "numberbox_mode_vectorPathLengthHighPassCutoff";
static final String MODE_SHOW_WEBCAM_RAW_VIDEO = "toggle_mode_showWebcamRawVideo";
static final String MODE_FLIP_WEBCAM_INPUT = "toggle_mode_flipWebcam";
static final String MODE_ROTATE_WEBCAM_INPUT = "toggle_mode_rotateWebcam";
static final String MODE_SEND_BUTTON_ACTIVATE = "button_mode_sendButtonActivate";
static final String MODE_SEND_BUTTON_DEACTIVATE = "button_mode_sendButtonDeactivate";
static final String MODE_ADJUST_PREVIEW_CORD_OFFSET = "numberbox_mode_previewCordOffsetValue";
static final String MODE_CYCLE_DENSITY_PREVIEW_STYLE = "dropdown_mode_cycleDensityPreviewStyle";
static final String MODE_CHANGE_DENSITY_PREVIEW_POSTERIZE = "numberbox_mode_changeDensityPreviewPosterize";
static final String MODE_PREVIEW_PIXEL_DENSITY_RANGE = "minitoggle_mode_previewPixelDensityRange";
static final String MODE_CHANGE_POLYGONIZER = "dropdown_mode_changePolygonizer";
static final String MODE_CHANGE_POLYGONIZER_LENGTH = "numberbox_mode_changePolygonizerLength";
static final String MODE_CHANGE_POLYGONIZER_ADAPTATIVE_ANGLE = "numberbox_mode_changePolygonizerAdaptativeAngle";
static final String MODE_CHANGE_INVERT_MASK = "dropdown_mode_changeMaskInvert";
List<String> polygonizerStyles = Arrays.asList("ADAPTATIVE", "UNIFORMLENGTH");
List<String> invertMaskModes = Arrays.asList("Mask off", "Mask hides", "Mask shows");
static final int MASK_MODES_COUNT = 3;
static final int MASK_IS_UNUSED = 0;
static final int MASKED_COLOURS_ARE_HIDDEN = 1;
static final int MASKED_COLOURS_ARE_SHOWN = 2;
PVector statusTextPosition = new PVector(300.0, 12.0);
static String currentMode = MODE_BEGIN;
static String lastMode = MODE_BEGIN;
static PVector boxVector1 = null;
static PVector boxVector2 = null;
static PVector rowsVector1 = null;
static PVector rowsVector2 = null;
static final float MASKED_PIXEL_BRIGHTNESS = -1.0;
static int pixelExtractBrightThreshold = 255;
static int pixelExtractDarkThreshold = 0;
static boolean liftPenOnMaskedPixels = true;
int numberOfPixelsTotal = 0;
int numberOfPixelsCompleted = 0;
Date timerStart = null;
Date timeLastPixelStarted = null;
boolean pixelTimerRunning = false;
boolean displayingSelectedCentres = false;
boolean displayingRowGridlines = false;
boolean displayingInfoTextOnInputPage = false;
boolean displayingGridSpots = true;
boolean displayingImage = true;
boolean displayingVector = true;
boolean displayingQueuePreview = true;
boolean displayingDensityPreview = false;
boolean displayingGuides = true;
List<String> densityPreviewStyles = Arrays.asList("Round", "Diamond", "Native Simple", "Native Arc", "Round size", "Native size");
static final int DENSITY_PREVIEW_STYLE_COUNT = 6;
static final int DENSITY_PREVIEW_ROUND = 0;
static final int DENSITY_PREVIEW_DIAMOND = 1;
static final int DENSITY_PREVIEW_NATIVE = 2;
static final int DENSITY_PREVIEW_NATIVE_ARC = 3;
static final int DENSITY_PREVIEW_ROUND_SIZE = 4;
static final int DENSITY_PREVIEW_NATIVE_SIZE = 5;
static final int DEFAULT_DENSITY_PREVIEW_STYLE = DENSITY_PREVIEW_NATIVE;
int densityPreviewStyle = DEFAULT_DENSITY_PREVIEW_STYLE;
int densityPreviewPosterize = 255;
boolean previewPixelDensityRange = true;
static final byte COORD_MODE_NATIVE_STEPS = 0;
static final byte COORD_MODE_NATIVE_MM = 1;
static final byte COORD_MODE_CARTESIAN_MM_ABS = 2;
static final byte COORD_MODE_CARTESIAN_MM_SCALED = 3;
boolean useSerialPortConnection = false;
static final char BITMAP_BACKGROUND_COLOUR = 0x0F;
PVector homePointCartesian = null;
public color chromaKeyColour = color(0,255,0);
public int invertMaskMode = MASK_IS_UNUSED;
// used in the preview page
public color pageColour = color(220);
public color frameColour = color(200,0,0);
public color machineColour = color(150);
public color guideColour = color(255);
public color backgroundColour = color(100);
public color densityPreviewColour = color(0);
public Integer previewCordOffset = 0;
public boolean debugPanels = false;
public boolean showingSummaryOverlay = true;
public boolean showingDialogBox = false;
public Integer windowWidth = 650;
public Integer windowHeight = 400;
public static Integer serialPortNumber = -1;
public Textarea consoleArea = null;
public Println console = null;
public PrintStream savedOut = null;
Properties props = null;
public static String propertiesFilename = "default.properties.txt";
public static String newPropertiesFilename = null;
public static final String TAB_NAME_INPUT= "default";
public static final String TAB_LABEL_INPUT = "input";
public static final String TAB_NAME_ROVING = "tab_roving";
public static final String TAB_LABEL_ROVING = "Roving";
public static final String TAB_NAME_DETAILS = "tab_details";
public static final String TAB_LABEL_DETAILS = "Setup";
public static final String TAB_NAME_QUEUE = "tab_queue";
public static final String TAB_LABEL_QUEUE = "Queue";
public static final String TAB_NAME_TRACE = "tab_trace";
public static final String TAB_LABEL_TRACE = "Trace";
// Page states
public String currentTab = TAB_NAME_INPUT;
public static final String PANEL_NAME_INPUT = "panel_input";
public static final String PANEL_NAME_ROVING = "panel_roving";
public static final String PANEL_NAME_DETAILS = "panel_details";
public static final String PANEL_NAME_QUEUE = "panel_queue";
public static final String PANEL_NAME_TRACE = "panel_trace";
public static final String PANEL_NAME_GENERAL = "panel_general";
public final PVector DEFAULT_CONTROL_SIZE = new PVector(100.0, 20.0);
public final PVector CONTROL_SPACING = new PVector(4.0, 4.0);
public PVector mainPanelPosition = new PVector(10.0, 85.0);
public final Integer PANEL_MIN_HEIGHT = 400;
public Set<String> panelNames = null;
public List<String> tabNames = null;
public Set<String> controlNames = null;
public Map<String, List<Controller>> controlsForPanels = null;
public Map<String, Controller> allControls = null;
public Map<String, String> controlLabels = null;
public Set<String> controlsToLockIfBoxNotSpecified = null;
public Set<String> controlsToLockIfImageNotLoaded = null;
public Map<String, Set<Panel>> panelsForTabs = null;
public Map<String, Panel> panels = null;
// machine moving
PVector machineDragOffset = new PVector (0.0, 0.0);
PVector lastMachineDragPosition = new PVector (0.0, 0.0);
public final float MIN_SCALING = 0.01;
public final float MAX_SCALING = 30.0;
RShape vectorShape = null;
String vectorFilename = null;
float vectorScaling = 100;
PVector vectorPosition = new PVector(0.0,0.0);
int minimumVectorLineLength = 2;
public static final int VECTOR_FILTER_LOW_PASS = 0;
public Set<String> controlsToLockIfVectorNotLoaded = null;
String storeFilename = "comm.txt";
boolean overwriteExistingStoreFile = true;
static boolean drawingTraceShape = true;
static boolean retraceShape = true;
static boolean flipWebcamImage = false;
static boolean rotateWebcamImage = false;
static boolean confirmedDraw = false;
static PImage liveImage = null;
static PImage processedLiveImage = null;
static PImage capturedImage = null;
static PImage processedCapturedImage = null;
static final Integer LIVE_SIMPLIFICATION_MIN = 1;
static final Integer LIVE_SIMPLIFICATION_MAX = 32;
static int pathLengthHighPassCutoff = 0;
static final Integer PATH_LENGTH_HIGHPASS_CUTOFF_MAX = 10000;
static final Integer PATH_LENGTH_HIGHPASS_CUTOFF_MIN = 0;
BlobDetector blob_detector;
int liveSimplification = 5;
int blurValue = 1;
int posterizeValue = 5;
int sepKeyColour = color(0, 0, 255);
Map<Integer, PImage> colourSeparations = null;
RShape traceShape = null;
RShape captureShape = null;
String shapeSavePath = "../../savedcaptures/";
String shapeSavePrefix = "shape-";
String shapeSaveExtension = ".svg";
static Float gcodeZAxisDrawingHeight = 1.0; //-0.125000;
String filePath = null;
static PApplet parentPapplet = null;
boolean rescaleDisplayMachine = true;
// Polygonization. It's a geomerative thing.
int polygonizer = 0;
float polygonizerLength = 0.0;
float polygonizerAdaptativeAngle = 0.0F;
void setup()
{
println("Running polargraph controller");
frame.setResizable(true);
initLogging();
parentPapplet = this;
try
{
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}
catch (Exception e)
{
e.printStackTrace();
}
RG.init(this);
loadFromPropertiesFile();
size(200, 200);
size(windowWidth, windowHeight);
this.cp5 = new ControlP5(this);
initTabs();
String[] serialPorts = Serial.list();
println("Serial ports available on your machine:");
println(serialPorts);
if (getSerialPortNumber() >= 0)
{
println("About to connect to serial port in slot " + getSerialPortNumber());
// Print a list of the serial ports, for debugging purposes:
if (serialPorts.length > 0)
{
String portName = null;
try
{
println("Get serial port no: "+getSerialPortNumber());
portName = serialPorts[getSerialPortNumber()];
myPort = new Serial(this, portName, getBaudRate());
//read bytes into a buffer until you get a linefeed (ASCII 10):
myPort.bufferUntil('\n');
useSerialPortConnection = true;
println("Successfully connected to port " + portName);
}
catch (Exception e)
{
println("Attempting to connect to serial port "
+ portName + " in slot " + getSerialPortNumber()
+ " caused an exception: " + e.getMessage());
}
}
else
{
println("No serial ports found.");
useSerialPortConnection = false;
}
}
else
{
useSerialPortConnection = false;
}
currentMode = MODE_BEGIN;
preLoadCommandQueue();
changeTab(TAB_NAME_INPUT, TAB_NAME_INPUT);
addEventListeners();
frameRate(8);
noLoop();
}
void fitDisplayMachineToWindow() {
Rectangle gr = panels.get(PANEL_NAME_GENERAL).getOutline();
println(gr);
Rectangle ir = panels.get(PANEL_NAME_INPUT).getOutline();
println(ir);
float targetHeight = ir.getBottom() - gr.getTop() - CONTROL_SPACING.y;
println("Target height is " + targetHeight + " pixels");
float machineHeight = getDisplayMachine().getOutline().getHeight();
println(machineHeight);
machineScaling = (targetHeight / machineHeight);
println(machineScaling);
if (machineScaling < 0) {
machineScaling = 1.0;
}
getDisplayMachine().getOffset().x = ((gr.getRight() > ir.getRight()) ? gr.getRight() : ir.getRight()) + CONTROL_SPACING.x;
getDisplayMachine().getOffset().y = gr.getTop();
}
void addEventListeners()
{
frame.addComponentListener(new ComponentAdapter()
{
public void componentResized(ComponentEvent event)
{
windowResized();
if (event.getSource()==frame)
{
windowResized();
}
}
}
);
addMouseWheelListener(new java.awt.event.MouseWheelListener()
{
public void mouseWheelMoved(java.awt.event.MouseWheelEvent evt)
{
mouseWheel(evt.getWheelRotation());
}
}
);
}
void preLoadCommandQueue()
{
addToCommandQueue(CMD_SETPENWIDTH+currentPenWidth+",END");
addToCommandQueue(CMD_SETMOTORSPEED+currentMachineMaxSpeed+",END");
addToCommandQueue(CMD_SETMOTORACCEL+currentMachineAccel+",END");
}
void windowResized()
{
noLoop();
windowWidth = frame.getWidth();
windowHeight = frame.getHeight();
println("New window size: " + windowWidth + " x " + windowHeight);
if (frame.getExtendedState() == Frame.MAXIMIZED_BOTH) {
println("Max");
frame.setExtendedState(0);
frame.setSize(windowWidth, windowHeight);
}
for (String key : getPanels().keySet())
{
Panel p = getPanels().get(key);
p.setSizeByHeight(windowHeight - p.getOutline().getTop() - (DEFAULT_CONTROL_SIZE.y*2));
if (debugPanels) {
println("Resize " + key + " to be " + p.getOutline().getWidth() + "px across, " + p.getOutline().getHeight() + "px tall");
}
}
// Required to tell CP5 to be able to use the new sized window
// How does this work?
cp5.setGraphics(this,0,0);
loop();
}
void draw()
{
if (getCurrentTab() == TAB_NAME_INPUT) {
drawImagePage();
}
else if (getCurrentTab() == TAB_NAME_QUEUE) {
drawCommandQueuePage();
}
else if (getCurrentTab() == TAB_NAME_DETAILS) {
drawDetailsPage();
}
else if (getCurrentTab() == TAB_NAME_ROVING) {
drawRovingPage();
}
else if (getCurrentTab() == TAB_NAME_TRACE) {
drawTracePage();
}
else {
drawDetailsPage();
}
if (isShowingSummaryOverlay()) {
drawSummaryOverlay();
}
if (isShowingDialogBox()) {
drawDialogBox();
}
if (drawbotReady) {
dispatchCommandQueue();
}
}
String getCurrentTab()
{
return this.currentTab;
}
boolean isShowingSummaryOverlay()
{
return this.showingSummaryOverlay;
}
void drawSummaryOverlay()
{
}
boolean isShowingDialogBox()
{
return false;
}
void drawDialogBox()
{
}
String getVectorFilename()
{
return this.vectorFilename;
}
void setVectorFilename(String filename)
{
this.vectorFilename = filename;
}
RShape getVectorShape()
{
return this.vectorShape;
}
void setVectorShape(RShape shape)
{
this.vectorShape = shape;
}
color getPageColour()
{
return this.pageColour;
}
color getMachineColour()
{
return this.machineColour;
}
color getBackgroundColour()
{
return this.backgroundColour;
}
color getGuideColour()
{
return this.guideColour;
}
color getFrameColour()
{
return this.frameColour;
}
Panel getPanel(String panelName)
{
return getPanels().get(panelName);
}
void drawImagePage()
{
noLoop();
strokeWeight(1);
background(getBackgroundColour());
noFill();
stroke(255, 150, 255, 100);
strokeWeight(3);
stroke(150);
noFill();
if (rescaleDisplayMachine) {
fitDisplayMachineToWindow();
rescaleDisplayMachine = false;
}
getDisplayMachine().draw();
drawMoveImageOutline();
stroke(255, 0, 0);
for (Panel panel : getPanelsForTab(TAB_NAME_INPUT))
{
panel.draw();
}
stroke(200,200);
text(propertiesFilename, getPanel(PANEL_NAME_GENERAL).getOutline().getLeft(), getPanel(PANEL_NAME_GENERAL).getOutline().getTop()-7);
showGroupBox();
showCurrentMachinePosition();
try {
if (displayingQueuePreview)
previewQueue();
}
catch (ConcurrentModificationException cme)
{
// not doing anything with this exception - I don't mind if it's wrong on the screen for a second or two.
println("Caught the pesky ConcurrentModificationException: " + cme.getMessage());
}
if (displayingInfoTextOnInputPage)
showText(250,45);
drawStatusText((int)statusTextPosition.x, (int)statusTextPosition.y);
showCommandQueue((int) getDisplayMachine().getOutline().getRight()+6, 20);
}
void drawMachineOutline()
{
rect(machinePosition.x,machinePosition.y, machinePosition.x+getDisplayMachine().getWidth(), machinePosition.y+getDisplayMachine().getHeight());
}
void drawDetailsPage()
{
strokeWeight(1);
background(100);
noFill();
stroke(255, 150, 255, 100);
strokeWeight(3);
stroke(150);
noFill();
getDisplayMachine().drawForSetup();
stroke(255, 0, 0);
for (Panel panel : getPanelsForTab(TAB_NAME_DETAILS))
{
panel.draw();
}
text(propertiesFilename, getPanel(PANEL_NAME_GENERAL).getOutline().getLeft(), getPanel(PANEL_NAME_GENERAL).getOutline().getTop()-7);
// showCurrentMachinePosition();
if (displayingInfoTextOnInputPage)
showText(250,45);
drawStatusText((int)statusTextPosition.x, (int)statusTextPosition.y);
showCommandQueue((int) getDisplayMachine().getOutline().getRight()+6, 20);
}
void drawRovingPage()
{
strokeWeight(1);
background(100);
noFill();
stroke(255, 150, 255, 100);
strokeWeight(3);
stroke(150);
noFill();
getDisplayMachine().drawForSetup();
stroke(255, 0, 0);
for (Panel panel : getPanelsForTab(TAB_NAME_ROVING))
{
panel.draw();
}
text(propertiesFilename, getPanel(PANEL_NAME_GENERAL).getOutline().getLeft(), getPanel(PANEL_NAME_GENERAL).getOutline().getTop()-7);
// showCurrentMachinePosition();
showGroupBox();
showCurrentMachinePosition();
if (displayingInfoTextOnInputPage)
showText(250,45);
drawStatusText((int)statusTextPosition.x, (int)statusTextPosition.y);
showCommandQueue((int) getDisplayMachine().getOutline().getRight()+6, 20);
}
void drawTracePage()
{
strokeWeight(1);
background(100);
noFill();
stroke(255, 150, 255, 100);
strokeWeight(3);
stroke(150);
noFill();
getDisplayMachine().drawForTrace();
if (displayingImage && getDisplayMachine().imageIsReady() && retraceShape)
{
processedLiveImage = trace_processImageForTrace(getDisplayMachine().getImage());
colourSeparations = trace_buildSeps(processedLiveImage, sepKeyColour);
traceShape = trace_traceImage(colourSeparations);
drawingTraceShape = true;
}
stroke(255, 0, 0);
for (Panel panel : getPanelsForTab(TAB_NAME_TRACE))
{
panel.draw();
}
text(propertiesFilename, getPanel(PANEL_NAME_GENERAL).getOutline().getLeft(), getPanel(PANEL_NAME_GENERAL).getOutline().getTop()-7);
if (displayingInfoTextOnInputPage)
showText(250,45);
drawStatusText((int)statusTextPosition.x, (int)statusTextPosition.y);
showCommandQueue((int) width-200, 20);
// processGamepadInput();
//
// if (displayGamepadOverlay)
// displayGamepadOverlay();
}
void drawCommandQueuePage()
{
cursor(ARROW);
background(100);
// machine outline
fill(100);
drawMachineOutline();
showingSummaryOverlay = false;
int right = 0;
for (Panel panel : getPanelsForTab(TAB_NAME_QUEUE))
{
panel.draw();
float r = panel.getOutline().getRight();
if (r > right)
right = (int) r;
}
text(propertiesFilename, getPanel(PANEL_NAME_GENERAL).getOutline().getLeft(), getPanel(PANEL_NAME_GENERAL).getOutline().getTop()-7);
showCommandQueue(right, (int)mainPanelPosition.y);
drawStatusText((int)statusTextPosition.x, (int)statusTextPosition.y);
}
void drawImageLoadPage()
{
drawImagePage();
}
void drawMoveImageOutline()
{
if (MODE_MOVE_IMAGE == currentMode && getDisplayMachine().getImage() != null)
{
// get scaled size of the image