Project

General

Profile

BlindHelp.java

Catarina Ricardo, 10/06/2019 16:56

Download (10.3 KB)

 
1
import java.awt.EventQueue;
2
import javax.swing.JFrame;
3
import javax.swing.JTextField;
4
import java.awt.BorderLayout;
5
import javax.swing.JLabel;
6
import java.awt.SystemColor;
7
import java.awt.Font;
8
import javax.swing.JProgressBar;
9
import java.awt.Color;
10
import javax.swing.JButton;
11
import javax.swing.JComboBox;
12
import javax.swing.JMenu;
13
import javax.swing.JPanel;
14
import javax.swing.JList;
15
import javax.swing.AbstractListModel;
16
import javax.swing.JTextPane;
17
import com.fazecast.jSerialComm.SerialPort;
18
import javax.swing.Box;
19
import java.awt.Canvas;
20
import java.awt.Graphics;
21
import java.awt.event.ActionListener;
22
import java.util.Scanner;
23
import java.awt.event.ActionEvent;
24
import gnu.io.*;
25
import javax.swing.JLayeredPane;
26
import javax.swing.UIManager;
27
import javax.swing.JFormattedTextField;
28
import javax.swing.SwingConstants;
29
import javax.swing.JRadioButton;
30

    
31
public class BlindHelp {
32

    
33
        private static JFrame BlindHelp;
34
        private static JTextField Below_Values;
35
        private static JTextField Above_Values;
36
        private static JTextField Ground_Values;
37
        static SerialPort chosenPort;
38
        private static JButton connectButton;
39
        private static JComboBox<String> portList;
40
        static int x = 0;
41
        private static JTextField Angle_Value;
42
        private JLabel lblAngle;
43
        private JTextField buz_up;
44
        private JTextField buz_down;
45
        private JTextField buz_ground;
46

    
47
        public double cmToMeters(double d) {
48
                double meters = d / 100;
49
                return meters;
50
        }
51

    
52
        // when a value is less than required the vibration motors are activated 
53
        public void vibrationMotorsOnOff(JTextField values, JTextField buz, double range) {
54
                double n = Double.parseDouble(values.getText());
55
                // setting maximum according to the sensor
56
                if (n <= range) {
57
                        buz.setText("ON"); buz.setBackground(Color.green);
58
                } else {
59
                        buz.setText("OFF"); buz.setBackground(Color.red);
60
                }
61

    
62
        }
63

    
64
        public void vibrationMotorsGround(JTextField values, JTextField buz, double range) {
65
                double n = Double.parseDouble(values.getText());
66
                if (n >= range) {
67
                        buz.setText("ON"); buz.setBackground(Color.green);
68
                } else {
69
                        buz.setText("OFF"); buz.setBackground(Color.red);
70
                }
71
        }
72

    
73
        /**
74
         * Launch the application.
75
         */
76
        public static void main(String[] args) {
77
                EventQueue.invokeLater(new Runnable() {
78

    
79
                        @SuppressWarnings("static-access")
80
                        public void run() {
81
                                try {
82
                                        BlindHelp window = new BlindHelp();
83
                                        window.BlindHelp.setVisible(true);
84

    
85
                                } catch (Exception e) {
86
                                        e.printStackTrace();
87
                                }
88

    
89
                        }
90
                });
91
        }
92

    
93
        /**
94
         * Create the application.
95
         */
96
        public BlindHelp() {
97
                initialize();
98

    
99
        }
100

    
101
        /**
102
         * Initialize the contents of the frame.
103
         */
104
        private void initialize() {
105

    
106
// create and configure the window
107
                BlindHelp = new JFrame();
108
                BlindHelp.setBackground(SystemColor.activeCaption);
109
                BlindHelp.setTitle("Blind Help");
110
                BlindHelp.setResizable(false);
111
                BlindHelp.setBounds(100, 100, 450, 300);
112
                BlindHelp.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
113
                BlindHelp.getContentPane().setLayout(null);
114

    
115
// create a drop-down box and connect button, then place them at the top of the window
116
                JComboBox<String> portList = new JComboBox<String>();
117
                portList.setBounds(28, 21, 69, 20);
118
                BlindHelp.getContentPane().add(portList);
119

    
120
// Connect Button to COM ports                
121
                JButton connectButton = new JButton("Connect");
122
                connectButton.setBounds(137, 21, 102, 20);
123
                BlindHelp.getContentPane().add(connectButton);
124

    
125
// populate the drop-down box
126
                SerialPort[] portNames = SerialPort.getCommPorts();
127
                for (int i = 0; i < portNames.length; i++)
128
                        portList.addItem(portNames[i].getSystemPortName());
129

    
130
                Box verticalBox = Box.createVerticalBox();
131
                verticalBox.setBounds(190, 183, 16, -10);
132
                BlindHelp.getContentPane().add(verticalBox);
133

    
134
                lblAngle = new JLabel("Angle");
135
                lblAngle.setFont(new Font("Tahoma", Font.BOLD, 11));
136
                lblAngle.setBounds(28, 64, 42, 14);
137
                BlindHelp.getContentPane().add(lblAngle);
138

    
139
                JLabel lblUpSensor = new JLabel("Up Sensor");
140
                lblUpSensor.setFont(new Font("Tahoma", Font.BOLD, 11));
141
                lblUpSensor.setBounds(28, 159, 118, 14);
142
                BlindHelp.getContentPane().add(lblUpSensor);
143

    
144
                JLabel lblDownSensor = new JLabel("Down Sensor");
145
                lblDownSensor.setFont(new Font("Tahoma", Font.BOLD, 11));
146
                lblDownSensor.setBounds(28, 186, 118, 14);
147
                BlindHelp.getContentPane().add(lblDownSensor);
148

    
149
                JLabel lblGroundSensor = new JLabel("Ground Sensor");
150
                lblGroundSensor.setFont(new Font("Tahoma", Font.BOLD, 11));
151
                lblGroundSensor.setBounds(28, 211, 118, 14);
152
                BlindHelp.getContentPane().add(lblGroundSensor);
153

    
154
                JFormattedTextField frmtdtxtfldObstaclesDetection = new JFormattedTextField();
155
                frmtdtxtfldObstaclesDetection.setHorizontalAlignment(SwingConstants.CENTER);
156
                frmtdtxtfldObstaclesDetection.setEditable(false);
157
                frmtdtxtfldObstaclesDetection.setText("Obstacles Detection");
158
                frmtdtxtfldObstaclesDetection.setBounds(28, 96, 388, 20);
159
                BlindHelp.getContentPane().add(frmtdtxtfldObstaclesDetection);
160

    
161
                JLabel lblDegrees = new JLabel("degrees");
162
                lblDegrees.setFont(new Font("Tahoma", Font.PLAIN, 11));
163
                lblDegrees.setBounds(156, 64, 42, 14);
164
                BlindHelp.getContentPane().add(lblDegrees);
165

    
166
//DISTANCE VALUES =============================================================================
167

    
168
                Above_Values = new JTextField();
169
                Above_Values.setEditable(false);
170
                Above_Values.setColumns(10);
171
                Above_Values.setBackground(Color.WHITE);
172
                Above_Values.setBounds(167, 156, 59, 20);
173
                BlindHelp.getContentPane().add(Above_Values);
174

    
175
                Below_Values = new JTextField();
176
                Below_Values.setBackground(SystemColor.text);
177
                Below_Values.setEditable(false);
178
                Below_Values.setColumns(10);
179
                Below_Values.setBounds(167, 183, 59, 20);
180
                BlindHelp.getContentPane().add(Below_Values);
181

    
182
                Ground_Values = new JTextField();
183
                Ground_Values.setEditable(false);
184
                Ground_Values.setColumns(10);
185
                Ground_Values.setBackground(Color.WHITE);
186
                Ground_Values.setBounds(167, 208, 59, 20);
187
                BlindHelp.getContentPane().add(Ground_Values);
188

    
189
//BUZZERS ================================================================================
190
                JLabel lblBuzzers = new JLabel("Buzzers");
191
                lblBuzzers.setFont(new Font("Tahoma", Font.BOLD, 11));
192
                lblBuzzers.setBounds(302, 127, 52, 14);
193
                BlindHelp.getContentPane().add(lblBuzzers);
194

    
195
                JLabel lblDistance = new JLabel("Distance (meters)");
196
                lblDistance.setFont(new Font("Tahoma", Font.BOLD, 11));
197
                lblDistance.setBounds(145, 127, 118, 14);
198
                BlindHelp.getContentPane().add(lblDistance);
199

    
200
                JTextField Angle_Values = new JTextField();
201
                Angle_Values.setEditable(false);
202
                Angle_Values.setColumns(10);
203
                Angle_Values.setBackground(Color.WHITE);
204
                Angle_Values.setBounds(87, 61, 59, 20);
205
                BlindHelp.getContentPane().add(Angle_Values);
206

    
207
                buz_up = new JTextField();
208
                buz_up.setHorizontalAlignment(SwingConstants.CENTER);
209
                buz_up.setForeground(new Color(255, 255, 255));
210
                buz_up.setFont(new Font("Tahoma", Font.BOLD, 11));
211
                buz_up.setEditable(false);
212
                buz_up.setBounds(312, 156, 31, 20);
213
                BlindHelp.getContentPane().add(buz_up);
214
                buz_up.setColumns(10);
215

    
216
                buz_down = new JTextField();
217
                buz_down.setForeground(new Color(255, 255, 255));
218
                buz_down.setHorizontalAlignment(SwingConstants.CENTER);
219
                buz_down.setFont(new Font("Tahoma", Font.BOLD, 11));
220
                buz_down.setEditable(false);
221
                buz_down.setColumns(10);
222
                buz_down.setBounds(312, 183, 31, 20);
223
                BlindHelp.getContentPane().add(buz_down);
224

    
225
                buz_ground = new JTextField();
226
                buz_ground.setHorizontalAlignment(SwingConstants.CENTER);
227
                buz_ground.setForeground(new Color(255, 255, 255));
228
                buz_ground.setFont(new Font("Tahoma", Font.BOLD, 11));
229
                buz_ground.setEditable(false);
230
                buz_ground.setColumns(10);
231
                buz_ground.setBounds(312, 208, 31, 20);
232
                BlindHelp.getContentPane().add(buz_ground);
233

    
234
                // show the window
235
                BlindHelp.setVisible(true);
236

    
237
                // configure the connect button and use another thread to listen for data
238
                connectButton.addActionListener(new ActionListener() {
239

    
240
                        @Override
241
                        public void actionPerformed(ActionEvent arg0) {
242
                                if (connectButton.getText().equals("Connect")) {
243
                                        // attempt to connect to the serial port
244
                                        chosenPort = SerialPort.getCommPort(portList.getSelectedItem().toString());
245
                                        chosenPort.setComPortTimeouts(SerialPort.TIMEOUT_SCANNER, 0, 0);
246

    
247
                                        if (chosenPort.openPort()) {
248
                                                connectButton.setText("Disconnect");
249
                                                portList.setEnabled(false);
250
                                        }
251

    
252
                                        // create a new thread that listens for incoming text and populates the graph
253
                                        Thread thread = new Thread() {
254
                                                @Override
255
                                                public void run() {
256
                                                        Scanner scanner = new Scanner(chosenPort.getInputStream());
257
                                                        while (scanner.hasNextLine()) {
258
                                                                try {
259
                                                                        while (scanner.hasNextLine()) {
260
                                                                                String line = scanner.nextLine();
261
                                                                                if (line.matches("Begin")) {
262
                                                                                        while (scanner.hasNextInt()) {
263
                                                                                                double angle = (scanner.nextDouble());
264
                                                                                                double dif = cmToMeters(scanner.nextDouble());
265
                                                                                                double up = cmToMeters(scanner.nextInt());
266
                                                                                                double middle = cmToMeters(scanner.nextInt());
267
                                                                                                double down = cmToMeters(scanner.nextInt());
268
                                                                                                double dist_dif = dif + 0.5;
269
                                                                                                Angle_Values.setText(Double.toString(180 - angle));
270
                                                                                                                                                                                                
271
                                                                                                Above_Values.setText(Double.toString(up));
272
                                                                                                vibrationMotorsOnOff(Above_Values, buz_up, dist_dif);
273
                                                                                                                                                                                                
274
                                                                                                Below_Values.setText(Double.toString(middle));
275
                                                                                                vibrationMotorsOnOff(Below_Values, buz_down, 0.5);
276
                                                                                                                                                                                                
277
                                                                                                Ground_Values.setText(Double.toString(down));
278
                                                                                                vibrationMotorsGround(Ground_Values, buz_ground, 0.5);                                                                                                                                                                                
279
                                                                                        }
280
                                                                                        BlindHelp.repaint();
281
                                                                                }
282
                                                                        }
283

    
284
                                                                } catch (Exception e) {
285
                                                                }
286
                                                        }
287
                                                        scanner.close();
288
                                                }
289
                                        };
290
                                        thread.start();
291

    
292
                                } else {
293

    
294
                                        // disconnect from the serial port
295
                                        chosenPort.closePort();
296
                                        portList.setEnabled(true);
297
                                        connectButton.setText("Connect");
298
                                        x = 0;
299
                                }
300
                        }
301
                });
302

    
303
                JButton btnExit = new JButton("Exit");
304
                btnExit.addActionListener(new ActionListener() {
305
                        public void actionPerformed(ActionEvent arg0) {
306
                                System.exit(0);
307
                                chosenPort.closePort();
308
                        }
309
                });
310
                btnExit.setBounds(375, 237, 59, 23);
311
                BlindHelp.getContentPane().add(btnExit);
312

    
313
        }
314
}