forked from microbit-more/pxt-mbit-more-v2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MbitMoreService.cpp
304 lines (265 loc) · 8.32 KB
/
MbitMoreService.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
#include "pxt.h"
#if MICROBIT_CODAL
#include "nrf_saadc.h"
/**
* Class definition for the Scratch3 MicrobitMore Service.
* Provides a BLE service to Scratch3.
*/
#include "MicroBitConfig.h"
#if CONFIG_ENABLED(DEVICE_BLE)
// #include "ble_advdata.h"
#include "MbitMoreService.h"
#include "MicroBitButton.h"
// service ID: 0b50f3e4-607f-4151-9091-7d008d6ffc5c
const uint8_t MbitMoreService::baseUUID[16] = {0x0b, 0x50, 0xf3, 0xe4, 0x60, 0x7f, 0x41, 0x51, 0x90, 0x91, 0x7d, 0x00, 0x8d, 0x6f, 0xfc, 0x5c};
const uint16_t MbitMoreService::serviceUUID = 0xf3e4;
const uint16_t MbitMoreService::charUUID[mbitmore_cIdx_COUNT] = {
0x0100, // COMMAND
0x0101, // STATE
0x0102, // MOTION
0x0110, // PIN_EVENT
0x0111, // ACTION_EVENT
0x0120, // ANALOG_IN_P0
0x0121, // ANALOG_IN_P1
0x0122, // ANALOG_IN_P2
0x0130 // MESSAGE
};
/**
* Constructor.
* Create a representation of default extension for Scratch3.
*/
MbitMoreService::MbitMoreService() : uBit(pxt::uBit) {
mbitMore = &MbitMoreDevice::getInstance();
mbitMore->moreService = this;
// Create the service.
bs_uuid_type = BLE_UUID_TYPE_UNKNOWN;
RegisterBaseUUID(baseUUID);
CreateService(serviceUUID);
// Add each of our characteristics.
CreateCharacteristic(
mbitmore_cIdx_COMMAND,
charUUID[mbitmore_cIdx_COMMAND],
(uint8_t *)(commandChBuffer),
MM_CH_BUFFER_SIZE_COMMAND,
MM_CH_BUFFER_SIZE_COMMAND,
microbit_propWRITE | microbit_propWRITE_WITHOUT | microbit_propREAD);
CreateCharacteristic(
mbitmore_cIdx_STATE,
charUUID[mbitmore_cIdx_STATE],
(uint8_t *)(stateChBuffer),
MM_CH_BUFFER_SIZE_STATE,
MM_CH_BUFFER_SIZE_STATE,
microbit_propREAD);
CreateCharacteristic(
mbitmore_cIdx_MOTION,
charUUID[mbitmore_cIdx_MOTION],
(uint8_t *)(motionChBuffer),
MM_CH_BUFFER_SIZE_MOTION,
MM_CH_BUFFER_SIZE_MOTION,
microbit_propREAD);
CreateCharacteristic(
mbitmore_cIdx_PIN_EVENT,
charUUID[mbitmore_cIdx_PIN_EVENT],
(uint8_t *)(pinEventChBuffer),
MM_CH_BUFFER_SIZE_NOTIFY,
MM_CH_BUFFER_SIZE_NOTIFY,
microbit_propREAD | microbit_propNOTIFY);
CreateCharacteristic(
mbitmore_cIdx_ACTION_EVENT,
charUUID[mbitmore_cIdx_ACTION_EVENT],
(uint8_t *)(actionEventChBuffer),
MM_CH_BUFFER_SIZE_NOTIFY,
MM_CH_BUFFER_SIZE_NOTIFY,
microbit_propREAD | microbit_propNOTIFY);
CreateCharacteristic(
mbitmore_cIdx_ANALOG_IN_P0,
charUUID[mbitmore_cIdx_ANALOG_IN_P0],
(uint8_t *)(analogInP0ChBuffer),
MM_CH_BUFFER_SIZE_ANALOG_IN,
MM_CH_BUFFER_SIZE_ANALOG_IN,
microbit_propREAD | microbit_propREADAUTH);
CreateCharacteristic(
mbitmore_cIdx_ANALOG_IN_P1,
charUUID[mbitmore_cIdx_ANALOG_IN_P1],
(uint8_t *)(analogInP1ChBuffer),
MM_CH_BUFFER_SIZE_ANALOG_IN,
MM_CH_BUFFER_SIZE_ANALOG_IN,
microbit_propREAD | microbit_propREADAUTH);
CreateCharacteristic(
mbitmore_cIdx_ANALOG_IN_P2,
charUUID[mbitmore_cIdx_ANALOG_IN_P2],
(uint8_t *)(analogInP2ChBuffer),
MM_CH_BUFFER_SIZE_ANALOG_IN,
MM_CH_BUFFER_SIZE_ANALOG_IN,
microbit_propREAD | microbit_propREADAUTH);
CreateCharacteristic(
mbitmore_cIdx_DATA,
charUUID[mbitmore_cIdx_DATA],
(uint8_t *)(dataChBuffer),
MM_CH_BUFFER_SIZE_NOTIFY,
MM_CH_BUFFER_SIZE_NOTIFY,
microbit_propREAD | microbit_propNOTIFY);
// // Stop advertising.
// uBit.ble->stopAdvertising();
// // Configure advertising.
// ble_uuid_t adv_uuids[] = {{serviceUUID, BLE_UUID_TYPE_BLE}};
// ble_advdata_t advdata;
// memset(&advdata, 0, sizeof(advdata));
// advdata.name_type = BLE_ADVDATA_FULL_NAME;
// advdata.include_appearance = true;
// advdata.flags = BLE_GAP_ADV_FLAGS_LE_ONLY_GENERAL_DISC_MODE;
// advdata.uuids_complete.uuid_cnt = sizeof(adv_uuids) / sizeof(adv_uuids[0]);
// advdata.uuids_complete.p_uuids = adv_uuids;
// bool connectable = true;
// bool discoverable = true;
// bool whitelist = false;
// uBit.ble->configureAdvertising(connectable, discoverable, whitelist,
// MICROBIT_BLE_ADVERTISING_INTERVAL,
// MICROBIT_BLE_ADVERTISING_TIMEOUT, &advdata);
// // Start advertising.
// uBit.ble->advertise();
fiber_add_idle_component(this);
}
/**
* Invoked when BLE connects.
*/
void MbitMoreService::onConnect(const microbit_ble_evt_t *p_ble_evt) {
mbitMore->updateVersionData();
}
/**
* Invoked when BLE disconnects.
*/
void MbitMoreService::onDisconnect(const microbit_ble_evt_t *p_ble_evt) {
}
/**
* Callback. Invoked when any of our attributes are written via BLE.
*/
void MbitMoreService::onDataWritten(const microbit_ble_evt_write_t *params) {
if (params->handle == valueHandle(mbitmore_cIdx_COMMAND) && params->len > 0) {
mbitMore->onCommandReceived((uint8_t *)params->data, params->len);
}
}
/**
* Callback. Invoked when any of our attributes are read via BLE.
* Set params->data and params->length to update the value
*/
void MbitMoreService::onDataRead(microbit_onDataRead_t *params) {
if (params->handle == valueHandle(mbitmore_cIdx_ANALOG_IN_P0)) {
mbitMore->updateAnalogIn(analogInP0ChBuffer, 0);
params->data = analogInP0ChBuffer;
params->length = 2;
} else if (params->handle == valueHandle(mbitmore_cIdx_ANALOG_IN_P1)) {
mbitMore->updateAnalogIn(analogInP1ChBuffer, 1);
params->data = analogInP1ChBuffer;
params->length = 2;
} else if (params->handle == valueHandle(mbitmore_cIdx_ANALOG_IN_P2)) {
mbitMore->updateAnalogIn(analogInP2ChBuffer, 2);
params->data = analogInP2ChBuffer;
params->length = 2;
}
}
/**
* Periodic callback from MicroBit idle thread.
*/
void MbitMoreService::idleCallback() {
if (getConnected()) {
} else {
mbitMore->displayFriendlyName();
}
}
/**
* @brief Notify action event.
*/
void MbitMoreService::notifyActionEvent() {
if (!getConnected())
return;
notifyChrValue(mbitmore_cIdx_ACTION_EVENT, actionEventChBuffer,
MM_CH_BUFFER_SIZE_NOTIFY);
}
/**
* @brief Notify pin event.
*/
void MbitMoreService::notifyPinEvent() {
if (!getConnected())
return;
notifyChrValue(mbitmore_cIdx_PIN_EVENT, pinEventChBuffer,
MM_CH_BUFFER_SIZE_NOTIFY);
}
/**
* Notify message to Scratch3
*/
void MbitMoreService::notifyData() {
if (!getConnected())
return;
notifyChrValue(mbitmore_cIdx_DATA, dataChBuffer, MM_CH_BUFFER_SIZE_NOTIFY);
}
/**
* Notify data to Scratch3
*/
void MbitMoreService::notify() {}
/**
* Update all sensors.
*/
void MbitMoreService::update() {
if (getConnected()) {
mbitMore->updateState(stateChBuffer);
mbitMore->updateMotion(motionChBuffer);
}
}
/**
* @brief Register data label and retrun ID for the label.
*
* @param dataLabel label to register
* @param dataType type of the data to be received
* @return int ID for the label
*/
int MbitMoreService::registerWaitingDataLabel(ManagedString dataLabel, MbitMoreDataContentType dataType) {
return mbitMore->registerWaitingDataLabel(dataLabel, dataType);
}
/**
* @brief Get type of content for the label
*
* @param labelID ID for the label
* @return type of content [number | string]
*/
MbitMoreDataContentType MbitMoreService::dataType(int labelID) {
return mbitMore->dataType(labelID);
}
/**
* @brief Return content of the data as number
*
* @param labelID ID for the label
* @return content of the data
*/
float MbitMoreService::dataContentAsNumber(int labelID) {
return mbitMore->dataContentAsNumber(labelID);
}
/**
* @brief Return content of the data as string
*
* @param labelID ID for the label
* @return content of the data
*/
ManagedString MbitMoreService::dataContentAsText(int labelID) {
return mbitMore->dataContentAsText(labelID);
}
/**
* @brief Send a float with label to Scratch.
*
* @param dataLabel label of the data
* @param dataContent content of the data
*/
void MbitMoreService::sendNumberWithLabel(ManagedString dataLabel, float dataContent) {
mbitMore->sendNumberWithLabel(dataLabel, dataContent);
}
/**
* @brief Send a string with label to Scratch.
*
* @param dataLabel label of the data
* @param dataContent content of the data
*/
void MbitMoreService::sendTextWithLabel(ManagedString dataLabel, ManagedString dataContent) {
mbitMore->sendTextWithLabel(dataLabel, dataContent);
}
#endif // CONFIG_ENABLED(DEVICE_BLE)
#endif // MICROBIT_CODAL