Advertisement
Morbo

Plotter

Apr 1st, 2012
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.71 KB | None | 0 0
  1. //plotter.java
  2.  
  3. package com.tinkerlog.kritzler;
  4.  
  5.  
  6. import geomerative.RG;
  7. import geomerative.RPath;
  8. import geomerative.RPoint;
  9. import geomerative.RShape;
  10.  
  11. import java.io.File;
  12. import java.io.FilenameFilter;
  13. import java.util.ArrayList;
  14. import java.util.List;
  15.  
  16. import processing.core.PApplet;
  17. import processing.serial.Serial;
  18.  
  19. @SuppressWarnings("serial")
  20. public class Plotter extends PApplet {
  21.  
  22.   private static final String BUFFER_ACC_PATH = "buffer_acc/";
  23.   private static final String BUFFER_DONE_PATH = "buffer_done/";
  24.   private static final String BUFFER_DENIED_PATH = "buffer_denied/";
  25.    
  26.   private static final int MAX_PLOTTER_X = 7000;
  27.   private static final int MAX_PLOTTER_Y = 8000;
  28.  
  29.   //private static final int MAX_SCREEN_X = 800;
  30.   private static final int MAX_SCREEN_Y = 700;
  31.  
  32.   private static final int SCREEN_PADDING = 20;
  33.  
  34.   private static final int START_X = 4000;
  35.   private static final int START_Y = 4000;
  36.   private static final int HOME_X = 7500;
  37.   private static final int HOME_Y = 7500;
  38.   //changed 7500 to 7515
  39.  
  40.   private static final int STATE_START = 1;
  41.   private static final int STATE_WAITING = 2;
  42.   private static final int STATE_PLOTTING_SCREEN = 3;
  43.   private static final int STATE_WAITING_INPUT = 4;
  44.   private static final int STATE_SETUP_PLOTTER = 5;
  45.   private static final int STATE_PLOTTING = 6;
  46.   private int state = STATE_START;
  47.  
  48.   private boolean serialAvailable = true;
  49.   // private boolean serialAvailable = false;
  50.  
  51.   private static final float STROKE_WEIGHT = 4.0F;
  52.   private static final float DELTA_STEP = 10.0F;
  53.  
  54.   float screenScale = 0.0F;
  55.   float plotterScale = 1.0F;
  56.   float dx, dy = 0.0F;
  57.  
  58.   private RShape shape;
  59.   private Serial port;
  60.   private Kritzler plotter;
  61.   private String currentFileName;
  62.  
  63.   private long nextUpdate = 0;  
  64.   private boolean paused = false;
  65.   private boolean plotting = false;
  66.    
  67.   public void setup() {
  68.    
  69.     if (serialAvailable) {
  70.       println("Available serial ports:");
  71.       println(Serial.list());
  72.       // port = new Serial(this, Serial.list()[0], 38400);
  73.       port = new Serial(this, Serial.list()[0], 57600);
  74.     }
  75.  
  76.     RG.init(this);    
  77.    
  78.     screenScale = (MAX_SCREEN_Y - 2F*SCREEN_PADDING) / MAX_PLOTTER_Y;    
  79.     int xsize = (int)(MAX_PLOTTER_X * screenScale) + 2 * SCREEN_PADDING;
  80.     int ysize = (int)(MAX_PLOTTER_Y * screenScale) + 2 * SCREEN_PADDING;
  81.     System.out.println(screenScale + " " + xsize + " " + ysize);
  82.     // screenScale *= 10;
  83.    
  84.     smooth();
  85.     size(xsize, ysize);
  86.     background(100);
  87.   }
  88.  
  89.  
  90.   public void draw() {
  91.     switch (state) {
  92.     case STATE_START:
  93.       dx = 0;
  94.       dy = 0;
  95.       shape = null;
  96.       translate(SCREEN_PADDING, SCREEN_PADDING);
  97.       scale(screenScale);
  98.       rect(0, 0, MAX_PLOTTER_X, MAX_PLOTTER_Y);
  99.       state = STATE_WAITING;
  100.       break;
  101.     case STATE_WAITING:
  102.       if (shape == null && System.currentTimeMillis() > nextUpdate) {
  103.         nextUpdate = System.currentTimeMillis() + 3000;
  104.         shape = loadNewShape();
  105.         if (shape != null) {
  106.           // analyzeShape(shape, "");
  107.           state = STATE_PLOTTING_SCREEN;
  108.         }
  109.       }
  110.       break;
  111.     case STATE_PLOTTING_SCREEN:
  112.       translate(SCREEN_PADDING, SCREEN_PADDING);
  113.       scale(screenScale * plotterScale);
  114.       rect(0, 0, MAX_PLOTTER_X, MAX_PLOTTER_Y);
  115.       translate(dx, dy);
  116.       strokeWeight(STROKE_WEIGHT);
  117.       drawShape(shape);
  118.       state = STATE_WAITING_INPUT;
  119.       break;
  120.     case STATE_WAITING_INPUT:
  121.       if (plotting) {
  122.         state = STATE_SETUP_PLOTTER;
  123.         plotting = false;
  124.       }
  125.       break;
  126.     case STATE_SETUP_PLOTTER:
  127.       List<Instruction> instructions = new ArrayList<Instruction>();
  128.       convertToInstructions(instructions, shape);
  129.       setupPlotter(instructions);
  130.       state = STATE_PLOTTING;
  131.       break;
  132.     case STATE_PLOTTING:
  133.       if (!paused) {
  134.         plotter.checkSerial();
  135.         if (plotter.isFinished()) {
  136.           plotter.setScale(1.0F);
  137.           plotter.translate(0, 0);
  138.           plotter.sendInstruction(new Instruction(Instruction.MOVE_ABS, HOME_X, HOME_Y));
  139.           moveShapeDone();
  140.           state = STATE_WAITING;
  141.         }
  142.       }
  143.       break;
  144.     }      
  145.   }
  146.  
  147.   public void setupPlotter(List<Instruction> instructions) {
  148.     plotter = new Kritzler(this, port);
  149.     plotter.setInstructions(instructions);
  150.     plotter.setScale(1.0F);
  151.     plotter.translate(START_X + dx, START_Y + dy);
  152.     plotter.setScale(plotterScale);
  153.     plotter.sendInstruction(new Instruction(Instruction.MOVE_REL, 0, 0));    
  154.   }
  155.  
  156.   public void drawShape(RShape shape) {
  157.     for (int i = 0; i < shape.countChildren(); i++) {
  158.       RShape s = shape.children[i];
  159.       drawShape(s);
  160.     }
  161.     for (int i = 0; i < shape.countPaths(); i++) {
  162.       RPath p = shape.paths[i];
  163.       RPoint[] points = p.getPoints();
  164.       for (int k = 0; k < points.length-1; k++) {
  165.         line(points[k].x, points[k].y, points[k+1].x, points[k+1].y);
  166.         // ellipse(points[k].x, points[k].y, 10, 10);
  167.         // ellipse(points[k+1].x, points[k+1].y, 10, 10);
  168.       }
  169.     }    
  170.   }
  171.  
  172.   public void convertToInstructions(List<Instruction> instructions, RShape shape) {
  173.     for (int i = 0; i < shape.countChildren(); i++) {
  174.       RShape s = shape.children[i];
  175.       convertToInstructions(instructions, s);
  176.     }
  177.     for (int i = 0; i < shape.countPaths(); i++) {
  178.       RPath p = shape.paths[i];
  179.       RPoint[] points = p.getPoints();
  180.       RPoint p1 = points[0];
  181.       instructions.add(new Instruction(Instruction.MOVE_ABS, p1.x, p1.y));
  182.       for (int k = 0; k < points.length-1; k++) {
  183.         RPoint p2 = points[k];
  184.         instructions.add(new Instruction(Instruction.LINE_ABS, p2.x, p2.y));
  185.       }
  186.     }    
  187.   }
  188.  
  189.   public void keyPressed() {
  190.     switch (key) {
  191.     case 'p':
  192.       plotting = true;
  193.       break;
  194.     case ' ':
  195.       paused = !paused;
  196.       break;
  197.     case CODED:
  198.       if (shape == null || state == STATE_PLOTTING) return;
  199.       switch (keyCode) {
  200.       case UP:
  201.         dy -= DELTA_STEP;
  202.         state = STATE_PLOTTING_SCREEN;
  203.         break;
  204.       case DOWN:
  205.         dy += DELTA_STEP;
  206.         state = STATE_PLOTTING_SCREEN;
  207.         break;
  208.       case LEFT:
  209.         dx -= DELTA_STEP;
  210.         state = STATE_PLOTTING_SCREEN;
  211.         break;
  212.       case RIGHT:
  213.         dx += DELTA_STEP;
  214.         state = STATE_PLOTTING_SCREEN;
  215.         break;
  216.       }
  217.       break;
  218.     case 'l':
  219.       if (shape == null) return;
  220.       shape.scale(0.95F);
  221.       print(shape, "l: ");
  222.       state = STATE_PLOTTING_SCREEN;
  223.       break;
  224.     case 'L':
  225.       if (shape == null) return;
  226.       shape.scale(1.05F);
  227.       print(shape, "L: ");
  228.       state = STATE_PLOTTING_SCREEN;
  229.       break;
  230.     case 'm':      
  231.       if (shape == null) return;
  232.       float width = shape.getBottomRight().x;
  233.       shape.scale(-1.0F, 1.0F);
  234.       shape.translate(width, 0);
  235.       print(shape, "m3 : ");
  236.       state = STATE_PLOTTING_SCREEN;
  237.       break;
  238.     case 'M':
  239.       if (shape == null) return;
  240.       shape.scale(1.0F, -1.0F);
  241.       state = STATE_PLOTTING_SCREEN;
  242.       break;
  243.     case 's':
  244.       if (shape == null) return;
  245.       moveShapeDenied();
  246.       state = STATE_START;
  247.       break;
  248.     }    
  249.   }
  250.  
  251.   private void print(RShape p, String msg) {
  252.     RPoint p1 = p.getTopLeft();
  253.     RPoint p2 = p.getBottomRight();
  254.     System.out.println(msg + " (" + p1.x + ", " + p1.y + "), (" + p2.x + ", " + p2.y + ")");
  255.   }
  256.  
  257.   private void moveShapeDone() {
  258.     System.out.println("moving file to " + BUFFER_DONE_PATH + currentFileName);
  259.     File file = new File(BUFFER_ACC_PATH + currentFileName);
  260.     File newFile = new File(BUFFER_DONE_PATH + currentFileName);
  261.     file.renameTo(newFile);
  262.   }
  263.  
  264.   private void moveShapeDenied() {
  265.     System.out.println("moving file to " + BUFFER_DENIED_PATH + currentFileName);
  266.     File file = new File(BUFFER_ACC_PATH + currentFileName);
  267.     File newFile = new File(BUFFER_DENIED_PATH + currentFileName);
  268.     file.renameTo(newFile);
  269.   }
  270.  
  271.   private RShape loadNewShape() {
  272.     File dir = new File(BUFFER_ACC_PATH);
  273.     String[] listing = dir.list(new FilenameFilter() {
  274.       public boolean accept(File file, String filename) {
  275.         return filename.endsWith("svg");
  276.       }
  277.     });
  278.     if (listing != null && listing.length > 0) {
  279.       currentFileName = listing[0];
  280.       System.out.println("loading " + currentFileName);
  281.       RShape shape = RG.loadShape(BUFFER_ACC_PATH + currentFileName);
  282.       shape.scale(10.0F);
  283.       print(shape, "loaded: ");
  284.       return shape;
  285.     }
  286.     return null;
  287.   }
  288.          
  289.   public static void main(String args[]) {
  290.     PApplet.main(new String[] { "com.tinkerlog.kritzler.Plotter" });
  291.   }    
  292.  
  293. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement