Hardware Reference
In-Depth Information
Listing 8-11. Function to Keep the Graph Focused on the Most Current Data
private void refreshChart() {
// check if a shift of the graph view is needed
if (xCount > SensorRenderer.getXAxisMax()) {
SensorRenderer.setXAxisMax(xCount);
SensorRenderer.setXAxisMin(++lastMinX);
}
SensorChartView.repaint();
}
The graph will fail to redraw when the Android device rotates to a new orientation. This happens because
the application has not been programmed to handle the screen-rotation event.
Caution
Listing 8-12 shows the last two functions needed to complete the Android application. The first function is the
clearScreen function, associated with the Clear button. The clearScreen function sends a command of an ASCII b to
the Arduino to inform it that the Android device is no longer synchronized. The clearScreen function then performs
an operation to reset the graph and the text box back to their initial settings.
The SyncData function is associated with the Sync button on the user interface; it first checks whether the
data is currently synchronized to avoid resending the data when the button is clicked multiple times. The SyncData
function send an ASCII command of a to the Arduino, initiating the transfer of the sensor.log file located on the
SD card attached to the Arduino. The transfer is captured by the running thread that is checking for incoming data.
The Arduino transfers 34 bytes at a time to the Android device, and the information of the three sensors is added to
the graph. While the Arduino is connected and the data has been synchronized, new data will be transferred to the
Android device and recorded to the log file on the SD card.
Listing 8-12. Clear-Screen and Sync-Data Button Events
public void clearScreen(View v) {
byte[] BytestoSend = new byte[1];
BytestoSend[0] = 'b';
write(BytestoSend);
Sensor1CurrentSeries.clear();
Sensor2CurrentSeries.clear();
Sensor3CurrentSeries.clear();
xCount = 0 ;
lastMinX = 0 ;
SensorRenderer.setYAxisMin(−127);
SensorRenderer.setYAxisMax(127);
SensorRenderer.setXAxisMin(0);
SensorRenderer.setXAxisMax(100);
Sync = false ;
SensorChartView.repaint();
DataFromArduino.setText(null);
}// end clearScreen
public void SyncData(View v) {
if (!Sync){
byte[] BytestoSend = new byte[1];
 
 
Search WWH ::




Custom Search