Subversion Repositories Code-Repo

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
103 Kevin 1
package IEEERobotics.IOIOAI.VT;
2
 
3
import java.text.DecimalFormat;
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.content.Context;
13
import android.content.SharedPreferences;
14
import android.graphics.Color;
15
import android.os.Handler;
16
import android.preference.PreferenceManager;
17
import android.view.View;
18
import android.view.ViewGroup;
19
import android.widget.LinearLayout;
20
import android.widget.ScrollView;
21
import android.widget.TextView;
22
import android.widget.TableRow.LayoutParams;
23
import ioio.lib.api.IOIO;
24
import ioio.lib.api.exception.ConnectionLostException;
25
 
26
public class DebugCode {
27
	private IOIORoboticsActivity _parent;
28
	private IOIO _ioio;
29
	private Context _appContext;
30
	private View _view;
31
	private SharedPreferences _sharedPrefs;
32
	private Handler _handler = new Handler();
33
	private Boolean _connected = false;
34
 
35
	// Input classes to be implemented
36
	private InputInterface _lineFollowIn;
37
	private InputInterface _voltageIn;
38
	private InputInterface _waveformIn;
39
	private InputInterface _capacitanceIn;
40
	private InputInterface _temperatureIn;
41
 
42
	// Updater thread
43
	private Thread _pollingThread;
44
 
45
	// Labels
46
	private TextView _lineFollowLabel;
47
	private TextView _voltageLabel;
48
	private TextView _waveformLabel;
49
	private TextView _capacitanceLabel;
50
	private TextView _temperatureLabel;
51
 
52
	// Plots
53
	private XYPlot _lineFollowPlot;
54
	private XYPlot _voltagePlot;
55
	private XYPlot _waveformPlot;
56
	private XYPlot _capacitancePlot;
57
	private XYPlot _temperaturePlot;
58
 
59
	// Data for each plot
60
	private GenericXYSeries _lineFollowData;
61
	private GenericXYSeries _voltageData;
62
	private GenericXYSeries _waveformData;
63
	private GenericXYSeries _capacitanceData;
64
	private GenericXYSeries _temperatureData;
65
 
66
	// Constant values
67
	private static int _graphHeight = 80;	// Height in dp
68
	private static int _textSize = 15;		// TextSize for the labels
69
	private static int _graphLength;
70
	private static int _updateSleepPeriod;
71
 
72
	/** Class initializer */
73
	public DebugCode(IOIORoboticsActivity parent) {
74
		_parent = parent;
75
		_appContext = IOIORoboticsActivity.context_;
76
 
77
		// Get values from shared preferences
78
		_sharedPrefs = PreferenceManager.getDefaultSharedPreferences(_appContext);
79
		_graphLength = Integer.parseInt(_sharedPrefs.getString("pref_GraphLength", "100"));
80
 
81
		// Create the overall view with the labels and plots
82
		createView();
83
	}
84
 
85
	/** Starting poing for executing Debug code */
86
	public void Run() {
87
		_updateSleepPeriod = Integer.parseInt(_sharedPrefs.getString("pref_UpdateInterval", "100"));
88
 
89
		// Initialize the input classes
90
		initializeInputs();
91
 
92
		// Create the updater thread
93
		setupPollingThread();
94
 
95
		_parent.logMessage("Updater thread started");
96
		_pollingThread.start();
97
	}
98
 
99
	/** Initialize input classes */
100
	private void initializeInputs() {
101
		_parent.logMessage("Initializing inputs");
102
 
103
		_ioio = IOIORoboticsActivity.IOIOInstance_;
104
 
105
		// Initialize each input class (opens pins)
106
		try {
107
			_lineFollowIn = new InputLineFollower();
108
			_lineFollowIn.initialize(_ioio);
109
 
110
			_voltageIn = new InputVoltage();
111
			_voltageIn.initialize(_ioio);
112
 
113
			_waveformIn = new InputWaveform();
114
			_waveformIn.initialize(_ioio);
115
 
116
			_capacitanceIn = new InputCapacitance();
117
			_capacitanceIn.initialize(_ioio);
118
 
119
			_temperatureIn = new InputTemperature();
120
			_temperatureIn.initialize(_ioio);
121
 
122
			_connected = true;
123
		} catch (ConnectionLostException e) {
124
 
125
		}
126
 
127
		if (_connected) {
128
			// Update the status labels for each graphs
129
			_lineFollowLabel.setText("Pin #" + _lineFollowIn.getPinDescription());
130
			_voltageLabel.setText("Pin #" + _voltageIn.getPinDescription());
131
			_waveformLabel.setText("Pin #" + _waveformIn.getPinDescription());
132
			_capacitanceLabel.setText("Pin #" + _capacitanceIn.getPinDescription());
133
			_temperatureLabel.setText("Pin #" + _temperatureIn.getPinDescription());
134
		}
135
	}
136
 
137
	/** Sets up the updater thread to poll for data */
138
	private void setupPollingThread() {
139
		// Create a new thread to poll for new data at set interval
140
		_pollingThread = new Thread(new Runnable() {
141
			// Output decimal format
142
			DecimalFormat df = new DecimalFormat("#.####");
143
			@Override
144
			public void run() {
145
				try {
146
					while (true) {
147
						// Run until the interrupt signal is sent to the thread
148
						if (Thread.currentThread().isInterrupted()) {
149
							break;
150
						}
151
 
152
						if (_connected) {
153
							// Read in values from input classes
154
							double ret = _lineFollowIn.getValue();
155
							updateLineFollowText(_lineFollowIn.getPinDescription() + "   Current Value: " + df.format(ret));
156
							_lineFollowData.addDataToList(ret);
157
 
158
							ret = _voltageIn.getValue();
159
							updateVoltageText(_voltageIn.getPinDescription() + "   Current Value: " + df.format(ret));
160
							_voltageData.addDataToList(ret);
161
 
162
							ret = _waveformIn.getValue();
163
							updateWaveformText(_waveformIn.getPinDescription() + "   Current Value: " + df.format(ret));
164
							_waveformData.addDataToList(ret);
165
 
166
							ret = _capacitanceIn.getValue();
167
							updateCapacitanceText(_capacitanceIn.getPinDescription() + "   Current Value: " + df.format(ret));
168
							_capacitanceData.addDataToList(ret);
169
 
170
							ret = _temperatureIn.getValue();
171
							updateTemperatureText(_temperatureIn.getPinDescription() + "   Current Value: " + df.format(ret));
172
							_temperatureData.addDataToList(ret);
173
 
174
							// Redraw graph and sleep for set period of time
175
							redrawGraphs();
176
						}
177
						Thread.sleep(_updateSleepPeriod);
178
					}
179
				} catch (Exception e) {
180
					stopPollingThread();
181
				}
182
			}
183
		});
184
	}
185
 
186
	/** Sends an interrupt signal to the thread and notifies input classes to close pins */
187
	public void stopPollingThread() {
188
		if (_pollingThread.isAlive()) {
189
			_parent.logMessage("Stopping updater thread");
190
			_pollingThread.interrupt();
191
		}
192
 
193
		if (_connected) {
194
			_lineFollowIn.closePins();
195
			_voltageIn.closePins();
196
			_waveformIn.closePins();
197
			_capacitanceIn.closePins();
198
			_temperatureIn.closePins();
199
		}
200
 
201
		_connected = false;
202
	}
203
 
204
	/** Create the view containing the text labels and plots */
205
	private void createView() {
206
		_lineFollowLabel = new TextView(_appContext);
207
		_lineFollowLabel.setText("Line Follow Plot Label");
208
		_lineFollowLabel.setTextSize(_textSize);
209
 
210
		_lineFollowPlot = new XYPlot(_appContext, "LineFollowPlot");
211
		_lineFollowPlot.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, _graphHeight));
