Skip to content

Commit 462ac21

Browse files
committed
standalone Mouse library
1 parent 1290515 commit 462ac21

File tree

2 files changed

+138
-0
lines changed

2 files changed

+138
-0
lines changed

libraries/Mouse/Mouse.cpp

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
#if 1 //defined(USBCON)
2+
3+
#include "Mouse.h"
4+
5+
const u8 _hidReportDescriptor[] PROGMEM = {
6+
7+
// Mouse
8+
0x05, 0x01, // USAGE_PAGE (Generic Desktop) // 54
9+
0x09, 0x02, // USAGE (Mouse)
10+
0xa1, 0x01, // COLLECTION (Application)
11+
0x09, 0x01, // USAGE (Pointer)
12+
0xa1, 0x00, // COLLECTION (Physical)
13+
0x85, 0x01, // REPORT_ID (1)
14+
0x05, 0x09, // USAGE_PAGE (Button)
15+
0x19, 0x01, // USAGE_MINIMUM (Button 1)
16+
0x29, 0x03, // USAGE_MAXIMUM (Button 3)
17+
0x15, 0x00, // LOGICAL_MINIMUM (0)
18+
0x25, 0x01, // LOGICAL_MAXIMUM (1)
19+
0x95, 0x03, // REPORT_COUNT (3)
20+
0x75, 0x01, // REPORT_SIZE (1)
21+
0x81, 0x02, // INPUT (Data,Var,Abs)
22+
0x95, 0x01, // REPORT_COUNT (1)
23+
0x75, 0x05, // REPORT_SIZE (5)
24+
0x81, 0x03, // INPUT (Cnst,Var,Abs)
25+
0x05, 0x01, // USAGE_PAGE (Generic Desktop)
26+
0x09, 0x30, // USAGE (X)
27+
0x09, 0x31, // USAGE (Y)
28+
0x09, 0x38, // USAGE (Wheel)
29+
0x15, 0x81, // LOGICAL_MINIMUM (-127)
30+
0x25, 0x7f, // LOGICAL_MAXIMUM (127)
31+
0x75, 0x08, // REPORT_SIZE (8)
32+
0x95, 0x03, // REPORT_COUNT (3)
33+
0x81, 0x06, // INPUT (Data,Var,Rel)
34+
0xc0, // END_COLLECTION
35+
0xc0, // END_COLLECTION
36+
};
37+
38+
size_t getsizeof_hidReportDescriptor() {
39+
return sizeof(_hidReportDescriptor);
40+
}
41+
42+
Mouse_ Mouse;
43+
44+
//================================================================================
45+
//================================================================================
46+
// Mouse
47+
48+
Mouse_::Mouse_(void) : _buttons(0)
49+
{
50+
}
51+
52+
void Mouse_::begin(void)
53+
{
54+
}
55+
56+
void Mouse_::end(void)
57+
{
58+
}
59+
60+
void Mouse_::click(uint8_t b)
61+
{
62+
_buttons = b;
63+
move(0,0,0);
64+
_buttons = 0;
65+
move(0,0,0);
66+
}
67+
68+
void Mouse_::move(signed char x, signed char y, signed char wheel)
69+
{
70+
u8 m[4];
71+
m[0] = _buttons;
72+
m[1] = x;
73+
m[2] = y;
74+
m[3] = wheel;
75+
HID_SendReport(1,m,4);
76+
}
77+
78+
void Mouse_::buttons(uint8_t b)
79+
{
80+
if (b != _buttons)
81+
{
82+
_buttons = b;
83+
move(0,0,0);
84+
}
85+
}
86+
87+
void Mouse_::press(uint8_t b)
88+
{
89+
buttons(_buttons | b);
90+
}
91+
92+
void Mouse_::release(uint8_t b)
93+
{
94+
buttons(_buttons & ~b);
95+
}
96+
97+
bool Mouse_::isPressed(uint8_t b)
98+
{
99+
if ((b & _buttons) > 0)
100+
return true;
101+
return false;
102+
}
103+
104+
#endif

libraries/Mouse/Mouse.h

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#ifndef MOUSE_h
2+
#define MOUSE_h
3+
4+
#if 1 //defined(USBCON)
5+
6+
#include "HID.h"
7+
//================================================================================
8+
//================================================================================
9+
// Mouse
10+
11+
#define MOUSE_LEFT 1
12+
#define MOUSE_RIGHT 2
13+
#define MOUSE_MIDDLE 4
14+
#define MOUSE_ALL (MOUSE_LEFT | MOUSE_RIGHT | MOUSE_MIDDLE)
15+
16+
class Mouse_
17+
{
18+
private:
19+
uint8_t _buttons;
20+
void buttons(uint8_t b);
21+
public:
22+
Mouse_(void);
23+
void begin(void);
24+
void end(void);
25+
void click(uint8_t b = MOUSE_LEFT);
26+
void move(signed char x, signed char y, signed char wheel = 0);
27+
void press(uint8_t b = MOUSE_LEFT); // press LEFT by default
28+
void release(uint8_t b = MOUSE_LEFT); // release LEFT by default
29+
bool isPressed(uint8_t b = MOUSE_LEFT); // check LEFT by default
30+
};
31+
extern Mouse_ Mouse;
32+
33+
#endif
34+
#endif

0 commit comments

Comments
 (0)