Repository navigation
Expand file tree
/
Copy pathextension.cpp
More file actions
83 lines (73 loc) · 2 KB
/
Copy pathextension.cpp
File metadata and controls
83 lines (73 loc) · 2 KB
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
77
78
79
80
81
82
83
#include "pxt.h"
#include "BlueDotService.h"
enum BlueDotButtonState {
//% block="down"
Down = 1,
//% block="up"
Up = 2
};
enum BlueDotDimension {
//% block="x"
X,
//% block="y"
Y
};
namespace bluedot {
BlueDotService* _pService = NULL;
/**
* Starts the BlueDot BLE service
**/
//% blockId=bluedot_start block="blue dot start"
void startService() {
if (NULL == _pService) {
_pService = new BlueDotService(*uBit.ble);
}
}
/**
* Registers code to run when the button is in a particular state
*/
//% blockId=bluedot_on_button_changed block="on blue dot %state"
void onButton(BlueDotButtonState state, Action handler) {
startService();
registerWithDal(MICROBIT_ID_BLUEDOT, state, handler);
}
/**
* Register code to run when the position has changed
*/
//% blockId=bluedot_on_position_chaged block="on blue dot moved"
void onMoved(Action handler) {
startService();
registerWithDal(MICROBIT_ID_BLUEDOT, MICROBIT_BLUEDOT_EVT_POSITION_UPDATE, handler);
}
/**
* Gets a value indicating if the button is pressed
*/
//% blockId=bluedot_buttonpressed block="blue dot is pressed"
bool isPressed() {
startService();
return _pService->getButton() > 0;
}
/**
* Gets the position of the cursor on the bluedot, between -1023 (left) and 1023 (right)
*/
//%
int x() {
startService();
return (_pService->getX() - (2 << 15)) >> 5;
}
/**
* Gets the position of the cursor on the bluedot, between -1023 (left) and 1023 (right)
*/
//%
int y() {
startService();
return (_pService->getY() - (2 << 15)) >> 5;
}
/**
* Gets the position of the touch point on the blue dot
*/
//% blockId=bluedot_position block="blue dot %name"
int position(BlueDotDimension dimension) {
return dimension == BlueDotDimension::Y ? y() : x();
}
}