This shows you the differences between two versions of the page.
| en:examples:usb:crazy_mouse [2010/02/16 11:44] – created mikk.leini | en:examples:usb:crazy_mouse [2010/03/14 14:17] (current) – removed mikk.leini | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| - | ====== Crazy USB mouse ====== | ||
| - | <code c> | ||
| - | // | ||
| - | // | ||
| - | // Crazy USB mouse. | ||
| - | // | ||
| - | // Copyright (c) 2009 TUT Department of Mechatronics | ||
| - | // | ||
| - | // | ||
| - | |||
| - | #include < | ||
| - | #include < | ||
| - | #include < | ||
| - | #include < | ||
| - | #include < | ||
| - | #include < | ||
| - | #include < | ||
| - | #include < | ||
| - | #include < | ||
| - | #include < | ||
| - | #include < | ||
| - | #include < | ||
| - | #include < | ||
| - | #include " | ||
| - | |||
| - | // | ||
| - | // | ||
| - | // Global variables. | ||
| - | // | ||
| - | // | ||
| - | volatile tBoolean g_bConnected; | ||
| - | volatile tBoolean g_bCanSend; | ||
| - | |||
| - | // | ||
| - | // | ||
| - | // Mouse handler. | ||
| - | // | ||
| - | // | ||
| - | unsigned long MouseHandler(void *pvCBData, unsigned long ulEvent, | ||
| - | | ||
| - | { | ||
| - | switch (ulEvent) | ||
| - | { | ||
| - | // | ||
| - | // The USB host has connected to and configured the device. | ||
| - | // | ||
| - | case USB_EVENT_CONNECTED: | ||
| - | { | ||
| - | g_bConnected = true; | ||
| - | g_bCanSend = true; | ||
| - | break; | ||
| - | } | ||
| - | |||
| - | // | ||
| - | // The USB host has disconnected from the device. | ||
| - | // | ||
| - | case USB_EVENT_DISCONNECTED: | ||
| - | { | ||
| - | g_bConnected = false; | ||
| - | g_bCanSend = false; | ||
| - | break; | ||
| - | } | ||
| - | |||
| - | // | ||
| - | // A report was sent to the host. | ||
| - | // | ||
| - | case USB_EVENT_TX_COMPLETE: | ||
| - | { | ||
| - | g_bCanSend = true; | ||
| - | break; | ||
| - | } | ||
| - | |||
| - | } | ||
| - | return(0); | ||
| - | } | ||
| - | |||
| - | // | ||
| - | // | ||
| - | // Main function. | ||
| - | // | ||
| - | // | ||
| - | int main(void) | ||
| - | { | ||
| - | unsigned long ulAngle = 0; | ||
| - | char cDeltaX = 0, cDeltaY = 0; | ||
| - | |||
| - | // | ||
| - | // Set the clocking to run from the PLL at 50MHz | ||
| - | // | ||
| - | SysCtlClockSet(SYSCTL_SYSDIV_4 | SYSCTL_USE_PLL | SYSCTL_OSC_MAIN | | ||
| - | | ||
| - | |||
| - | // | ||
| - | // Drivers configuring. | ||
| - | // | ||
| - | DeviceConfigure(); | ||
| - | |||
| - | // | ||
| - | // Pass the USB library our device information, | ||
| - | // controller and connect the device to the bus. | ||
| - | // | ||
| - | USBDHIDMouseInit(0, | ||
| - | |||
| - | // | ||
| - | // Enable processor interrupts. | ||
| - | // | ||
| - | IntMasterEnable(); | ||
| - | |||
| - | // | ||
| - | // Loop forever. | ||
| - | // | ||
| - | while (true) | ||
| - | { | ||
| - | // | ||
| - | // Device connected and ready ? | ||
| - | // | ||
| - | if (g_bConnected && g_bCanSend) | ||
| - | { | ||
| - | // | ||
| - | // Rotate by 1 / 256 of full circle. | ||
| - | // | ||
| - | ulAngle += 0x1000000; | ||
| - | |||
| - | // | ||
| - | // Calculate X and Y movement. | ||
| - | // Use sine function with fixed point numbers. | ||
| - | // | ||
| - | cDeltaX = sine(ulAngle) >> 14; | ||
| - | cDeltaY = sine(ulAngle - 0x40000000) >> 14; | ||
| - | |||
| - | // | ||
| - | // Keep a small break. | ||
| - | // | ||
| - | DelayMS(5); | ||
| - | |||
| - | // | ||
| - | // Update mouse state. | ||
| - | // | ||
| - | USBDHIDMouseStateChange((void *)& | ||
| - | cDeltaX, cDeltaY, | ||
| - | (ButtonIsPressed() ? MOUSE_REPORT_BUTTON_1 : 0)); | ||
| - | } | ||
| - | } | ||
| - | } | ||
| - | </ | ||