====== Crazy USB mouse ======
//Requirements: [HW] [[en:hardware:avr-can:controller]]//
[{{ :images:projects:crazy_mouse:crazy_mouse_windows_cursor.png|Mouse cursor moving round}}]
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 =====
//*****************************************************************************
//
// Crazy USB mouse.
//
// Copyright (c) 2009 TUT Department of Mechatronics
//
//*****************************************************************************
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include "usb_mouse_structs.h"
//*****************************************************************************
//
// Global variables.
//
//*****************************************************************************
volatile tBoolean g_bConnected;
volatile tBoolean g_bCanSend;
//*****************************************************************************
//
// Mouse handler.
//
//*****************************************************************************
unsigned long MouseHandler(void *pvCBData, unsigned long ulEvent,
unsigned long ulMsgData, void *pvMsgData)
{
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, initialize the USB
// controller and connect the device to the bus.
//
USBDHIDMouseInit(0, (tUSBDHIDMouseDevice *)&g_sMouseDevice);
//
// 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 *)&g_sMouseDevice,
cDeltaX, cDeltaY,
(ButtonIsPressed() ? MOUSE_REPORT_BUTTON_1 : 0));
}
}
}
===== usb_mouse_struct.h =====
//*****************************************************************************
//
// 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 =====
//*****************************************************************************
//
// usb_mouse_structs.c - Data structures defining the USB mouse device.
//
//*****************************************************************************
#include "inc/hw_types.h"
#include "driverlib/usb.h"
#include "usblib/usblib.h"
#include "usblib/usbhid.h"
#include "usblib/usb-ids.h"
#include "usblib/device/usbdevice.h"
#include "usblib/device/usbdhid.h"
#include "usblib/device/usbdhidmouse.h"
#include "usb_mouse_structs.h"
//*****************************************************************************
//
// 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,
'T', 0, 'U', 0, 'T', 0, ' ', 0, 'D', 0, 'e', 0, 'p', 0, 'a', 0,
'r', 0, 't', 0, 'm', 0, 'e', 0, 'n', 0, 't', 0, ' ', 0, 'o', 0,
'f', 0, ' ', 0, 'm', 0, 'e', 0, 'c', 0, 'h', 0, 'a', 0, 't', 0,
'r', 0, 'o', 0, 'n', 0, 'i', 0, 'c', 0, 's', 0
};
//*****************************************************************************
//
// The product string.
//
//*****************************************************************************
const unsigned char g_pProductString[] =
{
(11 + 1) * 2,
USB_DTYPE_STRING,
'C', 0, 'r', 0, 'a', 0, 'z', 0, 'y', 0, ' ', 0, 'M', 0, 'o', 0, 'u', 0,
's', 0, 'e', 0
};
//*****************************************************************************
//
// The serial number string.
//
//*****************************************************************************
const unsigned char g_pSerialNumberString[] =
{
(8 + 1) * 2,
USB_DTYPE_STRING,
'F', 0, 'F', 0, 'F', 0, 'F', 0, 'F', 0, 'F', 0, 'F', 0, 'F', 0
};
//*****************************************************************************
//
// The interface description string.
//
//*****************************************************************************
const unsigned char g_pHIDInterfaceString[] =
{
(19 + 1) * 2,
USB_DTYPE_STRING,
'H', 0, 'I', 0, 'D', 0, ' ', 0, 'M', 0, 'o', 0, 'u', 0, 's', 0,
'e', 0, ' ', 0, 'I', 0, 'n', 0, 't', 0, 'e', 0, 'r', 0, 'f', 0,
'a', 0, 'c', 0, 'e', 0
};
//*****************************************************************************
//
// The configuration description string.
//
//*****************************************************************************
const unsigned char g_pConfigString[] =
{
(23 + 1) * 2,
USB_DTYPE_STRING,
'H', 0, 'I', 0, 'D', 0, ' ', 0, 'M', 0, 'o', 0, 'u', 0, 's', 0,
'e', 0, ' ', 0, 'C', 0, 'o', 0, 'n', 0, 'f', 0, 'i', 0, 'g', 0,
'u', 0, 'r', 0, 'a', 0, 't', 0, 'i', 0, 'o', 0, 'n', 0
};
//*****************************************************************************
//
// 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_sMouseDevice,
g_pStringDescriptors,
NUM_STRING_DESCRIPTORS,
&g_sMouseInstance
};