mirror of
https://github.com/arduino/Arduino.git
synced 2025-01-17 06:52:18 +01:00
Updated MouseController library API
This commit is contained in:
parent
84c7c3ed26
commit
a17c422b47
@ -28,69 +28,56 @@ void mouseMoved() __attribute__ ((weak, alias("__mouseControllerEmptyCallback
|
|||||||
void mousePressed() __attribute__ ((weak, alias("__mouseControllerEmptyCallback")));
|
void mousePressed() __attribute__ ((weak, alias("__mouseControllerEmptyCallback")));
|
||||||
void mouseReleased() __attribute__ ((weak, alias("__mouseControllerEmptyCallback")));
|
void mouseReleased() __attribute__ ((weak, alias("__mouseControllerEmptyCallback")));
|
||||||
|
|
||||||
int mouseX = 0;
|
int MouseController::getXChange() {
|
||||||
int mouseY = 0;
|
int r = dx;
|
||||||
MouseButton mouseButton;
|
dx = 0;
|
||||||
bool mouseButtonPressed = false;
|
return r;
|
||||||
|
}
|
||||||
|
|
||||||
|
int MouseController::getYChange() {
|
||||||
|
int r = dy;
|
||||||
|
dy = 0;
|
||||||
|
return r;
|
||||||
|
}
|
||||||
|
|
||||||
void MouseController::OnMouseMove(MOUSEINFO *mi) {
|
void MouseController::OnMouseMove(MOUSEINFO *mi) {
|
||||||
mouseX += mi->dX;
|
dx += mi->dX;
|
||||||
mouseY += mi->dY;
|
dy += mi->dY;
|
||||||
if (mouseX < 0)
|
if (buttons != 0)
|
||||||
mouseX = 0;
|
|
||||||
if (mouseX > maxX)
|
|
||||||
mouseX = maxX;
|
|
||||||
if (mouseY < 0)
|
|
||||||
mouseY = 0;
|
|
||||||
if (mouseY > maxY)
|
|
||||||
mouseY = maxY;
|
|
||||||
if (mouseButtonPressed)
|
|
||||||
mouseDragged();
|
mouseDragged();
|
||||||
else
|
else
|
||||||
mouseMoved();
|
mouseMoved();
|
||||||
}
|
}
|
||||||
|
|
||||||
void MouseController::OnLeftButtonUp(MOUSEINFO *mi) {
|
void MouseController::OnLeftButtonUp(MOUSEINFO *mi) {
|
||||||
buttons--;
|
buttons &= ~LEFT_BUTTON;
|
||||||
mouseButtonPressed = (buttons > 0);
|
|
||||||
mouseButton = LEFT_BUTTON;
|
|
||||||
mouseReleased();
|
mouseReleased();
|
||||||
mouseClicked();
|
mouseClicked();
|
||||||
}
|
}
|
||||||
|
|
||||||
void MouseController::OnLeftButtonDown(MOUSEINFO *mi) {
|
void MouseController::OnLeftButtonDown(MOUSEINFO *mi) {
|
||||||
buttons++;
|
buttons |= LEFT_BUTTON;
|
||||||
mouseButtonPressed = (buttons > 0);
|
|
||||||
mouseButton = LEFT_BUTTON;
|
|
||||||
mousePressed();
|
mousePressed();
|
||||||
}
|
}
|
||||||
|
|
||||||
void MouseController::OnMiddleButtonUp(MOUSEINFO *mi) {
|
void MouseController::OnMiddleButtonUp(MOUSEINFO *mi) {
|
||||||
buttons--;
|
buttons &= ~MIDDLE_BUTTON;
|
||||||
mouseButtonPressed = (buttons > 0);
|
|
||||||
mouseButton = MIDDLE_BUTTON;
|
|
||||||
mouseReleased();
|
mouseReleased();
|
||||||
mouseClicked();
|
mouseClicked();
|
||||||
}
|
}
|
||||||
|
|
||||||
void MouseController::OnMiddleButtonDown(MOUSEINFO *mi) {
|
void MouseController::OnMiddleButtonDown(MOUSEINFO *mi) {
|
||||||
buttons++;
|
buttons |= MIDDLE_BUTTON;
|
||||||
mouseButtonPressed = (buttons > 0);
|
|
||||||
mouseButton = MIDDLE_BUTTON;
|
|
||||||
mousePressed();
|
mousePressed();
|
||||||
}
|
}
|
||||||
|
|
||||||
void MouseController::OnRightButtonUp(MOUSEINFO *mi) {
|
void MouseController::OnRightButtonUp(MOUSEINFO *mi) {
|
||||||
buttons--;
|
buttons &= ~RIGHT_BUTTON;
|
||||||
mouseButtonPressed = (buttons > 0);
|
|
||||||
mouseButton = RIGHT_BUTTON;
|
|
||||||
mouseReleased();
|
mouseReleased();
|
||||||
mouseClicked();
|
mouseClicked();
|
||||||
}
|
}
|
||||||
|
|
||||||
void MouseController::OnRightButtonDown(MOUSEINFO *mi) {
|
void MouseController::OnRightButtonDown(MOUSEINFO *mi) {
|
||||||
buttons++;
|
buttons |= RIGHT_BUTTON;
|
||||||
mouseButtonPressed = (buttons > 0);
|
|
||||||
mouseButton = RIGHT_BUTTON;
|
|
||||||
mousePressed();
|
mousePressed();
|
||||||
}
|
}
|
||||||
|
@ -22,25 +22,24 @@
|
|||||||
#include <hidboot.h>
|
#include <hidboot.h>
|
||||||
|
|
||||||
enum MouseButton {
|
enum MouseButton {
|
||||||
LEFT_BUTTON,
|
LEFT_BUTTON = 0x01,
|
||||||
MIDDLE_BUTTON,
|
MIDDLE_BUTTON = 0x02,
|
||||||
RIGHT_BUTTON
|
RIGHT_BUTTON = 0x04
|
||||||
};
|
};
|
||||||
|
|
||||||
extern int mouseX;
|
|
||||||
extern int mouseY;
|
|
||||||
extern MouseButton mouseButton;
|
extern MouseButton mouseButton;
|
||||||
extern bool mouseButtonPressed;
|
|
||||||
|
|
||||||
class MouseController : public MouseReportParser
|
class MouseController : public MouseReportParser
|
||||||
{
|
{
|
||||||
public:
|
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);
|
hostMouse.SetReportParser(0, this);
|
||||||
};
|
};
|
||||||
|
|
||||||
void setMaxX(int mx) { maxX = mx; };
|
bool getButton(MouseButton button) { return (buttons & button) == button; };
|
||||||
void setMaxY(int my) { maxY = my; };
|
int getXChange();
|
||||||
|
int getYChange();
|
||||||
|
// int getWheelChange(); // Not implemented
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
virtual void OnMouseMove(MOUSEINFO *mi);
|
virtual void OnMouseMove(MOUSEINFO *mi);
|
||||||
@ -53,8 +52,7 @@ protected:
|
|||||||
|
|
||||||
private:
|
private:
|
||||||
HIDBoot<HID_PROTOCOL_MOUSE> hostMouse;
|
HIDBoot<HID_PROTOCOL_MOUSE> hostMouse;
|
||||||
int maxX;
|
int dx, dy;
|
||||||
int maxY;
|
|
||||||
int buttons;
|
int buttons;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -10,36 +10,48 @@ MouseController mouse(usb);
|
|||||||
|
|
||||||
// This function intercepts mouse movements
|
// This function intercepts mouse movements
|
||||||
void mouseMoved() {
|
void mouseMoved() {
|
||||||
Serial1.print("Moving mouse: ");
|
Serial.print("Move: ");
|
||||||
Serial1.print(mouseX);
|
Serial.print(mouse.getXChange());
|
||||||
Serial1.print(", ");
|
Serial.print(", ");
|
||||||
Serial1.println(mouseY);
|
Serial.println(mouse.getYChange());
|
||||||
}
|
}
|
||||||
|
|
||||||
// This function intercepts mouse movements when a button is pressed
|
// This function intercepts mouse movements when a button is pressed
|
||||||
void mouseDragged() {
|
void mouseDragged() {
|
||||||
Serial1.print("DRAG: ");
|
Serial.print("DRAG: ");
|
||||||
Serial1.print(mouseX);
|
Serial.print(mouse.getXChange());
|
||||||
Serial1.print(", ");
|
Serial.print(", ");
|
||||||
Serial1.println(mouseY);
|
Serial.println(mouse.getYChange());
|
||||||
}
|
}
|
||||||
|
|
||||||
// This function intercepts mouse button press
|
// This function intercepts mouse button press
|
||||||
void mousePressed() {
|
void mousePressed() {
|
||||||
Serial1.print("Pressed: ");
|
Serial.print("Pressed: ");
|
||||||
Serial1.println(mouseButton);
|
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
|
// This function intercepts mouse button release
|
||||||
void mouseReleased() {
|
void mouseReleased() {
|
||||||
Serial1.print("Released: ");
|
Serial.print("Released: ");
|
||||||
Serial1.println(mouseButton);
|
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()
|
void setup()
|
||||||
{
|
{
|
||||||
Serial1.begin(115200);
|
Serial.begin(115200);
|
||||||
Serial1.println("Program started");
|
Serial.println("Program started");
|
||||||
delay(200);
|
delay(200);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -48,4 +60,3 @@ void loop()
|
|||||||
// Process USB tasks
|
// Process USB tasks
|
||||||
usb.Task();
|
usb.Task();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,72 +0,0 @@
|
|||||||
// This sketch demonstrate how to use low-level drivers (only for advanced users).
|
|
||||||
|
|
||||||
#include <hidboot.h>
|
|
||||||
|
|
||||||
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<HID_PROTOCOL_MOUSE> HostMouse(&Usb);
|
|
||||||
MouseRptParser Prs;
|
|
||||||
|
|
||||||
void setup()
|
|
||||||
{
|
|
||||||
Serial1.begin(115200);
|
|
||||||
Serial1.println("Program started:");
|
|
||||||
delay(200);
|
|
||||||
|
|
||||||
HostMouse.SetReportParser(0, &Prs);
|
|
||||||
}
|
|
||||||
|
|
||||||
void loop()
|
|
||||||
{
|
|
||||||
Usb.Task();
|
|
||||||
}
|
|
Loading…
x
Reference in New Issue
Block a user