Exemplo n.º 1
0
void main()
{
    CubeID cube(0);

    VideoBuffer vid;
    vid.initMode(BG0);
    vid.attach(cube);

    for (int x = -1; x < 17; x++) {
        drawColumn(vid, x);
    }

    /*
     * Scroll horizontally through our background based on the accelerometer's X axis.
     *
     * We can scroll with pixel accuracy within a column of tiles via bg0.setPanning().
     * When we get to either edge of the currently plotted tiles, draw a new column
     * of tiles from the source image.
     *
     * Because BG0 is 1 tile wider and taller than the viewport itself, we can pre-load
     * the next column of tiles into the column at the edge before it comes into view.
     */

    float x = 0;
    int prev_xt = 0;

    for (;;) {
        // Scroll based on accelerometer tilt
        Int2 accel = vid.physicalAccel().xy();

        // Floating point pixels
        x += accel.x * (40.0f / 128.0f);

        // Integer pixels
        int xi = x + 0.5f;

        // Integer tiles
        int xt = x / 8;

        while (prev_xt < xt) {
            // Fill in new tiles, just past the right edge of the screen
            drawColumn(vid, prev_xt + 17);
            prev_xt++;
        }

        while (prev_xt > xt) {
            // Fill in new tiles, just past the left edge
            drawColumn(vid, prev_xt - 2);
            prev_xt--;
        }

        // pixel-level scrolling within the current column
        vid.bg0.setPanning(vec(xi, 0));

        System::paint();
    }
}
Exemplo n.º 2
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);
    }
}