Hardware Reference
In-Depth Information
Listing 8-7 defines the new variables that need to be added to the framework to define the chart and the data
that will be drawn to the screen. The first two new variables declare the multiple-series data set and the renderer. The
data-set variable contains the three series that will make up the data from the sensors. The renderer uses the data set
to display the data and is used by the SensorChartView class's repaint function. Options for the renderer are set in
the SetupGraph function, described later. Each piece of the sensor's data is contained in a simple XYSeries variable
declared with the name on creation and will have data added to it as it is received from the Arduino board. The linear
layout has to be declared so that the registerUIobjects function can add the graph to the view for the user. The
buttons and the Edit text box are added in the same way as the ADK monitor. The last six variables are used to store
information for the placement within the graph and the beginning limits to display, along with a Boolean to inform
functions of the status of the synchronization with the Arduino board. Add the variables in Listing 8-7 to the program
after the beginning of the activity class and before the first function.
Listing 8-7. New Variables for the Android Sensor Network Application
// chart variables
private XYMultipleSeriesDataset SensorData = new XYMultipleSeriesDataset();
// the XYMultipleSeriesRenderer spans two lines in the topic
private XYMultipleSeriesRenderer SensorRenderer = new XYMultipleSeriesRenderer();
private XYSeries Sensor1CurrentSeries = new XYSeries("Sensor 1");
private XYSeries Sensor2CurrentSeries = new XYSeries("Sensor 2");
private XYSeries Sensor3CurrentSeries = new XYSeries("Sensor 3");
private GraphicalView SensorChartView;
// chart container and other UI objects
private LinearLayout layout;
private Button buttonSync;
private Button ScreenClear;
private EditText DataFromArduino;
// chart control variables
double[] limits = new double[] {0, 500000,-127,127}; // for chart limits
double x = 0;
double y = 0;
double xCount = 0;
double lastMinX = 0;
boolean Sync = false;
Listing 8-8 is the function that registers the user interface objects to the code. Both of the buttons and the text
box are set to the defined objects in main.xml , as was done in prior Android applications. The chart is a bit unusual
because the chart view must be added to the linear layout; this is done by adding the output of ChartFactory 's
getLineChartView function to the SensorChartView variable. Some information has to be included with the
getLineChartView function call—the instance of the program along with the data set and renderer that will be used
with the chart need to be passed to the function. Then the SensorChartView variable id added to the linear view
before this function is finished.
Listing 8-8. New registerUIobjects Function
private void registerUIobjects(){
buttonSync = (Button) findViewById(R.id.syncbutton);
ScreenClear = (Button) findViewById(R.id.clear);
DataFromArduino = (EditText)findViewById(R.id.incomingData);
layout = (LinearLayout) findViewById(R.id.chart);
// the next line spans two in the topic
 
Search WWH ::




Custom Search