Edit page History How do I edit this website?

GUI panel for Cobolt laser control

This is a simple script to obtain a GUI panel for easier control of 2 Cobolt laser (here named “Cobolt 491 nm” and “Cobolt 561 nm”); it can be adapted for any other similar device. Just copy this text in Beanshell script editor and run it after have performed right hardware configuration of both Arduino board and lasers By Rocco D’Antuono (Italy)

mmc.setProperty("Arduino-Shutter","OnOff",1);
mmc.setProperty("Arduino-Shutter","OnOff",0);

Laser491nm_state=false; //If Arduino configured w inverted logic///
Laser561nm_state=false; //If Arduino configured w inverted logic///
mmc.setProperty("Cobolt 491 nm", "Laser", "On"); mmc.setProperty("Cobolt
561 nm", "Laser", "On"); MaxPow491nm=200; //Maximum output power (mW)
MaxPow561nm=200; //Maximum output power (mW)

Laser491nm_power=(int)
Math.round(Float.parseFloat(mmc.getProperty("Cobolt 491 nm",
"Power"))*1000); Laser561nm_power=(int)
Math.round(Float.parseFloat(mmc.getProperty("Cobolt 561 nm",
"Power"))*1000); //print(Laser491nm_power); //test
//print(Laser561nm_power); //test

////////////////////////////Sliders////////////////////////////////////

slider_491nm = new JSlider(JSlider.HORIZONTAL, 0, MaxPow491nm,
Laser491nm_power); //Check right maximum power value//
slider_491nm.setBorder(BorderFactory.createTitledBorder("Laser 491 nm -
Power (mW)")); slider_491nm.setPaintTicks(true);
slider_491nm.setMinorTickSpacing(5);
slider_491nm.setMajorTickSpacing(50);
slider_491nm.setPaintTicks(true); slider_491nm.setPaintLabels(true);
slider_491nm.setLabelTable(slider_491nm.createStandardLabels(25));

slider_561nm = new JSlider(JSlider.HORIZONTAL, 0, MaxPow561nm,
Laser561nm_power); //Check right maximum power value//
slider_561nm.setBorder(BorderFactory.createTitledBorder("Laser 561 nm -
Power (mW)")); slider_561nm.setPaintTicks(true);
slider_561nm.setMinorTickSpacing(5);
slider_561nm.setMajorTickSpacing(50);
slider_561nm.setPaintTicks(true); slider_561nm.setPaintLabels(true);
slider_561nm.setLabelTable(slider_561nm.createStandardLabels(25));

////////////////////////////TextFields////////////////////////////////////
textField_491nm = new JFormattedTextField();
textField_491nm.setValue(slider_491nm.getValue());
textField_491nm.setColumns(4); textField_491nm.setEditable(true);
textField_491nm.setForeground(Color.red);

textField_561nm = new JFormattedTextField();
textField_561nm.setValue(slider_561nm.getValue());
textField_561nm.setColumns(4); textField_561nm.setEditable(true);
textField_561nm.setForeground(Color.red);

/////////////////////////Buttons/////////////////////////////////////
button_491nm= new JButton("Open Shutter-Laser 491nm"); button_561nm=
new JButton("Open Shutter-Laser 561nm");

/////////////////////////Frame///////////////////////////////////////
frame= new JFrame("Cobolt Laser-Power Controller"); frame.setSize(750,
200); frame.setVisible(true); cp = frame.getContentPane();
cp.setLayout(new GridLayout(2, 1 ));

Panel_491nm=new JPanel(new BorderLayout());
Panel_491nm.setBorder(BorderFactory.createTitledBorder("Laser 491
nm")); Panel_491nm.setLayout(new GridLayout(1, 3 ));

Panel_561nm=new JPanel(new BorderLayout());
Panel_561nm.setBorder(BorderFactory.createTitledBorder("Laser 561
nm")); Panel_561nm.setLayout(new GridLayout(1, 3 ));

Panel_491nm.add(slider_491nm); Panel_491nm.add(textField_491nm);
Panel_491nm.add(button_491nm);

Panel_561nm.add(slider_561nm); Panel_561nm.add(textField_561nm);
Panel_561nm.add(button_561nm);

cp.add(Panel_491nm); cp.add(Panel_561nm);

/////////////////////////////ChangeListeners///////////////////////////////////////

slider_491nm.addChangeListener(new ChangeListener() {

       stateChanged(ChangeEvent arg0) {
           JSlider s_491nm = (JSlider) arg0.getSource();
           if (!s_491nm.getValueIsAdjusting()) {
              Laser491nm_power=slider_491nm.getValue();
              textField_491nm.setValue(slider_491nm.getValue());
              mmc.setProperty("Cobolt 491 nm", "Power", (float) Laser491nm_power*0.001); ///P(mW)/// 
              gui.refreshGUI();
           }
       }
   });

slider_561nm.addChangeListener(new ChangeListener() {

       stateChanged(ChangeEvent arg1) {
           JSlider s_561nm = (JSlider) arg1.getSource();
           if (!s_561nm.getValueIsAdjusting()) {
              Laser561nm_power=slider_561nm.getValue();
              textField_561nm.setValue(slider_561nm.getValue());
              mmc.setProperty("Cobolt 561 nm", "Power", (float) Laser561nm_power*0.001); ///P(mW)///
              gui.refreshGUI();
           }
       }
   });

//////////////////TextField Listeners////////////////////////////////////

 textField_491nm.addActionListener(new ActionListener() {
     actionPerformed(ActionEvent e) {
     if (Integer.parseInt(textField_491nm.getText())<0)
                      {
                           JOptionPane.showMessageDialog(null,
                                   "Error: Please enter number bigger than 0", "Error Massage",
                                   JOptionPane.ERROR_MESSAGE);
                       textField_491nm.setValue(0);
                       }

       slider_491nm.setValue(Integer.parseInt(textField_491nm.getText()));
     }
   });

 textField_561nm.addActionListener(new ActionListener() {
     actionPerformed(ActionEvent e2) {
     if (Integer.parseInt(textField_561nm.getText())<0)
                      {
                           JOptionPane.showMessageDialog(null,
                                   "Error: Please enter number bigger than 0", "Error Massage",
                                   JOptionPane.ERROR_MESSAGE);
                        textField_561nm.setValue(0);
                       }

       slider_561nm.setValue(Integer.parseInt(textField_561nm.getText()));
     }
   });

//////////////////Button Listeners////////////////////////////////////

button_491nm.addActionListener(new ActionListener() {

        actionPerformed(ActionEvent evt) {  
            if(Laser491nm_state==false)
                   {Laser491nm_state=true;
               mmc.setProperty("Arduino-Switch","State", 16);
               mmc.setProperty("Arduino-Shutter","OnOff", 1);                 
               gui.refreshGUI();
               button_491nm.setText("Close Shutter-Laser 491nm");
               Laser561nm_state=false;
               button_561nm.setText("Open Shutter-Laser 561nm");
               //print(Laser491nm_state);  //test
                    }
               else if(Laser491nm_state==true)
                   {Laser491nm_state=false;
               mmc.setProperty("Arduino-Switch","State", 16);
               mmc.setProperty("Arduino-Shutter","OnOff", 0);                 
               gui.refreshGUI();
               button_491nm.setText("Open Shutter-Laser 491nm");
               //print(Laser491nm_state); //test
               }
           }  
       }); 

button_561nm.addActionListener(new ActionListener() {

           actionPerformed(ActionEvent evt2) {  
            if(Laser561nm_state==false)
                   {Laser561nm_state=true;
               mmc.setProperty("Arduino-Switch","State", 32);
               mmc.setProperty("Arduino-Shutter","OnOff", 1);                 
               gui.refreshGUI();
               button_561nm.setText("Close Shutter-Laser 561nm");
               Laser491nm_state=false;
               button_491nm.setText("Open Shutter-Laser 491nm");
               //print(Laser561nm_state); //test
                    }
               else if(Laser561nm_state==true)
                   {Laser561nm_state=false;
               mmc.setProperty("Arduino-Switch","State", 32);
               mmc.setProperty("Arduino-Shutter","OnOff", 0);                 
               gui.refreshGUI();
               button_561nm.setText("Open Shutter-Laser 561nm");
               //print(Laser561nm_state); //test
               } 
           }  
       });