diff --git a/NatBrailleTools/BrailleLogger/.classpath b/NatBrailleTools/BrailleLogger/.classpath new file mode 100644 index 0000000..5bc4564 --- /dev/null +++ b/NatBrailleTools/BrailleLogger/.classpath @@ -0,0 +1,6 @@ + + + + + + diff --git a/NatBrailleTools/BrailleLogger/.project b/NatBrailleTools/BrailleLogger/.project new file mode 100644 index 0000000..75c6405 --- /dev/null +++ b/NatBrailleTools/BrailleLogger/.project @@ -0,0 +1,17 @@ + + + BrailleLogger + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/NatBrailleTools/BrailleLogger/.settings/org.eclipse.jdt.core.prefs b/NatBrailleTools/BrailleLogger/.settings/org.eclipse.jdt.core.prefs new file mode 100644 index 0000000..ef8a789 --- /dev/null +++ b/NatBrailleTools/BrailleLogger/.settings/org.eclipse.jdt.core.prefs @@ -0,0 +1,12 @@ +eclipse.preferences.version=1 +org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled +org.eclipse.jdt.core.compiler.codegen.methodParameters=do not generate +org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.6 +org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve +org.eclipse.jdt.core.compiler.compliance=1.6 +org.eclipse.jdt.core.compiler.debug.lineNumber=generate +org.eclipse.jdt.core.compiler.debug.localVariable=generate +org.eclipse.jdt.core.compiler.debug.sourceFile=generate +org.eclipse.jdt.core.compiler.problem.assertIdentifier=error +org.eclipse.jdt.core.compiler.problem.enumIdentifier=error +org.eclipse.jdt.core.compiler.source=1.6 diff --git a/NatBrailleTools/BrailleLogger/.settings/org.eclipse.ltk.core.refactoring.prefs b/NatBrailleTools/BrailleLogger/.settings/org.eclipse.ltk.core.refactoring.prefs new file mode 100644 index 0000000..b196c64 --- /dev/null +++ b/NatBrailleTools/BrailleLogger/.settings/org.eclipse.ltk.core.refactoring.prefs @@ -0,0 +1,2 @@ +eclipse.preferences.version=1 +org.eclipse.ltk.core.refactoring.enable.project.refactoring.history=false diff --git a/NatBrailleTools/BrailleLogger/bin/Braille2GcodeEncoder.class b/NatBrailleTools/BrailleLogger/bin/Braille2GcodeEncoder.class new file mode 100644 index 0000000..cd03adb Binary files /dev/null and b/NatBrailleTools/BrailleLogger/bin/Braille2GcodeEncoder.class differ diff --git a/NatBrailleTools/BrailleLogger/bin/Braille6GcodeEncoding.class b/NatBrailleTools/BrailleLogger/bin/Braille6GcodeEncoding.class new file mode 100644 index 0000000..5c97c4e Binary files /dev/null and b/NatBrailleTools/BrailleLogger/bin/Braille6GcodeEncoding.class differ diff --git a/NatBrailleTools/BrailleLogger/bin/BrailleGcodeDot.class b/NatBrailleTools/BrailleLogger/bin/BrailleGcodeDot.class new file mode 100644 index 0000000..1241679 Binary files /dev/null and b/NatBrailleTools/BrailleLogger/bin/BrailleGcodeDot.class differ diff --git a/NatBrailleTools/BrailleLogger/bin/BrailleGcodeEncoding.class b/NatBrailleTools/BrailleLogger/bin/BrailleGcodeEncoding.class new file mode 100644 index 0000000..65b5a64 Binary files /dev/null and b/NatBrailleTools/BrailleLogger/bin/BrailleGcodeEncoding.class differ diff --git a/NatBrailleTools/BrailleLogger/bin/BrailleLogger.class b/NatBrailleTools/BrailleLogger/bin/BrailleLogger.class new file mode 100644 index 0000000..22f819b Binary files /dev/null and b/NatBrailleTools/BrailleLogger/bin/BrailleLogger.class differ diff --git a/NatBrailleTools/BrailleLogger/bin/GCodeGenerator.class b/NatBrailleTools/BrailleLogger/bin/GCodeGenerator.class new file mode 100644 index 0000000..6731139 Binary files /dev/null and b/NatBrailleTools/BrailleLogger/bin/GCodeGenerator.class differ diff --git a/NatBrailleTools/BrailleLogger/src/Braille2GcodeEncoder.java b/NatBrailleTools/BrailleLogger/src/Braille2GcodeEncoder.java new file mode 100644 index 0000000..1c10dab --- /dev/null +++ b/NatBrailleTools/BrailleLogger/src/Braille2GcodeEncoder.java @@ -0,0 +1,220 @@ +import java.awt.Point; +import java.awt.geom.Point2D; +import java.util.ArrayList; + +import javax.swing.JOptionPane; + +/** + * + */ + +/** + * Braille encoder class + * @author crocsg + * + */ +public class Braille2GcodeEncoder { + + private GCodeGenerator generator = new GCodeGenerator (); + private int page_num= 0; + private int char_cnt_line = 0; + + private BrailleGcodeEncoding encoder; + + private float position_x; + private float position_y; + + private ArrayList pagedot; + private ArrayList> linedot; + private String bufline = ""; + private String pagegcode = ""; + + private final float Braille_cell_width = 2.3f; + private final float Braille_cell_height = 2.3f; + private final float Braille_cell_padding = 6.1f; + private final float Braille_line_padding = 10f; + private boolean pageAvailable = false; + private boolean newpage=true; + + /** + * @return the pageAvailable + */ + public boolean isPageAvailable() { + return pageAvailable; + } + + + + /** + * @return the pagegcode + */ + public String getPagegcode() { + return pagegcode; + } + + + + /** + * Constructor + */ + private Braille2GcodeEncoder() { + + } + + /** + * Constructor + * @param encoder Braille Encoding + */ + public Braille2GcodeEncoder(BrailleGcodeEncoding encoder) { + this.encoder = encoder; + } + + public void startPage () + { + pagedot = new ArrayList (); + position_y = 2;//TODO avoid magic + pageAvailable = false; + newpage = false; + pagegcode = ""; + startLine (); + + + } + + public void endPage () + { + if ( ! pagedot.isEmpty()) + { + pagegcode = generator.Header(); + + for (Point2D.Float p: pagedot) + { + pagegcode += generator.moveTo(p.getX(),p.getY()); + pagegcode += generator.printDot(); + } + pagegcode += generator.Footer(); + + + pageAvailable = true; + + int n = JOptionPane.showConfirmDialog( + null, + "Page " + (page_num+1) + " chargez le papier quand l'embosseuse est disponible", + "BrailleRapSP Embosseuse Braille", + JOptionPane.OK_CANCEL_OPTION); + if (n == JOptionPane.CANCEL_OPTION) + { + // cancel print + System.exit(0); + + } + + page_num++; + } + } + + public void startLine () + { + linedot = new ArrayList > (); + for (int i =0; i < encoder.getNline(); i++ ) + { + linedot.add(new ArrayList()); + } + + position_x = 0; //TODO avoid magic + bufline = ""; + } + + private Point2D.Float getDotAbsolutePosition(Point p) + { + Point2D.Float absolute = new Point2D.Float(); + absolute.x = position_x + (float) p.x * Braille_cell_width; + absolute.y = position_y + (float) p.y * Braille_cell_height; + + return absolute; + } + + private void nextCellPosition () + { + this.position_x += Braille_cell_padding; + } + + private void nextLinePosition () + { + this.position_y += Braille_line_padding; + } + private void endLine () + { + ArrayList dots; + + // compute braille dots on line + for (int i =0; i < bufline.length(); i++) + { + dots = encoder.GetCharacterDots(bufline.charAt(i)); + if (dots != null) + { + for (int d= 0; d < dots.size (); d++) + { + int x = dots.get(d).x; + int y = dots.get(d).y; + + linedot.get(y).add(getDotAbsolutePosition(dots.get(d))); + } + + nextCellPosition (); + } + + } + + // store braille absolute dot on page + for (int l = 0; l < linedot.size(); l++) + { + pagedot.addAll(linedot.get(l)); + } + + nextLinePosition(); + } + + private void addCharToBuffer (char a) + { + bufline += a; + } + + public void Open () + { + startPage (); + } + public void processChar (char a) + { + if (a == '\n') + { + endLine (); + startLine (); + } + else if (a == 0x0c) + endPage (); + + else + { + if (newpage) + startPage(); + addCharToBuffer (a); + } + + } + + public void Close () + { + endLine (); + endPage (); + } + + + + public void requestNewPage() { + newpage = true; + pageAvailable = false; + startPage (); + } + +} diff --git a/NatBrailleTools/BrailleLogger/src/Braille6GcodeEncoding.java b/NatBrailleTools/BrailleLogger/src/Braille6GcodeEncoding.java new file mode 100644 index 0000000..e457315 --- /dev/null +++ b/NatBrailleTools/BrailleLogger/src/Braille6GcodeEncoding.java @@ -0,0 +1,141 @@ +import java.awt.Point; +import java.util.ArrayList; + +/** + * + */ + +/** + * @author lenovo + * + */ +public class Braille6GcodeEncoding implements BrailleGodeEncoding { + private static final BrailleGcodeDot[] dot = new BrailleGcodeDot[] { + new BrailleGcodeDot('a', new int[] {1}), + new BrailleGcodeDot('b', new int[] {1,2}), + new BrailleGcodeDot('c', new int[] {1,4}), + + + + new BrailleGcodeDot('d', new int[]{1,4,5}), + new BrailleGcodeDot('e', new int[] {1,5}), + new BrailleGcodeDot('f', new int[]{1,2,4}), + new BrailleGcodeDot('g', new int[]{1,2,4,5}), + new BrailleGcodeDot('h', new int[]{1,2,5}), + new BrailleGcodeDot('i', new int[]{2,4}), + new BrailleGcodeDot('j', new int[]{2,4,5}), + new BrailleGcodeDot('k', new int[]{1,3}), + new BrailleGcodeDot('l', new int[]{1,2,3}), + new BrailleGcodeDot('m', new int[]{1,3,4}), + new BrailleGcodeDot('n', new int[]{1,3,4,5}), + new BrailleGcodeDot('o', new int[]{1,3,5}), + new BrailleGcodeDot('p', new int[]{1,2,3,4}), + new BrailleGcodeDot('q', new int[]{1,2,3,4,5}), + new BrailleGcodeDot('r', new int[]{1,2,3,5}), + new BrailleGcodeDot('s', new int[]{2,3,4}), + new BrailleGcodeDot('t', new int[]{2,3,4,5}), + new BrailleGcodeDot('u', new int[]{1,3,6}), + new BrailleGcodeDot('v', new int[]{1,2,3,6}), + new BrailleGcodeDot('w', new int[]{2,4,5,6}), + new BrailleGcodeDot('x', new int[]{1,3,4,6}), + new BrailleGcodeDot('y', new int[]{1,3,4,5,6}), + new BrailleGcodeDot('z', new int[]{1,3,5,6}), + + new BrailleGcodeDot(' ', new int[]{}), + new BrailleGcodeDot('.', new int[]{2,5,6}), + new BrailleGcodeDot(',', new int[]{2}), + new BrailleGcodeDot('?', new int[]{2,6}), + new BrailleGcodeDot(';', new int[]{2,3}), + new BrailleGcodeDot(',', new int[]{2,4}), + new BrailleGcodeDot('!', new int[]{2,3,5}), + new BrailleGcodeDot('(', new int[]{2,3,6}), + new BrailleGcodeDot(')', new int[]{3,5,6}), + new BrailleGcodeDot('\'', new int[]{3}), + new BrailleGcodeDot('-', new int[]{3,6}), + new BrailleGcodeDot('/', new int[]{3,4}), + new BrailleGcodeDot('*', new int[]{3,5}), + new BrailleGcodeDot('+', new int[]{2,3,5}), + new BrailleGcodeDot('=', new int[]{2,3,5,6}), + new BrailleGcodeDot('0', new int[]{3, 4, 5, 6}), + new BrailleGcodeDot('1', new int[]{1, 6}), + new BrailleGcodeDot('2', new int[]{1, 2, 6}), + new BrailleGcodeDot('3', new int[]{1, 4, 6}), + new BrailleGcodeDot('4', new int[]{1, 4, 5, 6}), + new BrailleGcodeDot('5', new int[]{1, 5, 6}), + new BrailleGcodeDot('6', new int[]{1, 2, 4, 6}), + new BrailleGcodeDot('7', new int[]{1, 2, 4, 5, 6}), + new BrailleGcodeDot('8', new int[]{1, 2, 5, 6}), + new BrailleGcodeDot('9', new int[]{2, 4, 6}), + + new BrailleGcodeDot('à', new int[]{1, 2, 3,5, 6}), + new BrailleGcodeDot('â', new int[]{1, 6}), + new BrailleGcodeDot('ç', new int[]{1,2, 3, 4, 6}), + new BrailleGcodeDot('è', new int[]{2, 3, 4, 6}), + new BrailleGcodeDot('é', new int[]{1, 2, 3, 4, 5, 6}), + new BrailleGcodeDot('ê', new int[]{1, 2, 6}), + new BrailleGcodeDot('ë', new int[]{1, 2, 4, 6}), + new BrailleGcodeDot('î', new int[]{1, 4, 6}), + new BrailleGcodeDot('ï', new int[]{1,2, 4, 5, 6}), + new BrailleGcodeDot('ù', new int[]{2, 3, 4, 5, 6}), + new BrailleGcodeDot('û', new int[]{1, 5, 6}), + new BrailleGcodeDot('ü', new int[]{1, 2, 5, 6}), + + new BrailleGcodeDot('¨', new int[]{1, 3}), // Majuscule NatBraille + new BrailleGcodeDot('`', new int[]{3, 4, 5, 6}) // Chiffres NatBraille + + }; + + private final int nrow = 2; // number of row in braille encoding + private final int nline = 3; // number of line in braille encoding + + + + + public Braille6GcodeEncoding() { + super(); + // TODO Auto-generated constructor stub + } + + /* (non-Javadoc) + * @see BrailleGodeEncoding#GetCharacterDots(char) + */ + @Override + public ArrayList GetCharacterDots(char a) { + //TODO make this fast + + for (int i =0; i < dot.length;i++) + { + if (dot[i].getCar() == a) + { + ArrayList Braille = new ArrayList(); + for (int d = 0; d < dot[i].getPos().length; d++) + { + int x = dot[i].getPos()[d] <= nline ? 0 : 1; + int y = dot[i].getPos()[d] <= nline ? dot[i].getPos()[d] - 1 : dot[i].getPos()[d] - 1 - nline; + + Braille.add(new Point(x,y)); + } + + return Braille; + } + } + return null; + } + + /* (non-Javadoc) + * @see BrailleGodeEncoding#getNRow() + */ + @Override + public int getNRow() { + return nrow; + } + + /* (non-Javadoc) + * @see BrailleGodeEncoding#getNline() + */ + @Override + public int getNline() { + return nline; + } + +} diff --git a/NatBrailleTools/BrailleLogger/src/BrailleGcodeDot.java b/NatBrailleTools/BrailleLogger/src/BrailleGcodeDot.java new file mode 100644 index 0000000..94012a5 --- /dev/null +++ b/NatBrailleTools/BrailleLogger/src/BrailleGcodeDot.java @@ -0,0 +1,52 @@ + + +/** + * + */ + +/** + * @author lenovo + * + */ +public class BrailleGcodeDot { + + + private char car; + + public char getCar() { + return car; + } + public void setCar(char car) { + this.car = car; + } + + private int[] pos; + + public int[] getPos() { + return pos; + } + public void setPos(int[] pos) { + this.pos = pos; + } + + + /** + * + */ + public BrailleGcodeDot() { + // TODO Auto-generated constructor stub + } + /** + * @param car + * @param position + */ + public BrailleGcodeDot(char car, int[] position) { + this.car = car; + this.pos = position; + } + + public BrailleGcodeDot(char car) { + this.car = car; + + } +} diff --git a/NatBrailleTools/BrailleLogger/src/BrailleGcodeEncoding.java b/NatBrailleTools/BrailleLogger/src/BrailleGcodeEncoding.java new file mode 100644 index 0000000..f10c434 --- /dev/null +++ b/NatBrailleTools/BrailleLogger/src/BrailleGcodeEncoding.java @@ -0,0 +1,18 @@ +import java.awt.Point; +import java.util.ArrayList; + +/** + * + */ + +/** + * @author lenovo + * + */ +public interface BrailleGcodeEncoding { + + public ArrayList GetCharacterDots (char a); + public int getNRow (); + public int getNline (); + +} diff --git a/NatBrailleTools/BrailleLogger/src/BrailleLogger.java b/NatBrailleTools/BrailleLogger/src/BrailleLogger.java new file mode 100644 index 0000000..6de2fb3 --- /dev/null +++ b/NatBrailleTools/BrailleLogger/src/BrailleLogger.java @@ -0,0 +1,71 @@ +import java.awt.geom.Point2D; +import java.io.FileNotFoundException; +import java.io.FileReader; +import java.io.IOException; +import java.util.ArrayList; + +/** + * Main program + */ + +/** + * @author lenovo + * + */ +public class BrailleLogger { + + + ArrayList braillepoints = new ArrayList (); + /** + * @param args + */ + public static void main(String[] args) { + // TODO Auto-generated method stub + System.out.println ("; BrailleRapSP GCODE generator"); + int arg = 0; + for (String s: args) + { + System.out.println(";" + arg); + System.out.println(";" + s); + } + + if (args[0] != null) + { + FileReader fr; + Braille2GcodeEncoder encoder = new Braille2GcodeEncoder(new Braille6GcodeEncoding()); + try { + fr = new FileReader(args[0]); + + int i; + while ((i=fr.read()) != -1) + { + encoder.processChar((char) i); + if (encoder.isPageAvailable()) + { + System.out.println(encoder.getPagegcode()); + encoder.requestNewPage (); + } + } + + encoder.Close (); + if (encoder.isPageAvailable()) + { + System.out.println(encoder.getPagegcode()); + + } + fr.close(); + + } catch (FileNotFoundException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + + + } + } + +} diff --git a/NatBrailleTools/BrailleLogger/src/GCodeGenerator.java b/NatBrailleTools/BrailleLogger/src/GCodeGenerator.java new file mode 100644 index 0000000..944f910 --- /dev/null +++ b/NatBrailleTools/BrailleLogger/src/GCodeGenerator.java @@ -0,0 +1,67 @@ +import java.text.DecimalFormat; +import java.text.DecimalFormatSymbols; + +/** + * + */ + +/** + * @author lenovo + * + */ +public class GCodeGenerator { + + + public String Home () + { + return ("G28 X\nG28 Y\n"); + } + public String Speed() + { + return ("G1 F8000\n"); + } + + public String Position() + { + return ("G90\n"); + } + + public String Motoroff () + { + return "M84\n"; + } + + public String Header () + { + String gcode = ""; + gcode = Home (); + gcode += Speed (); + + return gcode; + } + + public String Footer () + { + // TODO avoid magic + String gcode = "G1 X10.00 Y600.00\n"; // eject page + gcode += Motoroff (); + + return gcode; + } + + public String moveTo(double x, double y) + { + DecimalFormatSymbols sym= new DecimalFormatSymbols (); + sym.setDecimalSeparator('.'); + DecimalFormat decimalFormat = new DecimalFormat("##0.000", sym); + + String gcode = "G1 X" + decimalFormat.format(x) + " Y" + decimalFormat.format(y) + "\n"; + return gcode; + } + + public String printDot() { + + return ("M3 S1\nM3 S0\n"); + + } +} diff --git a/NatBrailleTools/simpleJavaGrblStreamer/.classpath b/NatBrailleTools/simpleJavaGrblStreamer/.classpath new file mode 100644 index 0000000..fd7ad7f --- /dev/null +++ b/NatBrailleTools/simpleJavaGrblStreamer/.classpath @@ -0,0 +1,26 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/NatBrailleTools/simpleJavaGrblStreamer/.project b/NatBrailleTools/simpleJavaGrblStreamer/.project new file mode 100644 index 0000000..1ff8812 --- /dev/null +++ b/NatBrailleTools/simpleJavaGrblStreamer/.project @@ -0,0 +1,23 @@ + + + simpleJavaGrblStreamer + + + + + + org.eclipse.jdt.core.javabuilder + + + + + org.eclipse.m2e.core.maven2Builder + + + + + + org.eclipse.jdt.core.javanature + org.eclipse.m2e.core.maven2Nature + + diff --git a/NatBrailleTools/simpleJavaGrblStreamer/.settings/org.eclipse.core.resources.prefs b/NatBrailleTools/simpleJavaGrblStreamer/.settings/org.eclipse.core.resources.prefs new file mode 100644 index 0000000..e9441bb --- /dev/null +++ b/NatBrailleTools/simpleJavaGrblStreamer/.settings/org.eclipse.core.resources.prefs @@ -0,0 +1,3 @@ +eclipse.preferences.version=1 +encoding//src/main/java=UTF-8 +encoding/=UTF-8 diff --git a/NatBrailleTools/simpleJavaGrblStreamer/.settings/org.eclipse.jdt.core.prefs b/NatBrailleTools/simpleJavaGrblStreamer/.settings/org.eclipse.jdt.core.prefs new file mode 100644 index 0000000..60105c1 --- /dev/null +++ b/NatBrailleTools/simpleJavaGrblStreamer/.settings/org.eclipse.jdt.core.prefs @@ -0,0 +1,5 @@ +eclipse.preferences.version=1 +org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.6 +org.eclipse.jdt.core.compiler.compliance=1.6 +org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning +org.eclipse.jdt.core.compiler.source=1.6 diff --git a/NatBrailleTools/simpleJavaGrblStreamer/LICENSE b/NatBrailleTools/simpleJavaGrblStreamer/LICENSE new file mode 100644 index 0000000..3ffdaba --- /dev/null +++ b/NatBrailleTools/simpleJavaGrblStreamer/LICENSE @@ -0,0 +1,21 @@ +The MIT License (MIT) + +Copyright (c) 2015 Will Winder + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/NatBrailleTools/simpleJavaGrblStreamer/README.md b/NatBrailleTools/simpleJavaGrblStreamer/README.md new file mode 100644 index 0000000..a26b741 --- /dev/null +++ b/NatBrailleTools/simpleJavaGrblStreamer/README.md @@ -0,0 +1,9 @@ +# simpleJavaGrblStreamer + +A minimal Java application to stream a file to GRBL as quickly as possible. + +Uses a blocking queue to send events from the serial event thread to the main thread which is sending the file. + +Build with "mvn compile assembly:single" + +Run with "java -jar " diff --git a/NatBrailleTools/simpleJavaGrblStreamer/pom.xml b/NatBrailleTools/simpleJavaGrblStreamer/pom.xml new file mode 100644 index 0000000..9da9675 --- /dev/null +++ b/NatBrailleTools/simpleJavaGrblStreamer/pom.xml @@ -0,0 +1,37 @@ + + + 4.0.0 + com.willwinder.sjgs + simpleJavaGrblStreamer + 1.0-SNAPSHOT + jar + + + org.scream3r + jssc + 2.8.0 + + + + UTF-8 + 1.6 + 1.6 + + + + + maven-assembly-plugin + + + + com.willwinder.sjgs.simplejavagrblstreamer.SimpleJavaGrblStreamer + + + + jar-with-dependencies + + + + + + diff --git a/NatBrailleTools/simpleJavaGrblStreamer/src/main/java/com/willwinder/sjgs/simplejavagrblstreamer/SimpleJavaGrblStreamer.java b/NatBrailleTools/simpleJavaGrblStreamer/src/main/java/com/willwinder/sjgs/simplejavagrblstreamer/SimpleJavaGrblStreamer.java new file mode 100644 index 0000000..87bc5c8 --- /dev/null +++ b/NatBrailleTools/simpleJavaGrblStreamer/src/main/java/com/willwinder/sjgs/simplejavagrblstreamer/SimpleJavaGrblStreamer.java @@ -0,0 +1,171 @@ +package com.willwinder.sjgs.simplejavagrblstreamer; + +import java.io.BufferedReader; +import java.io.File; +import java.io.FileInputStream; +import java.io.FileReader; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.util.LinkedList; +import java.util.concurrent.BlockingQueue; +import java.util.concurrent.LinkedBlockingQueue; +import java.util.logging.Level; +import java.util.logging.Logger; +import jssc.SerialPort; +import jssc.SerialPortEvent; +import jssc.SerialPortEventListener; +import jssc.SerialPortException; + +/** + * + * @author wwinder + */ +public class SimpleJavaGrblStreamer { + static int GRBL_BUFFER_SIZE = 125; + + public static void main(String argv[]) throws SerialPortException, IOException, InterruptedException { + if (argv.length != 3 && argv.length != 2) { + System.out.println("Arguments: "); + return; + } + + String commPort = argv[0]; + Integer baud = Integer.parseInt(argv[1]); + InputStream file = null; + if (argv.length == 2) + file = System.in; + else { + file = new FileInputStream(argv[2]); + System.out.println("Streaming file " + argv[2]); + } + + System.out.println("Streaming to port <" + commPort + " : " + baud + ">"); + SimpleJavaGrblStreamer s = new SimpleJavaGrblStreamer(commPort, baud); + s.send(file); + + System.out.println("Done senting file."); + } + + String commPort = null; + Integer baud = null; + + private SimpleJavaGrblStreamer(String commPort, Integer baud) { + this.commPort = commPort; + this.baud = baud; + } + + private void send(InputStream f) throws SerialPortException, IOException, InterruptedException { + BlockingQueue commandComplete = new LinkedBlockingQueue(); + + SerialPort serialPort = new SerialPort(commPort); + serialPort.openPort(); + serialPort.setParams(baud, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE, true, true); + + Thread.sleep(5000); + + serialPort.addEventListener(new GrblSerialListener(serialPort, commandComplete)); + GrblSerialSender sender = new GrblSerialSender(f, serialPort, commandComplete, GRBL_BUFFER_SIZE); + sender.send(); + serialPort.closePort(); + } + + private static class GrblSerialListener implements SerialPortEventListener { + SerialPort serialPort = null; + BlockingQueue completedCommands; + StringBuilder inputBuffer; + private GrblSerialListener(SerialPort port, BlockingQueue completedCommands) { + this.serialPort = port; + this.completedCommands = completedCommands; + inputBuffer = new StringBuilder(); + } + + @Override + public void serialEvent(SerialPortEvent event) { + if(event.isRXCHAR() && event.getEventValue() > 0){ + try { + byte buffer[] = serialPort.readBytes(); + for (byte b: buffer) { + if (b == '\n') { + //System.out.println("Received command: " + inputBuffer.toString()); + if (inputBuffer.toString().startsWith("ok") || inputBuffer.toString().startsWith("error")) + completedCommands.add(1); + inputBuffer.setLength(0); + } else { + inputBuffer.append((char)b); + } + } + } + catch (SerialPortException ex) { + System.out.println(ex); + System.out.println("serialEvent"); + } + } + } + } + + private static class GrblSerialSender { + InputStream file = null; + SerialPort port = null; + Integer currentBufferSize = null; + Integer maxBufferSize = null; + + BlockingQueue completedCommands = null; + LinkedList activeCommandSizes; + + private GrblSerialSender(InputStream f, SerialPort sp, BlockingQueue completedCommands, Integer activeBufferSize) { + file = f; + port = sp; + maxBufferSize = activeBufferSize; + currentBufferSize = 0; + this.completedCommands = completedCommands; + activeCommandSizes = new LinkedList(); + } + + private void send() throws IOException { + // Java 8 + //try(Stream lines = Files.lines(Paths.get(file.getAbsolutePath()))){ + // lines.forEach(s -> sendLine(s)); + //} + + BufferedReader br = null; + try { + String sCurrentLine; + br = new BufferedReader(new InputStreamReader(file)); + while ((sCurrentLine = br.readLine()) != null) { + sendLine(sCurrentLine); + } + } finally { + if (br != null)br.close(); + } + } + + private void sendLine(String s) { + // Wait until there is room, if necessary. + while (maxBufferSize < (currentBufferSize + s.length() + 1)) { + try { + //System.out.println("waiting for room.... active command count: " + this.activeCommandSizes.size()); + + // Wait for a command to complete + completedCommands.take(); + currentBufferSize -= this.activeCommandSizes.removeFirst(); + } catch (InterruptedException ex) { + Logger.getLogger(SimpleJavaGrblStreamer.class.getName()).log(Level.SEVERE, null, ex); + return; + } + } + + try { + System.out.println("Sending command: " + s.trim()); + //System.out.printf("."); + this.port.writeString(s.trim() + "\n"); + int commandSize = s.length() + 1; + activeCommandSizes.add(commandSize); + currentBufferSize += commandSize; + } catch (SerialPortException ex) { + Logger.getLogger(SimpleJavaGrblStreamer.class.getName()).log(Level.SEVERE, null, ex); + return; + } + } + } +} diff --git a/NatBrailleTools/simpleJavaGrblStreamer/target/classes/com/willwinder/sjgs/simplejavagrblstreamer/SimpleJavaGrblStreamer$GrblSerialListener.class b/NatBrailleTools/simpleJavaGrblStreamer/target/classes/com/willwinder/sjgs/simplejavagrblstreamer/SimpleJavaGrblStreamer$GrblSerialListener.class new file mode 100644 index 0000000..184e392 Binary files /dev/null and b/NatBrailleTools/simpleJavaGrblStreamer/target/classes/com/willwinder/sjgs/simplejavagrblstreamer/SimpleJavaGrblStreamer$GrblSerialListener.class differ diff --git a/NatBrailleTools/simpleJavaGrblStreamer/target/classes/com/willwinder/sjgs/simplejavagrblstreamer/SimpleJavaGrblStreamer$GrblSerialSender.class b/NatBrailleTools/simpleJavaGrblStreamer/target/classes/com/willwinder/sjgs/simplejavagrblstreamer/SimpleJavaGrblStreamer$GrblSerialSender.class new file mode 100644 index 0000000..e3a7e06 Binary files /dev/null and b/NatBrailleTools/simpleJavaGrblStreamer/target/classes/com/willwinder/sjgs/simplejavagrblstreamer/SimpleJavaGrblStreamer$GrblSerialSender.class differ diff --git a/NatBrailleTools/simpleJavaGrblStreamer/target/classes/com/willwinder/sjgs/simplejavagrblstreamer/SimpleJavaGrblStreamer.class b/NatBrailleTools/simpleJavaGrblStreamer/target/classes/com/willwinder/sjgs/simplejavagrblstreamer/SimpleJavaGrblStreamer.class new file mode 100644 index 0000000..627119b Binary files /dev/null and b/NatBrailleTools/simpleJavaGrblStreamer/target/classes/com/willwinder/sjgs/simplejavagrblstreamer/SimpleJavaGrblStreamer.class differ