Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * This class will get the input from arduino board and output
- * the data to the scanner
- * Code adopted from Silveira Neto
- */
- package arduinojava;
- import java.io.OutputStream;
- import javax.swing.JOptionPane;
- import java.io.InputStream;
- import gnu.io.CommPortIdentifier;
- import gnu.io.SerialPort;
- import gnu.io.SerialPortEvent;
- import gnu.io.SerialPortEventListener;
- public class ArduinoJava extends javax.swing.JFrame implements SerialPortEventListener {
- /** Creates new form ColorSensorReaderWindow */
- public ArduinoJava() {
- initComponents();
- }
- @SuppressWarnings("unchecked")
- // <editor-fold defaultstate="collapsed" desc="Generated Code">
- private void initComponents() {
- button_calibrar = new javax.swing.JButton();
- painel_cor = new javax.swing.JPanel();
- jLabel1 = new javax.swing.JLabel();
- setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
- button_calibrar.setText("Calibrar");
- button_calibrar.addActionListener(new java.awt.event.ActionListener() {
- @Override
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- button_calibrarActionPerformed(evt);
- }
- });
- javax.swing.GroupLayout painel_corLayout = new javax.swing.GroupLayout(painel_cor);
- painel_cor.setLayout(painel_corLayout);
- painel_corLayout.setHorizontalGroup(
- painel_corLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addGap(0, 232, Short.MAX_VALUE)
- );
- painel_corLayout.setVerticalGroup(
- painel_corLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addGap(0, 153, Short.MAX_VALUE)
- );
- jLabel1.setText("Cor");
- javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
- getContentPane().setLayout(layout);
- layout.setHorizontalGroup(
- layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addGroup(layout.createSequentialGroup()
- .addGap(84, 84, 84)
- .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addComponent(painel_cor, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
- .addComponent(button_calibrar, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 232, Short.MAX_VALUE))
- .addGap(84, 84, 84))
- .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
- .addContainerGap(193, Short.MAX_VALUE)
- .addComponent(jLabel1)
- .addGap(190, 190, 190))
- );
- layout.setVerticalGroup(
- layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
- .addGap(34, 34, 34)
- .addComponent(jLabel1)
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
- .addComponent(painel_cor, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 29, Short.MAX_VALUE)
- .addComponent(button_calibrar)
- .addGap(41, 41, 41))
- );
- pack();
- }// </editor-fold>
- private void button_calibrarActionPerformed(java.awt.event.ActionEvent evt) {
- // TODO add your handling code here:
- System.out.println("Calibrate pressed");
- System.arraycopy(RawColor, 0, CalibrationColor, 0, 3);
- }
- static InputStream input;
- static OutputStream output;
- @Override
- public void serialEvent(SerialPortEvent oEvent) {
- if (oEvent.getEventType() == SerialPortEvent.DATA_AVAILABLE) {
- try {
- int available = input.available();
- byte chunk[] = new byte[available];
- input.read(chunk, 0, available);
- for(byte b : chunk){
- if(b == '\n' && !SerialData.isEmpty()) {
- String [] RGBValues = SerialData.split(" ");
- if(RGBValues.length != 3)
- throw new Error("Receivde data could not be decoded");
- for(int i=0; i<3; i++) {
- RawColor[i] = Integer.parseInt(RGBValues[i]);
- CalibratedColor[i] = Math.round(255.0f*(1024.0f-(float)RawColor[i])/(1024.0f-(float)CalibrationColor[i]));
- }
- //correct values if outside range
- if(CalibratedColor[RED]>255)
- CalibratedColor[RED]=255;
- if(CalibratedColor[GREEN]>255)
- CalibratedColor[GREEN]=255;
- if(CalibratedColor[BLUE]>255)
- CalibratedColor[BLUE]=255;
- //displayColor();
- System.out.println(Integer.toString(RawColor[RED]));
- System.out.println(Integer.toString(RawColor[GREEN]));
- System.out.println(Integer.toString(RawColor[BLUE]));
- SerialData="";
- }
- else
- SerialData += (char) b;
- }
- } catch (Exception e) {
- System.err.println(e.toString());
- JOptionPane.showMessageDialog(null,
- e.toString(),
- "Error",
- JOptionPane.ERROR_MESSAGE);
- //DisconnectButton.setEnabled(false);
- //ConnectButton.setEnabled(true);
- //closeSerialComm();
- }
- }
- public static void main(String[] args) throws Exception {
- System.out.println("Connecting to serial device...");
- MyPrefs.init();
- System.out.println("Using port: " + MyPrefs.get("serial.port"));
- System.out.println("Using board: " + MyPrefs.get("board"));
- CommPortIdentifier portId = CommPortIdentifier.getPortIdentifier(
- MyPrefs.get("serial.port"));
- SerialPort port = (SerialPort)portId.open("serial talker", 4000);
- // open the streams
- input = port.getInputStream();
- output = port.getOutputStream();
- port.setSerialPortParams(MyPrefs.getInteger("serial.debug_rate"),
- SerialPort.DATABITS_8,
- SerialPort.STOPBITS_1,
- SerialPort.PARITY_NONE);
- java.awt.EventQueue.invokeLater(new Runnable() {
- @Override
- public void run() {
- new ArduinoJava().setVisible(true);
- }
- });
- }
- // Variables declaration - do not modify
- private javax.swing.JButton button_calibrar;
- private javax.swing.JLabel jLabel1;
- private javax.swing.JPanel painel_cor;
- // End of variables declaration
- /*Incoming data from Arduino*/
- private String SerialData = "";
- private final int RED=0;
- private final int GREEN=1;
- private final int BLUE=2;
- private int CalibrationColor [] = {1,1,1};
- private int RawColor [] = {255,255,255};
- private int CalibratedColor [] = {255,255,255};
- }
Advertisement
Add Comment
Please, Sign In to add comment