Hardware Reference
In-Depth Information
if (dataLength == 4){ // send data to the preparation function if length is proper
PrepareDataForSD();
}
break;
case 0x8B: // if packet is a transmit response, perform action
Serial.println ("Transmit Response");
break;
case 0x88: // inform of information from command response
Serial.print("Command response :");
Serial.print (incomingBuffer[8], HEX);
Serial.println (incomingBuffer[9],HEX);
break;
default: // announce unknown packet type
Serial.println ("error: packet type not known");
} // end Switch Case
} // end void ReadPacket
In part 6, the next function preps the data to be contained in the SD card and that's ready for passing to the
Android device. When data is received and parsed from the incoming packets, it is sent to this function and placed in
the SD buffer according to the sensor's number. This function will place all three sensors into the respective locations,
and when the last sensor is received, the SD buffer is written to the SD card for storage. The data is sorted with a
switch that looks at the second position of the incomeData array, which contains the sensor number associated with
the sensor data. Once the third sensor is received, the SD buffer is printed to the serial connection to the computer for
debugging, and the SD buffer will be sent to the Android device if connected and the data in the log has been synced.
The method of logging the data after the third sensor has been received sometimes misses some of the other
sensors. In more professional setups, the program should make a request for the missing sensor data from the
network, but for this demonstration it is not necessary. This function can also be used to pull a local sensor to add
extra sensor data to the log. When the SD buffer is ready, the code opens the file for writing and finds the last position
by seeking to the end based upon the file size, and closes the file when finished. The positions that are associated with
the sensor data are reset to the initialization values returning back to the calling function.
Listing 8-3. Data Logger and ADK Handler, Part 6 of 8
void PrepareDataForSD(){
switch (incomeData[1]){
case '1':
SDoutBuffer[3] = incomeData[2];
SDoutBuffer[5] = incomeData[3];
break;
case '2':
SDoutBuffer[11] = incomeData[2];
SDoutBuffer[13] = incomeData[3];
break;
case '3':
SDoutBuffer[19] = incomeData[2];
SDoutBuffer[21] = incomeData[3];
// a local sensor can be pulled and added to the SD buffer at the L1 location
LogFile = SD.open("sensor.log", FILE_WRITE); // open file for writing
LogFile.seek(LogFile.size()); // find end of file to append
 
Search WWH ::




Custom Search