212
		_lineFollowData = new GenericXYSeries("Line Follow", _graphLength);
213
		setPlotDetails(_lineFollowPlot, _lineFollowData, 3.3, true);
214
 
215
		_voltageLabel = new TextView(_appContext);
216
		_voltageLabel.setText("Voltage Plot Label");
217
		_voltageLabel.setTextSize(_textSize);
218
 
219
		_voltagePlot = new XYPlot(_appContext, "VoltagePlot");
220
		_voltagePlot.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, _graphHeight));
221
		_voltageData = new GenericXYSeries("Voltage Data", _graphLength);
222
		setPlotDetails(_voltagePlot, _voltageData, 3.3, false);
223
 
224
		_waveformLabel = new TextView(_appContext);
225
		_waveformLabel.setText("Waveform Plot Label");
226
		_waveformLabel.setTextSize(_textSize);
227
 
228
		_waveformPlot = new XYPlot(_appContext, "WaveformPlot");
229
		_waveformPlot.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, _graphHeight));
230
		_waveformData = new GenericXYSeries("Waveform Data", _graphLength);
231
		setPlotDetails(_waveformPlot, _waveformData, 3.3, false);
232
 
233
		_capacitanceLabel = new TextView(_appContext);
234
		_capacitanceLabel.setText("Capacitance Plot Label");
