Subversion Repositories Code-Repo

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
90 Kevin 1
package ioio.debugger;
2
 
3
import java.util.LinkedList;
4
 
5
import com.androidplot.Plot.BorderStyle;
6
import com.androidplot.xy.BoundaryMode;
7
import com.androidplot.xy.LineAndPointFormatter;
8
import com.androidplot.xy.StepFormatter;
9
import com.androidplot.xy.XYPlot;
10
import com.androidplot.xy.XYStepMode;
11
 
12
import android.graphics.Color;
13
import android.os.Handler;
14
import android.view.View;
15
import android.widget.TextView;
16
 
17
public class ViewWidgetGraphText implements ViewWidget {
18
 
19
	private View view;
20
	private Runnable run;
21
	private Thread thread;
22
	private Handler handler = new Handler();
23
	private GenericXYSeries plotData;
24
	private XYPlot historyPlot;
25
	private int pin;
26
 
27
	public ViewWidgetGraphText() {
28
 
29
	}
30
 
31
	@Override
32
	public void stopThread() {
33
		thread.interrupt();
34
	}
35
 
36
	@Override
37
	public void startThread() {
38
		// Kill the thread if it is already running
39
		if (thread != null && thread.isAlive())
40
			thread.interrupt();
41
		// Start a new thread and run the passed Runnable
42
		thread = new Thread (run);
43
		thread.start();
44
	}
45
 
46
	@Override
47
	public View getView() {
48
		return view;
49
	}
50
 
51
	@Override
52
	public Thread getThread() {
53
		return thread;
54
	}
55
 
56
	@Override
57
	public void clearData() {
58
		plotData.clearData();
59
	}
60
 
61
	@Override
62
	public void setStopSignal() {
63
		handler.post(new Runnable() {
64
 
65
			@Override
66
			public void run() {
67
				TextView tv = (TextView)view.findViewById(R.id.layout_graphText_status);
68
				tv.setTextColor(Color.RED);
69
			}
70
		});
71
	}
72
 
73
	@Override
74
	public void resetStopSignal() {
75
		handler.post(new Runnable() {
76
 
77
			@Override
78
			public void run() {
79
				TextView tv = (TextView)view.findViewById(R.id.layout_graphText_status);
80
				tv.setTextColor(Color.WHITE);
81
			}
82
		});
83
	}
84
 
85
	@Override
86
	public void sendDataHistory() {
87
		LinkedList<Number> data = plotData.getData();
88
		Double[] data2 = data.toArray(new Double[data.size()]);
89
		IOIODebuggerActivity.get_networkMgr().sendData(pin, data2);
90
	}
91
 
92
	@Override
93
	public int getPin() {
94
		return pin;
95
	}
96
 
97
	public void setPin(final int p) {
98
		pin = p;
99
	}
100
 
101
	public void updateText(final String str) {
102
		handler.post(new Runnable() {
103
 
104
			@Override
105
			public void run() {
106
				TextView tv = (TextView)view.findViewById(R.id.layout_graphText_text);
107
				tv.setText(str);
108
			}
109
		});
110
	}
111
 
112
	public void updateStatus(final String str) {
113
		handler.post(new Runnable() {
114
 
115
			@Override
116
			public void run() {
117
				TextView tv = (TextView)view.findViewById(R.id.layout_graphText_status);
118
				tv.setText(str);
119
			}
120
		});
121
	}
122
 
123
	public void setView(View view, boolean useStep, int historyLength) {
124
		this.view = view;
125
 
126
		// Set graph plot details
127
		plotData = new GenericXYSeries("Analog In", historyLength);
128
		historyPlot = (XYPlot) view.findViewById(R.id.layout_graphText_graph);
129
 
130
		// Add data to plot
131
		if (!useStep)
132
			historyPlot.addSeries(plotData, new LineAndPointFormatter(Color.rgb(0, 0, 200), Color.rgb(0, 0, 100),Color.rgb(150, 150, 190)));
133
		else {
134
			StepFormatter stepFormatter = new StepFormatter(Color.rgb(0, 0, 0), Color.rgb(150, 190, 150));
135
			stepFormatter.getLinePaint().setStrokeWidth(1);
136
			historyPlot.addSeries(plotData, stepFormatter);
137
		}
138
		// Set boundary and ticks
139
		historyPlot.setRangeBoundaries(0, 3.3, BoundaryMode.FIXED);
140
		historyPlot.setDomainBoundaries(0, historyLength, BoundaryMode.FIXED);
141
		historyPlot.setDomainStepValue(5);
142
		historyPlot.setRangeStep(XYStepMode.SUBDIVIDE, 4);
143
		historyPlot.setTicksPerRangeLabel(3);
144
 
145
		// Remove labels and legend
146
		historyPlot.getLegendWidget().setVisible(false);
147
		historyPlot.getDomainLabelWidget().setVisible(false);
148
		historyPlot.getRangeLabelWidget().setVisible(false);
149
		historyPlot.getTitleWidget().setVisible(false);
150
 
151
		// Remove extra margin space
152
		historyPlot.getGraphWidget().setMargins(0, 10, 10, 0);
153
		historyPlot.getGraphWidget().setRangeLabelWidth(30);
154
		historyPlot.getGraphWidget().setDomainLabelWidth(15);
155
		historyPlot.getGraphWidget().setMarginLeft(-5);
156
		historyPlot.getGraphWidget().setMarginRight(25);
157
		historyPlot.setPlotMargins(0, 0, 0, 0);
158
		historyPlot.setPlotPadding(0, 0, 0, 0);
159
 
160
		// Remove background color
161
		historyPlot.getGraphWidget().setBackgroundPaint(null);
162
		historyPlot.setBackgroundPaint(null);
163
		historyPlot.setBorderPaint(null);
164
 
165
		historyPlot.setBorderStyle(BorderStyle.SQUARE, null, null);
166
		historyPlot.disableAllMarkup();
167
	}
168
 
169
	public void setRunnable(Runnable r) {
170
		run = r;
171
	}
172
 
173
	public void updateData(Number num) {
174
		plotData.addDataToList(num);
175
	}
176
 
177
	public void refreshGraph() {
178
		handler.post(new Runnable() {
179
 
180
			@Override
181
			public void run() {
182
				historyPlot.redraw();
183
			}
184
		});
185
	}
186
 
187
	public void setRange(Number start, Number end) {
188
		historyPlot.setRangeBoundaries(start, end, BoundaryMode.FIXED);
189
	}
190
 
191
	public void setDomain(Number length) {
192
		historyPlot.setDomainBoundaries(0, length, BoundaryMode.FIXED);
193
	}
194
}