Skip to content

Commit b5569f5

Browse files
committed
added working graph from input, the textfield is for giving the Hz
1 parent 841f0af commit b5569f5

File tree

5 files changed

+127
-30
lines changed

5 files changed

+127
-30
lines changed

app/src/processing/app/Editor.java

+4
Original file line numberDiff line numberDiff line change
@@ -954,6 +954,10 @@ protected void selectSerialPort(String name) {
954954
serialMonitor.closeSerialPort();
955955
serialMonitor.setVisible(false);
956956
serialMonitor = new SerialMonitor(Preferences.get("serial.port"));
957+
graphMonitor.closeSerialPort();
958+
graphMonitor.setVisible(false);
959+
graphMonitor = new GraphMonitor(Preferences.get("serial.port"));
960+
957961
//System.out.println("set to " + get("serial.port"));
958962
}
959963

app/src/processing/app/Graph.java

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package processing.app;
2+
//import processing.core.*;
3+
4+
import processing.app.debug.MessageConsumer;
5+
6+
7+
import java.io.*;
8+
import java.util.*;
9+
import org.jfree.chart.*;
10+
import org.jfree.chart.ChartFactory;
11+
import org.jfree.chart.ChartPanel;
12+
import org.jfree.chart.JFreeChart;
13+
import org.jfree.chart.axis.NumberTickUnit;
14+
import org.jfree.chart.axis.TickUnits;
15+
import org.jfree.chart.axis.ValueAxis;
16+
import org.jfree.chart.plot.DatasetRenderingOrder;
17+
import org.jfree.chart.plot.PlotOrientation;
18+
import org.jfree.chart.plot.XYPlot;
19+
import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
20+
import org.jfree.chart.renderer.xy.XYStepRenderer;
21+
import org.jfree.data.time.Millisecond;
22+
import org.jfree.data.time.TimeTableXYDataset;
23+
24+
25+
public class Graph {
26+
27+
private TimeTableXYDataset data;
28+
private String name;
29+
public Graph(){
30+
data = new TimeTableXYDataset();
31+
name="a";
32+
}
33+
public void add(Millisecond y, int x){
34+
data.add(y,x,name);
35+
}
36+
public void reset(){
37+
// data=null; //does not work :S
38+
// data=new TimeTableXYDataset();
39+
}
40+
public ChartPanel panel(){
41+
JFreeChart chart = ChartFactory.createXYLineChart(null, null, null,
42+
data, PlotOrientation.VERTICAL,
43+
false, false, false);
44+
ChartPanel panel = new ChartPanel(chart);
45+
46+
return panel;
47+
}
48+
}

app/src/processing/app/GraphMonitor.java

+69-26
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
import processing.core.*;
2323

2424
import java.awt.*;
25-
import java.util.Date;
25+
import java.util.*;
2626
import java.awt.event.*;
2727
import javax.swing.*;
2828
import javax.swing.border.*;
@@ -40,7 +40,7 @@
4040
import org.jfree.chart.plot.XYPlot;
4141
import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
4242
import org.jfree.chart.renderer.xy.XYStepRenderer;
43-
import org.jfree.data.time.Second;
43+
import org.jfree.data.time.Millisecond;
4444
import org.jfree.data.time.TimeTableXYDataset;
4545
public class GraphMonitor extends JFrame implements MessageConsumer {
4646
private Serial serial;
@@ -56,12 +56,16 @@ public class GraphMonitor extends JFrame implements MessageConsumer {
5656
private JComboBox lineEndings;
5757
private JComboBox serialRates;
5858
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;
6064
public GraphMonitor(String port) {
6165
super(port);
6266

6367
this.port = port;
64-
68+
graph= new Graph();
6569
addWindowListener(new WindowAdapter() {
6670
public void windowClosing(WindowEvent e) {
6771
closeSerialPort();
@@ -78,25 +82,29 @@ public void actionPerformed(ActionEvent e) {
7882
}});
7983

8084
getContentPane().setLayout(new BorderLayout());
81-
85+
stringdata= new LinkedList<Character>();
8286
Font font = Theme.getFont("console.font");
83-
toolbar= new GraphToolbar(null,null);
87+
toolbar= new GraphToolbar(this,null);
8488
textArea = new JTextArea(16, 10);
8589
textArea.setEditable(false);
8690
textArea.setFont(font);
8791
// don't automatically update the caret. that way we can manually decide
8892
// whether or not to do so based on the autoscroll checkbox.
8993
((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+
}});
9199
scrollPane = new JScrollPane(textArea);
92100
JPanel pane = new JPanel();
93101
pane.setLayout(new BoxLayout(pane, BoxLayout.Y_AXIS));
94102
pane.setBorder(new EmptyBorder(4, 4, 4, 4));
95-
pane.add(this.graph());
103+
pane.add(graph.panel());
96104
pane.add(scrollPane);
97105
getContentPane().add(toolbar,BorderLayout.NORTH);
98106
getContentPane().add(pane, BorderLayout.CENTER);
99-
107+
getContentPane().add(textField, BorderLayout.SOUTH);
100108
pack();
101109

102110
Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
@@ -162,27 +170,62 @@ public void closeSerialPort() {
162170
}
163171

164172
public void message(final String s) {
173+
//System.err.println(serial.readString());
165174
SwingUtilities.invokeLater(new Runnable() {//TODO implement 2 arrays for x and y values
166175
public void run() {
167-
textArea.append(s);
168-
if (autoscrollBox.isSelected()) {
169-
textArea.setCaretPosition(textArea.getDocument().getLength());
176+
if (start) {
177+
addtobuffer(s);
170178
}
179+
textArea.append(s);
180+
textArea.setCaretPosition(textArea.getDocument().getLength());
171181
}});
172182
}
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+
187230
}
188231
}

app/src/processing/app/GraphToolbar.java

+5-3
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ public class GraphToolbar extends JComponent implements MouseInputListener, KeyL
6565
static final int ROLLOVER = 1;
6666
static final int ACTIVE = 2;
6767

68-
Editor editor;
68+
GraphMonitor monitor;
6969

7070
Image offscreen;
7171
int width, height;
@@ -91,8 +91,8 @@ public class GraphToolbar extends JComponent implements MouseInputListener, KeyL
9191

9292
boolean shiftPressed;
9393

94-
public GraphToolbar(Editor editor, JMenu menu) {
95-
this.editor = editor;
94+
public GraphToolbar(GraphMonitor monitor, JMenu menu) {
95+
this.monitor = monitor;
9696
this.menu = menu;
9797

9898
buttonCount = 0;
@@ -304,9 +304,11 @@ public void mousePressed(MouseEvent e) {
304304

305305
switch (sel) {
306306
case START:
307+
monitor.handleStart();
307308
break;
308309

309310
case STOP:
311+
monitor.handleStop();
310312
break;
311313

312314
case COPY:

build/windows/launcher/config.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
<cp>lib/servlet.jar</cp>
2727
<cp>lib/jfreechart-swt.jar</cp>
2828
<cp>lib/gnujaxp.jar</cp>
29-
29+
<cp>lib/jcommon.jar</cp>
3030
</classPath>
3131
<jre>
3232
<path>java</path>

0 commit comments

Comments
 (0)