USB/HID stack for the loststone project

Dependents:   loststone

Fork of USBDevice by mbed official

Files at this revision

API Documentation at this revision

Comitter:
xxann5
Date:
Thu Mar 14 13:34:54 2013 +0000
Parent:
7:f8f057664123
Child:
11:7c95a42cf9f6
Commit message:
Added support for 4th and 5th mouse buttons as well as horizontal scrolling. In addition i changed the x and y coordinates to uint16_t, which cleans up allot of code. This change also includes a new HID report descriptor.

Changed in this revision

USBHID/USBHID.cpp Show annotated file Show diff for this revision Revisions of this file
USBHID/USBMouse.cpp Show annotated file Show diff for this revision Revisions of this file
USBHID/USBMouse.h Show annotated file Show diff for this revision Revisions of this file
--- a/USBHID/USBHID.cpp	Mon Jan 21 10:41:28 2013 +0000
+++ b/USBHID/USBHID.cpp	Thu Mar 14 13:34:54 2013 +0000
@@ -86,8 +86,8 @@
 bool USBHID::USBCallback_request() {
     bool success = false;
     CONTROL_TRANSFER * transfer = getTransferPtr();
-    uint8_t *hidDescriptor;
-
+    //uint8_t *hidDescriptor;
+    uint8_t hidDescriptor[] = { 't', 0, 'e', 0, 's', 0, 't', 0 };
     // Process additional standard requests
 
     if ((transfer->setup.bmRequestType.Type == STANDARD_TYPE))
@@ -109,7 +109,7 @@
                         break;
                     case HID_DESCRIPTOR:
                             // Find the HID descriptor, after the configuration descriptor
-                            hidDescriptor = findDescriptor(HID_DESCRIPTOR);
+                            //hidDescriptor = findDescriptor(HID_DESCRIPTOR);
                             if (hidDescriptor != NULL)
                             {
                                 transfer->remaining = HID_DESCRIPTOR_LENGTH;
--- a/USBHID/USBMouse.cpp	Mon Jan 21 10:41:28 2013 +0000
+++ b/USBHID/USBMouse.cpp	Thu Mar 14 13:34:54 2013 +0000
@@ -19,26 +19,10 @@
 #include "stdint.h"
 #include "USBMouse.h"
 
-bool USBMouse::update(int16_t x, int16_t y, uint8_t button, int8_t z) {
+bool USBMouse::update(int16_t x, int16_t y, uint8_t button, int8_t z, int8_t h) {
     switch (mouse_type) {
         case REL_MOUSE:
-            while (x > 127) {
-                if (!mouseSend(127, 0, button, z)) return false;
-                x = x - 127;
-            }
-            while (x < -128) {
-                if (!mouseSend(-128, 0, button, z)) return false;
-                x = x + 128;
-            }
-            while (y > 127) {
-                if (!mouseSend(0, 127, button, z)) return false;
-                y = y - 127;
-            }
-            while (y < -128) {
-                if (!mouseSend(0, -128, button, z)) return false;
-                y = y + 128;
-            }
-            return mouseSend(x, y, button, z);
+            return mouseSend(x, y, button, z, h);
         case ABS_MOUSE:
             HID_REPORT report;
 
@@ -57,24 +41,29 @@
     }
 }
 
-bool USBMouse::mouseSend(int8_t x, int8_t y, uint8_t buttons, int8_t z) {
+bool USBMouse::mouseSend(int16_t x, int16_t y, uint8_t buttons, int8_t z, int8_t h) {
     HID_REPORT report;
-    report.data[0] = buttons & 0x07;
-    report.data[1] = x;
-    report.data[2] = y;
-    report.data[3] = -z; // >0 to scroll down, <0 to scroll up
+
+    report.data[0] = buttons;// & 0x07;
 
-    report.length = 4;
+    report.data[1] = (unsigned int) x & 0x00FF;
+    report.data[2] = (unsigned int) x >> 8;
+    report.data[3] = (unsigned int) y & 0x00FF;
+    report.data[4] = (unsigned int) y >> 8;
+    report.data[5] = -z; // >0 to scroll down, <0 to scroll up
+    report.data[6] = h;
+
+    report.length = 7;
 
     return send(&report);
 }
 
 bool USBMouse::move(int16_t x, int16_t y) {
-    return update(x, y, button, 0);
+    return update(x, y, button, 0, 0);
 }
 
-bool USBMouse::scroll(int8_t z) {
-    return update(0, 0, button, z);
+bool USBMouse::scroll(int8_t z, int8_t h) {
+    return update(0, 0, button, z, h);
 }
 
 
@@ -86,58 +75,127 @@
 }
 
 bool USBMouse::click(uint8_t button) {
-    if (!update(0, 0, button, 0))
+    if (!update(0, 0, button, 0, 0))
         return false;
     wait(0.01);
-    return update(0, 0, 0, 0);
+    return update(0, 0, 0, 0, 0);
 }
 
 bool USBMouse::press(uint8_t button_) {
+    printf("btn_press\n\r");
     button = button_ & 0x07;
-    return update(0, 0, button, 0);
+    return update(0, 0, button, 0, 0);
 }
 
 bool USBMouse::release(uint8_t button_) {
+    printf("btn_release\n\r");
     button = (button & (~button_)) & 0x07;
-    return update(0, 0, button, 0);
+    return update(0, 0, button, 0, 0);
 }
 
 
 uint8_t * USBMouse::reportDesc() {
 
     if (mouse_type == REL_MOUSE) {
+//
+// Wheel Mouse - simplified version - 5 button, vertical and horizontal wheel
+//
+// Input report - 5 bytes
+//
+//     Byte | D7      D6      D5      D4      D3      D2      D1      D0
+//    ------+---------------------------------------------------------------------
+//      0   |  0       0       0    Forward  Back    Middle  Right   Left (Buttons)
+//      1   |                             X High
+//      2   |                             X Low
+//      3   |                             Y High
+//      4   |                             Y Low
+//      5   |                       Vertical Wheel
+//      6   |                    Horizontal (Tilt) Wheel
+//
+// Feature report - 1 byte
+//
+//     Byte | D7      D6      D5      D4   |  D3      D2  |   D1      D0
+//    ------+------------------------------+--------------+----------------
+//      0   |  0       0       0       0   |  Horizontal  |    Vertical
+//                                             (Resolution multiplier)
+//
+// Reference
+//    Wheel.docx in "Enhanced Wheel Support in Windows Vista" on MS WHDC
+//    http://www.microsoft.com/whdc/device/input/wheel.mspx
+//
         static uint8_t reportDescriptor[] = {
-            USAGE_PAGE(1),      0x01,       // Genric Desktop
-            USAGE(1),           0x02,       // Mouse
-            COLLECTION(1),      0x01,       // Application
-            USAGE(1),           0x01,       // Pointer
-            COLLECTION(1),      0x00,       // Physical
+            0x05, 0x01,        // USAGE_PAGE (Generic Desktop)
+            0x09, 0x02,        // USAGE (Mouse)
+            0xa1, 0x01,        // COLLECTION (Application)
+            0x09, 0x02,        //   USAGE (Mouse)
+            0xa1, 0x02,        //   COLLECTION (Logical)
+            0x09, 0x01,        //     USAGE (Pointer)
+            0xa1, 0x00,        //     COLLECTION (Physical)
+                               // ------------------------------  Buttons
+            0x05, 0x09,        //       USAGE_PAGE (Button)
+            0x19, 0x01,        //       USAGE_MINIMUM (Button 1)
+            0x29, 0x05,        //       USAGE_MAXIMUM (Button 5)
+            0x15, 0x00,        //       LOGICAL_MINIMUM (0)
+            0x25, 0x01,        //       LOGICAL_MAXIMUM (1)
+            0x75, 0x01,        //       REPORT_SIZE (1)
+            0x95, 0x05,        //       REPORT_COUNT (5)
+            0x81, 0x02,        //       INPUT (Data,Var,Abs)
+                               // ------------------------------  Padding
+            0x75, 0x03,        //       REPORT_SIZE (3)
+            0x95, 0x01,        //       REPORT_COUNT (1)
+            0x81, 0x03,        //       INPUT (Cnst,Var,Abs)
+                               // ------------------------------  X,Y position
+            0x05, 0x01,        //       USAGE_PAGE (Generic Desktop)
+            0x09, 0x30,        //       USAGE (X)
+            0x09, 0x31,        //       USAGE (Y)
+            0x16, 0x00, 0x81,  //       LOGICAL_MINIMUM (-32768)
+            0x26, 0xff, 0x7f,  //       LOGICAL_MAXIMUM (32767)
+            0x75, 0x10,        //       REPORT_SIZE (16)
+            0x95, 0x02,        //       REPORT_COUNT (2)
+            0x81, 0x06,        //       INPUT (Data,Var,Rel)
+            0xa1, 0x02,        //       COLLECTION (Logical)
+                               // ------------------------------  Vertical wheel res multiplier
+            0x09, 0x48,        //         USAGE (Resolution Multiplier)
+            0x15, 0x00,        //         LOGICAL_MINIMUM (0)
+            0x25, 0x01,        //         LOGICAL_MAXIMUM (1)
+            0x35, 0x01,        //         PHYSICAL_MINIMUM (1)
+            0x45, 0x04,        //         PHYSICAL_MAXIMUM (4)
+            0x75, 0x02,        //         REPORT_SIZE (2)
+            0x95, 0x01,        //         REPORT_COUNT (1)
+            0xa4,              //         PUSH
+            0xb1, 0x02,        //         FEATURE (Data,Var,Abs)
+                               // ------------------------------  Vertical wheel
+            0x09, 0x38,        //         USAGE (Wheel)
+            0x15, 0x81,        //         LOGICAL_MINIMUM (-127)
+            0x25, 0x7f,        //         LOGICAL_MAXIMUM (127)
+            0x35, 0x00,        //         PHYSICAL_MINIMUM (0)        - reset physical
+            0x45, 0x00,        //         PHYSICAL_MAXIMUM (0)
+            0x75, 0x08,        //         REPORT_SIZE (8)
+            0x81, 0x06,        //         INPUT (Data,Var,Rel)
+            0xc0,              //       END_COLLECTION
+            0xa1, 0x02,        //       COLLECTION (Logical)
+                               // ------------------------------  Horizontal wheel res multiplier
+            0x09, 0x48,        //         USAGE (Resolution Multiplier)
+            0xb4,              //         POP
+            0xb1, 0x02,        //         FEATURE (Data,Var,Abs)
+                               // ------------------------------  Padding for Feature report
+            0x35, 0x00,        //         PHYSICAL_MINIMUM (0)        - reset physical
+            0x45, 0x00,        //         PHYSICAL_MAXIMUM (0)
+            0x75, 0x04,        //         REPORT_SIZE (4)
+            0xb1, 0x03,        //         FEATURE (Cnst,Var,Abs)
+                               // ------------------------------  Horizontal wheel
+            0x05, 0x0c,        //         USAGE_PAGE (Consumer Devices)
+            0x0a, 0x38, 0x02,  //         USAGE (AC Pan)
+            0x15, 0x81,        //         LOGICAL_MINIMUM (-127)
+            0x25, 0x7f,        //         LOGICAL_MAXIMUM (127)
+            0x75, 0x08,        //         REPORT_SIZE (8)
+            0x81, 0x06,        //         INPUT (Data,Var,Rel)
+            0xc0,              //       END_COLLECTION
+            0xc0,              //     END_COLLECTION
+            0xc0,              //   END_COLLECTION
+           0xc0               // END_COLLECTION
+        };
 
-            REPORT_COUNT(1),    0x03,
-            REPORT_SIZE(1),     0x01,
-            USAGE_PAGE(1),      0x09,       // Buttons
-            USAGE_MINIMUM(1),       0x1,
-            USAGE_MAXIMUM(1),       0x3,
-            LOGICAL_MINIMUM(1),     0x00,
-            LOGICAL_MAXIMUM(1),     0x01,
-            INPUT(1),           0x02,
-            REPORT_COUNT(1),    0x01,
-            REPORT_SIZE(1),     0x05,
-            INPUT(1),           0x01,
-
-            REPORT_COUNT(1),    0x03,
-            REPORT_SIZE(1),     0x08,
-            USAGE_PAGE(1),      0x01,
-            USAGE(1),           0x30,       // X
-            USAGE(1),           0x31,       // Y
-            USAGE(1),           0x38,       // scroll
-            LOGICAL_MINIMUM(1),     0x81,
-            LOGICAL_MAXIMUM(1),     0x7f,
-            INPUT(1),           0x06,       // Relative data
-
-            END_COLLECTION(0),
-            END_COLLECTION(0),
-        };
         reportLength = sizeof(reportDescriptor);
         return reportDescriptor;
     } else if (mouse_type == ABS_MOUSE) {
--- a/USBHID/USBMouse.h	Mon Jan 21 10:41:28 2013 +0000
+++ b/USBHID/USBMouse.h	Thu Mar 14 13:34:54 2013 +0000
@@ -30,6 +30,8 @@
     MOUSE_LEFT = 1,
     MOUSE_RIGHT = 2,
     MOUSE_MIDDLE = 4,
+    MOUSE_FORWORD = 8,
+    MOUSE_BACK = 16
 };
 
 /* X and Y limits */
@@ -134,7 +136,7 @@
         * @param z wheel state (>0 to scroll down, <0 to scroll up)
         * @returns true if there is no error, false otherwise
         */
-        bool update(int16_t x, int16_t y, uint8_t buttons, int8_t z);
+        bool update(int16_t x, int16_t y, uint8_t buttons, int8_t z, int8_t h);
         
         
         /**
@@ -183,7 +185,7 @@
         * @param z value of the wheel (>0 to go down, <0 to go up)
         * @returns true if there is no error, false otherwise
         */
-        bool scroll(int8_t z);
+        bool scroll(int8_t z, int8_t h);
         
         /*
         * To define the report descriptor. Warning: this method has to store the length of the report descriptor in reportLength.
@@ -203,7 +205,7 @@
     private:
         MOUSE_TYPE mouse_type;
         uint8_t button;
-        bool mouseSend(int8_t x, int8_t y, uint8_t buttons, int8_t z);
+        bool mouseSend(int16_t x, int16_t y, uint8_t buttons, int8_t z, int8_t h);
 };
 
 #endif