diff --git a/hardware/arduino/sam/libraries/USBHost/MouseController.cpp b/hardware/arduino/sam/libraries/USBHost/MouseController.cpp index 7beb6fe61..3d528bc37 100644 --- a/hardware/arduino/sam/libraries/USBHost/MouseController.cpp +++ b/hardware/arduino/sam/libraries/USBHost/MouseController.cpp @@ -28,69 +28,56 @@ void mouseMoved() __attribute__ ((weak, alias("__mouseControllerEmptyCallback void mousePressed() __attribute__ ((weak, alias("__mouseControllerEmptyCallback"))); void mouseReleased() __attribute__ ((weak, alias("__mouseControllerEmptyCallback"))); -int mouseX = 0; -int mouseY = 0; -MouseButton mouseButton; -bool mouseButtonPressed = false; +int MouseController::getXChange() { + int r = dx; + dx = 0; + return r; +} + +int MouseController::getYChange() { + int r = dy; + dy = 0; + return r; +} void MouseController::OnMouseMove(MOUSEINFO *mi) { - mouseX += mi->dX; - mouseY += mi->dY; - if (mouseX < 0) - mouseX = 0; - if (mouseX > maxX) - mouseX = maxX; - if (mouseY < 0) - mouseY = 0; - if (mouseY > maxY) - mouseY = maxY; - if (mouseButtonPressed) + dx += mi->dX; + dy += mi->dY; + if (buttons != 0) mouseDragged(); else mouseMoved(); } void MouseController::OnLeftButtonUp(MOUSEINFO *mi) { - buttons--; - mouseButtonPressed = (buttons > 0); - mouseButton = LEFT_BUTTON; + buttons &= ~LEFT_BUTTON; mouseReleased(); mouseClicked(); } void MouseController::OnLeftButtonDown(MOUSEINFO *mi) { - buttons++; - mouseButtonPressed = (buttons > 0); - mouseButton = LEFT_BUTTON; + buttons |= LEFT_BUTTON; mousePressed(); } void MouseController::OnMiddleButtonUp(MOUSEINFO *mi) { - buttons--; - mouseButtonPressed = (buttons > 0); - mouseButton = MIDDLE_BUTTON; + buttons &= ~MIDDLE_BUTTON; mouseReleased(); mouseClicked(); } void MouseController::OnMiddleButtonDown(MOUSEINFO *mi) { - buttons++; - mouseButtonPressed = (buttons > 0); - mouseButton = MIDDLE_BUTTON; + buttons |= MIDDLE_BUTTON; mousePressed(); } void MouseController::OnRightButtonUp(MOUSEINFO *mi) { - buttons--; - mouseButtonPressed = (buttons > 0); - mouseButton = RIGHT_BUTTON; + buttons &= ~RIGHT_BUTTON; mouseReleased(); mouseClicked(); } void MouseController::OnRightButtonDown(MOUSEINFO *mi) { - buttons++; - mouseButtonPressed = (buttons > 0); - mouseButton = RIGHT_BUTTON; + buttons |= RIGHT_BUTTON; mousePressed(); } diff --git a/hardware/arduino/sam/libraries/USBHost/MouseController.h b/hardware/arduino/sam/libraries/USBHost/MouseController.h index a42fd3461..ad3412298 100644 --- a/hardware/arduino/sam/libraries/USBHost/MouseController.h +++ b/hardware/arduino/sam/libraries/USBHost/MouseController.h @@ -22,25 +22,24 @@ #include enum MouseButton { - LEFT_BUTTON, - MIDDLE_BUTTON, - RIGHT_BUTTON + LEFT_BUTTON = 0x01, + MIDDLE_BUTTON = 0x02, + RIGHT_BUTTON = 0x04 }; -extern int mouseX; -extern int mouseY; extern MouseButton mouseButton; -extern bool mouseButtonPressed; class MouseController : public MouseReportParser { public: - MouseController(USBHost &usb) : hostMouse(&usb), maxX(640), maxY(480), buttons(0) { + MouseController(USBHost &usb) : hostMouse(&usb), dx(0), dy(0), buttons(0) { hostMouse.SetReportParser(0, this); }; - void setMaxX(int mx) { maxX = mx; }; - void setMaxY(int my) { maxY = my; }; + bool getButton(MouseButton button) { return (buttons & button) == button; }; + int getXChange(); + int getYChange(); + // int getWheelChange(); // Not implemented protected: virtual void OnMouseMove(MOUSEINFO *mi); @@ -53,8 +52,7 @@ protected: private: HIDBoot hostMouse; - int maxX; - int maxY; + int dx, dy; int buttons; }; diff --git a/hardware/arduino/sam/libraries/USBHost/examples/MouseController/MouseController.ino b/hardware/arduino/sam/libraries/USBHost/examples/MouseController/MouseController.ino index 95f6ac979..d86c35c0c 100644 --- a/hardware/arduino/sam/libraries/USBHost/examples/MouseController/MouseController.ino +++ b/hardware/arduino/sam/libraries/USBHost/examples/MouseController/MouseController.ino @@ -10,36 +10,48 @@ MouseController mouse(usb); // This function intercepts mouse movements void mouseMoved() { - Serial1.print("Moving mouse: "); - Serial1.print(mouseX); - Serial1.print(", "); - Serial1.println(mouseY); + Serial.print("Move: "); + Serial.print(mouse.getXChange()); + Serial.print(", "); + Serial.println(mouse.getYChange()); } // This function intercepts mouse movements when a button is pressed void mouseDragged() { - Serial1.print("DRAG: "); - Serial1.print(mouseX); - Serial1.print(", "); - Serial1.println(mouseY); + Serial.print("DRAG: "); + Serial.print(mouse.getXChange()); + Serial.print(", "); + Serial.println(mouse.getYChange()); } // This function intercepts mouse button press void mousePressed() { - Serial1.print("Pressed: "); - Serial1.println(mouseButton); + Serial.print("Pressed: "); + if (mouse.getButton(LEFT_BUTTON)) + Serial.print("L"); + if (mouse.getButton(MIDDLE_BUTTON)) + Serial.print("M"); + if (mouse.getButton(RIGHT_BUTTON)) + Serial.print("R"); + Serial.println(); } // This function intercepts mouse button release void mouseReleased() { - Serial1.print("Released: "); - Serial1.println(mouseButton); + Serial.print("Released: "); + if (mouse.getButton(LEFT_BUTTON)) + Serial.print("L"); + if (mouse.getButton(MIDDLE_BUTTON)) + Serial.print("M"); + if (mouse.getButton(RIGHT_BUTTON)) + Serial.print("R"); + Serial.println(); } void setup() { - Serial1.begin(115200); - Serial1.println("Program started"); + Serial.begin(115200); + Serial.println("Program started"); delay(200); } @@ -48,4 +60,3 @@ void loop() // Process USB tasks usb.Task(); } - diff --git a/hardware/arduino/sam/libraries/USBHost/examples/MouseControllerAdvanced/MouseControllerAdvanced.ino b/hardware/arduino/sam/libraries/USBHost/examples/MouseControllerAdvanced/MouseControllerAdvanced.ino deleted file mode 100644 index 4dfaa4bfd..000000000 --- a/hardware/arduino/sam/libraries/USBHost/examples/MouseControllerAdvanced/MouseControllerAdvanced.ino +++ /dev/null @@ -1,72 +0,0 @@ -// This sketch demonstrate how to use low-level drivers (only for advanced users). - -#include - -class MouseRptParser : public MouseReportParser -{ -protected: - virtual void OnMouseMove(MOUSEINFO *mi); - virtual void OnLeftButtonUp(MOUSEINFO *mi); - virtual void OnLeftButtonDown(MOUSEINFO *mi); - virtual void OnRightButtonUp(MOUSEINFO *mi); - virtual void OnRightButtonDown(MOUSEINFO *mi); - virtual void OnMiddleButtonUp(MOUSEINFO *mi); - virtual void OnMiddleButtonDown(MOUSEINFO *mi); -}; - -void MouseRptParser::OnMouseMove(MOUSEINFO *mi) -{ - Serial1.print("Moving { "); - Serial1.print(mi->dX); - Serial1.print(", "); - Serial1.print(mi->dY); - Serial1.println(" }"); -}; - -void MouseRptParser::OnLeftButtonUp(MOUSEINFO *mi) -{ - Serial1.println("Left Button UP"); -}; - -void MouseRptParser::OnLeftButtonDown(MOUSEINFO *mi) -{ - Serial1.println("Left Button DOWN"); -}; - -void MouseRptParser::OnRightButtonUp(MOUSEINFO *mi) -{ - Serial1.println("Right Button UP"); -}; - -void MouseRptParser::OnRightButtonDown(MOUSEINFO *mi) -{ - Serial1.println("Right Button DOWN"); -}; - -void MouseRptParser::OnMiddleButtonUp(MOUSEINFO *mi) -{ - Serial1.println("Middle Button UP"); -}; - -void MouseRptParser::OnMiddleButtonDown(MOUSEINFO *mi) -{ - Serial1.println("Middle Button DOWN"); -}; - -USBHost Usb; -HIDBoot HostMouse(&Usb); -MouseRptParser Prs; - -void setup() -{ - Serial1.begin(115200); - Serial1.println("Program started:"); - delay(200); - - HostMouse.SetReportParser(0, &Prs); -} - -void loop() -{ - Usb.Task(); -}