Hardware Reference
In-Depth Information
Part 6 is the function to create and send the packets over the XBee network. A pointer of the data and the length
of the data that need to be sent are received when the function is called. The packet is created with the destination
address set in the setup function and the pointer containing the data to be sent out. The packet that is created is a
transmit request that has no frame ID to limit the number of packets that are worked with for this example. The frame
ID can be used in situations where the receiving XBee may fail or go out of range, telling the program whether the
packet was received or not. The transmit-reply packet that is generated by the XBee network does not inform the
program that the packet was properly received by the Arduino; that is why the “OK” and “BAD” packets are used. The
CreatePacket function calculates the checksum needed for the packet as the last step before sending. The function
saves the packet length for possible resending and sets the FirstPacketsent flag to true to tell other functions that
one packet has been sent; otherwise, the program will fail if a “BAD” packet is received before one packet has been sent.
Listing 8-1. testApp.cpp, Part 6 of 7
void testApp::CreatePacket(unsigned char *Outdata, int length){
printf ("creating packet\n");
packetBuffer[17+ length] = 0;
packetBuffer[0] = 0x7E; // start byte
packetBuffer[1] = 0; // 1st length byte will be zero with current limitations
packetBuffer[3] = 0x10; // frame type
packetBuffer[4] = 0; // frame ID
for (int i = 5; i <= 14; i++){ // add addresses
packetBuffer[i] = destADR[i-5];
}
packetBuffer[15] = 0; // set both options
packetBuffer[16] = 0;
for (int i = 0; i < length; i++){
packetBuffer[i + 17] = Outdata [i]; // add data to packet
printf ("graph: %X\n",packetBuffer[i+17]); // print sent data to debug console
}
packetBuffer[2] = 14 + length; // set the lower length byte
for (int i = 0; i < packetBuffer[2]; i++){ // calculate the checksum
packetBuffer[17+ length] = packetBuffer[17+ length] + packetBuffer[i+3];
}
// finish packet by adding checksum to the final position
packetBuffer[17+ length]= 0xFF - packetBuffer[17+ length];
serial.writeBytes (packetBuffer, (18 + length)); // send the packet
lastPacketLength = 18 + length; // save last packet length
FirstPacketsent = true; // flag that at least the first packet is sent
} // end testApp::CreatePacket
The finishing touch for the openFrameworks code, in part 7, is to create a visual display for quick verification
of the position and data being sent. The graph that is generated will be re-created on the Android device. Figure 8-2
shows the approximate graph that is generated using the data generated in the setup function. The draw function
is called by openFrameworks after the update function is run and has to generate the view from scratch every time
draw is run. The function generates the grid by outlining a 256-pixel area with a square by counting out a 32-pixel line
spacing using a for loop. The data is drawn by a for loop that will step through each array of data and draw a series
of lines segments connected together corresponding to the data contained in the array. There is a vertical line that
is drawn dynamically to indicate the position from which the code is sending data. The position of the data point is
incremented when all three simulated sensors have been sent.
 
Search WWH ::




Custom Search