Guest User

Untitled

a guest
Jun 15th, 2011
318
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.49 KB | None | 0 0
  1. /*
  2. * This class will get the input from arduino board and output
  3. * the data to the scanner
  4. * Code adopted from Silveira Neto
  5. */
  6.  
  7. package arduinojava;
  8.  
  9. import java.io.OutputStream;
  10. import javax.swing.JOptionPane;
  11. import java.io.InputStream;
  12. import gnu.io.CommPortIdentifier;
  13. import gnu.io.SerialPort;
  14. import gnu.io.SerialPortEvent;
  15. import gnu.io.SerialPortEventListener;
  16.  
  17. public class ArduinoJava extends javax.swing.JFrame implements SerialPortEventListener {
  18.  
  19.     /** Creates new form ColorSensorReaderWindow */
  20.     public ArduinoJava() {
  21.         initComponents();
  22.     }
  23.  
  24.     @SuppressWarnings("unchecked")
  25.     // <editor-fold defaultstate="collapsed" desc="Generated Code">
  26.     private void initComponents() {
  27.  
  28.         button_calibrar = new javax.swing.JButton();
  29.         painel_cor = new javax.swing.JPanel();
  30.         jLabel1 = new javax.swing.JLabel();
  31.  
  32.         setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
  33.  
  34.         button_calibrar.setText("Calibrar");
  35.         button_calibrar.addActionListener(new java.awt.event.ActionListener() {
  36.             @Override
  37.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  38.                 button_calibrarActionPerformed(evt);
  39.             }
  40.         });
  41.  
  42.         javax.swing.GroupLayout painel_corLayout = new javax.swing.GroupLayout(painel_cor);
  43.         painel_cor.setLayout(painel_corLayout);
  44.         painel_corLayout.setHorizontalGroup(
  45.             painel_corLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  46.             .addGap(0, 232, Short.MAX_VALUE)
  47.         );
  48.         painel_corLayout.setVerticalGroup(
  49.             painel_corLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  50.             .addGap(0, 153, Short.MAX_VALUE)
  51.         );
  52.  
  53.         jLabel1.setText("Cor");
  54.  
  55.         javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
  56.         getContentPane().setLayout(layout);
  57.         layout.setHorizontalGroup(
  58.             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  59.             .addGroup(layout.createSequentialGroup()
  60.                 .addGap(84, 84, 84)
  61.                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  62.                     .addComponent(painel_cor, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
  63.                     .addComponent(button_calibrar, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 232, Short.MAX_VALUE))
  64.                 .addGap(84, 84, 84))
  65.             .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
  66.                 .addContainerGap(193, Short.MAX_VALUE)
  67.                 .addComponent(jLabel1)
  68.                 .addGap(190, 190, 190))
  69.         );
  70.         layout.setVerticalGroup(
  71.             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  72.             .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
  73.                 .addGap(34, 34, 34)
  74.                 .addComponent(jLabel1)
  75.                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  76.                 .addComponent(painel_cor, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
  77.                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 29, Short.MAX_VALUE)
  78.                 .addComponent(button_calibrar)
  79.                 .addGap(41, 41, 41))
  80.         );
  81.  
  82.         pack();
  83.     }// </editor-fold>
  84.    
  85.     private void button_calibrarActionPerformed(java.awt.event.ActionEvent evt) {
  86.         // TODO add your handling code here:
  87.         System.out.println("Calibrate pressed");
  88.          
  89.     System.arraycopy(RawColor, 0, CalibrationColor, 0, 3);
  90.     }
  91.    
  92.     static InputStream input;
  93.     static OutputStream output;
  94.  
  95.     @Override
  96.     public void serialEvent(SerialPortEvent oEvent) {
  97.         if (oEvent.getEventType() == SerialPortEvent.DATA_AVAILABLE) {
  98.             try {
  99.             int available = input.available();          
  100.             byte chunk[] = new byte[available];  
  101.             input.read(chunk, 0, available);
  102.  
  103.             for(byte b : chunk){
  104.                 if(b == '\n' && !SerialData.isEmpty()) {
  105.                     String [] RGBValues = SerialData.split(" ");
  106.                    
  107.                     if(RGBValues.length != 3)
  108.                             throw new Error("Receivde data could not be decoded");
  109.                    
  110.                     for(int i=0; i<3; i++) {
  111.                         RawColor[i] = Integer.parseInt(RGBValues[i]);
  112.                         CalibratedColor[i] = Math.round(255.0f*(1024.0f-(float)RawColor[i])/(1024.0f-(float)CalibrationColor[i]));
  113.                     }
  114.                    
  115.                     //correct values if outside range
  116.                        
  117.                     if(CalibratedColor[RED]>255)       
  118.                         CalibratedColor[RED]=255;
  119.                     if(CalibratedColor[GREEN]>255)
  120.             CalibratedColor[GREEN]=255;
  121.                     if(CalibratedColor[BLUE]>255)
  122.             CalibratedColor[BLUE]=255;
  123.  
  124.                     //displayColor();
  125.                     System.out.println(Integer.toString(RawColor[RED]));
  126.                     System.out.println(Integer.toString(RawColor[GREEN]));
  127.                     System.out.println(Integer.toString(RawColor[BLUE]));
  128.                                                    
  129.                     SerialData="";
  130.                 }
  131.         else
  132.                     SerialData += (char) b;
  133.             }
  134.             } catch (Exception e) {
  135.         System.err.println(e.toString());
  136.                 JOptionPane.showMessageDialog(null,
  137.               e.toString(),
  138.               "Error",
  139.               JOptionPane.ERROR_MESSAGE);
  140.  
  141.                 //DisconnectButton.setEnabled(false);
  142.         //ConnectButton.setEnabled(true);
  143.         //closeSerialComm();
  144.             }
  145.     }
  146.    
  147.     public static void main(String[] args) throws Exception {
  148.        
  149.         System.out.println("Connecting to serial device...");
  150.        
  151.         MyPrefs.init();
  152.         System.out.println("Using port: " + MyPrefs.get("serial.port"));
  153.         System.out.println("Using board: " + MyPrefs.get("board"));
  154.         CommPortIdentifier portId = CommPortIdentifier.getPortIdentifier(
  155.                 MyPrefs.get("serial.port"));
  156.  
  157.         SerialPort port = (SerialPort)portId.open("serial talker", 4000);
  158.        
  159.         // open the streams
  160.         input = port.getInputStream();
  161.         output = port.getOutputStream();
  162.         port.setSerialPortParams(MyPrefs.getInteger("serial.debug_rate"),
  163.                 SerialPort.DATABITS_8,
  164.                 SerialPort.STOPBITS_1,
  165.                 SerialPort.PARITY_NONE);
  166.  
  167.         java.awt.EventQueue.invokeLater(new Runnable() {
  168.             @Override
  169.             public void run() {
  170.                 new ArduinoJava().setVisible(true);
  171.             }
  172.         });
  173.        
  174.     }
  175.    
  176.     // Variables declaration - do not modify
  177.     private javax.swing.JButton button_calibrar;
  178.     private javax.swing.JLabel jLabel1;
  179.     private javax.swing.JPanel painel_cor;
  180.     // End of variables declaration
  181.     /*Incoming data from Arduino*/
  182.     private String SerialData = "";
  183.  
  184.     private final int RED=0;
  185.     private final int GREEN=1;
  186.     private final int BLUE=2;
  187.  
  188.     private int CalibrationColor [] = {1,1,1};
  189.     private int RawColor [] = {255,255,255};
  190.     private int CalibratedColor [] = {255,255,255};
  191. }
Advertisement
Add Comment
Please, Sign In to add comment