This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revision | |||
| en:projects:crazy_mouse [2010/03/14 16:46] – mikk.leini | en:projects:crazy_mouse [2020/07/20 12:00] (current) – external edit 127.0.0.1 | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| + | ====== Crazy USB mouse ====== | ||
| + | |||
| + | // | ||
| + | |||
| + | [{{ : | ||
| + | |||
| + | ARM-CAN USB mouse demo project. Software is based on TI StellarisWare USB library. When attaching the ARM-CAN controller board to the PC, it acts as a standard USB HID mouse. Fixed-point sine calculation functions are used to move cursor around. Push-button on the controller board acts as a left button. | ||
| + | |||
| + | ~~CL~~ | ||
| + | |||
| + | ===== crazy_mouse.c ===== | ||
| + | |||
| + | <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 | | ||
| + | SYSCTL_XTAL_8MHZ); | ||
| + | |||
| + | // | ||
| + | // 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, | ||
| + | (ButtonIsPressed() ? MOUSE_REPORT_BUTTON_1 : 0)); | ||
| + | } | ||
| + | } | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | ===== usb_mouse_struct.h ===== | ||
| + | |||
| + | <code c> | ||
| + | // | ||
| + | // | ||
| + | // usb_mouse_structs.h - Data structures defining the mouse USB device. | ||
| + | // | ||
| + | // | ||
| + | |||
| + | #ifndef _USB_MOUSE_STRUCTS_H_ | ||
| + | #define _USB_MOUSE_STRUCTS_H_ | ||
| + | // | ||
| + | // | ||
| + | // Application event handlers. | ||
| + | // | ||
| + | // | ||
| + | extern unsigned long MouseHandler(void *pvCBData, | ||
| + | unsigned long ulEvent, | ||
| + | unsigned long ulMsgData, | ||
| + | void *pvMsgData); | ||
| + | |||
| + | extern const tUSBDHIDMouseDevice g_sMouseDevice; | ||
| + | |||
| + | #endif | ||
| + | </ | ||
| + | |||
| + | ===== usb_mouse_struct.c ===== | ||
| + | |||
| + | <code c> | ||
| + | // | ||
| + | // | ||
| + | // usb_mouse_structs.c - Data structures defining the USB mouse device. | ||
| + | // | ||
| + | // | ||
| + | |||
| + | #include " | ||
| + | #include " | ||
| + | #include " | ||
| + | #include " | ||
| + | #include " | ||
| + | #include " | ||
| + | #include " | ||
| + | #include " | ||
| + | #include " | ||
| + | |||
| + | // | ||
| + | // | ||
| + | // The languages supported by this device. | ||
| + | // | ||
| + | // | ||
| + | const unsigned char g_pLangDescriptor[] = | ||
| + | { | ||
| + | 4, | ||
| + | USB_DTYPE_STRING, | ||
| + | USBShort(USB_LANG_EN_US) | ||
| + | }; | ||
| + | |||
| + | // | ||
| + | // | ||
| + | // The manufacturer string. | ||
| + | // | ||
| + | // | ||
| + | const unsigned char g_pManufacturerString[] = | ||
| + | { | ||
| + | (30 + 1) * 2, | ||
| + | USB_DTYPE_STRING, | ||
| + | ' | ||
| + | ' | ||
| + | ' | ||
| + | ' | ||
| + | }; | ||
| + | |||
| + | // | ||
| + | // | ||
| + | // The product string. | ||
| + | // | ||
| + | // | ||
| + | const unsigned char g_pProductString[] = | ||
| + | { | ||
| + | (11 + 1) * 2, | ||
| + | USB_DTYPE_STRING, | ||
| + | ' | ||
| + | ' | ||
| + | }; | ||
| + | |||
| + | // | ||
| + | // | ||
| + | // The serial number string. | ||
| + | // | ||
| + | // | ||
| + | const unsigned char g_pSerialNumberString[] = | ||
| + | { | ||
| + | (8 + 1) * 2, | ||
| + | USB_DTYPE_STRING, | ||
| + | ' | ||
| + | }; | ||
| + | |||
| + | // | ||
| + | // | ||
| + | // The interface description string. | ||
| + | // | ||
| + | // | ||
| + | const unsigned char g_pHIDInterfaceString[] = | ||
| + | { | ||
| + | (19 + 1) * 2, | ||
| + | USB_DTYPE_STRING, | ||
| + | ' | ||
| + | ' | ||
| + | ' | ||
| + | }; | ||
| + | |||
| + | // | ||
| + | // | ||
| + | // The configuration description string. | ||
| + | // | ||
| + | // | ||
| + | const unsigned char g_pConfigString[] = | ||
| + | { | ||
| + | (23 + 1) * 2, | ||
| + | USB_DTYPE_STRING, | ||
| + | ' | ||
| + | ' | ||
| + | ' | ||
| + | }; | ||
| + | |||
| + | // | ||
| + | // | ||
| + | // The descriptor string table. | ||
| + | // | ||
| + | // | ||
| + | const unsigned char * const g_pStringDescriptors[] = | ||
| + | { | ||
| + | g_pLangDescriptor, | ||
| + | g_pManufacturerString, | ||
| + | g_pProductString, | ||
| + | g_pSerialNumberString, | ||
| + | g_pHIDInterfaceString, | ||
| + | g_pConfigString | ||
| + | }; | ||
| + | |||
| + | #define NUM_STRING_DESCRIPTORS (sizeof(g_pStringDescriptors) / \ | ||
| + | sizeof(unsigned char *)) | ||
| + | |||
| + | // | ||
| + | // | ||
| + | // The HID mouse device initialization and customization structures. | ||
| + | // | ||
| + | // | ||
| + | tHIDMouseInstance g_sMouseInstance; | ||
| + | |||
| + | const tUSBDHIDMouseDevice g_sMouseDevice = | ||
| + | { | ||
| + | USB_VID_LUMINARY, | ||
| + | USB_PID_MOUSE, | ||
| + | 500, | ||
| + | USB_CONF_ATTR_SELF_PWR, | ||
| + | MouseHandler, | ||
| + | (void *)& | ||
| + | g_pStringDescriptors, | ||
| + | NUM_STRING_DESCRIPTORS, | ||
| + | & | ||
| + | }; | ||
| + | </ | ||