forked from microbit-more/pxt-mbit-more-v2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MbitMore.cpp
141 lines (122 loc) · 3.32 KB
/
MbitMore.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
#include "pxt.h"
#include "MicroBit.h"
#include "MicroBitConfig.h"
#include "MbitMoreCommon.h"
#define UPDATE_PERIOD 19
#define NOTIFY_PERIOD 101
#if MICROBIT_CODAL
#include "MbitMoreService.h"
#else // NOT MICROBIT_CODAL
#include "MbitMoreServiceDAL.h"
using MbitMoreService = MbitMoreServiceDAL;
#endif // NOT MICROBIT_CODAL
//% color=#FF9900 weight=95 icon="\uf1b0"
namespace MbitMore {
MbitMoreService *_pService = NULL;
#if MICROBIT_CODAL
#else // NOT MICROBIT_CODAL
int dummyDataLabelID = 0;
#endif // NOT MICROBIT_CODAL
void update() {
while (NULL != _pService) {
_pService->update();
fiber_sleep(UPDATE_PERIOD);
}
}
void notifyScratch() {
while (NULL != _pService) {
// notyfy data to Scratch
_pService->notify();
fiber_sleep(NOTIFY_PERIOD);
}
}
/**
* @brief Start Microbit More service.
*
*/
//%
void startMbitMoreService() {
if (NULL != _pService)
return;
_pService = new MbitMoreService();
create_fiber(update);
// create_fiber(notifyScratch);
}
/**
* @brief Register a label in waiting data list and return an ID for the label.
* This starts Microbit More service if it was not available.
*
* @param dataLabel label to register
* @param dataType type of the data to be received
* @return int ID for the label
*/
//%
int call_registerWaitingDataLabel(String dataLabel, MbitMoreDataContentType dataType) {
#if MICROBIT_CODAL
if (NULL == _pService)
startMbitMoreService();
int labelID = _pService->registerWaitingDataLabel(MSTR(dataLabel), dataType);
return labelID;
#else // NOT MICROBIT_CODAL
return ++dummyDataLabelID; // dummy
#endif // NOT MICROBIT_CODAL
}
/**
* @brief Get number which was received with the label.
*
* @param labelID ID in registered labels
* @return float received data with the label
*/
//%
float call_dataContentAsNumber(int labelID) {
#if MICROBIT_CODAL
return _pService->dataContentAsNumber(labelID);
#else // NOT MICROBIT_CODAL
return 0.0; // dummy
#endif // NOT MICROBIT_CODAL
}
/**
* @brief Get text which was received with the label.
*
* @param labelID ID in registered labels
* @return String received data with the label
*/
//%
String call_dataContentAsText(int labelID) {
#if MICROBIT_CODAL
return PSTR(_pService->dataContentAsText(labelID));
#else // NOT MICROBIT_CODAL
return String(""); // dummy
#endif // NOT MICROBIT_CODAL
}
/**
* @brief Send a float with labele to Scratch.
* Do nothing if Scratch was not connected.
*
* @param dataLabel - label of the data
* @param dataContent - content of the data
*/
//%
void call_sendNumberWithLabel(String dataLabel, float dataContent) {
#if MICROBIT_CODAL
if (NULL == _pService)
return;
_pService->sendNumberWithLabel(MSTR(dataLabel), dataContent);
#endif // MICROBIT_CODAL
}
/**
* @brief Send a text with label to Scratch.
* Do nothing if Scratch was not connected.
*
* @param dataLabel - label of the data
* @param dataContent - content of the data
*/
//%
void call_sendTextWithLabel(String dataLabel, String dataContent) {
#if MICROBIT_CODAL
if (NULL == _pService)
return;
_pService->sendTextWithLabel(MSTR(dataLabel), MSTR(dataContent));
#endif // MICROBIT_CODAL
}
} // namespace MbitMore