forked from bsiever/microbit-pxt-blehid
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathabsmouse.cpp
76 lines (61 loc) · 1.75 KB
/
absmouse.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
69
70
71
72
73
74
75
76
/**
* 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 "AbsMouseReporter.h"
#include "debug.h"
static AbsoluteMouseReporter *reporter = NULL;
static int constrain(int in, int min, int max) {
return (in<min)?min:(in>max?max:in);
}
using namespace pxt;
namespace absmouse {
static int lastX = 0;
static int lastY = 0;
bool isInitialized() {
if(reporter == NULL) {
uBit.display.scroll("Absolute Mouse not started");
return false;
} else {
return true;
}
}
//%
void startAbsoluteMouseService() {
if(reporter == NULL) {
reporter = AbsoluteMouseReporter::getInstance();
}
}
//%
void _send(int x, int y, int buttons) {
if(!isInitialized()) return;
x = x==0xFFFF ? lastX : x;
y = y==0xFFFF ? lastY : y;
x = constrain(x,-32767, 32767);
y = constrain(y,-32767, 32767);
reporter->send(x, y, buttons&0x1, buttons&0x2, buttons&0x4);
if(!(buttons&0x8))
reporter->send(x,y,false, false, false);
lastX = x;
lastY = y;
}
//%
bool isEnabled() {
return reporter ? reporter->isEnabled() : false;
}
//%
void setStatusChangeHandler(Action action) {
if(!reporter) return;
reporter->setStatusChangeHandler(action);
}
}