1
0
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:
Cristian Maglie 2012-10-08 15:47:13 +02:00
parent 84c7c3ed26
commit a17c422b47
4 changed files with 55 additions and 131 deletions

View File

@ -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();
}

View File

@ -22,25 +22,24 @@
#include <hidboot.h>
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<HID_PROTOCOL_MOUSE> hostMouse;
int maxX;
int maxY;
int dx, dy;
int buttons;
};

View File

@ -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();
}

View File

@ -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();
}