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 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 << " "; } } }