-
-
Notifications
You must be signed in to change notification settings - Fork 11
/
mouse.cpp
68 lines (56 loc) · 1.62 KB
/
mouse.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
/**
* Bill Siever
* 2021-11-20 Initial Version
*
* Development environment specifics:
* Written in Microsoft PXT
*
* This code is released under the [MIT License](http://opensource.org/licenses/MIT).
* Please review the LICENSE.md file included with this example. If you have any questions
* or concerns with licensing, please contact techsupport@sparkfun.com.
* Distributed as-is; no warranty is given.
*/
#include "pxt.h"
#include "MicroBit.h"
#include "MouseReporter.h"
#include "debug.h"
static MouseReporter *reporter = NULL;
static int constrain(int in, int min, int max) {
return (in<min)?min:(in>max?max:in);
}
using namespace pxt;
namespace mouse {
bool isInitialized() {
if(reporter == NULL) {
uBit.display.scroll("Mouse not started");
return false;
} else {
return true;
}
}
//%
void startMouseService() {
if(reporter == NULL) {
reporter = MouseReporter::getInstance();
}
}
//%
void _send(int dx, int dy, int scroll, int buttons) {
if(!isInitialized()) return;
dx = constrain(dx,-127, 127);
dy = constrain(dy,-127, 127);
scroll = constrain(scroll,-127, 127);
reporter->send(dx, dy, buttons&0x1, buttons&0x2, buttons&0x4, scroll);
if(!(buttons&0x8))
reporter->send(0,0,false, false, false, 0);
}
//%
bool isEnabled() {
return reporter ? reporter->isEnabled() : false;
}
//%
void setStatusChangeHandler(Action action) {
if(!reporter) return;
reporter->setStatusChangeHandler(action);
}
}