USB Mouse (relative) example for mbed NXP LPC11U24 beta

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers USBMouse.c Source File

USBMouse.c

00001 /* USBMouse.c */
00002 /* USB device example: Relative mouse */
00003 /* Copyright (c) 2011 ARM Limited. All rights reserved. */
00004 
00005 #include "stdint.h"
00006 
00007 #include "USBMouse.h"
00008 
00009 /*
00010  *  Descriptors
00011  */
00012 
00013 uint8_t * USBMouse::ReportDesc() {
00014     static uint8_t reportDescriptor[] = {
00015 
00016         /* Based on Appendix E.10 of "Device Class Definition for Human Interface
00017            Devices (HID)" Version 1.11. */
00018 
00019         USAGE_PAGE(1),      0x01,       /* Genric Desktop */
00020         USAGE(1),           0x02,       /* Mouse */
00021         COLLECTION(1),      0x01,       /* Application*/
00022         USAGE(1),           0x01,       /* Pointer */
00023         COLLECTION(1),      0x00,       /* Physical */
00024 
00025         REPORT_COUNT(1),    0x03,
00026         REPORT_SIZE(1),     0x01,
00027         USAGE_PAGE(1),      0x09,       /* Buttons */
00028         USAGE_MINIMUM(1),       0x1,
00029         USAGE_MAXIMUM(1),       0x3,
00030         LOGICAL_MINIMUM(1),     0x00,
00031         LOGICAL_MAXIMUM(1),     0x01,
00032         INPUT(1),           0x02,
00033         REPORT_COUNT(1),    0x01,
00034         REPORT_SIZE(1),     0x05,
00035         INPUT(1),           0x01,
00036 
00037         REPORT_COUNT(1),    0x03,
00038         REPORT_SIZE(1),     0x08,
00039         USAGE_PAGE(1),      0x01,
00040         USAGE(1),           0x30,       /* X */
00041         USAGE(1),           0x31,       /* Y */
00042         USAGE(1),           0x38,       /* scroll */
00043         LOGICAL_MINIMUM(1),     0x81,
00044         LOGICAL_MAXIMUM(1),     0x7f,
00045         INPUT(1),           0x06,       /* Relative data */
00046 
00047         END_COLLECTION(0),
00048         END_COLLECTION(0),
00049     };
00050     reportLength = sizeof(reportDescriptor);
00051     return reportDescriptor;
00052 }
00053 
00054 
00055 bool USBMouse::update(int16_t x, int16_t y, uint8_t button, int8_t z) {
00056     while (x > 127) {
00057         if (!mouseSend(127, 0, button, z)) return false;
00058         x = x - 127;
00059     }
00060     while (x < -128) {
00061         if (!mouseSend(-128, 0, button, z)) return false;
00062         x = x + 128;
00063     }
00064     while (y > 127) {
00065         if (!mouseSend(0, 127, button, z)) return false;
00066         y = y - 127;
00067     }
00068     while (y < -128) {
00069         if (!mouseSend(0, -128, button, z)) return false;
00070         y = y + 128;
00071     }
00072     return mouseSend(x, y, button, z);
00073 }
00074 
00075 bool USBMouse::mouseSend(int8_t x, int8_t y, uint8_t buttons, int8_t z) {
00076     HID_REPORT report;
00077     report.data[0] = buttons & 0x07;
00078     report.data[1] = x;
00079     report.data[2] = y;
00080     report.data[3] = -z; /* >0 to scroll down, <0 to scroll up */
00081 
00082     report.length = 4;
00083 
00084     return USBClass_HID_sendInputReport(EPINT_IN, &report);
00085 }