From 16f3d71df0d3db48387dae5ad0539516ab8d0a93 Mon Sep 17 00:00:00 2001 From: Andreas Watterott Date: Wed, 22 Apr 2015 22:31:13 +0200 Subject: [PATCH 1/4] Added absolute coordinates for HID mouse --- hardware/arduino/avr/cores/arduino/HID.cpp | 118 +++++++++++++++----- hardware/arduino/avr/cores/arduino/USBAPI.h | 4 + 2 files changed, 95 insertions(+), 27 deletions(-) diff --git a/hardware/arduino/avr/cores/arduino/HID.cpp b/hardware/arduino/avr/cores/arduino/HID.cpp index 75c37b24b2f..a6b7b697908 100644 --- a/hardware/arduino/avr/cores/arduino/HID.cpp +++ b/hardware/arduino/avr/cores/arduino/HID.cpp @@ -1,7 +1,7 @@ /* Copyright (c) 2011, Peter Barrett -** +** ** Permission to use, copy, modify, and/or distribute this software for ** any purpose with or without fee is hereby granted, provided that the ** above copyright notice and this permission notice appear in all copies. @@ -44,7 +44,51 @@ Keyboard_ Keyboard; extern const u8 _hidReportDescriptor[] PROGMEM; const u8 _hidReportDescriptor[] = { - // Mouse +#ifdef MOUSE_ABS_ENABLED + // Mouse absolute + 0x05, 0x01, // USAGE_PAGE (Generic Desktop) + 0x09, 0x02, // USAGE (Mouse) + 0xa1, 0x01, // COLLECTION (Application) + 0x09, 0x01, // USAGE (Pointer) + 0xa1, 0x00, // COLLECTION (Physical) + 0x85, 0x01, // REPORT_ID (1) + 0x05, 0x09, // USAGE_PAGE (Button) + 0x19, 0x01, // USAGE_MINIMUM (Button 1) + 0x29, 0x03, // USAGE_MAXIMUM (Button 3) + 0x15, 0x00, // LOGICAL_MINIMUM (0) + 0x25, 0x01, // LOGICAL_MAXIMUM (1) + 0x95, 0x03, // REPORT_COUNT (3) + 0x75, 0x01, // REPORT_SIZE (1) + 0x81, 0x02, // INPUT (Data,Var,Abs) + 0x95, 0x01, // REPORT_COUNT (1) + 0x75, 0x05, // REPORT_SIZE (5) + 0x81, 0x03, // INPUT (Cnst,Var,Abs) + + 0x05, 0x01, // USAGE_PAGE (Generic Desktop) + 0x09, 0x30, // USAGE (X) + 0x09, 0x31, // USAGE (Y) + 0x35, 0x00, // PHYSICAL_MINIMUM (0) + 0x46, 0xff, 0x0f, // PHYSICAL_MAXIMUM (4095) + 0x15, 0x00, // LOGICAL_MINIMUM (0) + 0x26, 0xff, 0x0f, // LOGICAL_MAXIMUM (4095) + 0x75, 0x10, // REPORT_SIZE (16) + 0x95, 0x02, // REPORT_COUNT (2) + 0x81, 0x02, // INPUT (Data,Var,Abs) + + 0x09, 0x38, // USAGE (Wheel) + 0x35, 0x00, // PHYSICAL_MINIMUM (0) + 0x45, 0x00, // PHYSICAL_MAXIMUM (0) + 0x15, 0x81, // LOGICAL_MINIMUM (-127) + 0x25, 0x7f, // LOGICAL_MAXIMUM (127) + 0x75, 0x08, // REPORT_SIZE (8) + 0x95, 0x01, // REPORT_COUNT (1) + 0x81, 0x06, // INPUT (Data,Var,Rel) + + 0xc0, // END_COLLECTION + 0xc0, // END_COLLECTION + +#else + // Mouse relative 0x05, 0x01, // USAGE_PAGE (Generic Desktop) // 54 0x09, 0x02, // USAGE (Mouse) 0xa1, 0x01, // COLLECTION (Application) @@ -74,55 +118,57 @@ const u8 _hidReportDescriptor[] = { 0xc0, // END_COLLECTION 0xc0, // END_COLLECTION +#endif + // Keyboard 0x05, 0x01, // USAGE_PAGE (Generic Desktop) // 47 0x09, 0x06, // USAGE (Keyboard) 0xa1, 0x01, // COLLECTION (Application) 0x85, 0x02, // REPORT_ID (2) 0x05, 0x07, // USAGE_PAGE (Keyboard) - - 0x19, 0xe0, // USAGE_MINIMUM (Keyboard LeftControl) + + 0x19, 0xe0, // USAGE_MINIMUM (Keyboard LeftControl) 0x29, 0xe7, // USAGE_MAXIMUM (Keyboard Right GUI) 0x15, 0x00, // LOGICAL_MINIMUM (0) 0x25, 0x01, // LOGICAL_MAXIMUM (1) 0x75, 0x01, // REPORT_SIZE (1) - - 0x95, 0x08, // REPORT_COUNT (8) + + 0x95, 0x08, // REPORT_COUNT (8) 0x81, 0x02, // INPUT (Data,Var,Abs) 0x95, 0x01, // REPORT_COUNT (1) 0x75, 0x08, // REPORT_SIZE (8) 0x81, 0x03, // INPUT (Cnst,Var,Abs) - - 0x95, 0x06, // REPORT_COUNT (6) + + 0x95, 0x06, // REPORT_COUNT (6) 0x75, 0x08, // REPORT_SIZE (8) 0x15, 0x00, // LOGICAL_MINIMUM (0) 0x25, 0x65, // LOGICAL_MAXIMUM (101) 0x05, 0x07, // USAGE_PAGE (Keyboard) - - 0x19, 0x00, // USAGE_MINIMUM (Reserved (no event indicated)) + + 0x19, 0x00, // USAGE_MINIMUM (Reserved (no event indicated)) 0x29, 0x65, // USAGE_MAXIMUM (Keyboard Application) 0x81, 0x00, // INPUT (Data,Ary,Abs) 0xc0, // END_COLLECTION #ifdef RAWHID_ENABLED - // RAW HID - 0x06, LSB(RAWHID_USAGE_PAGE), MSB(RAWHID_USAGE_PAGE), // 30 - 0x0A, LSB(RAWHID_USAGE), MSB(RAWHID_USAGE), + // RAW HID + 0x06, LSB(RAWHID_USAGE_PAGE), MSB(RAWHID_USAGE_PAGE), // 30 + 0x0A, LSB(RAWHID_USAGE), MSB(RAWHID_USAGE), - 0xA1, 0x01, // Collection 0x01 + 0xA1, 0x01, // Collection 0x01 0x85, 0x03, // REPORT_ID (3) - 0x75, 0x08, // report size = 8 bits - 0x15, 0x00, // logical minimum = 0 - 0x26, 0xFF, 0x00, // logical maximum = 255 - - 0x95, 64, // report count TX - 0x09, 0x01, // usage - 0x81, 0x02, // Input (array) - - 0x95, 64, // report count RX - 0x09, 0x02, // usage - 0x91, 0x02, // Output (array) - 0xC0 // end collection + 0x75, 0x08, // report size = 8 bits + 0x15, 0x00, // logical minimum = 0 + 0x26, 0xFF, 0x00, // logical maximum = 255 + + 0x95, 64, // report count TX + 0x09, 0x01, // usage + 0x81, 0x02, // Input (array) + + 0x95, 64, // report count RX + 0x09, 0x02, // usage + 0x91, 0x02, // Output (array) + 0xC0 // end collection #endif }; @@ -211,6 +257,22 @@ void Mouse_::end(void) { } +#ifdef MOUSE_ABS_ENABLED + +void Mouse_::moveAbs(unsigned int x, unsigned int y, signed char wheel, unsigned char buttons) +{ + uint8_t m[6]; + m[0] = buttons; + m[1] = LSB(x); + m[2] = MSB(x); + m[3] = LSB(y); + m[4] = MSB(y); + m[5] = wheel; + HID_SendReport(1,m,6); +} + +#else + void Mouse_::click(uint8_t b) { _buttons = b; @@ -221,7 +283,7 @@ void Mouse_::click(uint8_t b) void Mouse_::move(signed char x, signed char y, signed char wheel) { - u8 m[4]; + uint8_t m[4]; m[0] = _buttons; m[1] = x; m[2] = y; @@ -255,6 +317,8 @@ bool Mouse_::isPressed(uint8_t b) return false; } +#endif + //================================================================================ //================================================================================ // Keyboard diff --git a/hardware/arduino/avr/cores/arduino/USBAPI.h b/hardware/arduino/avr/cores/arduino/USBAPI.h index 2fab957f930..eb225bbcce9 100644 --- a/hardware/arduino/avr/cores/arduino/USBAPI.h +++ b/hardware/arduino/avr/cores/arduino/USBAPI.h @@ -110,11 +110,15 @@ class Mouse_ Mouse_(void); void begin(void); void end(void); +#ifdef MOUSE_ABS_ENABLED + void moveAbs(unsigned int x, unsigned int y, signed char wheel, unsigned char buttons); // x and y have the range of 0 to 4095 +#else void click(uint8_t b = MOUSE_LEFT); void move(signed char x, signed char y, signed char wheel = 0); void press(uint8_t b = MOUSE_LEFT); // press LEFT by default void release(uint8_t b = MOUSE_LEFT); // release LEFT by default bool isPressed(uint8_t b = MOUSE_LEFT); // check LEFT by default +#endif }; extern Mouse_ Mouse; From 345cb0956fb7c3e10eea7776ee201e01b8e72546 Mon Sep 17 00:00:00 2001 From: Andreas Watterott Date: Thu, 23 Apr 2015 21:32:13 +0200 Subject: [PATCH 2/4] renamed moveAbs() to moveAbsolute() code cleanup --- hardware/arduino/avr/cores/arduino/HID.cpp | 30 ++++++++++----------- hardware/arduino/avr/cores/arduino/USBAPI.h | 2 +- 2 files changed, 16 insertions(+), 16 deletions(-) diff --git a/hardware/arduino/avr/cores/arduino/HID.cpp b/hardware/arduino/avr/cores/arduino/HID.cpp index a6b7b697908..aaa69497456 100644 --- a/hardware/arduino/avr/cores/arduino/HID.cpp +++ b/hardware/arduino/avr/cores/arduino/HID.cpp @@ -155,20 +155,20 @@ const u8 _hidReportDescriptor[] = { 0x06, LSB(RAWHID_USAGE_PAGE), MSB(RAWHID_USAGE_PAGE), // 30 0x0A, LSB(RAWHID_USAGE), MSB(RAWHID_USAGE), - 0xA1, 0x01, // Collection 0x01 - 0x85, 0x03, // REPORT_ID (3) - 0x75, 0x08, // report size = 8 bits - 0x15, 0x00, // logical minimum = 0 - 0x26, 0xFF, 0x00, // logical maximum = 255 - - 0x95, 64, // report count TX - 0x09, 0x01, // usage - 0x81, 0x02, // Input (array) - - 0x95, 64, // report count RX - 0x09, 0x02, // usage - 0x91, 0x02, // Output (array) - 0xC0 // end collection + 0xA1, 0x01, // COLLECTION 0x01 + 0x85, 0x03, // REPORT_ID (3) + 0x75, 0x08, // REPORT_SIZE (8) + 0x15, 0x00, // LOGICAL_MINIMUM (0) + 0x26, 0xFF, 0x00, // LOGICAL_MAXIMUM (255) + + 0x95, 64, // REPORT_COUNT (TX) + 0x09, 0x01, // USAGE + 0x81, 0x02, // INPUT (array) + + 0x95, 64, // REPORT_COUNT (RX) + 0x09, 0x02, // USAGE + 0x91, 0x02, // OUTPUT (array) + 0xC0 // END_COLLECTION #endif }; @@ -259,7 +259,7 @@ void Mouse_::end(void) #ifdef MOUSE_ABS_ENABLED -void Mouse_::moveAbs(unsigned int x, unsigned int y, signed char wheel, unsigned char buttons) +void Mouse_::moveAbsolute(unsigned int x, unsigned int y, signed char wheel, unsigned char buttons) { uint8_t m[6]; m[0] = buttons; diff --git a/hardware/arduino/avr/cores/arduino/USBAPI.h b/hardware/arduino/avr/cores/arduino/USBAPI.h index eb225bbcce9..2f1f28a69c0 100644 --- a/hardware/arduino/avr/cores/arduino/USBAPI.h +++ b/hardware/arduino/avr/cores/arduino/USBAPI.h @@ -111,7 +111,7 @@ class Mouse_ void begin(void); void end(void); #ifdef MOUSE_ABS_ENABLED - void moveAbs(unsigned int x, unsigned int y, signed char wheel, unsigned char buttons); // x and y have the range of 0 to 4095 + void moveAbsolute(unsigned int x, unsigned int y, signed char wheel = 0, unsigned char buttons = 0); // x and y have the range of 0 to 4095 #else void click(uint8_t b = MOUSE_LEFT); void move(signed char x, signed char y, signed char wheel = 0); From da9641410a4b8fa0865f0a31d0b884d14f5e7670 Mon Sep 17 00:00:00 2001 From: Andreas Watterott Date: Wed, 29 Apr 2015 14:18:10 +0200 Subject: [PATCH 3/4] absolute mouse API now compatible with normal relative mouse API --- hardware/arduino/avr/cores/arduino/HID.cpp | 46 +++++++++++++++++---- hardware/arduino/avr/cores/arduino/USBAPI.h | 12 ++++-- 2 files changed, 45 insertions(+), 13 deletions(-) diff --git a/hardware/arduino/avr/cores/arduino/HID.cpp b/hardware/arduino/avr/cores/arduino/HID.cpp index aaa69497456..533fd498ba9 100644 --- a/hardware/arduino/avr/cores/arduino/HID.cpp +++ b/hardware/arduino/avr/cores/arduino/HID.cpp @@ -246,6 +246,9 @@ bool WEAK HID_Setup(Setup& setup) // Mouse Mouse_::Mouse_(void) : _buttons(0) +#ifdef MOUSE_ABS_ENABLED +, _x(0), _y(0) +#endif { } @@ -259,10 +262,23 @@ void Mouse_::end(void) #ifdef MOUSE_ABS_ENABLED -void Mouse_::moveAbsolute(unsigned int x, unsigned int y, signed char wheel, unsigned char buttons) +void Mouse_::moveAbsolute(int x, int y, signed char wheel) { uint8_t m[6]; - m[0] = buttons; + + if (x < 0) + x = 0; + else if (x > 4095) + x = 4095; + _x = x; + + if (y < 0) + y = 0; + else if (y > 4095) + y = 4095; + _y = y; + + m[0] = _buttons; m[1] = LSB(x); m[2] = MSB(x); m[3] = LSB(y); @@ -271,16 +287,19 @@ void Mouse_::moveAbsolute(unsigned int x, unsigned int y, signed char wheel, uns HID_SendReport(1,m,6); } -#else +void Mouse_::moveAbsolute(int x, int y, signed char wheel, unsigned char buttons) +{ + _buttons = buttons; + moveAbsolute(x, y, wheel); +} -void Mouse_::click(uint8_t b) +void Mouse_::move(signed char x, signed char y, signed char wheel) { - _buttons = b; - move(0,0,0); - _buttons = 0; - move(0,0,0); + moveAbsolute((_x+x), (_y+y), wheel); } +#else + void Mouse_::move(signed char x, signed char y, signed char wheel) { uint8_t m[4]; @@ -291,6 +310,16 @@ void Mouse_::move(signed char x, signed char y, signed char wheel) HID_SendReport(1,m,4); } +#endif + +void Mouse_::click(uint8_t b) +{ + _buttons = b; + move(0,0,0); + _buttons = 0; + move(0,0,0); +} + void Mouse_::buttons(uint8_t b) { if (b != _buttons) @@ -317,7 +346,6 @@ bool Mouse_::isPressed(uint8_t b) return false; } -#endif //================================================================================ //================================================================================ diff --git a/hardware/arduino/avr/cores/arduino/USBAPI.h b/hardware/arduino/avr/cores/arduino/USBAPI.h index 2f1f28a69c0..8fefb3602a0 100644 --- a/hardware/arduino/avr/cores/arduino/USBAPI.h +++ b/hardware/arduino/avr/cores/arduino/USBAPI.h @@ -105,20 +105,24 @@ class Mouse_ { private: uint8_t _buttons; +#ifdef MOUSE_ABS_ENABLED + int _x, _y; +#endif void buttons(uint8_t b); public: Mouse_(void); void begin(void); void end(void); #ifdef MOUSE_ABS_ENABLED - void moveAbsolute(unsigned int x, unsigned int y, signed char wheel = 0, unsigned char buttons = 0); // x and y have the range of 0 to 4095 -#else - void click(uint8_t b = MOUSE_LEFT); + void moveAbsolute(int x, int y, signed char wheel = 0); // x and y have the range 0...4095 + void moveAbsolute(int x, int y, signed char wheel, unsigned char buttons); // x and y have the range 0...4095 +#endif void move(signed char x, signed char y, signed char wheel = 0); + void click(uint8_t b = MOUSE_LEFT); // click LEFT by default void press(uint8_t b = MOUSE_LEFT); // press LEFT by default void release(uint8_t b = MOUSE_LEFT); // release LEFT by default bool isPressed(uint8_t b = MOUSE_LEFT); // check LEFT by default -#endif + }; extern Mouse_ Mouse; From 5decc56426399825b7003f4882b18a75fe03fe66 Mon Sep 17 00:00:00 2001 From: Andreas Watterott Date: Wed, 29 Apr 2015 16:36:33 +0200 Subject: [PATCH 4/4] removed hardcoded size value --- hardware/arduino/avr/cores/arduino/HID.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/hardware/arduino/avr/cores/arduino/HID.cpp b/hardware/arduino/avr/cores/arduino/HID.cpp index 533fd498ba9..7dd00958b67 100644 --- a/hardware/arduino/avr/cores/arduino/HID.cpp +++ b/hardware/arduino/avr/cores/arduino/HID.cpp @@ -284,7 +284,7 @@ void Mouse_::moveAbsolute(int x, int y, signed char wheel) m[3] = LSB(y); m[4] = MSB(y); m[5] = wheel; - HID_SendReport(1,m,6); + HID_SendReport(1, m, sizeof(m)); } void Mouse_::moveAbsolute(int x, int y, signed char wheel, unsigned char buttons) @@ -307,7 +307,7 @@ void Mouse_::move(signed char x, signed char y, signed char wheel) m[1] = x; m[2] = y; m[3] = wheel; - HID_SendReport(1,m,4); + HID_SendReport(1, m, sizeof(m)); } #endif @@ -365,7 +365,7 @@ void Keyboard_::end(void) void Keyboard_::sendReport(KeyReport* keys) { - HID_SendReport(2,keys,sizeof(KeyReport)); + HID_SendReport(2, keys, sizeof(KeyReport)); } extern