Advertisement
Morbo

kritz

Apr 1st, 2012
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.65 KB | None | 0 0
  1. //kritzler
  2.  
  3. package com.tinkerlog.kritzler;
  4.  
  5.  
  6. import java.util.List;
  7.  
  8. import processing.core.PApplet;
  9. import processing.serial.Serial;
  10.  
  11. public class Kritzler {
  12.    
  13.   private Serial port;
  14.   private List<Instruction> instructions;
  15.   private int currentInst;
  16.   private StringBuilder buf = new StringBuilder();
  17.  
  18.   private float tx, ty;
  19.   private float scale;
  20.  
  21.   public Kritzler(PApplet parent, Serial port) {
  22.     this.port = port;
  23.   }
  24.  
  25.   public void setInstructions(List<Instruction> instructions) {
  26.     this.instructions = instructions;
  27.     this.currentInst = 0;
  28.   }
  29.  
  30.   public void translate(float x, float y) {
  31.     this.tx = x;
  32.     this.ty = y;
  33.   }
  34.  
  35.   public void setScale(float s) {
  36.     this.scale = s;
  37.   }
  38.  
  39.   public boolean isFinished() {
  40.     if (port == null) return true;
  41.     return instructions.size() == currentInst;
  42.   }
  43.  
  44.   public void checkSerial() {
  45.     if (port != null && port.available() > 0) {
  46.       processSerial();
  47.     }
  48.   }
  49.  
  50.   public void processSerial() {
  51.     while (port.available() > 0) {
  52.       int c = port.read();
  53.       if (c != 10) {
  54.         buf.append((char)c);        
  55.       }
  56.       else {
  57.         String message = buf.toString();
  58.         buf.setLength(0);
  59.         message = message.substring(0, message.length()-1);        
  60.         if (message.startsWith("#")) {
  61.           System.out.println("bot: " + message);
  62.         }
  63.         else {          
  64.           processMessage(message);
  65.         }        
  66.       }
  67.     }
  68.   }
  69.  
  70.   public void processMessage(String message) {
  71.     if (message.equals("OK")) {
  72.       System.out.println("received ok");
  73.       if (instructions != null) {
  74.         if (currentInst >= instructions.size()) {
  75.           System.out.println("nothing to do");
  76.         }
  77.         else {
  78.           Instruction inst = instructions.get(currentInst);
  79.           currentInst++;
  80.           sendInstruction(inst);
  81.         }
  82.       }      
  83.     }
  84.     else {
  85.       System.out.println("unknown: " + message);
  86.     }
  87.   }
  88.  
  89.   public void sendInstruction(Instruction i) {
  90.     if (port == null) return;
  91.     String msg = null;
  92.     int x = (int)(i.x * scale);
  93.     int y = (int)(i.y * scale);
  94.     switch (i.type) {
  95.     case Instruction.MOVE_ABS:
  96.       msg = "M " + (x + tx) + " " + (y + ty) + '\r';
  97.       break;
  98.     case Instruction.LINE_ABS:
  99.       msg = "L " + (x + tx) + " " + (y + ty) + '\r';
  100.       break;
  101.     case Instruction.MOVE_REL:
  102.       msg = "m " + x + " " + y + '\r';
  103.       break;
  104.     case Instruction.LINE_REL:
  105.       msg = "l " + x + " " + y + '\r';
  106.       break;
  107.     }
  108.     System.out.println("sending (" + currentInst + "): " + msg);
  109.     port.write(msg);
  110.   }
  111.  
  112. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement