USB Mouse (relative) example for mbed NXP LPC11U24 beta
HID_devices/USBMouse.c@0:163560051396, 2011-10-24 (annotated)
- Committer:
- chris
- Date:
- Mon Oct 24 10:26:27 2011 +0000
- Revision:
- 0:163560051396
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
chris | 0:163560051396 | 1 | /* USBMouse.c */ |
chris | 0:163560051396 | 2 | /* USB device example: Relative mouse */ |
chris | 0:163560051396 | 3 | /* Copyright (c) 2011 ARM Limited. All rights reserved. */ |
chris | 0:163560051396 | 4 | |
chris | 0:163560051396 | 5 | #include "stdint.h" |
chris | 0:163560051396 | 6 | |
chris | 0:163560051396 | 7 | #include "USBMouse.h" |
chris | 0:163560051396 | 8 | |
chris | 0:163560051396 | 9 | /* |
chris | 0:163560051396 | 10 | * Descriptors |
chris | 0:163560051396 | 11 | */ |
chris | 0:163560051396 | 12 | |
chris | 0:163560051396 | 13 | uint8_t * USBMouse::ReportDesc() { |
chris | 0:163560051396 | 14 | static uint8_t reportDescriptor[] = { |
chris | 0:163560051396 | 15 | |
chris | 0:163560051396 | 16 | /* Based on Appendix E.10 of "Device Class Definition for Human Interface |
chris | 0:163560051396 | 17 | Devices (HID)" Version 1.11. */ |
chris | 0:163560051396 | 18 | |
chris | 0:163560051396 | 19 | USAGE_PAGE(1), 0x01, /* Genric Desktop */ |
chris | 0:163560051396 | 20 | USAGE(1), 0x02, /* Mouse */ |
chris | 0:163560051396 | 21 | COLLECTION(1), 0x01, /* Application*/ |
chris | 0:163560051396 | 22 | USAGE(1), 0x01, /* Pointer */ |
chris | 0:163560051396 | 23 | COLLECTION(1), 0x00, /* Physical */ |
chris | 0:163560051396 | 24 | |
chris | 0:163560051396 | 25 | REPORT_COUNT(1), 0x03, |
chris | 0:163560051396 | 26 | REPORT_SIZE(1), 0x01, |
chris | 0:163560051396 | 27 | USAGE_PAGE(1), 0x09, /* Buttons */ |
chris | 0:163560051396 | 28 | USAGE_MINIMUM(1), 0x1, |
chris | 0:163560051396 | 29 | USAGE_MAXIMUM(1), 0x3, |
chris | 0:163560051396 | 30 | LOGICAL_MINIMUM(1), 0x00, |
chris | 0:163560051396 | 31 | LOGICAL_MAXIMUM(1), 0x01, |
chris | 0:163560051396 | 32 | INPUT(1), 0x02, |
chris | 0:163560051396 | 33 | REPORT_COUNT(1), 0x01, |
chris | 0:163560051396 | 34 | REPORT_SIZE(1), 0x05, |
chris | 0:163560051396 | 35 | INPUT(1), 0x01, |
chris | 0:163560051396 | 36 | |
chris | 0:163560051396 | 37 | REPORT_COUNT(1), 0x03, |
chris | 0:163560051396 | 38 | REPORT_SIZE(1), 0x08, |
chris | 0:163560051396 | 39 | USAGE_PAGE(1), 0x01, |
chris | 0:163560051396 | 40 | USAGE(1), 0x30, /* X */ |
chris | 0:163560051396 | 41 | USAGE(1), 0x31, /* Y */ |
chris | 0:163560051396 | 42 | USAGE(1), 0x38, /* scroll */ |
chris | 0:163560051396 | 43 | LOGICAL_MINIMUM(1), 0x81, |
chris | 0:163560051396 | 44 | LOGICAL_MAXIMUM(1), 0x7f, |
chris | 0:163560051396 | 45 | INPUT(1), 0x06, /* Relative data */ |
chris | 0:163560051396 | 46 | |
chris | 0:163560051396 | 47 | END_COLLECTION(0), |
chris | 0:163560051396 | 48 | END_COLLECTION(0), |
chris | 0:163560051396 | 49 | }; |
chris | 0:163560051396 | 50 | reportLength = sizeof(reportDescriptor); |
chris | 0:163560051396 | 51 | return reportDescriptor; |
chris | 0:163560051396 | 52 | } |
chris | 0:163560051396 | 53 | |
chris | 0:163560051396 | 54 | |
chris | 0:163560051396 | 55 | bool USBMouse::update(int16_t x, int16_t y, uint8_t button, int8_t z) { |
chris | 0:163560051396 | 56 | while (x > 127) { |
chris | 0:163560051396 | 57 | if (!mouseSend(127, 0, button, z)) return false; |
chris | 0:163560051396 | 58 | x = x - 127; |
chris | 0:163560051396 | 59 | } |
chris | 0:163560051396 | 60 | while (x < -128) { |
chris | 0:163560051396 | 61 | if (!mouseSend(-128, 0, button, z)) return false; |
chris | 0:163560051396 | 62 | x = x + 128; |
chris | 0:163560051396 | 63 | } |
chris | 0:163560051396 | 64 | while (y > 127) { |
chris | 0:163560051396 | 65 | if (!mouseSend(0, 127, button, z)) return false; |
chris | 0:163560051396 | 66 | y = y - 127; |
chris | 0:163560051396 | 67 | } |
chris | 0:163560051396 | 68 | while (y < -128) { |
chris | 0:163560051396 | 69 | if (!mouseSend(0, -128, button, z)) return false; |
chris | 0:163560051396 | 70 | y = y + 128; |
chris | 0:163560051396 | 71 | } |
chris | 0:163560051396 | 72 | return mouseSend(x, y, button, z); |
chris | 0:163560051396 | 73 | } |
chris | 0:163560051396 | 74 | |
chris | 0:163560051396 | 75 | bool USBMouse::mouseSend(int8_t x, int8_t y, uint8_t buttons, int8_t z) { |
chris | 0:163560051396 | 76 | HID_REPORT report; |
chris | 0:163560051396 | 77 | report.data[0] = buttons & 0x07; |
chris | 0:163560051396 | 78 | report.data[1] = x; |
chris | 0:163560051396 | 79 | report.data[2] = y; |
chris | 0:163560051396 | 80 | report.data[3] = -z; /* >0 to scroll down, <0 to scroll up */ |
chris | 0:163560051396 | 81 | |
chris | 0:163560051396 | 82 | report.length = 4; |
chris | 0:163560051396 | 83 | |
chris | 0:163560051396 | 84 | return USBClass_HID_sendInputReport(EPINT_IN, &report); |
chris | 0:163560051396 | 85 | } |