Hardware Reference
In-Depth Information
Once the data is received from the Arduino, the three sensors' data is pulled from the 34-byte array and added
as the y value to the appropriate series of data. Because the data that was sent to the Arduino from openFrameworks
was normalized to a unsigned byte, you have to normalize the data back to a zero value of the sine wave function by
subtracting 127 from the sensor value to make the byte signed. The x value is controlled by a count that is incremented
every time a data transition is received; the same count value is added to all three series. A special function is called
after the data is added to the graph to check if the data is outside of the view; if so, it will scroll to the last position,
keeping the current incoming data always in the view area. The old data is not lost as the graph scrolls, and can be
viewed by scrolling back to the left.
After the graph is printed to the screen, the entire data buffer is appended to the text box to add an extra view for
possible debugging. The information in the text box could be accessed for further processing, such as saving the data
to a file on the Android device. A decent tutorial on reading and writing to the storage of an Android device can be
found at www.java-samples.com/showtutorial.php?tutorialid=1523 . This tutorial can be modified to work with
this example because the data is printed to a text box. Listing 8-10 replaces the existing incoming data handler within
the framework.
Some online examples for AChartEngine call for a separate thread to be created to be able update the chart
dynamically for new data. This is not necessary for ADK applications, because of the existing thread used to respond to
incoming information from the Mega ADK. This thread provides an event to update the graph when new data is received.
Note
Listing 8-10. Incoming Data Handler Function
Handler IncomingDataHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
BufferData IncomingBuffer = (BufferData) msg.obj;
byte[] buffer = IncomingBuffer.getBuffer();
// pull and add sensor data to the graph
byte sen1 = (byte) (buffer[5] - 127);
byte sen2 = (byte) (buffer[13] - 127);
byte sen3 = (byte) (buffer[21] - 127);
Sensor1CurrentSeries.add(xCount, sen1 );
Sensor2CurrentSeries.add(xCount, sen2 );
Sensor3CurrentSeries.add(xCount, sen3 );
// check if a scroll is needed
refreshChart();
xCount++;
if (SensorChartView != null) {
SensorChartView.repaint();
}
// add data buffer to text box
String str = new String(buffer);
DataFromArduino.append(str);
}// end handleMessage(Message msg)
};// end Handler IncomingDataHandler = new Handler()
The refreshChart function described in Listing 8-11 provides the mechanism to scroll the graph when the
current incoming data exceeds the view area on the screen. The scroll is accomplished by checking if the current
x value count is greater than the highest value of the graph being drawn. When the count is greater, the function
increments the minimum x value and sets the values of the minimum and the new maximum to the graph, creating
the scrolling effect.
 
 
Search WWH ::




Custom Search