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();
 }
Example #2
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 #3
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);
 }
Example #4
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 #5
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 #6
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 #7
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 #8
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;
      }
    }
  }