/** * Send whole buffer - this will be the task which will be run periodically * * @throws InterruptedException */ public void sendBuffer(Vector<data.Data> dataToSend, Long duration) throws IOException, BCDPrecisionException, InterruptedException { Iterator<data.Data> iDataToSend = dataToSend.iterator(); /* Duration as BCD */ Long durationBCD = Long.parseLong(duration.toString(), 16); /* Send start byte */ outputStream.write(0xFF); /* Send duration */ outputStream.write(Data.intToByteArray(durationBCD)); /* For each element */ while (iDataToSend.hasNext()) { /* Get current data */ data.Data current = iDataToSend.next(); /* Send single data */ send(current); /* Wait as it was specified by the user */ Thread.sleep(5); } /* Send finish byte */ outputStream.write(0xAA); }
public void GetRecentPaperSize() { String id = Long.toString(robot_uid); paper_left = Double.parseDouble(prefs.get(id + "_paper_left", "0")); paper_right = Double.parseDouble(prefs.get(id + "_paper_right", "0")); paper_top = Double.parseDouble(prefs.get(id + "_paper_top", "0")); paper_bottom = Double.parseDouble(prefs.get(id + "_paper_bottom", "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); }
void SaveConfig() { String id = Long.toString(robot_uid); prefs.put(id + "_limit_top", Double.toString(limit_top)); prefs.put(id + "_limit_bottom", Double.toString(limit_bottom)); prefs.put(id + "_limit_right", Double.toString(limit_right)); prefs.put(id + "_limit_left", Double.toString(limit_left)); prefs.put(id + "_m1invert", Boolean.toString(m1invert)); prefs.put(id + "_m2invert", Boolean.toString(m2invert)); }
void LoadConfig() { String id = Long.toString(robot_uid); limit_top = Double.valueOf(prefs.get(id + "_limit_top", "0")); limit_bottom = Double.valueOf(prefs.get(id + "_limit_bottom", "0")); limit_left = Double.valueOf(prefs.get(id + "_limit_left", "0")); limit_right = Double.valueOf(prefs.get(id + "_limit_right", "0")); m1invert = Boolean.parseBoolean(prefs.get(id + "_m1invert", "false")); m2invert = Boolean.parseBoolean(prefs.get(id + "_m2invert", "false")); }
/** * 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; }
/** based on http://www.exampledepot.com/egs/java.net/Post.html */ private void GetNewRobotUID() { try { // Send data URL url = new URL("http://marginallyclever.com/drawbot_getuid.php"); URLConnection conn = url.openConnection(); BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream())); robot_uid = Long.parseLong(rd.readLine()); rd.close(); } catch (Exception e) { } // did read go ok? if (robot_uid != 0) { SendLineToRobot("UID " + robot_uid); } }
/** Send data to Serial Port (e.g FTDI module) */ public void send(Data aData) throws IOException, BCDPrecisionException { try { /* Get frequency as BCD */ Long frequencyBCD = aData.getDataAsBCD(); /* Log */ System.out.println( "Sending: " + frequencyBCD + ". It is 0x" + Long.toHexString(frequencyBCD) + " in BCD."); /* Send frequency */ outputStream.write(Data.intToByteArray(frequencyBCD)); } catch (BCDPrecisionException e) { System.err.println( "Impossible to send: " + e.getValue() + " using current precision. Try to send: " + e.getPossibleValue()); e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } }