Example #1
0
 /**
  * stop sending file commands to the robot.
  *
  * @todo add an e-stop command?
  */
 public void Halt() {
   running = false;
   paused = false;
   linesProcessed = 0;
   previewPane.setLinesProcessed(0);
   previewPane.setRunning(running);
   UpdateMenuBar();
 }
  /** When receive a message, analyze message content and then execute the command: Draw or Clear */
  public void receive(Message msg) {
    byte[] buf = msg.getRawBuffer();
    if (buf == null) {
      System.err.println(
          "["
              + channel.getAddress()
              + "] received null buffer from "
              + msg.getSrc()
              + ", headers: "
              + msg.printHeaders());
      return;
    }

    try {
      DrawCommand comm =
          (DrawCommand)
              Util.streamableFromByteBuffer(
                  DrawCommand.class, buf, msg.getOffset(), msg.getLength());
      switch (comm.mode) {
        case DrawCommand.DRAW:
          if (drawPanel != null) drawPanel.drawPoint(comm);
          break;
        case DrawCommand.CLEAR:
          clearPanel();
        default:
          System.err.println("***** received invalid draw command " + comm.mode);
          break;
      }
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
  /**
   * Init JWhiteBoard interface
   *
   * @throws Exception
   */
  public void go() throws Exception {
    if (!noChannel && !useState) channel.connect(groupName);
    mainFrame = new JFrame();
    mainFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    drawPanel = new DrawPanel(useState);
    drawPanel.setBackground(backgroundColor);
    subPanel = new JPanel();
    mainFrame.getContentPane().add("Center", drawPanel);
    clearButton = new JButton("Clean");
    clearButton.setFont(defaultFont);
    clearButton.addActionListener(this);
    leaveButton = new JButton("Exit");
    leaveButton.setFont(defaultFont);
    leaveButton.addActionListener(this);
    subPanel.add("South", clearButton);
    subPanel.add("South", leaveButton);
    mainFrame.getContentPane().add("South", subPanel);
    mainFrame.setBackground(backgroundColor);
    clearButton.setForeground(Color.blue);
    leaveButton.setForeground(Color.blue);
    mainFrame.pack();
    mainFrame.setLocation(15, 25);
    mainFrame.setBounds(new Rectangle(250, 250));

    if (!noChannel && useState) {
      channel.connect(groupName, null, stateTimeout);
    }
    mainFrame.setVisible(true);
  }
Example #4
0
  /**
   * Opens a file. If the file can be opened, get a drawing time estimate, update recent files list,
   * and repaint the preview tab.
   *
   * @param filename what file to open
   */
  public void LoadGCode(String filename) {
    CloseFile();

    try {
      Scanner scanner = new Scanner(new FileInputStream(filename));
      linesTotal = 0;
      gcode = new ArrayList<String>();
      try {
        while (scanner.hasNextLine()) {
          gcode.add(scanner.nextLine());
          ++linesTotal;
        }
      } finally {
        scanner.close();
      }
    } catch (IOException e) {
      Log("<span style='color:red'>File could not be opened.</span>\n");
      RemoveRecentFile(filename);
      return;
    }

    previewPane.setGCode(gcode);
    fileOpened = true;

    EstimateDrawTime();
    UpdateRecentFiles(filename);
    Halt();
  }
Example #5
0
 // save paper limits
 public void SetRecentPaperSize() {
   String id = Long.toString(robot_uid);
   prefs.putDouble(id + "_paper_left", paper_left);
   prefs.putDouble(id + "_paper_right", paper_right);
   prefs.putDouble(id + "_paper_top", paper_top);
   prefs.putDouble(id + "_paper_bottom", paper_bottom);
   previewPane.setPaperSize(paper_top, paper_bottom, paper_left, paper_right);
 }
  /**
   * Called in case of emergency after deserialization. Deserialization doesn't save listeners
   * because they are transient because listeners are mostly windows which cannot be serialized.
   * After Deserialization we have to find instances of classes implementing IChangesListener and
   * reconstruct listeners list once again to get everything working smooth.
   */
  public synchronized void recoverChangesListeners() {
    if (listeners == null || listeners.size() < 2) {
      listeners = new java.util.concurrent.LinkedBlockingQueue<IChangesListener>();

      listeners.add(VehicleInspector.getInstance());
      listeners.add(BuildingInspector.getInstance());
      listeners.add(DrawPanel.getInstance());
    }
  }
Example #7
0
  public Container CreateContentPane() {
    // Create the content-pane-to-be.
    JPanel contentPane = new JPanel(new BorderLayout());
    contentPane.setOpaque(true);

    // the log panel
    log = new JTextPane();
    log.setEditable(false);
    log.setBackground(Color.BLACK);
    logPane = new JScrollPane(log);
    kit = new HTMLEditorKit();
    doc = new HTMLDocument();
    log.setEditorKit(kit);
    log.setDocument(doc);
    DefaultCaret c = (DefaultCaret) log.getCaret();
    c.setUpdatePolicy(DefaultCaret.ALWAYS_UPDATE);
    ClearLog();

    // the preview panel
    previewPane = new DrawPanel();
    previewPane.setPaperSize(paper_top, paper_bottom, paper_left, paper_right);

    // status bar
    statusBar = new StatusBar();
    Font f = statusBar.getFont();
    statusBar.setFont(f.deriveFont(Font.BOLD, 15));
    Dimension d = statusBar.getMinimumSize();
    d.setSize(d.getWidth(), d.getHeight() + 30);
    statusBar.setMinimumSize(d);

    // layout
    Splitter split = new Splitter(JSplitPane.VERTICAL_SPLIT);
    split.add(previewPane);
    split.add(logPane);
    split.setDividerSize(8);

    contentPane.add(statusBar, BorderLayout.SOUTH);
    contentPane.add(split, BorderLayout.CENTER);

    // open the file
    if (recentFiles[0].length() > 0) {
      OpenFileOnDemand(recentFiles[0]);
    }

    // connect to the last port
    ListSerialPorts();
    if (Arrays.asList(portsDetected).contains(recentPort)) {
      OpenPort(recentPort);
    }

    return contentPane;
  }
Example #8
0
  /**
   * Complete the handshake, load robot-specific configuration, update the menu, repaint the preview
   * with the limits.
   *
   * @return true if handshake succeeds.
   */
  public boolean ConfirmPort() {
    if (portConfirmed == true) return true;
    String hello = "HELLO WORLD! I AM DRAWBOT #";
    int found = line3.lastIndexOf(hello);
    if (found >= 0) {
      portConfirmed = true;

      // get the UID reported by the robot
      String[] lines = line3.substring(found + hello.length()).split("\\r?\\n");
      if (lines.length > 0) {
        try {
          robot_uid = Long.parseLong(lines[0]);
        } catch (NumberFormatException e) {
        }
      }

      // new robots have UID=0
      if (robot_uid == 0) GetNewRobotUID();

      mainframe.setTitle("Drawbot #" + Long.toString(robot_uid) + " connected");

      // load machine specific config
      GetRecentPaperSize();
      LoadConfig();
      if (limit_top == 0 && limit_bottom == 0 && limit_left == 0 && limit_right == 0) {
        UpdateConfig();
      }

      previewPane.setMachineLimits(limit_top, limit_bottom, limit_left, limit_right);
      SendConfig();

      UpdateMenuBar();
      previewPane.setConnected(true);
    }
    return portConfirmed;
  }
Example #9
0
  // Take the next line from the file and send it to the robot, if permitted.
  public void SendFileCommand() {
    if (running == false
        || paused == true
        || fileOpened == false
        || portConfirmed == false
        || linesProcessed >= linesTotal) return;

    String line;
    do {
      // are there any more commands?
      line = gcode.get((int) linesProcessed++).trim();
      previewPane.setLinesProcessed(linesProcessed);
      statusBar.SetProgress(linesProcessed, linesTotal);
      // loop until we find a line that gets sent to the robot, at which point we'll
      // pause for the robot to respond.  Also stop at end of file.
    } while (ProcessLine(line) && linesProcessed < linesTotal);

    if (linesProcessed == linesTotal) {
      // end of file
      Halt();
    }
  }
Example #10
0
  public void ClosePort() {
    if (portOpened) {
      if (serialPort != null) {
        try {
          // Close the I/O streams.
          out.close();
          in.close();
          // Close the port.
          serialPort.removeEventListener();
          serialPort.close();
        } catch (IOException e) {
          // Don't care
        }
      }

      ClearLog();
      portOpened = false;
      portConfirmed = false;
      previewPane.setConnected(false);
      UpdateMenuBar();
    }
  }
Example #11
0
 /** Clear all the content on a whiteboard */
 public void clearPanel() {
   if (drawPanel != null) drawPanel.clear();
 }
Example #12
0
 /** Set state of the Group */
 public void setState(InputStream istream) throws Exception {
   drawPanel.readState(istream);
 }
Example #13
0
 /** Get state of the Group */
 public void getState(OutputStream ostream) throws Exception {
   drawPanel.writeState(ostream);
 }
Example #14
0
  // The user has done something.  respond to it.
  public void actionPerformed(ActionEvent e) {
    Object subject = e.getSource();

    if (subject == buttonZoomIn) {
      previewPane.ZoomIn();
      return;
    }
    if (subject == buttonZoomOut) {
      previewPane.ZoomOut();
      return;
    }
    if (subject == buttonOpenFile) {
      OpenFileDialog();
      return;
    }

    if (subject == buttonStart) {
      if (fileOpened) {
        paused = false;
        running = true;
        UpdateMenuBar();
        linesProcessed = 0;
        previewPane.setRunning(running);
        previewPane.setLinesProcessed(linesProcessed);
        statusBar.Start();
        SendFileCommand();
      }
      return;
    }
    if (subject == buttonPause) {
      if (running) {
        if (paused == true) {
          buttonPause.setText("Pause");
          paused = false;
          // @TODO: if the robot is not ready to unpause, this might fail and the program would
          // appear to hang.
          SendFileCommand();
        } else {
          buttonPause.setText("Unpause");
          paused = true;
        }
      }
      return;
    }
    if (subject == buttonDrive) {
      Drive();
      return;
    }
    if (subject == buttonHalt) {
      Halt();
      return;
    }
    if (subject == buttonRescan) {
      ListSerialPorts();
      UpdateMenuBar();
      return;
    }
    if (subject == buttonConfig) {
      UpdateConfig();
      return;
    }
    if (subject == buttonJogMotors) {
      JogMotors();
      return;
    }
    if (subject == buttonAbout) {
      JOptionPane.showMessageDialog(
          null,
          "Created by Dan Royer ([email protected]).\n\n"
              + "Find out more at http://www.marginallyclever.com/\n"
              + "Get the latest version and read the documentation online @ http://github.com/i-make-robots/DrawBot/");
      return;
    }
    if (subject == buttonCheckForUpdate) {
      CheckForUpdate();
      return;
    }
    if (subject == buttonExit) {
      System.exit(0); // @TODO: be more graceful?
      return;
    }

    int i;
    for (i = 0; i < 10; ++i) {
      if (subject == buttonRecent[i]) {
        OpenFileOnDemand(recentFiles[i]);
        return;
      }
    }

    for (i = 0; i < portsDetected.length; ++i) {
      if (subject == buttonPorts[i]) {
        OpenPort(portsDetected[i]);
        return;
      }
    }
  }