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
import processing.serial.*;

Vector list = new Vector();
Serial port;
void setup() { 
  port=new Serial(this,Serial.list()[0],9600);
  size(1000,800); 
  String file = dataPath("hack-a-day.svg");


  try {
    Scanner sc = new Scanner (new File(file));
    while(sc.hasNext()) {
      String line = sc.nextLine();
      if(match(line,"d=.M")!=null) {
        String[] dots = line.split("L");
        //println(dots);
        Vector data = new Vector();
        for(int i=0; i<dots.length; i++) {
          //println(dots[i]);
          String[] v = match(dots[i],"(\\d+.\\d+) (\\d+.\\d+)") ; // [0] all [1] first [2] second
          Double x = Double.parseDouble(v[1]);
          Double y = Double.parseDouble(v[2]);
          data.add(x.intValue());
          data.add(y.intValue());
          print(x.intValue()+" "+y.intValue());
          if(i<dots.length-1) print(" ");
        }
        list.add(data);
        println();
      }
      else
        if(match(line," d=.m ")!=null) {
          line=line.substring(12);
          String[] dots = line.split(" ");
          //println(dots);
          Vector data = new Vector();
          Double x=new Double(0);
          Double y= new Double(0);
          for(int i=0; i<dots.length; i++) {
            //println(dots[i]);
            String[] v = match(dots[i],"([0-9\\-\\.]+),([0-9\\-\\.]+)") ; // [0] all [1] first [2] second
            //println(i);println(v);
            if(i==0) {
              x = Double.parseDouble(v[1]);
              y = Double.parseDouble(v[2]);
            }
            else {
              x+= Double.parseDouble(v[1]);
              y+= Double.parseDouble(v[2]);
            }
            data.add(x.intValue());
            data.add(y.intValue());
            print(x.intValue()+" "+y.intValue());
            if(i<dots.length-1) print(" ");
          }
          list.add(data);
          println();
        }
    }
  } 
  catch (Exception e) { 
    println("EXCEPTION "+e); 
  }
}

void mousePressed() {
  println("Starting to print..."); 

  Enumeration e = list.elements();
  while(e.hasMoreElements()) {
    Vector data = (Vector) e.nextElement();
    Enumeration i = data.elements();
    boolean nofirst=false;
    while(i.hasMoreElements()) {
      int x = (Integer) i.nextElement();
      int y = (Integer) i.nextElement(); 
      port.write(x+" "+y);
      if (i.hasMoreElements()) port.write(" "); else   port.write(0xd);
      if (nofirst) while(port.available()<2) ; 
      nofirst=true;
      println(port.readString());
    }
   // now wait for OK response
   // while(port.available()<2) ; 
    // println(port.readString());
  }

}

void draw() {
  Enumeration e = list.elements();
  while(e.hasMoreElements()) {
    Vector data = (Vector) e.nextElement();
    Enumeration i = data.elements();
    int ox=-1,oy=0;
    while(i.hasMoreElements()) {
      int x = (Integer) i.nextElement();
      int y = (Integer) i.nextElement(); 
      if(ox>-1) line(ox,oy,x,y);
      ox=x;
      oy=y; 
    }
  }
}