void packetHexDumpLine(const UsbPacket &packet, String<17> &str, unsigned index) { str.clear(); // Write up to 8 characters for (unsigned i = 0; i < 8; ++i, ++index) { if (index < packet.size()) { str << Hex(packet.bytes()[index], 2); } else { str << " "; } } }
void readPacket() { /* * This is one way to read packets from the BluetoothPipe; using read(), * and copying them into our own buffer. A faster but slightly more complex * method would use peek() to access the next packet, and pop() to remove it. * * We only attempt to read n packets to avoid the edge case in which packets * are arriving often enough to keep us in this loop indefinitely. Not likely * a concern for real applications, but keeps things flowing smoothly for * this example. */ UsbPacket packet; unsigned n = usbPipe.receiveQueue.capacity(); while (n && usbPipe.read(packet)) { n--; /* * We received a packet over USB! * Dump out its contents in hexadecimal, to the log and the display. */ LOG("Received: %d bytes, type=%02x, data=%19h\n", packet.size(), packet.type(), packet.bytes()); String<17> str; str << "len=" << Hex(packet.size(), 2) << " type=" << Hex(packet.type(), 2); vid.bg0rom.text(vec(1,10), str); packetHexDumpLine(packet, str, 0); vid.bg0rom.text(vec(0,12), str); packetHexDumpLine(packet, str, 8); vid.bg0rom.text(vec(0,13), str); packetHexDumpLine(packet, str, 16); vid.bg0rom.text(vec(0,14), str); // Update our counters updatePacketCounts(0, 1); } }
void ReadPacket() { UsbPacket packet; while (usbPipe.read(packet)) { unsigned char *message = packet.bytes(); switch (message[0]) { case 1: //update bound variables/values (param, minValue, maxValue, bar_idx, local/remote) { //the format for the message is cube, length of parameter, parameter, minValue (length of 3), maxValue (length of 3), bar_idx, local/remote unsigned idCube = message[1]; char *newVal; Val[idCube][0] = message[3]+'0'; Val[idCube][1] = '.'; Val[idCube][2] = message[5]+'0'; update[idCube] = true; break; } case 2: //update the binding data- who is it bound to { //the message format is cubeId,length of param , element type, name, concentration/speed value unsigned idCube = message[1]; boundTo[idCube] = message[3]; Val[idCube][0] = message[4]+'0'; Val[idCube][1] = '.'; Val[idCube][2] = message[5]+'0'; update[idCube] = true; break; } case 3: //update the concentration/speed value has changed { //the mesage format is cube, local or remote unsigned idCube = message[1]; Val[idCube][0] = message[3]+'0'; Val[idCube][1] = '.'; Val[idCube][2] = message[5]+'0'; update[idCube] = true; break; } case 4: // to unbind the cube { unsigned idCube = message[1]; boundTo[idCube] = 0; update[idCube] = true; break; } case 5: //to change the state of the cube { unsigned idCube = message[1]; state[idCube]=message[3]; update[idCube] = true; break; } } } }