Browse Source

move cursor, move wheel, press/release button

master
Robin Thoni 8 years ago
parent
commit
67273d560b
3 changed files with 73 additions and 28 deletions
  1. 27
    10
      UsbMouse.h
  2. 43
    8
      UsbMouse.hxx
  3. 3
    10
      examples/UsbMouseDemo1/main.ino

+ 27
- 10
UsbMouse.h View File

@@ -34,23 +34,40 @@ typedef struct{
34 34
     char    dWheel;
35 35
 }report_t;
36 36
 
37
+#define MOUSE_BUTTON_LEFT   1
38
+#define MOUSE_BUTTON_RIGHT  2
39
+#define MOUSE_BUTTON_MIDDLE 4
40
+
37 41
 static uchar    idleRate;/* repeat rate for keyboards, never used for mice */
38 42
 
39 43
 class UsbMouseDevice {
40
- public:
41
-  UsbMouseDevice();
44
+public:
45
+    UsbMouseDevice();
42 46
 
43
-  bool isUsbReady();
47
+    bool isUsbReady();
44 48
     
45
-  void update() {
46
-    usbPoll();
47
-  }
48
-  void move(char dx, char dy);
49
+    void update() {
50
+        usbPoll();
51
+    }
52
+
53
+    void move(char dx, char dy);
54
+
55
+    void moveWheel(char dy);
56
+
57
+    void pressButton(char button);
58
+
59
+    void releaseButton(char button);
60
+
61
+    void setButtonState(char button, bool pressed);
62
+
63
+    //void clickButton(char button);
64
+
65
+    void updateStatus();
49 66
 
50
-  void updateStatus();
67
+    void resetStatus();
51 68
     
52
-  //private: TODO: Make friend?
53
-  report_t    reportBuffer;    // buffer for HID reports [ 1 modifier byte + (len-1) key strokes]
69
+    //private: TODO: Make friend?
70
+    report_t reportBuffer;    // buffer for HID reports [ 1 modifier byte + (len-1) key strokes]
54 71
 
55 72
 private:
56 73
 

+ 43
- 8
UsbMouse.hxx View File

@@ -51,17 +51,10 @@ UsbMouseDevice::UsbMouseDevice()
51 51
     cli();
52 52
     usbDeviceDisconnect();
53 53
     usbDeviceConnect();
54
-
55
-
56 54
     usbInit();
57
-
58 55
     sei();
59 56
 
60
-    reportBuffer.buttonMask = 0;
61
-    reportBuffer.dx = 0;
62
-    reportBuffer.dy = 0;
63
-    reportBuffer.dWheel = 0;
64
-
57
+    resetStatus();
65 58
 }
66 59
 
67 60
 bool UsbMouseDevice::isUsbReady()
@@ -77,10 +70,52 @@ void UsbMouseDevice::move(char dx, char dy)
77 70
     updateStatus();
78 71
 }
79 72
 
73
+void UsbMouseDevice::moveWheel(char dy)
74
+{
75
+    reportBuffer.dWheel += dy;
76
+    updateStatus();
77
+}
78
+
79
+void UsbMouseDevice::pressButton(char button)
80
+{
81
+    reportBuffer.buttonMask |= button;
82
+    updateStatus();
83
+}
84
+
85
+void UsbMouseDevice::releaseButton(char button)
86
+{
87
+    reportBuffer.buttonMask &= ~button;
88
+    updateStatus();
89
+}
90
+
91
+void UsbMouseDevice::setButtonState(char button, bool pressed)
92
+{
93
+    if (pressed)
94
+    {
95
+        pressButton(button);
96
+    }
97
+    else
98
+    {
99
+        releaseButton(button);
100
+    }
101
+}
102
+
103
+/*void UsbMouseDevice::clickButton(char button)
104
+{
105
+    pressButton(button);
106
+    while (!usbInterruptIsReady()) {
107
+    }
108
+    releaseButton(button);
109
+}*/
110
+
80 111
 void UsbMouseDevice::updateStatus()
81 112
 {
82 113
     usbSetInterrupt((unsigned char*)(void *)&reportBuffer, sizeof(reportBuffer));
114
+    resetStatus();
115
+}
83 116
 
117
+void UsbMouseDevice::resetStatus()
118
+{
84 119
     reportBuffer.buttonMask = 0;
85 120
     reportBuffer.dx = 0;
86 121
     reportBuffer.dy = 0;

+ 3
- 10
examples/UsbMouseDemo1/main.ino View File

@@ -16,17 +16,10 @@ void loop()
16 16
     WAIT_USB;
17 17
 
18 18
     UsbMouse.move(50, 0);
19
+    //UsbMouse.moveWheel(1);
20
+    //UsbMouse.pressButton(MOUSE_BUTTON_LEFT);
21
+    //UsbMouse.releaseButton(MOUSE_BUTTON_LEFT);
19 22
 
20
-    /*reportBuffer.buttonMask = 0;
21
-    reportBuffer.dx = 50;
22
-    reportBuffer.dy = 0;
23
-    reportBuffer.dWheel = 0;
24
-
25
-    usbSetInterrupt((unsigned char*)(void *)&reportBuffer, sizeof(reportBuffer));
26 23
     ArdUtils::delayMs(1000);
27
-    usbSetInterrupt((unsigned char*)(void *)&reportBuffer, sizeof(reportBuffer));*/
28
-
29
-    //ArdUtils::delayMs(1000);
30 24
     digitalWrite(ledPin, !digitalRead(ledPin));
31
-    //while(1);
32 25
 }

Loading…
Cancel
Save