22
22
import processing .core .*;
23
23
24
24
import java .awt .*;
25
- import java .util .Date ;
25
+ import java .util .* ;
26
26
import java .awt .event .*;
27
27
import javax .swing .*;
28
28
import javax .swing .border .*;
40
40
import org .jfree .chart .plot .XYPlot ;
41
41
import org .jfree .chart .renderer .xy .XYLineAndShapeRenderer ;
42
42
import org .jfree .chart .renderer .xy .XYStepRenderer ;
43
- import org .jfree .data .time .Second ;
43
+ import org .jfree .data .time .Millisecond ;
44
44
import org .jfree .data .time .TimeTableXYDataset ;
45
45
public class GraphMonitor extends JFrame implements MessageConsumer {
46
46
private Serial serial ;
@@ -56,12 +56,16 @@ public class GraphMonitor extends JFrame implements MessageConsumer {
56
56
private JComboBox lineEndings ;
57
57
private JComboBox serialRates ;
58
58
private int serialRate ;
59
-
59
+ private boolean start =false ;
60
+ private long starttime ;
61
+ private LinkedList <Character > stringdata ;
62
+ private Graph graph ;
63
+ private int hz =1000 ;
60
64
public GraphMonitor (String port ) {
61
65
super (port );
62
66
63
67
this .port = port ;
64
-
68
+ graph = new Graph ();
65
69
addWindowListener (new WindowAdapter () {
66
70
public void windowClosing (WindowEvent e ) {
67
71
closeSerialPort ();
@@ -78,25 +82,29 @@ public void actionPerformed(ActionEvent e) {
78
82
}});
79
83
80
84
getContentPane ().setLayout (new BorderLayout ());
81
-
85
+ stringdata = new LinkedList < Character >();
82
86
Font font = Theme .getFont ("console.font" );
83
- toolbar = new GraphToolbar (null ,null );
87
+ toolbar = new GraphToolbar (this ,null );
84
88
textArea = new JTextArea (16 , 10 );
85
89
textArea .setEditable (false );
86
90
textArea .setFont (font );
87
91
// don't automatically update the caret. that way we can manually decide
88
92
// whether or not to do so based on the autoscroll checkbox.
89
93
((DefaultCaret )textArea .getCaret ()).setUpdatePolicy (DefaultCaret .NEVER_UPDATE );
90
-
94
+ textField = new JTextField (40 );
95
+ textField .addActionListener (new ActionListener () {
96
+ public void actionPerformed (ActionEvent e ) {
97
+ hz =1000 /Integer .valueOf (textField .getText ()).intValue ();
98
+ }});
91
99
scrollPane = new JScrollPane (textArea );
92
100
JPanel pane = new JPanel ();
93
101
pane .setLayout (new BoxLayout (pane , BoxLayout .Y_AXIS ));
94
102
pane .setBorder (new EmptyBorder (4 , 4 , 4 , 4 ));
95
- pane .add (this . graph ());
103
+ pane .add (graph . panel ());
96
104
pane .add (scrollPane );
97
105
getContentPane ().add (toolbar ,BorderLayout .NORTH );
98
106
getContentPane ().add (pane , BorderLayout .CENTER );
99
-
107
+ getContentPane (). add ( textField , BorderLayout . SOUTH );
100
108
pack ();
101
109
102
110
Dimension screen = Toolkit .getDefaultToolkit ().getScreenSize ();
@@ -162,27 +170,62 @@ public void closeSerialPort() {
162
170
}
163
171
164
172
public void message (final String s ) {
173
+ //System.err.println(serial.readString());
165
174
SwingUtilities .invokeLater (new Runnable () {//TODO implement 2 arrays for x and y values
166
175
public void run () {
167
- textArea .append (s );
168
- if (autoscrollBox .isSelected ()) {
169
- textArea .setCaretPosition (textArea .getDocument ().getLength ());
176
+ if (start ) {
177
+ addtobuffer (s );
170
178
}
179
+ textArea .append (s );
180
+ textArea .setCaretPosition (textArea .getDocument ().getLength ());
171
181
}});
172
182
}
173
- public ChartPanel graph (){
174
- TimeTableXYDataset data = new TimeTableXYDataset ();
175
- Second second = new Second (new Date (System .currentTimeMillis () + 1000 ));
176
- data .add (second , 20.0 ,"a" );
177
- Second second2 = new Second (new Date (System .currentTimeMillis () + 2000 ));
178
- data .add (second2 , 30.8 ,"a" );
179
- Second second3 = new Second (new Date (System .currentTimeMillis () + 3000 ));
180
- data .add (second3 , 60.5 ,"a" );
181
- JFreeChart chart = ChartFactory .createXYLineChart (null , null , null ,
182
- data , PlotOrientation .VERTICAL ,
183
- false , false , false );
184
- ChartPanel panel = new ChartPanel (chart );
185
-
186
- return panel ;
183
+ public void addtobuffer (String s ){
184
+ for (int i =0 ; i ==(s .length ()-1 );i ++ ) {
185
+ stringdata .add (s .charAt (i ));
186
+ }
187
+
188
+ }
189
+ public void handleStart (){
190
+ start =true ;
191
+ starttime =System .currentTimeMillis ();
192
+ //serial.write("****");
193
+ //graph.reset();
194
+ if (textField .getText ()!="" ) {
195
+ hz =1000 /Integer .valueOf (textField .getText ()).intValue ();
196
+
197
+ }
198
+ }
199
+ public void handleStop (){
200
+ start =false ;
201
+
202
+ //serial.write("****");
203
+ parse ();
204
+ }
205
+ public void parse (){
206
+ graph .reset ();
207
+ int count =0 ;
208
+ String number ="" ;
209
+ char c ;
210
+ int isNumber =1 ;
211
+ while (stringdata .size ()!=0 ){
212
+ while (isNumber ==1 ){
213
+ c =stringdata .poll ();
214
+ if (Character .isDigit (c )){
215
+ number +=c ;
216
+ }else {
217
+ isNumber =0 ;
218
+ }
219
+ }
220
+ if (number .length ()>0 ){
221
+ int temp = Integer .valueOf ( number ).intValue ();
222
+ graph .add (new Millisecond (new Date (count *hz )),temp );
223
+ number ="" ;
224
+ count ++;
225
+ }
226
+ isNumber =1 ;
227
+ }
228
+ //textArea.append("====");
229
+
187
230
}
188
231
}
0 commit comments