235
		_capacitanceLabel.setTextSize(_textSize);
236
 
237
		_capacitancePlot = new XYPlot(_appContext, "CapacitancePlot");
238
		_capacitancePlot.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, _graphHeight));
239
		_capacitanceData = new GenericXYSeries("Capacitance Data", _graphLength);
240
		setPlotDetails(_capacitancePlot, _capacitanceData, 3.3, false);
241
 
242
		_temperatureLabel = new TextView(_appContext);
243
		_temperatureLabel.setText("Temperature Plot Label");
244
		_temperatureLabel.setTextSize(_textSize);
245
 
246
		_temperaturePlot = new XYPlot(_appContext, "TemperaturePlot");
247
		_temperaturePlot.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, _graphHeight));
248
		_temperatureData = new GenericXYSeries("Temperature Data", _graphLength);
249
		setPlotDetails(_temperaturePlot, _temperatureData, 3.3, false);
250
 
251
		// Create the main layout and add each subview to it
252
		LinearLayout layout = new LinearLayout(_appContext);
253
		layout.setOrientation(LinearLayout.VERTICAL);
254
		layout.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
255
		layout.addView(_lineFollowLabel);
256
		layout.addView(_lineFollowPlot);
257
		addDivider(layout);
258
		layout.addView(_voltageLabel);
259
		layout.addView(_voltagePlot);
260
		addDivider(layout);
261
		layout.addView(_waveformLabel);
262
		layout.addView(_waveformPlot);
263
		addDivider(layout);
264
		layout.addView(_capacitanceLabel);
265
		layout.addView(_capacitancePlot);
266
		addDivider(layout);
267
		layout.addView(_temperatureLabel);
268
		layout.addView(_temperaturePlot);
269
 
270
		ScrollView scroller = new ScrollView(_appContext);
271
    	scroller.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
272
    	scroller.addView(layout);
273
 
274
		_view = scroller;
275
	}
276
 
277
	/** Adds a divider to the specified layout */
278
	private void addDivider(View layout) {
279
		View ruler = new View(_appContext);
280
		ruler.setLayoutParams(new ViewGroup.LayoutParams( ViewGroup.LayoutParams.FILL_PARENT, 2));
281
		ruler.setBackgroundColor(Color.rgb(50, 50, 50));
282
 
283
		((LinearLayout)layout).addView(ruler);
284
	}
285
 
286
	/** Associates the graph with the data and set how the graph looks */
