Example #1
0
void writePacket()
{
   /*
    * This is one way to write packets to the UsbPipe; using reserve()
    * and commit(). If you already have a buffer that you want to copy to the
    * UsbPipe, you can use write().
    */

    if (Usb::isConnected() && usbPipe.writeAvailable()) {

        /*
         * Access some buffer space for writing the next packet. This
         * is the zero-copy API for writing packets. Both reading and writing
         * have a traditional (one copy) API and a zero-copy API.
         */

        UsbPacket &packet = usbPipe.sendQueue.reserve();

        /*
         * Fill most of the packet with dummy data
         */

        // 28-bit type code, for our own application's use
        packet.setType(0x5A);

        packet.resize(packet.capacity());

        for (unsigned i = 0; i < packet.capacity(); ++i) {
            packet.bytes()[i] = 'a' + i;
        }

        /* 
         * Fill the first 3 bytes with accelerometer data from Cube 0
         */

        Byte3 accel = vid.physicalAccel();

        packet.bytes()[0] = accel.x;
        packet.bytes()[1] = accel.y;
        packet.bytes()[2] = accel.z;

        /*
         * Log the packet for debugging, and commit it to the FIFO.
         * The system will asynchronously send it to our peer.
         */

        LOG("Sending: %d bytes, type=%02x, data=%19h\n",
            packet.size(), packet.type(), packet.bytes());

        usbPipe.sendQueue.commit();
        updatePacketCounts(1, 0);
    }
}
Example #2
0
void WritePacket(int type, int packetLength, unsigned char* bytes)
{
   /*
    * This is one way to write packets to the UsbPipe; using reserve()
    * and commit(). If you already have a buffer that you want to copy to the
    * UsbPipe, you can use write().
    */
    if (Usb::isConnected() && usbPipe.writeAvailable()) {

        /*
         * Access some buffer space for writing the next packet. This
         * is the zero-copy API for writing packets. Both reading and writing
         * have a traditional (one copy) API and a zero-copy API.
         */

        UsbPacket &packet = usbPipe.sendQueue.reserve();

        /*
         * Fill most of the packet with dummy data
         */

        // 28-bit type code, for our own application's use
       // packet.setType(0x5);
         packet.setType(type);

        packet.resize(packet.capacity());

        unsigned i;
        for (i = 0; i<packetLength; i++)
        packet.bytes()[i] = bytes[i];

        for (; i < packet.capacity(); ++i) {
            packet.bytes()[i] = 0;
        }


        /*
         * Log the packet for debugging, and commit it to the FIFO.
         * The system will asynchronously send it to our peer.
         */

        usbPipe.sendQueue.commit();
    }
}