287
	private void setPlotDetails(XYPlot plot, GenericXYSeries data, double range, boolean useStep) {
288
		// Associate data with plot
289
		if (!useStep)
290
			plot.addSeries(data, new LineAndPointFormatter(Color.rgb(0, 0, 200), Color.rgb(0, 0, 100),Color.rgb(150, 150, 190)));
291
		else {
292
			StepFormatter stepFormatter = new StepFormatter(Color.rgb(0, 0, 0), Color.rgb(150, 190, 150));
293
			stepFormatter.getLinePaint().setStrokeWidth(1);
294
			plot.addSeries(data, stepFormatter);
295
		}
296
		// Set boundary and ticks
297
		plot.setRangeBoundaries(0, range, BoundaryMode.FIXED);
298
		plot.setDomainBoundaries(0, _graphLength, BoundaryMode.FIXED);
299
		plot.setDomainStepValue(5);
300
		plot.setRangeStep(XYStepMode.SUBDIVIDE, 4);
301
		plot.setTicksPerRangeLabel(3);
302
 
303
		// Remove labels and legend
304
		plot.getLegendWidget().setVisible(false);
305
		plot.getDomainLabelWidget().setVisible(false);
306
		plot.getRangeLabelWidget().setVisible(false);
307
		plot.getTitleWidget().setVisible(false);
308
 
309
		// Remove extra margin space
310
		plot.getGraphWidget().setMargins(0, 10, 10, 0);
311
		plot.getGraphWidget().setRangeLabelWidth(30);
312
		plot.getGraphWidget().setDomainLabelWidth(15);
313
		plot.getGraphWidget().setMarginLeft(-5);
314
		plot.getGraphWidget().setMarginRight(25);
315
		plot.setPlotMargins(0, -10, 0, -5);
316
		plot.setPlotPadding(0, 0, 0, 0);
317
 
318
		// Background color for the plot and divider line colors
319
		plot.getGraphWidget().getGridBackgroundPaint().setColor(Color.BLACK);
320
		plot.getGraphWidget().getGridLinePaint().setColor(Color.GRAY);
321
		plot.getGraphWidget().getDomainOriginLinePaint().setColor(Color.WHITE);
322
        plot.getGraphWidget().getRangeOriginLinePaint().setColor(Color.WHITE);
323
 
324
        // Background for the widget (not the plot itself)
325
		plot.getGraphWidget().setBackgroundPaint(null);
326
		plot.setBackgroundPaint(null);
327
		plot.setBorderPaint(null);
328
 
329
		plot.setBorderStyle(BorderStyle.SQUARE, null, null);
330
		plot.disableAllMarkup();
331
	}
332
 
333
	private void updateLineFollowText(final String message) {
334
		_handler.post(new Runnable() {
335
			@Override
336
			public void run() {
337
				_lineFollowLabel.setText(message);
338
			}
339
		});
340
	}
341
 
342
	private void updateVoltageText(final String message) {
343
		_handler.post(new Runnable() {
344
			@Override
345
			public void run() {
346
				_voltageLabel.setText(message);
347
			}
348
		});
349
	}
350
 
351
	private void updateWaveformText(final String message) {
352
		_handler.post(new Runnable() {
353
			@Override
354
			public void run() {
355
				_waveformLabel.setText(message);
356
			}
357
		});
358
	}
359
 
360
	private void updateCapacitanceText(final String message) {
361
		_handler.post(new Runnable() {
362
			@Override
363
			public void run() {
364
				_capacitanceLabel.setText(message);
365
			}
366
		});
367
	}
368
 
369
	private void updateTemperatureText(final String message) {
370
		_handler.post(new Runnable() {
371
			@Override
372
			public void run() {
373
				_temperatureLabel.setText(message);
374
			}
375
		});
376
	}
377
 
378
	/** Redraws the graphs */
379
	private void redrawGraphs() {
380
		_handler.post(new Runnable() {
381
			@Override
382
			public void run() {
383
				_lineFollowPlot.redraw();
384
				_voltagePlot.redraw();
385
				_waveformPlot.redraw();
386
				_capacitancePlot.redraw();
387
				_temperaturePlot.redraw();
388
			}
389
		});
390
	}
391
 
392
	public View getView() {
393
		return _view;
394
	}
395
}